Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7b566e74c6a6d3d46ac7d97ce8e8335191ed229d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*******************************************************************************
 * Copyright (c) 2010-2017 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/
package org.eclipse.scout.rt.platform.html;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.scout.rt.platform.html.internal.EmptyHtmlNodeBuilder;
import org.eclipse.scout.rt.platform.html.internal.HtmlContentBuilder;
import org.eclipse.scout.rt.platform.html.internal.HtmlDocumentBuilder;
import org.eclipse.scout.rt.platform.html.internal.HtmlImageBuilder;
import org.eclipse.scout.rt.platform.html.internal.HtmlInputBuilder;
import org.eclipse.scout.rt.platform.html.internal.HtmlLinkBuilder;
import org.eclipse.scout.rt.platform.html.internal.HtmlListElement;
import org.eclipse.scout.rt.platform.html.internal.HtmlNodeBuilder;
import org.eclipse.scout.rt.platform.html.internal.HtmlPlainBuilder;
import org.eclipse.scout.rt.platform.html.internal.HtmlTableBuilder;
import org.eclipse.scout.rt.platform.html.internal.HtmlTableColgroupBuilder;
import org.eclipse.scout.rt.platform.html.internal.HtmlTableColgroupColBuilder;
import org.eclipse.scout.rt.platform.html.internal.HtmlTableDataBuilder;
import org.eclipse.scout.rt.platform.html.internal.HtmlTableHeadBuilder;
import org.eclipse.scout.rt.platform.html.internal.HtmlTableRowBuilder;
import org.eclipse.scout.rt.platform.html.internal.StyleElementBuilder;

/**
 * Convenience for building a HTML document or parts of it with encoded text.
 * <p>
 * Only the most common cases are supported, not intended to be complete.
 * <p>
 * Because {@link IHtmlContent} extends {@link Serializable}, all {@link CharSequence} provided as arguments must be
 * serializable (constraint verified by constructor of {@link HtmlPlainBuilder} and {@link HtmlContentBuilder}.
 */
public final class HTML {

  // pattern for Scout font icon 'font:CHAR', pattern for custom font icon: 'font:FONT-NAME CHAR'
  private static final Pattern FONT_ICON_PATTERN = Pattern.compile("font:([^ ]+)(?:$| (.+$))");

  /**
   * Utility class
   */
  private HTML() {
  }

  /**
   * Creates a <code>&lt;head&gt...&lt;/head&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.head("text").toHtml();</code>
   */
  public static IHtmlContent head(CharSequence... elements) {
    return new HtmlNodeBuilder("head", elements);
  }

  /**
   * Creates a <code>&lt;body&gt...&lt;/body&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.body("text").toHtml();</code>
   */
  public static IHtmlContent body(CharSequence... elements) {
    return new HtmlNodeBuilder("body", elements);
  }

  /**
   * Creates a <code>&lt;b&gt...&lt;/b&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.bold("text").toHtml();</code>
   */
  public static IHtmlElement bold(CharSequence... text) {
    return new HtmlNodeBuilder("b", text);
  }

  /**
   * Creates a <code>&lt;i&gt...&lt;/i&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.italic("text").toHtml();</code>
   */
  public static IHtmlElement italic(CharSequence... text) {
    return new HtmlNodeBuilder("i", text);
  }

  /**
   * Creates a <code>&lt;p&gt...&lt;/p&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.p("text").toHtml();</code>
   */
  public static IHtmlElement p(CharSequence... text) {
    return new HtmlNodeBuilder("p", text);
  }

  /**
   * Creates a <code>&lt;br&gt</code> element without content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.br().toHtml();</code>
   */
  public static IHtmlElement br() {
    return new EmptyHtmlNodeBuilder("br");
  }

  /**
   * Creates a <code>&lt;hr&gt</code> element without content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.hr().toHtml();</code>
   */
  public static IHtmlElement hr() {
    return new EmptyHtmlNodeBuilder("hr");
  }

  /**
   * Creates a <code>&lt;h1&gt...&lt;/h1&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.h1("text").toHtml();</code>
   */
  public static IHtmlElement h1(CharSequence... text) {
    return new HtmlNodeBuilder("h1", text);
  }

  /**
   * Creates a <code>&lt;h2&gt...&lt;/h2&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.h2("text").toHtml();</code>
   */
  public static IHtmlElement h2(CharSequence... text) {
    return new HtmlNodeBuilder("h2", text);
  }

  /**
   * Creates a <code>&lt;h3&gt...&lt;/h3&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.h3("text").toHtml();</code>
   */
  public static IHtmlElement h3(CharSequence... text) {
    return new HtmlNodeBuilder("h3", text);
  }

  /**
   * Creates a <code>&lt;h4&gt...&lt;/h4&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.h4("text").toHtml();</code>
   */
  public static IHtmlElement h4(CharSequence... text) {
    return new HtmlNodeBuilder("h4", text);
  }

  /**
   * Creates a <code>&lt;h5&gt...&lt;/h5&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.h5("text").toHtml();</code>
   */
  public static IHtmlElement h5(CharSequence... text) {
    return new HtmlNodeBuilder("h5", text);
  }

  /**
   * Creates a <code>&lt;h6&gt...&lt;/h6&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.h6("text").toHtml();</code>
   */
  public static IHtmlElement h6(CharSequence... text) {
    return new HtmlNodeBuilder("h6", text);
  }

  /**
   * Creates a <code>&lt;div&gt...&lt;/div&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.div("text").toHtml();</code>
   */
  public static IHtmlElement div(CharSequence... content) {
    return div(Arrays.asList(content));
  }

  /**
   * Creates a <code>&lt;div&gt...&lt;/div&gt</code> element with encoded content.
   */
  public static IHtmlElement div(List<CharSequence> contents) {
    return new HtmlNodeBuilder("div", contents);
  }

  /**
   * Creates a <code>&lt;span&gt...&lt;/span&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.span("text").toHtml();</code>
   */
  public static IHtmlElement span(CharSequence... text) {
    return new HtmlNodeBuilder("span", text);
  }

  /**
   * Creates a <code>&lt;a&gt...&lt;/a&gt</code> element with encoded text and attribute
   * <code>rel="noreferrer noopener"</code>.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.link("http://xyz.com","text").toHtml();</code>
   */
  public static IHtmlElement link(CharSequence url, CharSequence text) {
    return link(url, text, false);
  }

  /**
   * Creates a <code>&lt;a&gt...&lt;/a&gt</code> element with encoded text.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.link("http://xyz.com","text").toHtml();</code>
   * </p>
   *
   * @param preserveOpener
   *          Specifies if the opener and referrer should be available to the target browser window. Because of security
   *          reasons this should be disabled ({@code false}) in most cases. If disabled, the attribute
   *          {@code rel="noreferrer noopener"} is added to the link element.
   * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types
   * @see https://mathiasbynens.github.io/rel-noopener/
   */
  public static IHtmlElement link(CharSequence url, CharSequence text, boolean preserveOpener) {
    return new HtmlLinkBuilder(url, text, preserveOpener);
  }

  /**
   * Creates a <code>&lt;img src="..."&gt</code> element.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.img("image.png").toHtml();</code>
   *
   * @param src
   *          image source path
   */
  public static IHtmlElement img(CharSequence src) {
    return new HtmlImageBuilder(src);
  }

  /**
   * Creates a <code>&lt;img src="binaryResource:..."&gt</code> element.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.imgByBinaryResource("resourceName").toHtml();</code>
   *
   * @param binaryResource
   *          image source path
   */
  public static IHtmlElement imgByBinaryResource(CharSequence binaryResource) {
    return new HtmlImageBuilder("binaryResource:" + binaryResource);
  }

  /**
   * Creates a <code>&lt;img src="iconId:..."&gt</code> element.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.imgByIconId("icon_name").toHtml();</code>
   */
  public static IHtmlElement imgByIconId(CharSequence iconId) {
    return new HtmlImageBuilder("iconId:" + iconId);
  }

  /**
   * Creates an appropriate element for the given icon:
   * <ul>
   * <li>For a font icon, a <code>span</code> element is returned.
   * <li>For all other icons, an <code>img</code> element is returned.
   * </ul>
   *
   * @param icon
   *          icon like {@link AbstractIcons#Info}
   */
  public static IHtmlElement icon(final CharSequence icon) {
    final Matcher matcher = FONT_ICON_PATTERN.matcher(icon);
    if (matcher.find()) {
      // font icon
      if (matcher.group(2) == null) {
        return span(matcher.group(1)).cssClass("font-icon"); // icon from Scout font
      }
      else {
        return span(matcher.group(2)).cssClass("font-" + matcher.group(1)); // icon from custom font
      }
    }
    else {
      // image icon
      return imgByIconId(icon);
    }
  }

  /**
   * Creates a <code>&lt;li&gt...&lt;/li&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.li("text").toHtml();</code>
   */
  public static IHtmlListElement li(CharSequence text) {
    return new HtmlListElement(text);
  }

  /**
   * Creates a <code>&lt;ul&gt...&lt;/ul&gt</code> element with the given list elements.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.ul(HTML.li("text"),...).toHtml();</code>
   */
  public static IHtmlElement ul(IHtmlListElement... li) {
    return ul(Arrays.asList(li));
  }

  /**
   * Creates a <code>&lt;ul&gt...&lt;/ul&gt</code> element with the given list elements.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.ul(HTML.li("text"),...).toHtml();</code>
   */
  public static IHtmlElement ul(List<IHtmlListElement> li) {
    return new HtmlNodeBuilder("ul", li);
  }

  /**
   * Creates a <code>&lt;ol&gt...&lt;/ol&gt</code> element with the given list elements.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.ol(HTML.li("text"),...).toHtml();</code>
   */
  public static IHtmlContent ol(IHtmlListElement... li) {
    return new HtmlNodeBuilder("ol", li);
  }

  /**
   * Creates a <code>&lt;colgroup&gt...&lt;/colgroup&gt</code> element with the given <code>col</code> elements
   * <p>
   * Example see {@link #table(IHtmlTableColgroup, List)}.
   */
  public static IHtmlTableColgroup colgroup(IHtmlTableColgroupCol... cols) {
    return new HtmlTableColgroupBuilder(Arrays.asList(cols));
  }

  /**
   * Creates a <code>&lt;colgroup&gt...&lt;/colgroup&gt</code> element with the given <code>col</code> elements
   * <p>
   * Example see {@link #table(IHtmlTableColgroup, List)}.
   */
  public static IHtmlTableColgroup colgroup(List<IHtmlTableColgroupCol> cols) {
    return new HtmlTableColgroupBuilder(cols);
  }

  /**
   * Creates a <code>&lt;col&gt</code> element.
   * <p>
   * Example see {@link #table(IHtmlTableColgroup, List)}.
   */
  public static IHtmlTableColgroupCol col() {
    return new HtmlTableColgroupColBuilder();
  }

  /**
   * Creates a <code>&lt;th&gt...&lt;/th&gt</code> element with encoded content.
   * <p>
   * Example see {@link #table(IHtmlTableRow...)}.
   */
  public static IHtmlTableCell th(CharSequence... text) {
    return new HtmlTableHeadBuilder(Arrays.asList(text));
  }

  /**
   * Creates a <code>&lt;td&gt...&lt;/td&gt</code> element with encoded content.
   * <p>
   * Example see {@link #table(IHtmlTableRow...)}.
   */
  public static IHtmlTableCell td(CharSequence... text) {
    return new HtmlTableDataBuilder(Arrays.asList(text));
  }

  /**
   * Creates a <code>&lt;tr&gt...&lt;/tr&gt</code> element with encoded content.
   * <p>
   * Example see {@link #table(IHtmlTableRow...)}.
   */
  public static IHtmlTableRow tr(IHtmlTableCell... td) {
    return tr(Arrays.asList(td));
  }

  /**
   * Creates a <code>&lt;tr&gt...&lt;/tr&gt</code> element with encoded content.
   * <p>
   * Example see {@link #table(IHtmlTableRow...)}.
   */
  public static IHtmlTableRow tr(List<IHtmlTableCell> td) {
    return new HtmlTableRowBuilder(td);
  }

  /**
   * Creates a <code>&lt;table&gt...&lt;/table&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   *
   * <pre>
   * HTML.table(
   *   HTML.tr(
   *     HTML.th("header1"),
   *     HTML.th("header2")
   *   ),
   *   HTML.tr(
   *     HTML.td("cell1"),
   *     HTML.td("cell2")
   *   ),
   *   HTML.tr(
   *     HTML.td("cell3"),
   *     HTML.td("cell4")
   * )
   * </pre>
   */
  public static IHtmlTable table(IHtmlTableRow... rows) {
    return new HtmlTableBuilder(Arrays.asList(rows));
  }

  /**
   * Creates a <code>&lt;table&gt...&lt;/table&gt</code> element with encoded content.
   * <p>
   * Example see {@link #table(IHtmlTableRow...)}.
   */
  public static IHtmlTable table(List<IHtmlTableRow> rows) {
    return new HtmlTableBuilder(rows);
  }

  /**
   * Creates a <code>&lt;table&gt...&lt;/table&gt</code> element with encoded content.
   * <p>
   * <i>Example:</i><br>
   *
   * <pre>
   * HTML.table(
   *   HTML.colgroup(
   *     HTML.col(),
   *     HTML.col()
   *   ),
   *   HTML.tr(
   *     HTML.td("cell1"),
   *     HTML.td("cell2")
   *   ),
   *   HTML.tr(
   *     HTML.td("cell3"),
   *     HTML.td("cell4")
   * )
   * </pre>
   */
  public static IHtmlTable table(IHtmlTableColgroup colgroup, IHtmlTableRow... rows) {
    return new HtmlTableBuilder(colgroup, Arrays.asList(rows));
  }

  /**
   * Creates a <code>&lt;table&gt...&lt;/table&gt</code> element with encoded content.
   * <p>
   * Example see {@link #table(IHtmlTableColgroup, IHtmlTableRow...)}.
   */
  public static IHtmlTable table(IHtmlTableColgroup colgroup, List<IHtmlTableRow> rows) {
    return new HtmlTableBuilder(colgroup, rows);
  }

  /**
   * Creates an application local link with encoded text.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.appLink("path","text").toHtml();</code>
   *
   * @param ref
   *          what the link is referring to
   * @param text
   *          the link text
   */
  public static IHtmlElement appLink(CharSequence ref, CharSequence text) {
    return span(text).appLink(ref);
  }

  /**
   * Creates HTML content from multiple elements (concatenation). The fragment element itself has no representation in
   * the resulting HTML document.
   * <p>
   * <i>Example:</i><br>
   *
   * <pre>
   * String encodedHtml = HTML.fragment(
   *     HTML.bold("Attention: "),
   *     "This message is ",
   *     HTML.italic("not important.")).toHtml();
   *
   * // Result:
   * // &lt;b&gt;Attention: &lt;/b&gt; This message is &lt;i&gt;not important.&lt;i&gt;</code>
   * </pre>
   */
  public static IHtmlContent fragment(CharSequence... elements) {
    return new HtmlContentBuilder(elements);
  }

  /**
   * Creates HTML content from multiple elements (concatenation). The fragment element itself has no representation in
   * the resulting HTML document.
   * <p>
   * <i>Example:</i><br>
   *
   * <pre>
   * String encodedHtml = HTML.fragment(
   *     HTML.bold("Attention: "),
   *     "This message is ",
   *     HTML.italic("not important.")).toHtml();
   *
   * // Result:
   * // &lt;b&gt;Attention: &lt;/b&gt; This message is &lt;i&gt;not important.&lt;i&gt;</code>
   * </pre>
   */
  public static IHtmlContent fragment(List<? extends CharSequence> elements) {
    return new HtmlContentBuilder(elements);
  }

  /**
   * Creates a <code>&lt;style type="text/css"&gt...&lt;/style&gt</code> element with encoded content.
   */
  public static IHtmlElement cssStyle(CharSequence... cssContent) {
    return new StyleElementBuilder(cssContent).type("text/css");
  }

  /**
   * Creates a <code>&lt;html&gt...&lt;/html&gt</code> with HTML5 doctype.
   */
  public static IHtmlDocument html5(CharSequence head, CharSequence body) {
    return new HtmlDocumentBuilder(head(head), body(body)).doctype();
  }

  /**
   * Creates a <code>&lt;html&gt...&lt;/html&gt</code> without doctype.
   */
  public static IHtmlDocument html(CharSequence head, CharSequence body) {
    return new HtmlDocumentBuilder(head(head), body(body));
  }

  /**
   * Creates HTML content that will not be encoded with {@link IHtmlContent#toHtml()}. <b>Use with caution!</b>
   */
  public static IHtmlContent raw(CharSequence... text) {
    return new HtmlPlainBuilder(text);
  }

  /**
   * Creates a <code>&lt;input&gt</code> element without content.
   */
  public static IHtmlInput input() {
    return new HtmlInputBuilder();
  }

  /**
   * Creates a HTML element for the given tag name with encoded content.
   * <p>
   * Use this method, if no specific method is available.
   * <p>
   * <i>Example:</i><br>
   * <code>String encodedHtml = HTML.tag("my-special-tag").toHtml();</code>
   */
  public static IHtmlElement tag(String tag, CharSequence... text) {
    return new HtmlNodeBuilder(tag, text);
  }
}

Back to the top