forked from openjdk/jdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HtmlTree.java
1026 lines (945 loc) · 30.8 KB
/
HtmlTree.java
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.javadoc.internal.doclets.formats.html.markup;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlAttr.Role;
import jdk.javadoc.internal.doclets.toolkit.Content;
import jdk.javadoc.internal.doclets.toolkit.util.DocletConstants;
/**
* A tree node representing an HTML element, containing the name of the element,
* a collection of attributes, and content.
*
* Except where otherwise stated, all methods in this class will throw
* {@code NullPointerException} for any arguments that are {@code null}
* or that are arrays or collections that contain {@code null}.
*
* Many methods in this class return {@code this}, to enable a series
* of chained method calls on a single object.
*
* Terminology: An HTML element is typically composed of a start tag, some
* enclosed content and typically an end tag. The start tag contains any
* attributes for the element. See:
* <a href="https://en.wikipedia.org/wiki/HTML_element">HTML element</a>.
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*
* @see <a href="https://html.spec.whatwg.org/multipage/syntax.html#normal-elements">WhatWG: Normal Elements</a>
* @see <a href="https://www.w3.org/TR/html51/syntax.html#writing-html-documents-elements">HTML 5.1: Elements</a>
*/
public class HtmlTree extends Content {
/**
* The name of the HTML element.
* This value is never {@code null}.
*/
public final TagName tagName;
/**
* The attributes for the HTML element.
* The keys and values in this map are never {@code null}.
*/
private Map<HtmlAttr, String> attrs = Map.of();
/**
* The enclosed content ("inner HTML") for this HTML element.
* The items in this list are never null.
*/
private List<Content> content = List.of();
/**
* A sentinel value to explicitly indicate empty content.
* The '==' identity of this object is significant.
*/
public static final Content EMPTY = Text.of("");
/**
* Creates an {@code HTMLTree} object representing an HTML element
* with the given name.
*
* @param tagName the name
*/
public HtmlTree(TagName tagName) {
this.tagName = Objects.requireNonNull(tagName);
}
/**
* Adds an attribute.
*
* @param attrName the name of the attribute
* @param attrValue the value of the attribute
* @return this object
*/
public HtmlTree put(HtmlAttr attrName, String attrValue) {
if (attrs.isEmpty())
attrs = new LinkedHashMap<>(3);
attrs.put(Objects.requireNonNull(attrName), Entity.escapeHtmlChars(attrValue));
return this;
}
/**
* Sets the {@code id} attribute.
*
* @param id the value for the attribute
* @return this object
*/
public HtmlTree setId(HtmlId id) {
return put(HtmlAttr.ID, id.name());
}
/**
* Sets the {@code title} attribute.
* Any nested start or end tags in the content will be removed.
*
* @param body the content for the title attribute
* @return this object
*/
public HtmlTree setTitle(Content body) {
return put(HtmlAttr.TITLE, stripHtml(body));
}
/**
* Sets the {@code role} attribute.
*
* @param role the role
* @return this object
*/
public HtmlTree setRole(Role role) {
return put(HtmlAttr.ROLE, role.toString());
}
/**
* Sets the {@code class} attribute.
*
* @param style the value for the attribute
* @return this object
*/
public HtmlTree setStyle(HtmlStyle style) {
return put(HtmlAttr.CLASS, style.cssName());
}
public HtmlTree addStyle(HtmlStyle style) {
return addStyle(style.cssName());
}
public HtmlTree addStyle(String style) {
if (attrs.isEmpty())
attrs = new LinkedHashMap<>(3);
attrs.compute(HtmlAttr.CLASS, (attr, existingStyle) ->
existingStyle == null ? style : existingStyle + " " + style);
return this;
}
/**
* Adds additional content for the HTML element.
*
* @param content the content
*/
@Override
public HtmlTree add(Content content) {
if (content instanceof ContentBuilder cb) {
cb.contents.forEach(this::add);
}
else if (content == HtmlTree.EMPTY || content.isValid()) {
// quietly avoid adding empty or invalid nodes (except EMPTY)
if (this.content.isEmpty())
this.content = new ArrayList<>();
this.content.add(content);
}
return this;
}
/**
* Adds text content for the HTML element.
*
* If the last content member that was added is a {@code StringContent},
* appends the string to that item; otherwise, creates and uses a new {@code StringContent}
* for the new text content.
*
* @param stringContent string content that needs to be added
*/
@Override
public HtmlTree add(CharSequence stringContent) {
if (!content.isEmpty()) {
Content lastContent = content.get(content.size() - 1);
if (lastContent instanceof TextBuilder)
lastContent.add(stringContent);
else {
add(new TextBuilder(stringContent));
}
}
else {
add(new TextBuilder(stringContent));
}
return this;
}
/**
* Adds each of a list of content items.
*
* @param list the list
* @return this object
*/
public HtmlTree add(List<? extends Content> list) {
list.forEach(this::add);
return this;
}
@Override
public int charCount() {
int n = 0;
for (Content c : content) {
n += c.charCount();
}
return n;
}
/*
* The sets of ASCII URI characters to be left unencoded.
* See "Uniform Resource Identifier (URI): Generic Syntax"
* IETF RFC 3986. https://tools.ietf.org/html/rfc3986
*/
public static final BitSet MAIN_CHARS;
public static final BitSet QUERY_FRAGMENT_CHARS;
static {
BitSet alphaDigit = bitSet(bitSet('A', 'Z'), bitSet('a', 'z'), bitSet('0', '9'));
BitSet unreserved = bitSet(alphaDigit, bitSet("-._~"));
BitSet genDelims = bitSet(":/?#[]@");
BitSet subDelims = bitSet("!$&'()*+,;=");
MAIN_CHARS = bitSet(unreserved, genDelims, subDelims);
BitSet pchar = bitSet(unreserved, subDelims, bitSet(":@"));
QUERY_FRAGMENT_CHARS = bitSet(pchar, bitSet("/?"));
}
private static BitSet bitSet(String s) {
BitSet result = new BitSet();
for (int i = 0; i < s.length(); i++) {
result.set(s.charAt(i));
}
return result;
}
private static BitSet bitSet(char from, char to) {
BitSet result = new BitSet();
result.set(from, to + 1);
return result;
}
private static BitSet bitSet(BitSet... sets) {
BitSet result = new BitSet();
for (BitSet set : sets) {
result.or(set);
}
return result;
}
/**
* Apply percent-encoding to a URL.
* This is similar to {@link java.net.URLEncoder} but
* is less aggressive about encoding some characters,
* like '(', ')', ',' which are used in the anchor
* names for Java methods in HTML5 mode.
*
* @param url the url to be percent-encoded.
* @return a percent-encoded string.
*/
public static String encodeURL(String url) {
BitSet nonEncodingChars = MAIN_CHARS;
StringBuilder sb = new StringBuilder();
for (byte c : url.getBytes(StandardCharsets.UTF_8)) {
if (c == '?' || c == '#') {
sb.append((char) c);
// switch to the more restrictive set inside
// the query and/or fragment
nonEncodingChars = QUERY_FRAGMENT_CHARS;
} else if (nonEncodingChars.get(c & 0xFF)) {
sb.append((char) c);
} else {
sb.append(String.format("%%%02X", c & 0xFF));
}
}
return sb.toString();
}
/**
* Creates an HTML {@code A} element.
*
* @param ref the value for the {@code href} attribute}
* @param body the content for element
* @return the element
*/
public static HtmlTree A(String ref, Content body) {
return new HtmlTree(TagName.A)
.put(HtmlAttr.HREF, encodeURL(ref))
.add(body);
}
/**
* Creates an HTML {@code CAPTION} element with the given content.
*
* @param body content for the element
* @return the element
*/
public static HtmlTree CAPTION(Content body) {
return new HtmlTree(TagName.CAPTION)
.add(body);
}
/**
* Creates an HTML {@code CODE} element with the given content.
*
* @param body content for the element
* @return the element
*/
public static HtmlTree CODE(Content body) {
return new HtmlTree(TagName.CODE)
.add(body);
}
/**
* Creates an HTML {@code DD} element with the given content.
*
* @param body content for the element
* @return the element
*/
public static HtmlTree DD(Content body) {
return new HtmlTree(TagName.DD)
.add(body);
}
/**
* Creates an HTML {@code DL} element with the given style.
*
* @param style the style
* @return the element
*/
public static HtmlTree DL(HtmlStyle style) {
return new HtmlTree(TagName.DL)
.setStyle(style);
}
/**
* Creates an HTML {@code DL} element with the given style and content.
*
* @param style the style
* @param body the content
* @return the element
*/
public static HtmlTree DL(HtmlStyle style, Content body) {
return new HtmlTree(TagName.DL)
.setStyle(style)
.add(body);
}
/**
* Creates an HTML {@code DIV} element with the given style.
*
* @param style the style
* @return the element
*/
public static HtmlTree DIV(HtmlStyle style) {
return new HtmlTree(TagName.DIV)
.setStyle(style);
}
/**
* Creates an HTML {@code DIV} element with the given style and content.
*
* @param style the style
* @param body the content
* @return the element
*/
public static HtmlTree DIV(HtmlStyle style, Content body) {
return new HtmlTree(TagName.DIV)
.setStyle(style)
.add(body);
}
/**
* Creates an HTML {@code DIV} element with the given content.
*
* @param body the content
* @return the element
*/
public static HtmlTree DIV(Content body) {
return new HtmlTree(TagName.DIV)
.add(body);
}
/**
* Creates an HTML {@code DT} element with the given content.
*
* @param body the content
* @return the element
*/
public static HtmlTree DT(Content body) {
return new HtmlTree(TagName.DT)
.add(body);
}
/**
* Creates an HTML {@code FOOTER} element.
* The role is set to {@code contentinfo}.
*
* @return the element
*/
public static HtmlTree FOOTER() {
return new HtmlTree(TagName.FOOTER)
.setRole(Role.CONTENTINFO);
}
/**
* Creates an HTML {@code HEADER} element.
* The role is set to {@code banner}.
*
* @return the element
*/
public static HtmlTree HEADER() {
return new HtmlTree(TagName.HEADER)
.setRole(Role.BANNER);
}
/**
* Creates an HTML heading element with the given content.
*
* @param headingTag the tag for the heading
* @param body the content
* @return the element
*/
public static HtmlTree HEADING(TagName headingTag, Content body) {
return new HtmlTree(checkHeading(headingTag))
.add(body);
}
/**
* Creates an HTML heading element with the given style and content.
*
* @param headingTag the tag for the heading
* @param style the stylesheet class
* @param body the content
* @return the element
*/
public static HtmlTree HEADING(TagName headingTag, HtmlStyle style, Content body) {
return new HtmlTree(checkHeading(headingTag))
.setStyle(style)
.add(body);
}
/**
* Creates an HTML heading element with the given style and content.
* The {@code title} attribute is set from the content.
*
* @param headingTag the tag for the heading
* @param style the stylesheet class
* @param body the content
* @return the element
*/
public static HtmlTree HEADING_TITLE(TagName headingTag,
HtmlStyle style, Content body) {
return new HtmlTree(checkHeading(headingTag))
.setTitle(body)
.setStyle(style)
.add(body);
}
/**
* Creates an HTML heading element with the given style and content.
* The {@code title} attribute is set from the content.
*
* @param headingTag the tag for the heading
* @param body the content
* @return the element
*/
public static HtmlTree HEADING_TITLE(TagName headingTag, Content body) {
return new HtmlTree(checkHeading(headingTag))
.setTitle(body)
.add(body);
}
private static TagName checkHeading(TagName headingTag) {
switch (headingTag) {
case H1: case H2: case H3: case H4: case H5: case H6:
return headingTag;
default:
throw new IllegalArgumentException(headingTag.toString());
}
}
/**
* Creates an HTML {@code HTML} element with the given {@code lang} attribute,
* and {@code HEAD} and {@code BODY} contents.
*
* @param lang the value for the {@code lang} attribute
* @param head the {@code HEAD} element
* @param body the {@code BODY} element
* @return the {@code HTML} element
*/
public static HtmlTree HTML(String lang, Content head, Content body) {
return new HtmlTree(TagName.HTML)
.put(HtmlAttr.LANG, lang)
.add(head)
.add(body);
}
/**
* Creates an HTML {@code INPUT} element with the given id and initial value.
* The element as marked as initially disabled.
*
* @param type the type of input
* @param id the id
* @param value the initial value
* @return the element
*/
public static HtmlTree INPUT(String type, HtmlId id, String value) {
return new HtmlTree(TagName.INPUT)
.put(HtmlAttr.TYPE, type)
.setId(id)
.put(HtmlAttr.VALUE, value)
.put(HtmlAttr.DISABLED, "disabled");
}
/**
* Creates an HTML {@code LABEL} element with the given content.
*
* @param forLabel the value of the {@code for} attribute
* @param body the content
* @return the element
*/
public static HtmlTree LABEL(String forLabel, Content body) {
return new HtmlTree(TagName.LABEL)
.put(HtmlAttr.FOR, forLabel)
.add(body);
}
/**
* Creates an HTML {@code LI} element with the given content.
*
* @param body the content
* @return the element
*/
public static HtmlTree LI(Content body) {
return new HtmlTree(TagName.LI)
.add(body);
}
/**
* Creates an HTML {@code LI} element with the given style and the given content.
*
* @param style the style
* @param body the content
* @return the element
*/
public static HtmlTree LI(HtmlStyle style, Content body) {
return LI(body)
.setStyle(style);
}
/**
* Creates an HTML {@code LINK} tag with the given attributes.
*
* @param rel the relevance of the link: the {@code rel} attribute
* @param type the type of link: the {@code type} attribute
* @param href the path for the link: the {@code href} attribute
* @param title title for the link: the {@code title} attribute
* @return the element
*/
public static HtmlTree LINK(String rel, String type, String href, String title) {
return new HtmlTree(TagName.LINK)
.put(HtmlAttr.REL, rel)
.put(HtmlAttr.TYPE, type)
.put(HtmlAttr.HREF, href)
.put(HtmlAttr.TITLE, title);
}
/**
* Creates an HTML {@code MAIN} element.
* The role is set to {@code main}.
*
* @return the element
*/
public static HtmlTree MAIN() {
return new HtmlTree(TagName.MAIN)
.setRole(Role.MAIN);
}
/**
* Creates an HTML {@code MAIN} element with the given content.
* The role is set to {@code main}.
*
* @return the element
*/
public static HtmlTree MAIN(Content body) {
return new HtmlTree(TagName.MAIN)
.setRole(Role.MAIN)
.add(body);
}
/**
* Creates an HTML {@code META} element with {@code http-equiv} and {@code content} attributes.
*
* @param httpEquiv the value for the {@code http-equiv} attribute
* @param content the type of content, to be used in the {@code content} attribute
* @param charset the character set for the document, to be used in the {@code content} attribute
* @return the element
*/
public static HtmlTree META(String httpEquiv, String content, String charset) {
return new HtmlTree(TagName.META)
.put(HtmlAttr.HTTP_EQUIV, httpEquiv)
.put(HtmlAttr.CONTENT, content + "; charset=" + charset);
}
/**
* Creates an HTML {@code META} element with {@code name} and {@code content} attributes.
*
* @param name the value for the {@code name} attribute
* @param content the value for the {@code content} attribute
* @return the element
*/
public static HtmlTree META(String name, String content) {
return new HtmlTree(TagName.META)
.put(HtmlAttr.NAME, name)
.put(HtmlAttr.CONTENT, content);
}
/**
* Creates an HTML {@code NAV} element.
* The role is set to {@code navigation}.
*
* @return the element
*/
public static HtmlTree NAV() {
return new HtmlTree(TagName.NAV)
.setRole(Role.NAVIGATION);
}
/**
* Creates an HTML {@code NOSCRIPT} element with some content.
*
* @param body the content
* @return the element
*/
public static HtmlTree NOSCRIPT(Content body) {
return new HtmlTree(TagName.NOSCRIPT)
.add(body);
}
/**
* Creates an HTML {@code P} element with some content.
*
* @param body the content
* @return the element
*/
public static HtmlTree P(Content body) {
return new HtmlTree(TagName.P)
.add(body);
}
/**
* Creates an HTML {@code P} element with the given style and some content.
*
* @param style the style
* @param body the content
* @return the element
*/
public static HtmlTree P(HtmlStyle style, Content body) {
return P(body)
.setStyle(style);
}
/**
* Creates an HTML {@code SCRIPT} element with some script content.
* The type of the script is set to {@code text/javascript}.
*
* @param src the content
* @return the element
*/
public static HtmlTree SCRIPT(String src) {
return new HtmlTree(TagName.SCRIPT)
.put(HtmlAttr.TYPE, "text/javascript")
.put(HtmlAttr.SRC, src);
}
/**
* Creates an HTML {@code SECTION} element with the given style.
*
* @param style the style
* @return the element
*/
public static HtmlTree SECTION(HtmlStyle style) {
return new HtmlTree(TagName.SECTION)
.setStyle(style);
}
/**
* Creates an HTML {@code SECTION} element with the given style and some content.
*
* @param style the style
* @param body the content
* @return the element
*/
public static HtmlTree SECTION(HtmlStyle style, Content body) {
return new HtmlTree(TagName.SECTION)
.setStyle(style)
.add(body);
}
/**
* Creates an HTML {@code SMALL} element with some content.
*
* @param body the content
* @return the element
*/
public static HtmlTree SMALL(Content body) {
return new HtmlTree(TagName.SMALL)
.add(body);
}
/**
* Creates an HTML {@code SPAN} element with some content.
*
* @param body the content
* @return the element
*/
public static HtmlTree SPAN(Content body) {
return new HtmlTree(TagName.SPAN)
.add(body);
}
/**
* Creates an HTML {@code SPAN} element with the given style and some content.
*
* @param styleClass the style
* @param body the content
* @return the element
*/
public static HtmlTree SPAN(HtmlStyle styleClass, Content body) {
return SPAN(body)
.setStyle(styleClass);
}
/**
* Creates an HTML {@code SPAN} element with the given id and some content.
*
* @param id the id
* @param body the content
* @return the element
*/
public static HtmlTree SPAN_ID(HtmlId id, Content body) {
return new HtmlTree(TagName.SPAN)
.setId(id)
.add(body);
}
/**
* Creates an HTML {@code SPAN} element with the given id and style, and some content.
*
* @param id the id
* @param style the style
* @param body the content
* @return the element
*/
public static HtmlTree SPAN(HtmlId id, HtmlStyle style, Content body) {
return new HtmlTree(TagName.SPAN)
.setId(id)
.setStyle(style)
.add(body);
}
/**
* Creates an HTML {@code SUP} element with the given content.
*
* @param body the content
* @return the element
*/
public static HtmlTree SUP(Content body) {
return new HtmlTree(TagName.SUP)
.add(body);
}
/**
* Creates an HTML {@code TD} element with the given style and some content.
*
* @param style the style
* @param body the content
* @return the element
*/
public static HtmlTree TD(HtmlStyle style, Content body) {
return new HtmlTree(TagName.TD)
.setStyle(style)
.add(body);
}
/**
* Creates an HTML {@code TH} element with the given style and scope, and some content.
*
* @param style the style
* @param scope the value for the {@code scope} attribute
* @param body the content
* @return the element
*/
public static HtmlTree TH(HtmlStyle style, String scope, Content body) {
return new HtmlTree(TagName.TH)
.setStyle(style)
.put(HtmlAttr.SCOPE, scope)
.add(body);
}
/**
* Creates an HTML {@code TH} element with the given scope, and some content.
*
* @param scope the value for the {@code scope} attribute
* @param body the content
* @return the element
*/
public static HtmlTree TH(String scope, Content body) {
return new HtmlTree(TagName.TH)
.put(HtmlAttr.SCOPE, scope)
.add(body);
}
/**
* Creates an HTML {@code TITLE} element with some content.
*
* @param body the content
* @return the element
*/
public static HtmlTree TITLE(String body) {
return new HtmlTree(TagName.TITLE)
.add(body);
}
/**
* Creates an HTML {@code UL} element with the given style and some content.
*
* @param style the style
* @param first the initial content
* @param more additional content
* @return the element
*/
public static HtmlTree UL(HtmlStyle style, Content first, Content... more) {
HtmlTree htmlTree = new HtmlTree(TagName.UL)
.setStyle(style);
htmlTree.add(first);
for (Content c : more) {
htmlTree.add(c);
}
return htmlTree;
}
@Override
public boolean isEmpty() {
return (!hasContent() && !hasAttrs());
}
/**
* Returns true if the HTML tree has content.
*
* @return true if the HTML tree has content else return false
*/
public boolean hasContent() {
return (!content.isEmpty());
}
/**
* Returns true if the HTML tree has attributes.
*
* @return true if the HTML tree has attributes else return false
*/
public boolean hasAttrs() {
return (!attrs.isEmpty());
}
/**
* Returns true if the HTML tree has a specific attribute.
*
* @param attrName name of the attribute to check within the HTML tree
* @return true if the HTML tree has the specified attribute else return false
*/
public boolean hasAttr(HtmlAttr attrName) {
return (attrs.containsKey(attrName));
}
/**
* Returns true if the HTML tree is valid. This check is more specific to
* standard doclet and not exactly similar to W3C specifications. But it
* ensures HTML validation.
*
* @return true if the HTML tree is valid
*/
@Override
public boolean isValid() {
switch (tagName) {
case A:
return (hasAttr(HtmlAttr.ID) || (hasAttr(HtmlAttr.HREF) && hasContent()));
case BR:
return (!hasContent() && (!hasAttrs() || hasAttr(HtmlAttr.CLEAR)));
case HR:
case INPUT:
return (!hasContent());
case IMG:
return (hasAttr(HtmlAttr.SRC) && hasAttr(HtmlAttr.ALT) && !hasContent());
case LINK:
return (hasAttr(HtmlAttr.HREF) && !hasContent());
case META:
return (hasAttr(HtmlAttr.CONTENT) && !hasContent());
case SCRIPT:
return ((hasAttr(HtmlAttr.TYPE) && hasAttr(HtmlAttr.SRC) && !hasContent()) ||
(hasAttr(HtmlAttr.TYPE) && hasContent()));
case SPAN:
return (hasAttr(HtmlAttr.ID) || hasContent());
case WBR:
return (!hasContent());
default :
return hasContent();
}
}
/**
* Returns true if the element is a normal element that is <em>phrasing content</em>.
*
* @return true if the HTML tag is an inline element
*
* @see <a href="https://www.w3.org/TR/html51/dom.html#kinds-of-content-phrasing-content">Phrasing Content</a>
*/
public boolean isInline() {
switch (tagName) {
case A: case BUTTON: case BR: case CODE: case EM: case I: case IMG:
case LABEL: case SMALL: case SPAN: case STRONG: case SUB: case SUP:
case WBR:
return true;
default:
return false;
}
}
/**
* Returns whether or not this is a <em>void</em> element.
*
* @return whether or not this is a void element
*
* @see <a href="https://www.w3.org/TR/html51/syntax.html#void-elements">Void Elements</a>
*/
public boolean isVoid() {
switch (tagName) {
case BR: case HR: case IMG: case INPUT: case LINK: case META: case WBR:
return true;
default:
return false;
}
}
@Override
public boolean write(Writer out, boolean atNewline) throws IOException {
boolean isInline = isInline();
if (!isInline && !atNewline)
out.write(DocletConstants.NL);
String tagString = tagName.toString();
out.write("<");
out.write(tagString);
Iterator<HtmlAttr> iterator = attrs.keySet().iterator();
HtmlAttr key;
String value;
while (iterator.hasNext()) {
key = iterator.next();
value = attrs.get(key);
out.write(" ");
out.write(key.toString());
if (!value.isEmpty()) {
out.write("=\"");
out.write(value);
out.write("\"");
}
}
out.write(">");
boolean nl = false;
for (Content c : content)
nl = c.write(out, nl);
if (!isVoid()) {
out.write("</");
out.write(tagString);
out.write(">");
}