1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html.serializer;
16
17 import static org.htmlunit.BrowserVersionFeatures.JS_INNER_TEXT_SVG_NL;
18
19 import java.util.ArrayList;
20 import java.util.IdentityHashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.htmlunit.Page;
26 import org.htmlunit.WebWindow;
27 import org.htmlunit.css.ComputedCssStyleDeclaration;
28 import org.htmlunit.css.StyleAttributes.Definition;
29 import org.htmlunit.html.DomCDataSection;
30 import org.htmlunit.html.DomComment;
31 import org.htmlunit.html.DomElement;
32 import org.htmlunit.html.DomNode;
33 import org.htmlunit.html.DomText;
34 import org.htmlunit.html.HtmlBody;
35 import org.htmlunit.html.HtmlBreak;
36 import org.htmlunit.html.HtmlCheckBoxInput;
37 import org.htmlunit.html.HtmlDetails;
38 import org.htmlunit.html.HtmlElement;
39 import org.htmlunit.html.HtmlExample;
40 import org.htmlunit.html.HtmlHiddenInput;
41 import org.htmlunit.html.HtmlInlineFrame;
42 import org.htmlunit.html.HtmlInput;
43 import org.htmlunit.html.HtmlListing;
44 import org.htmlunit.html.HtmlMenu;
45 import org.htmlunit.html.HtmlNoFrames;
46 import org.htmlunit.html.HtmlNoScript;
47 import org.htmlunit.html.HtmlOption;
48 import org.htmlunit.html.HtmlOrderedList;
49 import org.htmlunit.html.HtmlPlainText;
50 import org.htmlunit.html.HtmlPreformattedText;
51 import org.htmlunit.html.HtmlRadioButtonInput;
52 import org.htmlunit.html.HtmlResetInput;
53 import org.htmlunit.html.HtmlScript;
54 import org.htmlunit.html.HtmlSelect;
55 import org.htmlunit.html.HtmlStyle;
56 import org.htmlunit.html.HtmlSubmitInput;
57 import org.htmlunit.html.HtmlSummary;
58 import org.htmlunit.html.HtmlSvg;
59 import org.htmlunit.html.HtmlTable;
60 import org.htmlunit.html.HtmlTableBody;
61 import org.htmlunit.html.HtmlTableCell;
62 import org.htmlunit.html.HtmlTableFooter;
63 import org.htmlunit.html.HtmlTableHeader;
64 import org.htmlunit.html.HtmlTableRow;
65 import org.htmlunit.html.HtmlTextArea;
66 import org.htmlunit.html.HtmlTitle;
67 import org.htmlunit.html.HtmlUnorderedList;
68 import org.htmlunit.html.TableRowGroup;
69 import org.htmlunit.html.serializer.HtmlSerializerVisibleText.HtmlSerializerTextBuilder.Mode;
70 import org.htmlunit.svg.SvgDesc;
71 import org.htmlunit.svg.SvgTitle;
72 import org.htmlunit.util.StringUtils;
73
74
75
76
77
78
79
80
81
82 public class HtmlSerializerVisibleText {
83
84
85 private static final int ESTIMATED_CHAR_WIDTH = 16;
86
87
88 private static final int ESTIMATED_LINE_HEIGHT = 20;
89
90
91
92
93
94 private static final Set<String> BLOCK_DISPLAYS = Set.of(
95 "block", "list-item", "flex", "grid", "flow-root", "contents",
96 "table", "table-row", "table-caption",
97 "table-row-group", "table-header-group", "table-footer-group",
98 "ruby", "ruby-base", "ruby-text", "ruby-text-container");
99
100
101
102
103
104
105
106 private final Map<DomNode, Boolean> displayedCache_ = new IdentityHashMap<>();
107
108 private final Map<DomNode, Boolean> transparentCache_ = new IdentityHashMap<>();
109
110 private final Map<DomNode, Mode> whiteSpaceCache_ = new IdentityHashMap<>();
111
112
113 private final boolean webDriverVisibility_;
114
115
116
117
118 public HtmlSerializerVisibleText() {
119 this(true);
120 }
121
122
123
124
125
126
127
128
129
130
131 public HtmlSerializerVisibleText(final boolean webDriverVisibility) {
132 webDriverVisibility_ = webDriverVisibility;
133 }
134
135
136
137
138
139
140 public String asText(final DomNode node) {
141 if (node instanceof HtmlBreak) {
142 return "";
143 }
144
145 displayedCache_.clear();
146 transparentCache_.clear();
147 whiteSpaceCache_.clear();
148
149 final HtmlSerializerTextBuilder builder = new HtmlSerializerTextBuilder();
150 appendNode(builder, node, whiteSpaceStyle(node, Mode.WHITE_SPACE_NORMAL));
151 return builder.getText();
152 }
153
154
155
156
157
158
159
160
161 protected void appendChildren(final HtmlSerializerTextBuilder builder, final DomNode node, final Mode mode) {
162 final Mode childMode = updateWhiteSpaceStyle(node, mode);
163 for (final DomNode child : node.getChildren()) {
164 appendNode(builder, child, childMode);
165 }
166 }
167
168
169
170
171
172
173
174
175
176 protected void appendNode(final HtmlSerializerTextBuilder builder, final DomNode node, final Mode mode) {
177 if (node instanceof DomCDataSection) {
178
179 }
180 else if (node instanceof DomText text1) {
181 appendText(builder, text1, mode);
182 }
183 else if (node instanceof DomComment comment) {
184 appendComment(builder, comment, mode);
185 }
186 else if (node instanceof HtmlBreak break1) {
187 appendBreak(builder, break1, mode);
188 }
189 else if (node instanceof HtmlHiddenInput input4) {
190 appendHiddenInput(builder, input4, mode);
191 }
192 else if (node instanceof HtmlScript script1) {
193 appendScript(builder, script1, mode);
194 }
195 else if (node instanceof HtmlStyle style) {
196 appendStyle(builder, style, mode);
197 }
198 else if (node instanceof HtmlNoFrames frames) {
199 appendNoFrames(builder, frames, mode);
200 }
201 else if (node instanceof HtmlTextArea area) {
202 appendTextArea(builder, area, mode);
203 }
204 else if (node instanceof HtmlTitle title) {
205 appendTitle(builder, title, mode);
206 }
207 else if (node instanceof HtmlTableRow row) {
208 appendTableRow(builder, row, mode);
209 }
210 else if (node instanceof HtmlSelect select) {
211 appendSelect(builder, select, mode);
212 }
213 else if (node instanceof HtmlOption option) {
214 appendOption(builder, option, mode);
215 }
216 else if (node instanceof HtmlSubmitInput input3) {
217 appendSubmitInput(builder, input3, mode);
218 }
219 else if (node instanceof HtmlResetInput input2) {
220 appendResetInput(builder, input2, mode);
221 }
222 else if (node instanceof HtmlCheckBoxInput input1) {
223 appendCheckBoxInput(builder, input1, mode);
224 }
225 else if (node instanceof HtmlRadioButtonInput input) {
226 appendRadioButtonInput(builder, input, mode);
227 }
228 else if (node instanceof HtmlInput) {
229
230 }
231 else if (node instanceof HtmlTable table) {
232 appendTable(builder, table, mode);
233 }
234 else if (node instanceof HtmlOrderedList list1) {
235 appendOrderedList(builder, list1, mode);
236 }
237 else if (node instanceof HtmlUnorderedList list) {
238 appendUnorderedList(builder, list, mode);
239 }
240 else if (node instanceof HtmlPreformattedText
241 || node instanceof HtmlListing
242 || node instanceof HtmlExample
243 || node instanceof HtmlPlainText) {
244 appendPreformattedText(builder, (HtmlElement) node, mode);
245 }
246 else if (node instanceof HtmlInlineFrame frame) {
247 appendInlineFrame(builder, frame, mode);
248 }
249 else if (node instanceof HtmlMenu menu) {
250 appendMenu(builder, menu, mode);
251 }
252 else if (node instanceof HtmlDetails details) {
253 appendDetails(builder, details, mode);
254 }
255 else if (node instanceof HtmlSvg svg) {
256 appendSvg(builder, svg, mode);
257 }
258 else if (node instanceof HtmlNoScript script && node.getPage().getWebClient().isJavaScriptEnabled()) {
259 appendNoScript(builder, script, mode);
260 }
261 else {
262 appendDomNode(builder, node, mode);
263 }
264 }
265
266
267
268
269
270
271
272
273 protected void appendDomNode(final HtmlSerializerTextBuilder builder,
274 final DomNode domNode, final Mode mode) {
275 final boolean block;
276 if (domNode instanceof HtmlBody) {
277 block = false;
278 }
279 else if (domNode instanceof DomElement element) {
280 block = isBlockLevel(element);
281 }
282 else {
283 block = false;
284 }
285
286 if (block) {
287 builder.appendBlockSeparator();
288 }
289 appendChildren(builder, domNode, mode);
290 if (block) {
291 builder.appendBlockSeparator();
292 }
293 else if (isCssTableCell(domNode) && isCssTableCell(domNode.getNextElementSibling())) {
294 builder.appendCellSeparator();
295 }
296 }
297
298
299
300
301
302
303
304 private static boolean isCssTableCell(final DomNode node) {
305 if (node instanceof DomElement element && !(node instanceof HtmlTableCell)) {
306 final WebWindow window = node.getPage().getEnclosingWindow();
307 return "table-cell".equals(window.getComputedStyle(element, null).getDisplay());
308 }
309 return false;
310 }
311
312
313
314
315
316
317
318
319 protected void appendHiddenInput(final HtmlSerializerTextBuilder builder,
320 final HtmlHiddenInput htmlHiddenInput, final Mode mode) {
321
322 }
323
324
325
326
327
328
329
330
331 protected void appendScript(final HtmlSerializerTextBuilder builder,
332 final HtmlScript htmlScript, final Mode mode) {
333
334 }
335
336
337
338
339
340
341
342
343 protected void appendStyle(final HtmlSerializerTextBuilder builder,
344 final HtmlStyle htmlStyle, final Mode mode) {
345
346 }
347
348
349
350
351
352
353
354
355 protected void appendNoScript(final HtmlSerializerTextBuilder builder,
356 final HtmlNoScript htmlNoScript, final Mode mode) {
357
358 }
359
360
361
362
363
364
365
366
367 protected void appendNoFrames(final HtmlSerializerTextBuilder builder,
368 final HtmlNoFrames htmlNoFrames, final Mode mode) {
369
370 }
371
372
373
374
375
376
377
378
379 protected void appendSubmitInput(final HtmlSerializerTextBuilder builder,
380 final HtmlSubmitInput htmlSubmitInput, final Mode mode) {
381
382 }
383
384
385
386
387
388
389
390
391 protected void appendInput(final HtmlSerializerTextBuilder builder,
392 final HtmlInput htmlInput, final Mode mode) {
393 builder.append(htmlInput.getValueAttribute(), mode);
394 }
395
396
397
398
399
400
401
402
403 protected void appendResetInput(final HtmlSerializerTextBuilder builder,
404 final HtmlResetInput htmlResetInput, final Mode mode) {
405
406 }
407
408
409
410
411
412
413
414
415 private static boolean isBlockLevel(final DomNode node) {
416 if (node instanceof DomElement element) {
417 final WebWindow window = node.getPage().getEnclosingWindow();
418 final ComputedCssStyleDeclaration style = window.getComputedStyle(element, null);
419 final String display = style.getDisplay();
420 if (BLOCK_DISPLAYS.contains(display)) {
421 return true;
422 }
423
424
425 final String position = style.getPosition();
426 if ("absolute".equals(position) || "fixed".equals(position)) {
427 return true;
428 }
429 final String floatValue = style.getStyleAttribute(Definition.FLOAT, false);
430 if ("left".equals(floatValue) || "right".equals(floatValue)) {
431 return true;
432 }
433
434
435 if (element.getParentNode() instanceof DomElement parent) {
436 final String parentDisplay = window.getComputedStyle(parent, null).getDisplay();
437 return "flex".equals(parentDisplay) || "inline-flex".equals(parentDisplay)
438 || "grid".equals(parentDisplay) || "inline-grid".equals(parentDisplay);
439 }
440 }
441 return false;
442 }
443
444
445
446
447
448
449
450
451 protected void appendMenu(final HtmlSerializerTextBuilder builder,
452 final HtmlMenu htmlMenu, final Mode mode) {
453 builder.appendBlockSeparator();
454 boolean first = true;
455 for (final DomNode item : htmlMenu.getChildren()) {
456 if (!first) {
457 builder.appendBlockSeparator();
458 }
459 first = false;
460 appendNode(builder, item, mode);
461 }
462 builder.appendBlockSeparator();
463 }
464
465
466
467
468
469
470
471
472 protected void appendDetails(final HtmlSerializerTextBuilder builder,
473 final HtmlDetails htmlDetails, final Mode mode) {
474 if (htmlDetails.isOpen()) {
475 appendChildren(builder, htmlDetails, mode);
476 return;
477 }
478
479
480
481 for (final DomNode child : htmlDetails.getChildren()) {
482 if (child instanceof HtmlSummary || child instanceof DomText) {
483 appendNode(builder, child, mode);
484 }
485 }
486 }
487
488
489
490
491
492
493
494
495 protected void appendTitle(final HtmlSerializerTextBuilder builder,
496 final HtmlTitle htmlTitle, final Mode mode) {
497
498 }
499
500
501
502
503
504
505
506
507 protected void appendTableRow(final HtmlSerializerTextBuilder builder,
508 final HtmlTableRow htmlTableRow, final Mode mode) {
509 boolean first = true;
510 for (final HtmlTableCell cell : htmlTableRow.getCells()) {
511 if (first) {
512 first = false;
513 }
514 else {
515 builder.appendCellSeparator();
516 }
517 appendChildren(builder, cell, mode);
518 }
519 }
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534 protected boolean isDisplayed(final DomNode domNode) {
535 return displayedCache_.computeIfAbsent(domNode, node -> node.isDisplayed() && !isTransparent(node));
536 }
537
538
539
540
541
542
543
544
545
546
547
548 private static boolean isOffscreen(final DomElement element, final ComputedCssStyleDeclaration style) {
549 final String position = style.getPosition();
550 if (!"absolute".equals(position) && !"fixed".equals(position)) {
551 return false;
552 }
553
554 final int left = pixelValue(style.getStyleAttribute(Definition.LEFT, false));
555 if (left < 0) {
556 int width = pixelValue(style.getStyleAttribute(Definition.WIDTH, false));
557 if (width <= 0) {
558 width = element.getTextContent().length() * ESTIMATED_CHAR_WIDTH;
559 }
560 if (left + width <= 0) {
561 return true;
562 }
563 }
564
565 final int top = pixelValue(style.getStyleAttribute(Definition.TOP, false));
566 if (top < 0) {
567 int height = pixelValue(style.getStyleAttribute(Definition.HEIGHT, false));
568 if (height <= 0) {
569 height = ESTIMATED_LINE_HEIGHT;
570 }
571 return top + height <= 0;
572 }
573 return false;
574 }
575
576
577
578
579
580
581
582 private static int pixelValue(final String length) {
583 if (StringUtils.isEmptyOrNull(length) || !length.endsWith("px")) {
584 return 0;
585 }
586 try {
587 return (int) Double.parseDouble(length.substring(0, length.length() - 2).trim());
588 }
589 catch (final NumberFormatException e) {
590 return 0;
591 }
592 }
593
594
595
596
597
598
599
600
601
602 private boolean isTransparent(final DomNode domNode) {
603 if (!webDriverVisibility_) {
604 return false;
605 }
606
607 final Page page = domNode.getPage();
608 if (page == null) {
609 return false;
610 }
611 final WebWindow window = page.getEnclosingWindow();
612 if (!window.getWebClient().getOptions().isCssEnabled()) {
613 return false;
614 }
615
616
617
618 final List<DomNode> path = new ArrayList<>();
619 boolean transparent = false;
620 DomNode node = domNode;
621 while (node != null) {
622 final Boolean cached = transparentCache_.get(node);
623 if (cached != null) {
624 transparent = cached;
625 break;
626 }
627 path.add(node);
628 if (node instanceof DomElement element
629 && isTransparentElement(element, window.getComputedStyle(element, null))) {
630 transparent = true;
631 break;
632 }
633 node = node.getParentNode();
634 }
635
636 for (final DomNode pathNode : path) {
637 transparentCache_.put(pathNode, transparent);
638 }
639 return transparent;
640 }
641
642
643
644
645
646
647
648
649
650 private static boolean isTransparentElement(final DomElement element, final ComputedCssStyleDeclaration style) {
651
652
653
654 if ("hidden".equals(style.getStyleAttribute("content-visibility"))
655 || isOffscreen(element, style)) {
656 return true;
657 }
658
659 final String opacity = style.getStyleAttribute(Definition.OPACITY, false);
660 if (!StringUtils.isEmptyOrNull(opacity)) {
661 try {
662 return Double.parseDouble(opacity.trim()) <= 0;
663 }
664 catch (final NumberFormatException e) {
665
666 }
667 }
668 return false;
669 }
670
671
672
673
674
675
676
677
678 protected void appendTextArea(final HtmlSerializerTextBuilder builder,
679 final HtmlTextArea htmlTextArea, final Mode mode) {
680 if (isDisplayed(htmlTextArea)) {
681 builder.append(htmlTextArea.getDefaultValue(), whiteSpaceStyle(htmlTextArea, Mode.PRE));
682 builder.trimRight(Mode.PRE);
683 }
684 }
685
686
687
688
689
690
691
692
693 protected void appendTable(final HtmlSerializerTextBuilder builder,
694 final HtmlTable htmlTable, final Mode mode) {
695 builder.appendBlockSeparator();
696 final String caption = htmlTable.getCaptionText();
697 if (caption != null) {
698 builder.append(caption, mode);
699 builder.appendBlockSeparator();
700 }
701
702 boolean first = true;
703
704
705 final HtmlTableHeader tableHeader = htmlTable.getHeader();
706 if (tableHeader != null) {
707 first = appendTableRows(builder, mode, tableHeader.getRows(), true, null, null);
708 }
709 final HtmlTableFooter tableFooter = htmlTable.getFooter();
710
711
712 final boolean footerFirst = tableFooter != null && isFooterBeforeBody(htmlTable);
713 if (footerFirst) {
714 first = appendTableRows(builder, mode, tableFooter.getRows(), first, null, null);
715 }
716
717 final List<HtmlTableRow> tableRows = htmlTable.getRows();
718 first = appendTableRows(builder, mode, tableRows, first, tableHeader, tableFooter);
719
720 if (tableFooter != null && !footerFirst) {
721 first = appendTableRows(builder, mode, tableFooter.getRows(), first, null, null);
722 }
723 else if (tableRows.isEmpty()) {
724 final DomNode firstChild = htmlTable.getFirstChild();
725 if (firstChild != null) {
726 appendNode(builder, firstChild, mode);
727 }
728 }
729
730 builder.appendBlockSeparator();
731 }
732
733
734
735
736
737
738
739
740
741
742
743
744 protected boolean appendTableRows(final HtmlSerializerTextBuilder builder, final Mode mode,
745 final List<HtmlTableRow> rows, final boolean first, final TableRowGroup skipParent1,
746 final TableRowGroup skipParent2) {
747 boolean isFirst = first;
748 for (final HtmlTableRow row : rows) {
749 if (row.getParentNode() == skipParent1 || row.getParentNode() == skipParent2) {
750 continue;
751 }
752 if (!isFirst) {
753 builder.appendBlockSeparator();
754 }
755 isFirst = false;
756 appendTableRow(builder, row, mode);
757 }
758 return isFirst;
759 }
760
761
762
763
764
765
766
767
768 private static boolean isFooterBeforeBody(final HtmlTable htmlTable) {
769 for (final DomNode child : htmlTable.getChildren()) {
770 if (child instanceof HtmlTableFooter) {
771 return true;
772 }
773 if (child instanceof HtmlTableBody || child instanceof HtmlTableRow) {
774 return false;
775 }
776 }
777 return false;
778 }
779
780
781
782
783
784
785
786
787 protected void appendSelect(final HtmlSerializerTextBuilder builder,
788 final HtmlSelect htmlSelect, final Mode mode) {
789
790 final List<HtmlOption> options = htmlSelect.getOptions();
791 if (options.isEmpty()) {
792 return;
793 }
794
795 builder.appendBlockSeparator();
796 boolean leadingNlPending = false;
797
798
799 for (final HtmlOption item : options) {
800 if (leadingNlPending) {
801 builder.appendBlockSeparator();
802 }
803
804 builder.resetContentAdded();
805 appendNode(builder, item, mode);
806 leadingNlPending = builder.wasContentAdded();
807 }
808 builder.appendBlockSeparator();
809 }
810
811
812
813
814
815
816
817
818 protected void appendOption(final HtmlSerializerTextBuilder builder,
819 final HtmlOption htmlOption, final Mode mode) {
820
821
822 if (htmlOption.getEnclosingSelect() == null) {
823 builder.appendBlockSeparator();
824 builder.appendBlockSeparator();
825 return;
826 }
827
828
829
830 for (final DomNode child : htmlOption.getChildren()) {
831 appendNode(builder, child, Mode.WHITE_SPACE_NORMAL);
832 }
833 }
834
835
836
837
838
839
840
841
842 protected void appendOrderedList(final HtmlSerializerTextBuilder builder,
843 final HtmlOrderedList htmlOrderedList, final Mode mode) {
844 appendListItems(builder, htmlOrderedList, mode);
845 }
846
847
848
849
850
851
852
853
854 protected void appendUnorderedList(final HtmlSerializerTextBuilder builder,
855 final HtmlUnorderedList htmlUnorderedList, final Mode mode) {
856 appendListItems(builder, htmlUnorderedList, mode);
857 }
858
859
860
861
862
863
864
865
866
867 private void appendListItems(final HtmlSerializerTextBuilder builder,
868 final HtmlElement list, final Mode mode) {
869 builder.appendBlockSeparator();
870 boolean leadingNlPending = false;
871 final Mode listMode = whiteSpaceStyle(list, mode);
872 for (final DomNode item : list.getChildren()) {
873 if (leadingNlPending) {
874 builder.appendBlockSeparator();
875 }
876
877 builder.resetContentAdded();
878 appendNode(builder, item, whiteSpaceStyle(item, listMode));
879 leadingNlPending = builder.wasContentAdded() && isBlockLevel(item);
880 }
881 builder.appendBlockSeparator();
882 }
883
884
885
886
887
888
889
890
891 protected void appendPreformattedText(final HtmlSerializerTextBuilder builder,
892 final HtmlElement htmlPreformattedText, final Mode mode) {
893 if (isDisplayed(htmlPreformattedText)) {
894 builder.appendBlockSeparator();
895 appendChildren(builder, htmlPreformattedText, whiteSpaceStyle(htmlPreformattedText, Mode.PRE));
896 builder.appendBlockSeparator();
897 }
898 }
899
900
901
902
903
904
905
906
907
908 protected void appendSvg(final HtmlSerializerTextBuilder builder,
909 final HtmlSvg htmlSvg, final Mode mode) {
910 final boolean block = htmlSvg.getPage().getWebClient().getBrowserVersion()
911 .hasFeature(JS_INNER_TEXT_SVG_NL)
912 && hasSvgText(htmlSvg);
913 if (block) {
914 builder.appendBlockSeparator();
915 }
916 appendChildren(builder, htmlSvg, mode);
917 if (block) {
918 builder.appendBlockSeparator();
919 }
920 }
921
922
923
924
925
926
927
928 private static boolean hasSvgText(final DomNode node) {
929 for (final DomNode child : node.getChildren()) {
930 if (child instanceof SvgTitle || child instanceof SvgDesc) {
931 continue;
932 }
933 if (child instanceof DomText text) {
934 if (!StringUtils.isBlank(text.getData())) {
935 return true;
936 }
937 }
938 else if (hasSvgText(child)) {
939 return true;
940 }
941 }
942 return false;
943 }
944
945
946
947
948
949
950
951
952 protected void appendInlineFrame(final HtmlSerializerTextBuilder builder,
953 final HtmlInlineFrame htmlInlineFrame, final Mode mode) {
954
955
956 if (isDisplayed(htmlInlineFrame)) {
957 appendChildren(builder, htmlInlineFrame, mode);
958 }
959 }
960
961
962
963
964
965
966
967
968 protected void appendText(final HtmlSerializerTextBuilder builder, final DomText domText, final Mode mode) {
969 final DomNode parent = domText.getParentNode();
970 if (parent instanceof HtmlTitle
971 || parent instanceof HtmlScript) {
972 builder.append(domText.getData(), Mode.WHITE_SPACE_PRE_LINE);
973 return;
974 }
975
976
977 if (parent == null
978 || parent instanceof HtmlOption
979 || isDisplayed(parent)) {
980 String data = domText.getData();
981
982 if (data.indexOf('\u200B') > -1) {
983 data = data.replace("\u200B", "");
984 }
985 builder.append(data, mode);
986 }
987 }
988
989
990
991
992
993
994
995
996 protected void appendComment(final HtmlSerializerTextBuilder builder,
997 final DomComment domComment, final Mode mode) {
998
999 }
1000
1001
1002
1003
1004
1005
1006
1007
1008 protected void appendBreak(final HtmlSerializerTextBuilder builder,
1009 final HtmlBreak htmlBreak, final Mode mode) {
1010
1011 if (builder.isEmpty()) {
1012 return;
1013 }
1014 builder.appendBreak(mode);
1015 }
1016
1017
1018
1019
1020
1021
1022
1023
1024 protected void appendCheckBoxInput(final HtmlSerializerTextBuilder builder,
1025 final HtmlCheckBoxInput htmlCheckBoxInput, final Mode mode) {
1026
1027 }
1028
1029
1030
1031
1032
1033
1034
1035
1036 protected void appendRadioButtonInput(final HtmlSerializerTextBuilder builder,
1037 final HtmlRadioButtonInput htmlRadioButtonInput, final Mode mode) {
1038
1039 }
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049 protected Mode whiteSpaceStyle(final DomNode domNode, final Mode defaultMode) {
1050 final Page page = domNode.getPage();
1051 if (page == null) {
1052 return defaultMode;
1053 }
1054 final WebWindow window = page.getEnclosingWindow();
1055 if (!window.getWebClient().getOptions().isCssEnabled()) {
1056 return defaultMode;
1057 }
1058
1059
1060
1061 final List<DomNode> path = new ArrayList<>();
1062 Mode mode = null;
1063 DomNode node = domNode;
1064 while (node != null) {
1065 if (whiteSpaceCache_.containsKey(node)) {
1066 mode = whiteSpaceCache_.get(node);
1067 break;
1068 }
1069 path.add(node);
1070 if (node instanceof DomElement element) {
1071 mode = whiteSpaceMode(window.getComputedStyle(element, null));
1072 if (mode != null) {
1073 break;
1074 }
1075 }
1076 node = node.getParentNode();
1077 }
1078
1079 for (final DomNode pathNode : path) {
1080 whiteSpaceCache_.put(pathNode, mode);
1081 }
1082 return mode == null ? defaultMode : mode;
1083 }
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093 protected Mode updateWhiteSpaceStyle(final DomNode domNode, final Mode defaultMode) {
1094 if (domNode instanceof DomElement element) {
1095 final Page page = element.getPage();
1096 if (page != null) {
1097 final WebWindow window = page.getEnclosingWindow();
1098 if (window.getWebClient().getOptions().isCssEnabled()) {
1099 final Mode mode = whiteSpaceMode(window.getComputedStyle(element, null));
1100 if (mode != null) {
1101 return mode;
1102 }
1103 }
1104 }
1105 }
1106 return defaultMode;
1107 }
1108
1109
1110
1111
1112
1113
1114
1115 private static Mode whiteSpaceMode(final ComputedCssStyleDeclaration style) {
1116 final String value = style.getStyleAttribute(Definition.WHITE_SPACE, false);
1117 if (!StringUtils.isEmptyOrNull(value)) {
1118 if ("normal".equalsIgnoreCase(value) || "nowrap".equalsIgnoreCase(value)) {
1119 return Mode.WHITE_SPACE_NORMAL;
1120 }
1121 if ("pre".equalsIgnoreCase(value) || "pre-wrap".equalsIgnoreCase(value)) {
1122 return Mode.WHITE_SPACE_PRE;
1123 }
1124
1125 if ("pre-line".equalsIgnoreCase(value) || "break-spaces".equalsIgnoreCase(value)) {
1126 return Mode.WHITE_SPACE_PRE_LINE;
1127 }
1128 }
1129
1130 final String collapse = style.getStyleAttribute(Definition.WHITE_SPACE_COLLAPSE, false);
1131 if ("preserve".equalsIgnoreCase(collapse)) {
1132 return Mode.WHITE_SPACE_PRE;
1133 }
1134 if ("preserve-breaks".equalsIgnoreCase(collapse)
1135 || "break-spaces".equalsIgnoreCase(collapse)) {
1136 return Mode.WHITE_SPACE_PRE_LINE;
1137 }
1138 return null;
1139 }
1140
1141
1142
1143
1144 protected static class HtmlSerializerTextBuilder {
1145
1146 protected enum Mode {
1147
1148
1149
1150 PRE,
1151
1152
1153
1154
1155
1156
1157 WHITE_SPACE_NORMAL,
1158
1159
1160
1161
1162
1163 WHITE_SPACE_PRE,
1164
1165
1166
1167
1168
1169
1170 WHITE_SPACE_PRE_LINE
1171 }
1172
1173 private enum State {
1174 DEFAULT,
1175 EMPTY,
1176 BLANK_AT_END,
1177 NEWLINE_AT_END,
1178 BREAK_AT_END,
1179 BLOCK_SEPARATOR_AT_END
1180 }
1181
1182 private State state_;
1183 private final StringBuilder builder_;
1184 private int trimRightPos_;
1185 private boolean contentAdded_;
1186 private int lineStart_;
1187
1188
1189
1190
1191 public HtmlSerializerTextBuilder() {
1192 builder_ = new StringBuilder();
1193 state_ = State.EMPTY;
1194 trimRightPos_ = 0;
1195 }
1196
1197
1198
1199
1200
1201
1202
1203
1204 public void append(final String content, final Mode mode) {
1205 if (content == null) {
1206 return;
1207 }
1208 final int length = content.length();
1209 if (length == 0) {
1210 return;
1211 }
1212
1213 final int lastIndex = length - 1;
1214 for (int i = 0; i < length; i++) {
1215 char c = content.charAt(i);
1216
1217
1218 if (c == '\r') {
1219 if (i != lastIndex) {
1220 continue;
1221 }
1222 c = '\n';
1223 }
1224
1225 if (c == '\n') {
1226 if (mode == Mode.WHITE_SPACE_PRE) {
1227 switch (state_) {
1228 case EMPTY:
1229 case BLOCK_SEPARATOR_AT_END:
1230 break;
1231 default:
1232 builder_.append('\n');
1233 state_ = State.NEWLINE_AT_END;
1234 trimRightPos_ = builder_.length();
1235 break;
1236 }
1237 continue;
1238 }
1239
1240 if (mode == Mode.PRE) {
1241
1242 if (state_ == State.EMPTY) {
1243 continue;
1244 }
1245 builder_.append('\n');
1246 state_ = State.NEWLINE_AT_END;
1247 trimRightPos_ = builder_.length();
1248
1249 continue;
1250 }
1251
1252 if (mode == Mode.WHITE_SPACE_PRE_LINE) {
1253 switch (state_) {
1254 case EMPTY:
1255 case BLOCK_SEPARATOR_AT_END:
1256 break;
1257 default:
1258 builder_.append('\n');
1259 state_ = State.NEWLINE_AT_END;
1260 trimRightPos_ = builder_.length();
1261 break;
1262 }
1263 continue;
1264 }
1265
1266 switch (state_) {
1267 case EMPTY:
1268 case BLANK_AT_END:
1269 case BLOCK_SEPARATOR_AT_END:
1270 case NEWLINE_AT_END:
1271 case BREAK_AT_END:
1272 break;
1273 default:
1274 builder_.append(' ');
1275 state_ = State.BLANK_AT_END;
1276 break;
1277 }
1278 continue;
1279 }
1280
1281 if (c == ' ' || c == '\t' || c == '\f') {
1282 if (mode == Mode.WHITE_SPACE_PRE || mode == Mode.PRE) {
1283 appendBlank();
1284 continue;
1285 }
1286
1287 if (mode == Mode.WHITE_SPACE_PRE_LINE) {
1288 switch (state_) {
1289 case EMPTY:
1290 case BLANK_AT_END:
1291 case BREAK_AT_END:
1292 break;
1293 default:
1294 builder_.append(' ');
1295 state_ = State.BLANK_AT_END;
1296 break;
1297 }
1298 continue;
1299 }
1300
1301 switch (state_) {
1302 case EMPTY:
1303 case BLANK_AT_END:
1304 case BLOCK_SEPARATOR_AT_END:
1305 case NEWLINE_AT_END:
1306 case BREAK_AT_END:
1307 break;
1308 default:
1309 builder_.append(' ');
1310 state_ = State.BLANK_AT_END;
1311 break;
1312 }
1313 continue;
1314 }
1315
1316 if (c == (char) 160) {
1317 appendBlank();
1318 if (mode == Mode.WHITE_SPACE_NORMAL || mode == Mode.WHITE_SPACE_PRE_LINE) {
1319 state_ = State.DEFAULT;
1320 }
1321 continue;
1322 }
1323 builder_.append(c);
1324 state_ = State.DEFAULT;
1325 trimRightPos_ = builder_.length();
1326 contentAdded_ = true;
1327 }
1328 }
1329
1330
1331
1332
1333 public void appendBlockSeparator() {
1334
1335 if (state_ == State.BLANK_AT_END
1336 && trimRightPos_ == builder_.length()
1337 && isWhitespaceOnly(lineStart_)) {
1338 return;
1339 }
1340
1341 final int length = builder_.length();
1342 appendBlockSeparatorWorker();
1343 if (builder_.length() != length) {
1344 lineStart_ = builder_.length();
1345 }
1346 }
1347
1348
1349
1350
1351
1352
1353
1354 private boolean isWhitespaceOnly(final int start) {
1355 for (int i = start; i < builder_.length(); i++) {
1356 final char c = builder_.charAt(i);
1357 if (c != ' ' && c != '\t' && c != '\n' && c != (char) 160) {
1358 return false;
1359 }
1360 }
1361 return true;
1362 }
1363
1364
1365
1366
1367 private void appendBlockSeparatorWorker() {
1368 switch (state_) {
1369 case EMPTY:
1370 break;
1371 case BLANK_AT_END:
1372 builder_.setLength(trimRightPos_);
1373 if (builder_.length() == 0) {
1374 state_ = State.EMPTY;
1375 }
1376 else {
1377 builder_.append('\n');
1378 state_ = State.BLOCK_SEPARATOR_AT_END;
1379 }
1380 break;
1381 case BLOCK_SEPARATOR_AT_END:
1382 break;
1383 case NEWLINE_AT_END:
1384 case BREAK_AT_END:
1385 builder_.setLength(builder_.length() - 1);
1386 trimRightPos_ = trimRightPos_ - 1;
1387 if (builder_.length() == 0) {
1388 state_ = State.EMPTY;
1389 }
1390 else {
1391 builder_.append('\n');
1392 state_ = State.BLOCK_SEPARATOR_AT_END;
1393 }
1394 break;
1395 default:
1396 builder_.append('\n');
1397 state_ = State.BLOCK_SEPARATOR_AT_END;
1398 break;
1399 }
1400 }
1401
1402
1403
1404
1405
1406
1407 public void appendBreak(final Mode mode) {
1408
1409 if (state_ != State.BLOCK_SEPARATOR_AT_END) {
1410 builder_.setLength(trimRightPos_);
1411 }
1412
1413 builder_.append('\n');
1414 state_ = State.BREAK_AT_END;
1415 trimRightPos_ = builder_.length();
1416 lineStart_ = builder_.length();
1417 }
1418
1419
1420
1421
1422 public void appendBlank() {
1423 builder_.append(' ');
1424 state_ = State.BLANK_AT_END;
1425 trimRightPos_ = builder_.length();
1426 }
1427
1428
1429
1430
1431
1432 public void appendCellSeparator() {
1433 switch (state_) {
1434 case EMPTY:
1435 case BLANK_AT_END:
1436 case BLOCK_SEPARATOR_AT_END:
1437 case NEWLINE_AT_END:
1438 case BREAK_AT_END:
1439 return;
1440 default:
1441 builder_.append(' ');
1442 state_ = State.BLANK_AT_END;
1443 }
1444 }
1445
1446
1447
1448
1449
1450
1451 public void trimRight(final Mode mode) {
1452 if (mode == Mode.PRE) {
1453 switch (state_) {
1454 case BLOCK_SEPARATOR_AT_END:
1455 case NEWLINE_AT_END:
1456 case BREAK_AT_END:
1457 if (trimRightPos_ == builder_.length()) {
1458 trimRightPos_--;
1459 }
1460 break;
1461 default:
1462 break;
1463 }
1464 }
1465
1466 builder_.setLength(trimRightPos_);
1467 state_ = State.DEFAULT;
1468 if (builder_.length() == 0) {
1469 state_ = State.EMPTY;
1470 }
1471 }
1472
1473
1474
1475
1476
1477
1478 public boolean isEmpty() {
1479 return state_ == State.EMPTY;
1480 }
1481
1482
1483
1484
1485
1486
1487 public boolean wasContentAdded() {
1488 return contentAdded_;
1489 }
1490
1491
1492
1493
1494 public void resetContentAdded() {
1495 contentAdded_ = false;
1496 }
1497
1498
1499
1500
1501
1502
1503 public String getText() {
1504 return builder_.substring(0, trimRightPos_);
1505 }
1506 }
1507 }