View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.html.serializer;
16  
17  import static org.htmlunit.BrowserVersionFeatures.JS_INNER_TEXT_SELECT_EMPTY;
18  import static org.htmlunit.BrowserVersionFeatures.JS_INNER_TEXT_SVG_NL;
19  
20  import java.util.ArrayList;
21  import java.util.IdentityHashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.htmlunit.BrowserVersion;
26  import org.htmlunit.SgmlPage;
27  import org.htmlunit.WebWindow;
28  import org.htmlunit.css.ComputedCssStyleDeclaration;
29  import org.htmlunit.css.StyleAttributes.Definition;
30  import org.htmlunit.html.DomElement;
31  import org.htmlunit.html.DomNode;
32  import org.htmlunit.html.DomText;
33  import org.htmlunit.html.HtmlAudio;
34  import org.htmlunit.html.HtmlBreak;
35  import org.htmlunit.html.HtmlCanvas;
36  import org.htmlunit.html.HtmlDefinitionTerm;
37  import org.htmlunit.html.HtmlDetails;
38  import org.htmlunit.html.HtmlDialog;
39  import org.htmlunit.html.HtmlElement;
40  import org.htmlunit.html.HtmlExample;
41  import org.htmlunit.html.HtmlHead;
42  import org.htmlunit.html.HtmlHiddenInput;
43  import org.htmlunit.html.HtmlImage;
44  import org.htmlunit.html.HtmlInlineFrame;
45  import org.htmlunit.html.HtmlInput;
46  import org.htmlunit.html.HtmlListItem;
47  import org.htmlunit.html.HtmlListing;
48  import org.htmlunit.html.HtmlMeter;
49  import org.htmlunit.html.HtmlNoFrames;
50  import org.htmlunit.html.HtmlNoScript;
51  import org.htmlunit.html.HtmlOption;
52  import org.htmlunit.html.HtmlParagraph;
53  import org.htmlunit.html.HtmlPlainText;
54  import org.htmlunit.html.HtmlPreformattedText;
55  import org.htmlunit.html.HtmlProgress;
56  import org.htmlunit.html.HtmlScript;
57  import org.htmlunit.html.HtmlSelect;
58  import org.htmlunit.html.HtmlStyle;
59  import org.htmlunit.html.HtmlSummary;
60  import org.htmlunit.html.HtmlSvg;
61  import org.htmlunit.html.HtmlTableCell;
62  import org.htmlunit.html.HtmlTableRow;
63  import org.htmlunit.html.HtmlTextArea;
64  import org.htmlunit.html.HtmlTitle;
65  import org.htmlunit.html.HtmlVideo;
66  import org.htmlunit.html.ScriptElement;
67  import org.htmlunit.html.serializer.HtmlSerializerInnerOuterText.HtmlSerializerTextBuilder.Mode;
68  import org.htmlunit.svg.SvgDesc;
69  import org.htmlunit.svg.SvgTitle;
70  import org.htmlunit.util.StringUtils;
71  
72  /**
73   * Special serializer to generate the output we need
74   * for innerText and outerText.
75   *
76   * @author Ronald Brill
77   */
78  public class HtmlSerializerInnerOuterText {
79  
80      private final BrowserVersion browserVersion_;
81      private DomNode root_;
82  
83      /*
84       * Caches for the current run; the dom and the styles are not changed while serializing
85       * and many checks are done more than once for an element or have to walk up the ancestors.
86       */
87  
88      /** The display value of the elements. */
89      private final Map<HtmlElement, String> displayCache_ = new IdentityHashMap<>();
90      /** The result of {@link DomNode#isDisplayed()}. */
91      private final Map<DomNode, Boolean> displayedCache_ = new IdentityHashMap<>();
92      /** The white-space mode defined by the node or the ancestors ({@code null} if not defined). */
93      private final Map<DomNode, Mode> whiteSpaceCache_ = new IdentityHashMap<>();
94  
95      /**
96       * Ctor.
97       *
98       * @param browserVersion the {@link BrowserVersion}
99       */
100     public HtmlSerializerInnerOuterText(final BrowserVersion browserVersion) {
101         browserVersion_ = browserVersion;
102     }
103 
104     /**
105      * Converts an HTML node to text.
106      * @param node a node
107      * @return the text representation according to the setting of this serializer
108      */
109     public String asText(final DomNode node) {
110         root_ = node;
111         displayCache_.clear();
112         displayedCache_.clear();
113         whiteSpaceCache_.clear();
114 
115         if (node instanceof HtmlBreak) {
116             return "";
117         }
118 
119         // included scripts are ignored, but if we ask for the script itself....
120         if (node instanceof ScriptElement) {
121             final HtmlSerializerTextBuilder builder = new HtmlSerializerTextBuilder();
122             appendChildren(builder, node, Mode.WHITE_SPACE_NORMAL, false, false);
123             return builder.getText();
124         }
125 
126         // an element that is not rendered (and an option) returns the text content
127         if (node instanceof HtmlOption
128                 || (node instanceof HtmlElement && !(node instanceof HtmlTitle) && isDisplayNone(node))) {
129             return node.getTextContent();
130         }
131 
132         // when calling on the title itself we have to output
133         final boolean insideHead = node instanceof HtmlTitle;
134 
135         final HtmlSerializerTextBuilder builder = new HtmlSerializerTextBuilder();
136         appendNode(builder, node, whiteSpaceStyle(node, Mode.WHITE_SPACE_NORMAL), insideHead, false);
137         return builder.getText();
138     }
139 
140     /**
141      * Iterates over all children and calls appendNode() for every child.
142      *
143      * @param builder the StringBuilder to add to
144      * @param node the node to process
145      * @param mode the {@link Mode} to use for processing
146      * @param insideHead true if inside head section
147      * @param insideSelect true if inside a select
148      */
149     protected void appendChildren(final HtmlSerializerTextBuilder builder, final DomNode node,
150             final Mode mode, final boolean insideHead, final boolean insideSelect) {
151         for (final DomNode child : node.getChildren()) {
152             appendNode(builder, child, mode, insideHead, insideSelect);
153         }
154     }
155 
156     /**
157      * The core distribution method; calls the different appendXXX
158      * methods depending on the type of the given node.
159      *
160      * @param builder the StringBuilder to add to
161      * @param node the node to process
162      * @param mode the {@link Mode} to use for processing
163      * @param insideHead true if inside head section
164      * @param insideSelect true if inside a select
165      */
166     protected void appendNode(final HtmlSerializerTextBuilder builder, final DomNode node,
167             final Mode mode, final boolean insideHead, final boolean insideSelect) {
168         if (insideSelect && node instanceof HtmlBreak) {
169             return;
170         }
171 
172         if (node instanceof DomText text) {
173             appendText(builder, text, mode);
174         }
175         else if (node instanceof HtmlBreak break1) {
176             appendBreak(builder, break1);
177         }
178         else if (node instanceof HtmlParagraph paragraph) {
179             appendParagraph(builder, paragraph, mode, insideHead, insideSelect);
180         }
181         else if (node instanceof HtmlListItem item) {
182             appendListItem(builder, item, mode, insideHead, insideSelect);
183         }
184         else if (node instanceof HtmlPreformattedText
185                 || node instanceof HtmlListing
186                 || node instanceof HtmlExample
187                 || node instanceof HtmlPlainText) {
188             appendPreformattedText(builder, (HtmlElement) node, insideHead, insideSelect);
189         }
190         else if (node instanceof HtmlCanvas
191                 || node instanceof HtmlMeter
192                 || node instanceof HtmlProgress
193                 || node instanceof HtmlInlineFrame
194                 || node instanceof HtmlVideo
195                 || node instanceof HtmlAudio
196                 || (node instanceof HtmlNoScript && isJavaScriptEnabled(node))) {
197             // the fallback content is not rendered
198         }
199         else if (node instanceof HtmlDetails details) {
200             appendDetails(builder, details, mode, insideHead, insideSelect);
201         }
202         else if (node instanceof HtmlHead) {
203             appendChildren(builder, node, mode, true, insideSelect);
204         }
205         else if (node instanceof HtmlNoFrames) {
206             appendChildren(builder, node, Mode.PLAIN, insideHead, insideSelect);
207         }
208         else if (node instanceof HtmlTitle && !insideHead) {
209             // nothing to do
210         }
211         else if (node instanceof HtmlTextArea) {
212             // nothing to do
213         }
214         else if (node instanceof ScriptElement) {
215             if (insideHead) {
216                 appendChildren(builder, node, mode, insideHead, insideSelect);
217             }
218         }
219         else if (node instanceof HtmlSelect select) {
220             appendSelect(builder, select, mode, insideHead, insideSelect);
221         }
222         else if (node instanceof HtmlDefinitionTerm item) {
223             appendDefinitionTerm(builder, item, mode, insideHead, insideSelect);
224         }
225         else if (node instanceof HtmlSvg) {
226             if (browserVersion_.hasFeature(JS_INNER_TEXT_SVG_NL) && hasRenderedSvgText(node)) {
227                 builder.appendRequiredLineBreak();
228                 appendChildren(builder, node, mode, insideHead, insideSelect);
229                 builder.appendRequiredLineBreak();
230             }
231             else {
232                 appendChildren(builder, node, mode, insideHead, insideSelect);
233             }
234         }
235         else if (node instanceof SvgTitle || node instanceof SvgDesc) {
236             // nothing to do
237         }
238         else {
239             appendElement(builder, node, mode, insideHead, insideSelect);
240         }
241     }
242 
243     /**
244      * Process all other nodes; block level elements are surrounded
245      * by a required line break and elements with an own white-space style
246      * switch the mode for their children.
247      *
248      * @param builder the StringBuilder to add to
249      * @param node the node to process
250      * @param mode the {@link Mode} to use for processing
251      * @param insideHead true if inside head section
252      * @param insideSelect true if inside a select
253      */
254     protected void appendElement(final HtmlSerializerTextBuilder builder, final DomNode node,
255             final Mode mode, final boolean insideHead, final boolean insideSelect) {
256         if (!(node instanceof HtmlElement) || insideHead || insideSelect) {
257             appendChildren(builder, node, mode, insideHead, insideSelect);
258             return;
259         }
260 
261         final HtmlElement element = (HtmlElement) node;
262         if (isNotRendered(element)) {
263             return;
264         }
265 
266         if ((element instanceof HtmlInput && !(element instanceof HtmlHiddenInput)
267                     || element instanceof HtmlImage)
268                 && !isBlockLevel(element)) {
269             // inline replaced elements (atomic inlines) produce no text, but they
270             // separate the white space before and after
271             builder.appendAtomicInline();
272             return;
273         }
274 
275         final Mode childMode = mode == Mode.PLAIN ? mode : whiteSpaceStyle(node, mode);
276         final boolean visible = isVisible(element);
277         final boolean block = visible && isBlockLevel(element);
278         if (block) {
279             builder.appendRequiredLineBreak();
280         }
281         appendChildren(builder, node, childMode, insideHead, insideSelect);
282         if (!visible) {
283             return;
284         }
285         if (block) {
286             builder.appendRequiredLineBreak();
287         }
288         else if (isTableCell(element)) {
289             // cells of a row are separated by a tab
290             final DomElement next = element.getNextElementSibling();
291             if (next instanceof HtmlElement nextElement && isTableCell(nextElement)) {
292                 builder.appendTab();
293             }
294         }
295         else if (node instanceof HtmlTableRow || "table-row".equals(display(element))) {
296             builder.appendRequiredLineBreak();
297         }
298     }
299 
300     /**
301      * An element with display none (or the hidden attribute) or with content-visibility hidden
302      * does not contribute to the text; this is not true for the element the text was requested for.
303      *
304      * @param element the element to check
305      * @return true if the element (and its children) has to be ignored
306      */
307     private boolean isNotRendered(final HtmlElement element) {
308         if (element == root_) {
309             return false;
310         }
311         return "none".equals(display(element)) || isContentVisibilityHidden(element);
312     }
313 
314     /**
315      * Checks if the element has the style 'content-visibility: hidden'; browsers
316      * skip such an element (including the line breaks) like a 'display: none' element.
317      *
318      * @param element the element to check
319      * @return true if the content visibility is hidden
320      */
321     private static boolean isContentVisibilityHidden(final HtmlElement element) {
322         final SgmlPage page = element.getPage();
323         final WebWindow window = page == null ? null : page.getEnclosingWindow();
324         if (window == null || !window.getWebClient().getOptions().isCssEnabled()) {
325             return false;
326         }
327         return "hidden".equals(window.getComputedStyle(element, null).getStyleAttribute("content-visibility"));
328     }
329 
330     /**
331      * Checks if the given node or one of its ancestors is not rendered because
332      * of the hidden attribute or the display style 'none'.
333      *
334      * @param node the node to check
335      * @return true if the node or one of the ancestors has display none
336      */
337     private boolean isDisplayNone(final DomNode node) {
338         DomNode current = node;
339         while (current != null) {
340             if (current instanceof HtmlElement element) {
341                 if ("none".equals(display(element))) {
342                     return true;
343                 }
344             }
345             current = current.getParentNode();
346         }
347         return false;
348     }
349 
350     /**
351      * An element with visibility hidden still contributes the text of visible children,
352      * but no line breaks.
353      *
354      * @param element the element to check
355      * @return true if the element is the root or displayed
356      */
357     private boolean isVisible(final HtmlElement element) {
358         return element == root_ || isDisplayed(element);
359     }
360 
361     /**
362      * Returns the result of {@link DomNode#isDisplayed()}; the result is cached for the current run
363      * because this has to check all the ancestors.
364      *
365      * @param node the node to check
366      * @return true if the node is displayed
367      */
368     private boolean isDisplayed(final DomNode node) {
369         return displayedCache_.computeIfAbsent(node, DomNode::isDisplayed);
370     }
371 
372     /**
373      * Determines the display value of the given element; the value is
374      * cached for the current run.
375      *
376      * @param element the element to check
377      * @return the computed display of the element (or the default display if css is disabled)
378      */
379     private String display(final HtmlElement element) {
380         return displayCache_.computeIfAbsent(element, HtmlSerializerInnerOuterText::computeDisplay);
381     }
382 
383     /**
384      * Determines the display value of the given element.
385      *
386      * @param element the element to check
387      * @return the computed display of the element (or the default display if css is disabled)
388      */
389     private static String computeDisplay(final HtmlElement element) {
390         final SgmlPage page = element.getPage();
391         final WebWindow window = page == null ? null : page.getEnclosingWindow();
392         if (window != null && window.getWebClient().getOptions().isCssEnabled()) {
393             return window.getComputedStyle(element, null).getDisplay();
394         }
395         if (element.isHidden()) {
396             return "none";
397         }
398         if (element instanceof HtmlDialog dialog) {
399             return dialog.isOpen() ? "block" : "none";
400         }
401         return element.getDefaultStyleDisplay().value();
402     }
403 
404     /**
405      * Checks if javascript is enabled for the page of the given node.
406      *
407      * @param node the node to check
408      * @return true if javascript is enabled
409      */
410     private static boolean isJavaScriptEnabled(final DomNode node) {
411         final SgmlPage page = node.getPage();
412         return page != null && page.getWebClient().isJavaScriptEnabled();
413     }
414 
415     /**
416      * Checks if the given svg element has some text that is rendered
417      * (text inside of title and desc is not rendered).
418      *
419      * @param node the svg element
420      * @return true if there is some rendered text
421      */
422     private static boolean hasRenderedSvgText(final DomNode node) {
423         for (final DomNode child : node.getChildren()) {
424             if (child instanceof SvgTitle || child instanceof SvgDesc) {
425                 continue;
426             }
427             if (child instanceof DomText text) {
428                 if (!StringUtils.isBlank(text.getData())) {
429                     return true;
430                 }
431             }
432             else if (hasRenderedSvgText(child)) {
433                 return true;
434             }
435         }
436         return false;
437     }
438 
439     /**
440      * Checks if the given element is a table cell (td/th or display 'table-cell').
441      *
442      * @param element the element to check
443      * @return true if the element is a table cell
444      */
445     private boolean isTableCell(final HtmlElement element) {
446         return element instanceof HtmlTableCell || "table-cell".equals(display(element));
447     }
448 
449     /**
450      * Checks if the given element is block level; this includes elements that are
451      * blockified (absolute positioned, floating, children of flex and grid containers).
452      *
453      * @param element the element to check
454      * @return true if the element is block level
455      */
456     private boolean isBlockLevel(final HtmlElement element) {
457         final String display = display(element);
458         if ("block".equals(display)
459                 || "flex".equals(display)
460                 || "grid".equals(display)
461                 || "flow-root".equals(display)
462                 || "list-item".equals(display)
463                 || "table".equals(display)
464                 || "table-caption".equals(display)) {
465             return true;
466         }
467         if ("none".equals(display) || "contents".equals(display)) {
468             return false;
469         }
470 
471         final SgmlPage page = element.getPage();
472         final WebWindow window = page == null ? null : page.getEnclosingWindow();
473         if (window == null || !window.getWebClient().getOptions().isCssEnabled()) {
474             return false;
475         }
476 
477         // absolute positioned and floating boxes are blockified
478         final ComputedCssStyleDeclaration style = window.getComputedStyle(element, null);
479         final String position = style.getPosition();
480         if ("absolute".equals(position) || "fixed".equals(position)) {
481             return true;
482         }
483         final String floatValue = style.getStyleAttribute(Definition.FLOAT, false);
484         if ("left".equals(floatValue) || "right".equals(floatValue)) {
485             return true;
486         }
487 
488         // children of flex and grid containers are blockified
489         if (element.getParentNode() instanceof HtmlElement parent) {
490             final String parentDisplay = display(parent);
491             return "flex".equals(parentDisplay) || "inline-flex".equals(parentDisplay)
492                     || "grid".equals(parentDisplay) || "inline-grid".equals(parentDisplay);
493         }
494         return false;
495     }
496 
497     /**
498      * Process {@link DomText}.
499      *
500      * @param builder the StringBuilder to add to
501      * @param domText the target to process
502      * @param mode the {@link Mode} to use for processing
503      */
504     protected void appendText(final HtmlSerializerTextBuilder builder, final DomText domText, final Mode mode) {
505         final DomNode parent = domText.getParentNode();
506         if (parent instanceof HtmlTitle
507                 || parent instanceof HtmlStyle
508                 || parent instanceof HtmlScript) {
509             builder.append(domText.getData(), Mode.PLAIN);
510             return;
511         }
512 
513         // options are not rendered by css; the display style is ignored
514         if (parent == null
515                 || parent instanceof HtmlNoFrames
516                 || parent instanceof HtmlOption
517                 || isDisplayed(parent)) {
518             builder.append(domText.getData(), mode);
519         }
520     }
521 
522     /**
523      * Process {@link HtmlBreak}.
524      *
525      * @param builder the StringBuilder to add to
526      * @param htmlBreak the target to process
527      */
528     protected void appendBreak(final HtmlSerializerTextBuilder builder,
529             final HtmlBreak htmlBreak) {
530         builder.appendLineBreak();
531     }
532 
533     /**
534      * Process {@link HtmlParagraph}.
535      *
536      * @param builder the StringBuilder to add to
537      * @param htmlParagraph the target to process
538      * @param mode the {@link Mode} to use for processing
539      * @param insideHead true if inside head section
540      * @param insideSelect true if inside a select
541      */
542     protected void appendParagraph(final HtmlSerializerTextBuilder builder,
543             final HtmlParagraph htmlParagraph, final Mode mode, final boolean insideHead, final boolean insideSelect) {
544         if (isNotRendered(htmlParagraph)) {
545             return;
546         }
547         // a paragraph is surrounded by a blank line, independent of the display style
548         final boolean visible = isVisible(htmlParagraph);
549         if (visible) {
550             builder.appendRequiredLineBreak(2);
551         }
552         appendChildren(builder, htmlParagraph, whiteSpaceStyle(htmlParagraph, mode), insideHead, insideSelect);
553         if (visible) {
554             builder.appendRequiredLineBreak(2);
555         }
556     }
557 
558     /**
559      * Process {@link HtmlPreformattedText}.
560      * The default white-space handling of a pre element is 'pre', but an explicit
561      * white-space style of the element (or one of its ancestors) wins.
562      *
563      * @param builder the StringBuilder to add to
564      * @param htmlPre the target to process
565      * @param insideHead true if inside head section
566      * @param insideSelect true if inside a select
567      */
568     protected void appendPreformattedText(final HtmlSerializerTextBuilder builder,
569             final HtmlElement htmlPre, final boolean insideHead, final boolean insideSelect) {
570         if (isNotRendered(htmlPre)) {
571             return;
572         }
573         builder.appendRequiredLineBreak();
574         appendChildren(builder, htmlPre, whiteSpaceStyle(htmlPre, Mode.WHITE_SPACE_PRE), insideHead, insideSelect);
575         builder.appendRequiredLineBreak();
576     }
577 
578     /**
579      * Process {@link HtmlListItem}.
580      *
581      * @param builder the StringBuilder to add to
582      * @param htmlListItem the target to process
583      * @param mode the {@link Mode} to use for processing
584      * @param insideHead true if inside head section
585      * @param insideSelect true if inside a select
586      */
587     protected void appendListItem(final HtmlSerializerTextBuilder builder,
588             final HtmlListItem htmlListItem, final Mode mode, final boolean insideHead, final boolean insideSelect) {
589         if (isNotRendered(htmlListItem)) {
590             return;
591         }
592         // a list item with display inline has no line breaks
593         final boolean block = isVisible(htmlListItem) && isBlockLevel(htmlListItem);
594         if (block) {
595             builder.appendRequiredLineBreak();
596         }
597         appendChildren(builder, htmlListItem, mode, insideHead, insideSelect);
598         if (block) {
599             builder.appendRequiredLineBreak();
600         }
601     }
602 
603     /**
604      * Process {@link HtmlDetails}.
605      *
606      * @param builder the StringBuilder to add to
607      * @param htmlDetails the target to process
608      * @param mode the {@link Mode} to use for processing
609      * @param insideHead true if inside head section
610      * @param insideSelect true if inside a select
611      */
612     protected void appendDetails(final HtmlSerializerTextBuilder builder,
613                     final HtmlDetails htmlDetails, final Mode mode,
614                     final boolean insideHead, final boolean insideSelect) {
615         if (htmlDetails.isOpen()) {
616             appendChildren(builder, htmlDetails, mode, insideHead, insideSelect);
617             return;
618         }
619 
620         for (final DomNode child : htmlDetails.getChildren()) {
621             if (child instanceof HtmlSummary) {
622                 // only the first summary is the summary of the details
623                 appendNode(builder, child, mode, insideHead, insideSelect);
624                 return;
625             }
626         }
627     }
628 
629     /**
630      * Process {@link HtmlDefinitionTerm}.
631      *
632      * @param builder the StringBuilder to add to
633      * @param htmlDefinitionTerm the target to process
634      * @param mode the {@link Mode} to use for processing
635      * @param insideHead true if inside head section
636      * @param insideSelect true if inside a select
637      */
638     protected void appendDefinitionTerm(final HtmlSerializerTextBuilder builder,
639             final HtmlDefinitionTerm htmlDefinitionTerm, final Mode mode,
640             final boolean insideHead, final boolean insideSelect) {
641         if (isNotRendered(htmlDefinitionTerm)) {
642             return;
643         }
644         final boolean block = isVisible(htmlDefinitionTerm) && isBlockLevel(htmlDefinitionTerm);
645         if (block) {
646             builder.appendRequiredLineBreak();
647         }
648         appendChildren(builder, htmlDefinitionTerm, mode, insideHead, insideSelect);
649         if (block) {
650             builder.appendRequiredLineBreak();
651         }
652     }
653 
654     /**
655      * Process {@link HtmlSelect}.
656      * <p>
657      * Select and its Option children are rendered as native form-control widgets in real browsers,
658      * not as normal inline/block boxes. The widget's internal text drawing doesn't go through the
659      * same CSS text-layout pipeline that divs or spans use,
660      * so properties like white-space: pre on a &lt;select&gt; or &lt;option&gt; get ignored by Chrome/Firefox/etc.
661      * </p>
662      *
663      * @param builder the StringBuilder to add to
664      * @param htmlSelect the target to process
665      * @param mode the {@link Mode} to use for processing
666      * @param insideHead true if inside head section
667      * @param insideSelect true if inside a select
668      */
669     protected void appendSelect(final HtmlSerializerTextBuilder builder,
670             final HtmlSelect htmlSelect, final Mode mode, final boolean insideHead, final boolean insideSelect) {
671         if (isNotRendered(htmlSelect)) {
672             return;
673         }
674 
675         // the select is an inline-block; the white space before and after is not collapsed
676         final boolean inline = htmlSelect != root_;
677 
678         // a list box (multiple or size > 1) shows the options
679         if (browserVersion_.hasFeature(JS_INNER_TEXT_SELECT_EMPTY)
680                 && !htmlSelect.isMultipleSelectEnabled()
681                 && htmlSelect.getSize() <= 1) {
682             if (inline) {
683                 builder.appendAtomicInline();
684             }
685             return;
686         }
687 
688         if (!inline) {
689             appendDescendantOptions(builder, htmlSelect, Mode.WHITE_SPACE_NORMAL, insideHead, true);
690             return;
691         }
692 
693         // inside of some text every option is placed on a line of its own
694         // and the line breaks are part of the content (e.g. 'Choice: \none\n L')
695         builder.appendAtomicInline();
696         boolean optionFound = false;
697         for (final HtmlOption option : htmlSelect.getOptions()) {
698             builder.appendLineBreak();
699             appendChildren(builder, option, Mode.WHITE_SPACE_NORMAL, insideHead, true);
700             optionFound = true;
701         }
702         if (optionFound) {
703             builder.appendLineBreak();
704         }
705         builder.appendAtomicInline();
706     }
707 
708     /**
709      * Iterate over all descendant options.
710      *
711      * @param builder the StringBuilder to add to
712      * @param node the node to process
713      * @param mode the {@link Mode} to use for processing
714      * @param insideHead true if inside head section
715      * @param insideSelect true if inside a select
716      */
717     protected void appendDescendantOptions(final HtmlSerializerTextBuilder builder, final DomNode node,
718             final Mode mode, final boolean insideHead, final boolean insideSelect) {
719         for (final DomNode child : node.getChildNodes()) {
720             if (child instanceof HtmlOption) {
721                 builder.appendRequiredLineBreak();
722                 appendChildren(builder, child, Mode.WHITE_SPACE_NORMAL, insideHead, true);
723             }
724             else {
725                 appendDescendantOptions(builder, child, Mode.WHITE_SPACE_NORMAL, insideHead, true);
726             }
727         }
728     }
729 
730     /**
731      * Determines the white-space mode for the given node; the white-space style of the node
732      * and the ancestors is checked. The result is cached for the current run.
733      *
734      * @param domNode the node
735      * @param defaultMode the mode used if no white-space style is defined
736      * @return the {@link Mode}
737      */
738     private Mode whiteSpaceStyle(final DomNode domNode, final Mode defaultMode) {
739         if (!(domNode instanceof DomElement)) {
740             return defaultMode;
741         }
742         final SgmlPage page = domNode.getPage();
743         if (page == null || !page.getWebClient().getOptions().isCssEnabled()) {
744             return defaultMode;
745         }
746         final WebWindow window = page.getEnclosingWindow();
747         if (window == null) {
748             return defaultMode;
749         }
750 
751         // walk up until the first element defining a white-space style or the first known result;
752         // all the nodes on the way share this result
753         final List<DomNode> path = new ArrayList<>();
754         Mode mode = null;
755         DomNode node = domNode;
756         while (node != null) {
757             if (whiteSpaceCache_.containsKey(node)) {
758                 mode = whiteSpaceCache_.get(node);
759                 break;
760             }
761             path.add(node);
762             if (node instanceof DomElement element) {
763                 mode = whiteSpaceMode(window.getComputedStyle(element, null));
764                 if (mode != null) {
765                     break;
766                 }
767             }
768             node = node.getParentNode();
769         }
770 
771         for (final DomNode pathNode : path) {
772             whiteSpaceCache_.put(pathNode, mode);
773         }
774         return mode == null ? defaultMode : mode;
775     }
776 
777     /**
778      * Maps the white-space (and white-space-collapse) style to the {@link Mode}.
779      *
780      * @param style the computed style
781      * @return the {@link Mode} or {@code null} if the style does not define the white-space handling
782      */
783     private static Mode whiteSpaceMode(final ComputedCssStyleDeclaration style) {
784         final String value = style.getStyleAttribute(Definition.WHITE_SPACE, false);
785         if (!StringUtils.isEmptyOrNull(value)) {
786             if ("normal".equalsIgnoreCase(value) || "nowrap".equalsIgnoreCase(value)) {
787                 return Mode.WHITE_SPACE_NORMAL;
788             }
789             if ("pre".equalsIgnoreCase(value)
790                     || "pre-wrap".equalsIgnoreCase(value)
791                     || "break-spaces".equalsIgnoreCase(value)) {
792                 return Mode.WHITE_SPACE_PRE;
793             }
794             if ("pre-line".equalsIgnoreCase(value)) {
795                 return Mode.WHITE_SPACE_PRE_LINE;
796             }
797         }
798 
799         final String collapse = style.getStyleAttribute(Definition.WHITE_SPACE_COLLAPSE, false);
800         if (!StringUtils.isEmptyOrNull(collapse)) {
801             if ("collapse".equalsIgnoreCase(collapse)) {
802                 return Mode.WHITE_SPACE_NORMAL;
803             }
804             if ("preserve".equalsIgnoreCase(collapse)
805                     || "break-spaces".equalsIgnoreCase(collapse)) {
806                 return Mode.WHITE_SPACE_PRE;
807             }
808             if ("preserve-breaks".equalsIgnoreCase(collapse)) {
809                 return Mode.WHITE_SPACE_PRE_LINE;
810             }
811         }
812         return null;
813     }
814 
815     /**
816      * Helper to compose the text for the serializer based on several modes.
817      */
818     protected static class HtmlSerializerTextBuilder {
819 
820         /** Mode. */
821         protected enum Mode {
822             /**
823              * The mode for plain.
824              */
825             PLAIN,
826 
827             /**
828              * Sequences of white space are collapsed. Newline characters
829              * in the source are handled the same as other white space.
830              * Lines are broken as necessary to fill line boxes.
831              */
832             WHITE_SPACE_NORMAL,
833 
834             /**
835              * Sequences of white space are preserved. Lines are only broken
836              * at newline characters in the source and at {@code <br>} elements.
837              */
838             WHITE_SPACE_PRE,
839 
840             /**
841              * Sequences of white space are collapsed. Lines are broken
842              * at newline characters, at {@code <br>} and as necessary
843              * to fill line boxes.
844              */
845             WHITE_SPACE_PRE_LINE
846         }
847 
848         private enum State {
849             DEFAULT,
850             EMPTY,
851             BLANK_AT_END,
852             NEWLINE_AT_END,
853             BREAK_AT_END,
854             REQUIRED_LINE_BREAK_AT_END
855         }
856 
857         private State state_;
858         private final StringBuilder builder_;
859         private int trimRightPos_;
860         private int pendingLineBreaks_;
861 
862         /**
863          * Ctor.
864          */
865         public HtmlSerializerTextBuilder() {
866             builder_ = new StringBuilder();
867             state_ = State.EMPTY;
868             trimRightPos_ = 0;
869         }
870 
871         /**
872          * Append a required line break (count 1).
873          */
874         public void appendRequiredLineBreak() {
875             appendRequiredLineBreak(1);
876         }
877 
878         /**
879          * Append a required line break count; consecutive required line breaks
880          * are merged to the biggest one. Required line breaks at the start
881          * or the end are dropped.
882          *
883          * @param count the number of line breaks
884          */
885         public void appendRequiredLineBreak(final int count) {
886             if (state_ == State.EMPTY) {
887                 return;
888             }
889 
890             // trimRight
891             builder_.setLength(trimRightPos_);
892             if (trimRightPos_ == 0) {
893                 state_ = State.EMPTY;
894                 pendingLineBreaks_ = 0;
895                 return;
896             }
897 
898             pendingLineBreaks_ = Math.max(pendingLineBreaks_, count);
899             state_ = State.REQUIRED_LINE_BREAK_AT_END;
900         }
901 
902         /**
903          * Append a line break that is part of the content (like the one from a br element).
904          */
905         public void appendLineBreak() {
906             // collapsible white space at the end of the line is removed
907             builder_.setLength(trimRightPos_);
908             flushPendingLineBreaks();
909             builder_.append('\n');
910             state_ = State.BREAK_AT_END;
911             trimRightPos_ = builder_.length();
912         }
913 
914         /**
915          * Marks the position of an atomic inline (like an input or image); this element
916          * has no text, but white space before and after is not collapsed.
917          */
918         public void appendAtomicInline() {
919             flushPendingLineBreaks();
920             trimRightPos_ = builder_.length();
921             state_ = State.DEFAULT;
922         }
923 
924         /**
925          * Append a tab (used to separate table cells).
926          */
927         public void appendTab() {
928             flushPendingLineBreaks();
929             builder_.append('\t');
930             state_ = State.BREAK_AT_END;
931             trimRightPos_ = builder_.length();
932         }
933 
934         /**
935          * Writes the pending required line breaks (if any) to the output.
936          */
937         private void flushPendingLineBreaks() {
938             if (pendingLineBreaks_ > 0) {
939                 builder_.setLength(trimRightPos_);
940                 for (int i = 0; i < pendingLineBreaks_; i++) {
941                     builder_.append('\n');
942                 }
943                 pendingLineBreaks_ = 0;
944                 trimRightPos_ = builder_.length();
945             }
946         }
947 
948         /**
949          * Append the provided content.
950          * see https://drafts.csswg.org/css-text-3/#white-space
951          *
952          * @param content the content to add
953          * @param mode the {@link Mode}
954          */
955         public void append(final String content, final Mode mode) {
956             if (content == null) {
957                 return;
958             }
959             final int length = content.length();
960             if (length == 0) {
961                 return;
962             }
963 
964             if (mode == Mode.PLAIN) {
965                 flushPendingLineBreaks();
966                 builder_.append(content);
967                 state_ = State.DEFAULT;
968                 trimRightPos_ = builder_.length();
969                 return;
970             }
971 
972             final int lastIndex = length - 1;
973             for (int i = 0; i < length; i++) {
974                 char c = content.charAt(i);
975 
976                 // a \r is ignored; only at the end it is handled like a \n
977                 if (c == '\r') {
978                     if (i != lastIndex) {
979                         continue;
980                     }
981                     c = '\n';
982                 }
983 
984                 if (c == '\n') {
985                     if (mode == Mode.WHITE_SPACE_PRE || mode == Mode.WHITE_SPACE_PRE_LINE) {
986                         if (mode == Mode.WHITE_SPACE_PRE_LINE && state_ == State.BLANK_AT_END) {
987                             // collapsible white space at the end of the line is removed
988                             builder_.setLength(trimRightPos_);
989                         }
990                         flushPendingLineBreaks();
991                         builder_.append('\n');
992                         state_ = State.NEWLINE_AT_END;
993                         trimRightPos_ = builder_.length();
994                         continue;
995                     }
996 
997                     switch (state_) {
998                         case EMPTY:
999                         case BLANK_AT_END:
1000                         case NEWLINE_AT_END:
1001                         case BREAK_AT_END:
1002                         case REQUIRED_LINE_BREAK_AT_END:
1003                             break;
1004                         default:
1005                             builder_.append(' ');
1006                             state_ = State.BLANK_AT_END;
1007                             break;
1008                     }
1009                     continue;
1010                 }
1011 
1012                 if (c == ' ' || c == '\t' || c == '\f') {
1013                     if (mode == Mode.WHITE_SPACE_PRE) {
1014                         flushPendingLineBreaks();
1015                         if (c == '\t') {
1016                             builder_.append('\t');
1017                         }
1018                         else {
1019                             builder_.append(' ');
1020                         }
1021                         state_ = State.BLANK_AT_END;
1022                         trimRightPos_ = builder_.length();
1023 
1024                         continue;
1025                     }
1026 
1027                     if (mode == Mode.WHITE_SPACE_PRE_LINE) {
1028                         switch (state_) {
1029                             case EMPTY:
1030                             case BLANK_AT_END:
1031                             case BREAK_AT_END:
1032                             case NEWLINE_AT_END:
1033                             case REQUIRED_LINE_BREAK_AT_END:
1034                                 break;
1035                             default:
1036                                 builder_.append(' ');
1037                                 state_ = State.BLANK_AT_END;
1038                                 break;
1039                         }
1040                         continue;
1041                     }
1042 
1043                     switch (state_) {
1044                         case EMPTY:
1045                         case BLANK_AT_END:
1046                         case NEWLINE_AT_END:
1047                         case BREAK_AT_END:
1048                         case REQUIRED_LINE_BREAK_AT_END:
1049                             break;
1050                         default:
1051                             builder_.append(' ');
1052                             state_ = State.BLANK_AT_END;
1053                             break;
1054                     }
1055                     continue;
1056                 }
1057 
1058                 flushPendingLineBreaks();
1059                 builder_.append(c);
1060                 state_ = State.DEFAULT;
1061                 trimRightPos_ = builder_.length();
1062             }
1063         }
1064 
1065         /**
1066          * Returns the constructed text.
1067          *
1068          * @return the constructed text.
1069          */
1070         public String getText() {
1071             return builder_.substring(0, trimRightPos_);
1072         }
1073     }
1074 }