-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathHTMLElements.java
More file actions
889 lines (807 loc) · 37.4 KB
/
Copy pathHTMLElements.java
File metadata and controls
889 lines (807 loc) · 37.4 KB
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
/*
* Copyright (c) 2002-2009 Andy Clark, Marc Guillemot
* Copyright (c) 2017-2024 Ronald Brill
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit.cyberneko;
import java.util.Locale;
import org.htmlunit.cyberneko.util.FastHashMap;
/**
* Collection of HTML element information. Parts if it was in the original
* HtmlElements, now HtmlElementsCollection. It has been changed to allow
* modifications at runtime in the sense of caching any lookup results,
* especially for not found elements to speed things up.
*
* @author Andy Clark
* @author Ahmed Ashour
* @author Marc Guillemot
* @author Ronald Brill
* @author Rene Schwietzke
*/
public class HTMLElements {
// element codes
// NOTE: The element codes *must* start with 0 and increment in
// sequence. The parent and closes references depends on
// this assumption. -Ac
// Note 2: This codes are here to maintain compatibility with
// existing code despite having the main use in HtmlElementsCollection.
public static final short A = 0;
public static final short ABBR = A + 1;
public static final short ACRONYM = ABBR + 1;
public static final short ADDRESS = ACRONYM + 1;
public static final short APPLET = ADDRESS + 1;
public static final short AREA = APPLET + 1;
public static final short ARTICLE = AREA + 1;
public static final short ASIDE = ARTICLE + 1;
public static final short AUDIO = ASIDE + 1;
public static final short B = AUDIO + 1;
public static final short BASE = B + 1;
public static final short BASEFONT = BASE + 1;
public static final short BDI = BASEFONT + 1;
public static final short BDO = BDI + 1;
public static final short BGSOUND = BDO + 1;
public static final short BIG = BGSOUND + 1;
public static final short BLINK = BIG + 1;
public static final short BLOCKQUOTE = BLINK + 1;
public static final short BODY = BLOCKQUOTE + 1;
public static final short BR = BODY + 1;
public static final short BUTTON = BR + 1;
public static final short CANVAS = BUTTON + 1;
public static final short CAPTION = CANVAS + 1;
public static final short CENTER = CAPTION + 1;
public static final short CITE = CENTER + 1;
public static final short CODE = CITE + 1;
public static final short COL = CODE + 1;
public static final short COLGROUP = COL + 1;
public static final short COMMENT = COLGROUP + 1;
public static final short DATA = COMMENT + 1;
public static final short DATALIST = DATA + 1;
public static final short DEL = DATALIST + 1;
public static final short DETAILS = DEL + 1;
public static final short DFN = DETAILS + 1;
public static final short DIALOG = DFN + 1;
public static final short DIR = DIALOG + 1;
public static final short DIV = DIR + 1;
public static final short DD = DIV + 1;
public static final short DL = DD + 1;
public static final short DT = DL + 1;
public static final short EM = DT + 1;
public static final short EMBED = EM + 1;
public static final short FIELDSET = EMBED + 1;
public static final short FIGCAPTION = FIELDSET + 1;
public static final short FIGURE = FIGCAPTION + 1;
public static final short FONT = FIGURE + 1;
public static final short FOOTER = FONT + 1;
public static final short FORM = FOOTER + 1;
public static final short FRAME = FORM + 1;
public static final short FRAMESET = FRAME + 1;
public static final short H1 = FRAMESET + 1;
public static final short H2 = H1 + 1;
public static final short H3 = H2 + 1;
public static final short H4 = H3 + 1;
public static final short H5 = H4 + 1;
public static final short H6 = H5 + 1;
public static final short HEAD = H6 + 1;
public static final short HEADER = HEAD + 1;
public static final short HR = HEADER + 1;
public static final short HTML = HR + 1;
public static final short I = HTML + 1;
public static final short IFRAME = I + 1;
public static final short ILAYER = IFRAME + 1;
public static final short IMG = ILAYER + 1;
public static final short IMAGE = IMG + 1;
public static final short INPUT = IMAGE + 1;
public static final short INS = INPUT + 1;
public static final short KBD = INS + 1;
public static final short KEYGEN = KBD + 1;
public static final short LABEL = KEYGEN + 1;
public static final short LAYER = LABEL + 1;
public static final short LEGEND = LAYER + 1;
public static final short LI = LEGEND + 1;
public static final short LINK = LI + 1;
public static final short LISTING = LINK + 1;
public static final short MAIN = LISTING + 1;
public static final short MAP = MAIN + 1;
public static final short MARK = MAP + 1;
public static final short MARQUEE = MARK + 1;
public static final short MENU = MARQUEE + 1;
public static final short META = MENU + 1;
public static final short METER = META + 1;
public static final short MULTICOL = METER + 1;
public static final short NAV = MULTICOL + 1;
public static final short NEXTID = NAV + 1;
public static final short NOBR = NEXTID + 1;
public static final short NOEMBED = NOBR + 1;
public static final short NOFRAMES = NOEMBED + 1;
public static final short NOLAYER = NOFRAMES + 1;
public static final short NOSCRIPT = NOLAYER + 1;
public static final short OBJECT = NOSCRIPT + 1;
public static final short OL = OBJECT + 1;
public static final short OPTGROUP = OL + 1;
public static final short OPTION = OPTGROUP + 1;
public static final short P = OPTION + 1;
public static final short PARAM = P + 1;
public static final short PICTURE = PARAM + 1;
public static final short PLAINTEXT = PICTURE + 1;
public static final short PRE = PLAINTEXT + 1;
public static final short PROGRESS = PRE + 1;
public static final short Q = PROGRESS + 1;
public static final short RB = Q + 1;
public static final short RBC = RB + 1;
public static final short RP = RBC + 1;
public static final short RT = RP + 1;
public static final short RTC = RT + 1;
public static final short RUBY = RTC + 1;
public static final short S = RUBY + 1;
public static final short SAMP = S + 1;
public static final short SCRIPT = SAMP + 1;
public static final short SECTION = SCRIPT + 1;
public static final short SELECT = SECTION + 1;
public static final short SLOT = SELECT + 1;
public static final short SMALL = SLOT + 1;
public static final short SOUND = SMALL + 1;
public static final short SOURCE = SOUND + 1;
public static final short SPACER = SOURCE + 1;
public static final short SPAN = SPACER + 1;
public static final short STRIKE = SPAN + 1;
public static final short STRONG = STRIKE + 1;
public static final short STYLE = STRONG + 1;
public static final short SUB = STYLE + 1;
public static final short SUMMARY = SUB + 1;
public static final short SUP = SUMMARY + 1;
public static final short SVG = SUP + 1;
public static final short TABLE = SVG + 1;
public static final short TBODY = TABLE + 1;
public static final short TD = TBODY + 1;
public static final short TEMPLATE = TD + 1;
public static final short TEXTAREA = TEMPLATE + 1;
public static final short TFOOT = TEXTAREA + 1;
public static final short TH = TFOOT + 1;
public static final short THEAD = TH + 1;
public static final short TIME = THEAD + 1;
public static final short TITLE = TIME + 1;
public static final short TR = TITLE + 1;
public static final short TRACK = TR + 1;
public static final short TT = TRACK + 1;
public static final short OUTPUT = TT + 1;
public static final short U = OUTPUT + 1;
public static final short UL = U + 1;
public static final short VAR = UL + 1;
public static final short VIDEO = VAR + 1;
public static final short WBR = VIDEO + 1;
public static final short XML = WBR + 1;
public static final short XMP = XML + 1;
public static final short UNKNOWN = XMP + 1;
// holds the original elements to query against
private final HTMLElementsCollection htmlElementsCollection_;
// this map helps us to know what elements we don't have and speed things up
private final FastHashMap<String, Boolean> unknownElements_ = new FastHashMap<>(17, 0.50f);
/**
* Create a new lookup instance based on the default elements. This instance
* features a cache for not found elements to speed things up.
*
* @param htmlElements the elements to lookup against
*/
public HTMLElements() {
this.htmlElementsCollection_ = HTMLElementsCollection.DEFAULT;
}
/**
* Create a new lookup instance based on the given elements. This instance
* features a cache for not found elements to speed things up.
*
* @param htmlElements the elements to lookup against
*/
public HTMLElements(final HTMLElementsCollection htmlElements) {
this.htmlElementsCollection_ = htmlElements;
}
/**
* Creates the default setup of HTML elements. We cannot statically set
* them up because some elements refer to others and these references
* might be changed later when adding custom elements.
*
* This is not for public use.
*
* @return the array of arrays of default elements
*/
static Element[][] setupDefaultHTMElements() {
final Element[][] elementsArray = new Element[26][];
// <!ENTITY % heading "H1|H2|H3|H4|H5|H6">
// <!ENTITY % fontstyle "TT | I | B | BIG | SMALL">
// <!ENTITY % phrase "EM | STRONG | DFN | CODE | SAMP | KBD | VAR | CITE | ABBR | ACRONYM" >
// <!ENTITY % special "A | IMG | OBJECT | BR | SCRIPT | MAP | Q | SUB | SUP | SPAN | BDO">
// <!ENTITY % formctrl "INPUT | SELECT | TEXTAREA | LABEL | BUTTON">
// <!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">
// <!ENTITY % block "P | %heading; | %list; | %preformatted; | DL | DIV
// | NOSCRIPT | BLOCKQUOTE | FORM | HR | TABLE | FIELDSET | ADDRESS">
// <!ENTITY % flow "%block; | %inline;">
// initialize array of element information
elementsArray['A' - 'A'] = new Element[] {
// A - - (%inline;)* -(A)
new Element(A, "A", Element.CONTAINER, BODY, new short[] {A}),
// ABBR - - (%inline;)*
new Element(ABBR, "ABBR", Element.INLINE, BODY, null),
// ACRONYM - - (%inline;)*
new Element(ACRONYM, "ACRONYM", Element.INLINE, BODY, null),
// ADDRESS - - (%inline;)*
new Element(ADDRESS, "ADDRESS", Element.BLOCK, BODY, new short[] {P}),
// APPLET
new Element(APPLET, "APPLET", Element.CONTAINER, BODY, null),
// AREA - O EMPTY
new Element(AREA, "AREA", Element.EMPTY, BODY, null),
new Element(ARTICLE, "ARTICLE", Element.BLOCK, BODY, new short[] {P}),
new Element(ASIDE, "ASIDE", Element.BLOCK, BODY, new short[] {P}),
new Element(AUDIO, "AUDIO", Element.CONTAINER, BODY, null),
};
elementsArray['B' - 'A'] = new Element[] {
// B - - (%inline;)*
new Element(B, "B", Element.INLINE, BODY, new short[] {SVG}),
// BASE - O EMPTY
new Element(BASE, "BASE", Element.EMPTY, HEAD, null),
// BASEFONT
new Element(BASEFONT, "BASEFONT", Element.EMPTY, HEAD, null),
new Element(BDI, "BDI", Element.INLINE, BODY, null),
// BDO - - (%inline;)*
new Element(BDO, "BDO", Element.INLINE, BODY, null),
// BGSOUND
new Element(BGSOUND, "BGSOUND", Element.EMPTY, HEAD, null),
// BIG - - (%inline;)*
new Element(BIG, "BIG", Element.INLINE, BODY, new short[]{SVG}),
// BLINK
new Element(BLINK, "BLINK", Element.INLINE, BODY, null),
// BLOCKQUOTE - - (%block;|SCRIPT)+
new Element(BLOCKQUOTE, "BLOCKQUOTE", Element.BLOCK, BODY, new short[]{P, SVG}),
// BODY O O (%block;|SCRIPT)+ +(INS|DEL)
new Element(BODY, "BODY", Element.CONTAINER, HTML, new short[]{HEAD, SVG}),
// BR - O EMPTY
new Element(BR, "BR", Element.EMPTY, BODY, new short[]{SVG}),
// BUTTON - - (%flow;)* -(A|%formctrl;|FORM|FIELDSET)
new Element(BUTTON, "BUTTON", Element.INLINE | Element.BLOCK, BODY, new short[]{BUTTON}),
};
elementsArray['C' - 'A'] = new Element[] {
new Element(CANVAS, "CANVAS", Element.CONTAINER, BODY, null),
// CAPTION - - (%inline;)*
new Element(CAPTION, "CAPTION", Element.INLINE, TABLE, null),
// CENTER,
new Element(CENTER, "CENTER", Element.CONTAINER, BODY, new short[] {P, SVG}),
// CITE - - (%inline;)*
new Element(CITE, "CITE", Element.INLINE, BODY, null),
// CODE - - (%inline;)*
new Element(CODE, "CODE", Element.INLINE, BODY, new short[]{SVG}),
// COL - O EMPTY
new Element(COL, "COL", Element.EMPTY, COLGROUP, null),
// COLGROUP - O (COL)*
new Element(COLGROUP, "COLGROUP", Element.CONTAINER, TABLE, new short[]{COL, COLGROUP}),
// COMMENT
new Element(COMMENT, "COMMENT", Element.SPECIAL, HTML, null),
};
elementsArray['D' - 'A'] = new Element[] {
new Element(DATA, "DATA", Element.CONTAINER, BODY, null),
new Element(DATALIST, "DATALIST", Element.CONTAINER, BODY, null),
// DEL - - (%flow;)*
new Element(DEL, "DEL", Element.INLINE, BODY, null),
new Element(DETAILS, "DETAILS", Element.BLOCK, BODY, new short[] {P}),
// DFN - - (%inline;)*
new Element(DFN, "DFN", Element.INLINE, BODY, null),
new Element(DIALOG, "DIALOG", Element.CONTAINER, BODY, new short[] {P}),
// DIR
new Element(DIR, "DIR", Element.CONTAINER, BODY, new short[] {P}),
// DIV - - (%flow;)*
new Element(DIV, "DIV", Element.CONTAINER, BODY, new short[]{P, SVG}),
// DD - O (%flow;)*
new Element(DD, "DD", Element.BLOCK, BODY, new short[]{DT, DD, P, SVG}),
// DL - - (DT|DD)+
new Element(DL, "DL", Element.BLOCK | Element.CONTAINER, BODY, new short[] {P, SVG}),
// DT - O (%inline;)*
new Element(DT, "DT", Element.BLOCK, BODY, new short[]{DT, DD, P, SVG}),
};
elementsArray['E' - 'A'] = new Element[] {
// EM - - (%inline;)*
new Element(EM, "EM", Element.INLINE, BODY, new short[]{SVG}),
// EMBED
new Element(EMBED, "EMBED", Element.EMPTY, BODY, new short[]{SVG}),
};
elementsArray['F' - 'A'] = new Element[] {
// FIELDSET - - (#PCDATA,LEGEND,(%flow;)*)
new Element(FIELDSET, "FIELDSET", Element.CONTAINER, BODY, new short[] {P}),
new Element(FIGCAPTION, "FIGCAPTION", Element.BLOCK, BODY, new short[] {P}),
new Element(FIGURE, "FIGURE", Element.BLOCK, BODY, new short[] {P}),
// FONT
new Element(FONT, "FONT", Element.CONTAINER, BODY, null),
new Element(FOOTER, "FOOTER", Element.BLOCK, BODY, new short[] {P}),
// FORM - - (%block;|SCRIPT)+ -(FORM)
new Element(FORM, "FORM", Element.CONTAINER, new short[]{BODY, TD, DIV}, new short[]{P}),
// FRAME - O EMPTY
new Element(FRAME, "FRAME", Element.EMPTY, FRAMESET, null),
// FRAMESET - - ((FRAMESET|FRAME)+ & NOFRAMES?)
new Element(FRAMESET, "FRAMESET", Element.CONTAINER, HTML, new short[]{HEAD}),
};
elementsArray['H' - 'A'] = new Element[] {
// (H1|H2|H3|H4|H5|H6) - - (%inline;)*
new Element(H1, "H1", Element.BLOCK, new short[]{BODY, A}, new short[]{H1, H2, H3, H4, H5, H6, P, SVG}),
new Element(H2, "H2", Element.BLOCK, new short[]{BODY, A}, new short[]{H1, H2, H3, H4, H5, H6, P, SVG}),
new Element(H3, "H3", Element.BLOCK, new short[]{BODY, A}, new short[]{H1, H2, H3, H4, H5, H6, P, SVG}),
new Element(H4, "H4", Element.BLOCK, new short[]{BODY, A}, new short[]{H1, H2, H3, H4, H5, H6, P, SVG}),
new Element(H5, "H5", Element.BLOCK, new short[]{BODY, A}, new short[]{H1, H2, H3, H4, H5, H6, P, SVG}),
new Element(H6, "H6", Element.BLOCK, new short[]{BODY, A}, new short[]{H1, H2, H3, H4, H5, H6, P, SVG}),
// HEAD O O (%head.content;) +(%head.misc;)
new Element(HEAD, "HEAD", 0, HTML, null),
new Element(HEADER, "HEADER", Element.BLOCK, BODY, new short[] {P}),
// HR - O EMPTY
new Element(HR, "HR", Element.EMPTY, new short[]{BODY, SELECT}, new short[]{P, SVG}),
// HTML O O (%html.content;)
new Element(HTML, "HTML", 0, null, null),
};
elementsArray['I' - 'A'] = new Element[] {
// I - - (%inline;)*
new Element(I, "I", Element.INLINE, BODY, new short[]{SVG}),
// IFRAME
new Element(IFRAME, "IFRAME", Element.BLOCK, BODY, null),
// ILAYER
new Element(ILAYER, "ILAYER", Element.BLOCK, BODY, null),
// IMG - O EMPTY
new Element(IMG, "IMG", Element.EMPTY, BODY, new short[]{SVG}),
new Element(IMAGE, "IMAGE", Element.EMPTY, BODY, null),
// INPUT - O EMPTY
new Element(INPUT, "INPUT", Element.EMPTY, BODY, null),
// INS - - (%flow;)*
new Element(INS, "INS", Element.INLINE, BODY, null),
};
elementsArray['K' - 'A'] = new Element[] {
// KBD - - (%inline;)*
new Element(KBD, "KBD", Element.INLINE, BODY, null),
// KEYGEN
new Element(KEYGEN, "KEYGEN", Element.EMPTY, BODY, null),
};
elementsArray['L' - 'A'] = new Element[] {
// LABEL - - (%inline;)* -(LABEL)
new Element(LABEL, "LABEL", Element.INLINE, BODY, null),
// LAYER
new Element(LAYER, "LAYER", Element.BLOCK, BODY, null),
// LEGEND - - (%inline;)*
new Element(LEGEND, "LEGEND", Element.INLINE, BODY, null),
// LI - O (%flow;)*
new Element(LI, "LI", Element.CONTAINER, new short[]{BODY, UL, OL, MENU}, new short[]{LI, P, SVG}),
// LINK - O EMPTY
new Element(LINK, "LINK", Element.EMPTY, HEAD, null),
// LISTING
new Element(LISTING, "LISTING", Element.BLOCK, BODY, new short[] {P, SVG}),
};
elementsArray['M' - 'A'] = new Element[] {
new Element(MAIN, "MAIN", Element.BLOCK, BODY, new short[] {P}),
// MAP - - ((%block;) | AREA)+
new Element(MAP, "MAP", Element.INLINE, BODY, null),
new Element(MARK, "MARK", Element.CONTAINER, BODY, null),
// MARQUEE
new Element(MARQUEE, "MARQUEE", Element.CONTAINER, BODY, null),
// MENU
new Element(MENU, "MENU", Element.CONTAINER, BODY, new short[] {P, SVG}),
new Element(METER, "METER", Element.CONTAINER, BODY, null),
// META - O EMPTY
new Element(META, "META", Element.EMPTY, HEAD, new short[]{STYLE, TITLE, SVG}),
// MULTICOL
new Element(MULTICOL, "MULTICOL", Element.CONTAINER, BODY, null),
};
elementsArray['N' - 'A'] = new Element[] {
new Element(NAV, "NAV", Element.BLOCK, BODY, new short[] {P}),
// NEXTID
new Element(NEXTID, "NEXTID", Element.INLINE, BODY, null),
// NOBR
new Element(NOBR, "NOBR", Element.INLINE, BODY, new short[]{NOBR, SVG}),
// NOEMBED
new Element(NOEMBED, "NOEMBED", Element.CONTAINER, BODY, null),
// NOFRAMES - - (BODY) -(NOFRAMES)
new Element(NOFRAMES, "NOFRAMES", Element.CONTAINER, null, null),
// NOLAYER
new Element(NOLAYER, "NOLAYER", Element.CONTAINER, BODY, null),
// NOSCRIPT - - (%block;)+
new Element(NOSCRIPT, "NOSCRIPT", Element.CONTAINER, new short[]{HEAD, BODY}, null),
};
elementsArray['O' - 'A'] = new Element[] {
// OBJECT - - (PARAM | %flow;)*
new Element(OBJECT, "OBJECT", Element.CONTAINER, BODY, null),
// OL - - (LI)+
new Element(OL, "OL", Element.BLOCK, BODY, new short[] {P, SVG}),
// OPTGROUP - - (OPTION)+
new Element(OPTGROUP, "OPTGROUP", Element.INLINE, BODY, new short[]{OPTION}),
// OPTION - O (#PCDATA)
new Element(OPTION, "OPTION", Element.INLINE, BODY, new short[]{OPTION}),
new Element(OUTPUT, "OUTPUT", Element.CONTAINER, BODY, null),
};
elementsArray['P' - 'A'] = new Element[] {
// P - O (%inline;)*
new Element(P, "P", Element.CONTAINER, BODY, new short[]{P, SVG}),
// PARAM - O EMPTY
new Element(PARAM, "PARAM", Element.EMPTY, BODY, null),
new Element(PICTURE, "PICTURE", Element.CONTAINER, BODY, null),
// PLAINTEXT
new Element(PLAINTEXT, "PLAINTEXT", Element.SPECIAL, BODY, new short[]{P}),
// PRE - - (%inline;)* -(%pre.exclusion;)
new Element(PRE, "PRE", Element.BLOCK, BODY, new short[] {P, SVG}),
new Element(PROGRESS, "PROGRESS", Element.CONTAINER, BODY, null),
};
elementsArray['Q' - 'A'] = new Element[] {
// Q - - (%inline;)*
new Element(Q, "Q", Element.INLINE, BODY, null),
};
elementsArray['R' - 'A'] = new Element[] {
// RB
new Element(RB, "RB", Element.INLINE, BODY, null),
// RBC
new Element(RBC, "RBC", 0, BODY, null),
// RP
new Element(RP, "RP", Element.INLINE, BODY, null),
// RT
new Element(RT, "RT", Element.INLINE, BODY, null),
// RTC
new Element(RTC, "RTC", Element.INLINE, BODY, null),
// RUBY
new Element(RUBY, "RUBY", Element.CONTAINER, BODY, new short[]{SVG}),
};
elementsArray['S' - 'A'] = new Element[] {
// S
new Element(S, "S", Element.INLINE, BODY, new short[]{SVG}),
// SAMP - - (%inline;)*
new Element(SAMP, "SAMP", Element.INLINE, BODY, null),
// SCRIPT - - %Script;
new Element(SCRIPT, "SCRIPT", Element.SPECIAL | Element.SCRIPT_SUPPORTING,
new short[]{HEAD, BODY}, null),
new Element(SECTION, "SECTION", Element.BLOCK, BODY, new short[]{SELECT, P}),
// SELECT - - (OPTGROUP|OPTION)+
new Element(SELECT, "SELECT", Element.CONTAINER, BODY, new short[]{SELECT}),
new Element(SLOT, "SLOT", Element.CONTAINER, BODY, null),
// SMALL - - (%inline;)*
new Element(SMALL, "SMALL", Element.INLINE, BODY, new short[]{SVG}),
// SOUND
new Element(SOUND, "SOUND", Element.EMPTY, HEAD, null),
new Element(SOURCE, "SOURCE", Element.EMPTY, BODY, null),
// SPACER
new Element(SPACER, "SPACER", Element.INLINE, BODY, null),
// SPAN - - (%inline;)*
new Element(SPAN, "SPAN", Element.CONTAINER, BODY, new short[]{SVG}),
// STRIKE
new Element(STRIKE, "STRIKE", Element.INLINE, BODY, new short[]{SVG}),
// STRONG - - (%inline;)*
new Element(STRONG, "STRONG", Element.INLINE, BODY, new short[]{SVG}),
// STYLE - - %StyleSheet;
new Element(STYLE, "STYLE", Element.SPECIAL, new short[]{HEAD, BODY}, new short[]{STYLE, TITLE, META}),
// SUB - - (%inline;)*
new Element(SUB, "SUB", Element.INLINE, BODY, new short[]{SVG}),
new Element(SUMMARY, "SUMMARY", Element.BLOCK, BODY, new short[] {P}),
// SUP - - (%inline;)*
new Element(SUP, "SUP", Element.INLINE, BODY, new short[]{SVG}),
// SVG - - (%SVG;)*
new Element(SVG, "SVG", Element.CONTAINER, BODY, null),
};
elementsArray['T' - 'A'] = new Element[] {
// TABLE - - (CAPTION?, (COL*|COLGROUP*), THEAD?, TFOOT?, TBODY+)
new Element(TABLE, "TABLE", Element.BLOCK | Element.CONTAINER, BODY, new short[]{SVG}),
// TBODY O O (TR)+
new Element(TBODY, "TBODY", 0, TABLE, new short[]{FORM, THEAD, TBODY, TFOOT, TD, TH, TR, COLGROUP}),
// TD - O (%flow;)*
new Element(TD, "TD", Element.CONTAINER, TR, TABLE, new short[]{TD, TH}),
new Element(TEMPLATE, "TEMPLATE", Element.CONTAINER | Element.SCRIPT_SUPPORTING,
new short[]{HEAD, BODY}, null),
// TEXTAREA - - (#PCDATA)
new Element(TEXTAREA, "TEXTAREA", Element.SPECIAL, BODY, null),
// TFOOT - O (TR)+
new Element(TFOOT, "TFOOT", 0, TABLE, new short[]{THEAD, TBODY, TFOOT, TD, TH, TR}),
// TH - O (%flow;)*
new Element(TH, "TH", Element.CONTAINER, TR, TABLE, new short[]{TD, TH}),
// THEAD - O (TR)+
new Element(THEAD, "THEAD", 0, TABLE, new short[]{THEAD, TBODY, TFOOT, TD, TH, TR, COLGROUP}),
new Element(TIME, "TIME", Element.CONTAINER, BODY, null),
// TITLE - - (#PCDATA) -(%head.misc;)
new Element(TITLE, "TITLE", Element.SPECIAL, new short[]{HEAD, BODY}, null),
// TR - O (TH|TD)+
new Element(TR, "TR", Element.BLOCK, new short[]{TBODY, THEAD, TFOOT}, TABLE,
new short[]{FORM, TD, TH, TR, COLGROUP, DIV}),
new Element(TRACK, "TRACK", Element.EMPTY, BODY, null),
// TT - - (%inline;)*
new Element(TT, "TT", Element.INLINE, BODY, new short[]{SVG}),
};
elementsArray['U' - 'A'] = new Element[] {
// U,
new Element(U, "U", Element.INLINE, BODY, new short[]{SVG}),
// UL - - (LI)+
new Element(UL, "UL", Element.CONTAINER, BODY, new short[] {P, SVG}),
};
elementsArray['V' - 'A'] = new Element[] {
// VAR - - (%inline;)*
new Element(VAR, "VAR", Element.INLINE, BODY, new short[]{SVG}),
new Element(VIDEO, "VIDEO", Element.CONTAINER, BODY, null),
};
elementsArray['W' - 'A'] = new Element[] {
// WBR
new Element(WBR, "WBR", Element.EMPTY, BODY, null),
};
elementsArray['X' - 'A'] = new Element[] {
// XML
new Element(XML, "XML", 0, BODY, null),
// XMP
new Element(XMP, "XMP", Element.SPECIAL, BODY, new short[] {P}),
};
return elementsArray;
}
/**
* Lookup table for elements by code. There is not range check applied
* for the sake of performance. Java will check that anyway, so we
* don't have to.
*
* @param code The element code.
* @return the element information for the specified element code.
*/
public final Element getElement(final short code) {
return this.htmlElementsCollection_.getElement(code);
}
/**
* Finds an element by its name and returns the element information. If
* the element is not known, a new element with the UNKNOWN code is
* returned.
*
* @param ename The element name.
* @return the element information for the specified element name or an new instance with the
* UNKNOWN code if not found.
*/
public final Element getElement(final String ename) {
Element element = getElement(ename, this.htmlElementsCollection_.NO_SUCH_ELEMENT);
if (element == this.htmlElementsCollection_.NO_SUCH_ELEMENT) {
element = new Element(UNKNOWN,
ename.toUpperCase(Locale.ROOT),
this.htmlElementsCollection_.NO_SUCH_ELEMENT.flags,
this.htmlElementsCollection_.NO_SUCH_ELEMENT.parentCodes_,
this.htmlElementsCollection_.NO_SUCH_ELEMENT.closes);
element.parent = this.htmlElementsCollection_.NO_SUCH_ELEMENT.parent;
element.parentCodes_ = this.htmlElementsCollection_.NO_SUCH_ELEMENT.parentCodes_;
}
return element;
}
/**
* Looks up an element by its name and returns the element information. If the
* current form of the name is not found, it tries again with the lowercase form.
* If still not found, the given default element is returned.
*
* @param ename The element name.
* @param element The default element to return if not found.
*
* @return the element information for the specified element name.
*/
public final Element getElement(final String ename, final Element element) {
// check the current form casing first, which is mostly lowercase only
Element r = this.htmlElementsCollection_.lookupElement(ename);
// we have not found it, so it might feature different casing
if (r == null) {
// check first if we know that we don't know and avoid the
// lowercasing later
if (this.unknownElements_.get(ename) != null) {
// we added it to the cache, so we know it has been
// queried once unsuccessfully before
return element;
}
// we have not found it in its current form, might be uppercase
// or mixed case, so try all lowercase for sanity, we speculated that
// good HTML is mostly all lowercase in the first place so this is the
// fallback for atypical HTML
// we also have not seen that element missing yet
r = this.htmlElementsCollection_.lookupElement(ename.toLowerCase(Locale.ROOT));
// remember that we had a miss
if (r == null) {
this.unknownElements_.put(ename, Boolean.TRUE);
return element;
}
}
return r;
}
/**
* Element information.
*
* @author Andy Clark
*/
public static class Element {
/** Inline element. */
public static final int INLINE = 0x01;
/** Block element. */
public static final int BLOCK = 0x02;
/** Empty element. */
public static final int EMPTY = 0x04;
/** Container element. */
public static final int CONTAINER = 0x08;
/** Special element. */
public static final int SPECIAL = 0x10;
/**
* Script-supporting elements.
* <a href='https://html.spec.whatwg.org/#script-supporting-elements'>Script-supporting elements</a>
*/
public static final int SCRIPT_SUPPORTING = 0x20;
/** The element code. */
public final short code;
/**
* hash code base on the name, kept here to avoid one extra memory
* access as well as the extra logic in String.hashCode()
*/
private final int hashCode_;
/** The element name. */
public final String name;
/** The element name. */
public final String lowercaseName;
/** Informational flags. */
public final int flags;
/**
* Parent elements, having that open is dangerous but for
* legacy reasons we won't change that right now
*/
public Element[] parent;
/** The bounding element code. */
public final short bounds;
/** List of elements this element can close. */
public final short[] closes;
/** Parent elements. */
short[] parentCodes_;
/**
* Constructs an element object.
*
* @param code The element code.
* @param name The element name.
* @param flags Informational flags
* @param parent Natural closing parent name.
* @param closes List of elements this element can close.
*/
public Element(final short code, final String name, final int flags,
final short parent, final short[] closes) {
this(code, name, flags, new short[]{parent}, (short) -1, closes);
}
/**
* Constructs an element object.
*
* @param code The element code.
* @param name The element name.
* @param flags Informational flags
* @param parent Natural closing parent name.
* @param bounds bounds
* @param closes List of elements this element can close.
*/
public Element(final short code, final String name, final int flags,
final short parent, final short bounds, final short[] closes) {
this(code, name, flags, new short[]{parent}, bounds, closes);
}
/**
* Constructs an element object.
*
* @param code The element code.
* @param name The element name.
* @param flags Informational flags
* @param parents Natural closing parent names.
* @param closes List of elements this element can close.
*/
public Element(final short code, final String name, final int flags,
final short[] parents, final short[] closes) {
this(code, name, flags, parents, (short) -1, closes);
}
/**
* Constructs an element object.
*
* @param code The element code.
* @param name The element name.
* @param flags Informational flags
* @param parents Natural closing parent names.
* @param bounds bounds
* @param closes List of elements this element can close.
*/
public Element(final short code, final String name, final int flags,
final short[] parents, final short bounds, final short[] closes) {
this.code = code;
this.name = name;
this.lowercaseName = name.toLowerCase(Locale.ROOT);
this.flags = flags;
this.parentCodes_ = parents;
this.parent = null;
this.bounds = bounds;
this.closes = closes;
// the name is never null, if so, the lowercase would have blown
// up already
this.hashCode_ = name.hashCode();
}
/**
* @return true if this element is an inline element.
*/
public final boolean isInline() {
return (flags & INLINE) != 0;
}
/**
* @return true if this element is a block element.
*/
public final boolean isBlock() {
return (flags & BLOCK) != 0;
}
/**
* @return true if this element is an empty element.
*/
public final boolean isEmpty() {
return (flags & EMPTY) != 0;
}
/**
* @return true if this element is a container element.
*/
public final boolean isContainer() {
return (flags & CONTAINER) != 0;
}
/**
* @return true if this element is special -- if its content
* should be parsed ignoring markup.
*/
public final boolean isSpecial() {
return (flags & SPECIAL) != 0;
}
/**
* @return true if this element is a script-supporting one.
*/
public final boolean isScriptSupporting() {
return (flags & SCRIPT_SUPPORTING) != 0;
}
/**
* @return true if this element can close the specified Element.
*
* @param tag The element.
*/
public boolean closes(final short tag) {
if (closes != null) {
for (final short close : closes) {
if (close == tag) {
return true;
}
}
}
return false;
}
/**
* @return the hash code for this object.
*/
@Override
public int hashCode() {
return this.hashCode_;
}
/**
* @return true if the objects are equal.
*/
@Override
public boolean equals(final Object o) {
if (o instanceof Element) {
final Element e = (Element) o;
return name.equals(e.name) || lowercaseName.equals(e.name);
}
return false;
}
/**
* @return a simple representation to make debugging easier
*/
@Override
public String toString() {
return super.toString() + "(name=" + name + ")";
}
/**
* Indicates if the provided element is an accepted parent of current element.
* @param element the element to test for "paternity"
* @return <code>true</code> if <code>element</code> belongs to the {@link #parent}
*/
public boolean isParent(final Element element) {
if (parent == null) {
return false;
}
for (final Element element2 : parent) {
if (element.code == element2.code) {
return true;
}
}
return false;
}
}
}