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_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   * Special serializer to generate the output we need
76   * at least for selenium WebElement#getText().
77   * <p>This is also used for estimations by ComputedCssStyleDeclaration.</p>
78   *
79   * @author Ronald Brill
80   * @author cd alexndr
81   */
82  public class HtmlSerializerVisibleText {
83  
84      /** Rough estimation of the width of a char used to detect offscreen elements. */
85      private static final int ESTIMATED_CHAR_WIDTH = 16;
86  
87      /** Rough estimation of the height of a line used to detect offscreen elements. */
88      private static final int ESTIMATED_LINE_HEIGHT = 20;
89  
90      /**
91       * The displays handled as block by WebDriver (everything that is not one of the
92       * inline display boxes 'inline', 'inline-block', 'inline-table', 'none', 'table-cell', ...).
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      * Caches for the current run; the dom and the styles are not changed while serializing
102      * and many checks have to walk up the ancestors for every node.
103      */
104 
105     /** The result of {@link #isDisplayed(DomNode)}. */
106     private final Map<DomNode, Boolean> displayedCache_ = new IdentityHashMap<>();
107     /** The result of {@link #isTransparent(DomNode)}. */
108     private final Map<DomNode, Boolean> transparentCache_ = new IdentityHashMap<>();
109     /** The white-space mode defined by the node or the ancestors ({@code null} if not defined). */
110     private final Map<DomNode, Mode> whiteSpaceCache_ = new IdentityHashMap<>();
111 
112     /** Whether the WebDriver specific visibility checks (opacity, content-visibility, offscreen) are done. */
113     private final boolean webDriverVisibility_;
114 
115     /**
116      * Ctor; creates a serializer for the WebDriver visible text.
117      */
118     public HtmlSerializerVisibleText() {
119         this(true);
120     }
121 
122     /**
123      * Ctor.
124      * <p>The layout (e.g. the estimation of the size of an element) has to use the text
125      * without the WebDriver specific visibility checks; the browsers do the layout
126      * also for elements that are transparent or positioned outside of the page.</p>
127      *
128      * @param webDriverVisibility whether the WebDriver specific visibility checks
129      *        (opacity, content-visibility, offscreen) are done
130      */
131     public HtmlSerializerVisibleText(final boolean webDriverVisibility) {
132         webDriverVisibility_ = webDriverVisibility;
133     }
134 
135     /**
136      * Converts an HTML node to text.
137      * @param node a node
138      * @return the text representation according to the setting of this serializer
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      * Iterates over all children and calls appendNode() for every child.
156      *
157      * @param builder the StringBuilder to add to
158      * @param node the node to process
159      * @param mode the {@link Mode} to use for processing
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      * The core distribution method; calls the different appendXXX
170      * methods depending on the type of the given node.
171      *
172      * @param builder the StringBuilder to add to
173      * @param node the node to process
174      * @param mode the {@link Mode} to use for processing
175      */
176     protected void appendNode(final HtmlSerializerTextBuilder builder, final DomNode node, final Mode mode) {
177         if (node instanceof DomCDataSection) {
178             // ignore
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             // nothing
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      * Process {@link DomNode}.
268      *
269      * @param builder the StringBuilder to add to
270      * @param domNode the target to process
271      * @param mode the {@link Mode} to use for processing
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      * Checks if the given node is an element with display 'table-cell'.
300      *
301      * @param node the node to check
302      * @return true if the node is an element with display 'table-cell'
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      * Process {@link HtmlHiddenInput}.
314      *
315      * @param builder the StringBuilder to add to
316      * @param htmlHiddenInput the target to process
317      * @param mode the {@link Mode} to use for processing
318      */
319     protected void appendHiddenInput(final HtmlSerializerTextBuilder builder,
320             final HtmlHiddenInput htmlHiddenInput, final Mode mode) {
321         // nothing to do
322     }
323 
324     /**
325      * Process {@link HtmlScript}.
326      *
327      * @param builder the StringBuilder to add to
328      * @param htmlScript the target to process
329      * @param mode the {@link Mode} to use for processing
330      */
331     protected void appendScript(final HtmlSerializerTextBuilder builder,
332             final HtmlScript htmlScript, final Mode mode) {
333         // nothing to do
334     }
335 
336     /**
337      * Process {@link HtmlStyle}.
338      *
339      * @param builder the StringBuilder to add to
340      * @param htmlStyle the target to process
341      * @param mode the {@link Mode} to use for processing
342      */
343     protected void appendStyle(final HtmlSerializerTextBuilder builder,
344             final HtmlStyle htmlStyle, final Mode mode) {
345         // nothing to do
346     }
347 
348     /**
349      * Process {@link HtmlNoScript}.
350      *
351      * @param builder the StringBuilder to add to
352      * @param htmlNoScript the target to process
353      * @param mode the {@link Mode} to use for processing
354      */
355     protected void appendNoScript(final HtmlSerializerTextBuilder builder,
356             final HtmlNoScript htmlNoScript, final Mode mode) {
357         // nothing to do
358     }
359 
360     /**
361      * Process {@link HtmlNoFrames}.
362      *
363      * @param builder the StringBuilder to add to
364      * @param htmlNoFrames the target to process
365      * @param mode the {@link Mode} to use for processing
366      */
367     protected void appendNoFrames(final HtmlSerializerTextBuilder builder,
368             final HtmlNoFrames htmlNoFrames, final Mode mode) {
369         // nothing to do
370     }
371 
372     /**
373      * Process {@link HtmlSubmitInput}.
374      *
375      * @param builder the StringBuilder to add to
376      * @param htmlSubmitInput the target to process
377      * @param mode the {@link Mode} to use for processing
378      */
379     protected void appendSubmitInput(final HtmlSerializerTextBuilder builder,
380             final HtmlSubmitInput htmlSubmitInput, final Mode mode) {
381         // nothing to do
382     }
383 
384     /**
385      * Process {@link HtmlInput}.
386      *
387      * @param builder the StringBuilder to add to
388      * @param htmlInput the target to process
389      * @param mode the {@link Mode} to use for processing
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      * Process {@link HtmlResetInput}.
398      *
399      * @param builder the StringBuilder to add to
400      * @param htmlResetInput the target to process
401      * @param mode the {@link Mode} to use for processing
402      */
403     protected void appendResetInput(final HtmlSerializerTextBuilder builder,
404             final HtmlResetInput htmlResetInput, final Mode mode) {
405         // nothing to do
406     }
407 
408     /**
409      * Checks if the given node is block level; this includes elements that are
410      * blockified (children of flex and grid containers).
411      *
412      * @param node the node to check
413      * @return true if the node is a block level element
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             // absolute positioned and floating boxes are blockified
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             // children of flex and grid containers are blockified
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      * Process {@link HtmlMenu}; every child is placed on a line of its own.
446      *
447      * @param builder the StringBuilder to add to
448      * @param htmlMenu the target to process
449      * @param mode the {@link Mode} to use for processing
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      * Process {@link HtmlDetails}.
467      *
468      * @param builder the StringBuilder to add to
469      * @param htmlDetails the target to process
470      * @param mode the {@link Mode} to use for processing
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         // like WebDriver: element children of a closed details are hidden (except the summary),
480         // text children are still visible
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      * Process {@link HtmlTitle}.
490      *
491      * @param builder the StringBuilder to add to
492      * @param htmlTitle the target to process
493      * @param mode the {@link Mode} to use for processing
494      */
495     protected void appendTitle(final HtmlSerializerTextBuilder builder,
496             final HtmlTitle htmlTitle, final Mode mode) {
497         // nothing to do
498     }
499 
500     /**
501      * Process {@link HtmlTableRow}.
502      *
503      * @param builder the StringBuilder to add to
504      * @param htmlTableRow the target to process
505      * @param mode the {@link Mode} to use for processing
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      * Checks if the given node is visible for WebDriver; this includes the
523      * checks done by {@link DomNode#isDisplayed()} and the WebDriver specific checks
524      * (opacity, content-visibility, offscreen).
525      * The result is cached for the current run.
526      * <p>WebDriver (and the browsers) also hide the content of an element with a size of zero
527      * and hidden overflow. HtmlUnit does not check this because the size of many elements
528      * is calculated by javascript (e.g. GWT layout panels) based on the estimated sizes
529      * of other elements; a wrong estimation would hide content that is visible in real browsers.</p>
530      *
531      * @param domNode the node to check
532      * @return true if the node is visible
533      */
534     protected boolean isDisplayed(final DomNode domNode) {
535         return displayedCache_.computeIfAbsent(domNode, node -> node.isDisplayed() && !isTransparent(node));
536     }
537 
538     /**
539      * Checks if the element is positioned outside of the page (e.g. 'left: -9999px').
540      * <p>The calculated width/height can't be used here because the calculation
541      * itself depends on the visible text. Therefore we use the explicit width/height
542      * or a rough estimation based on the text length.</p>
543      *
544      * @param element the element
545      * @param style the computed style of the element
546      * @return true if the element is not visible
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      * Returns the integer value of a pixel length; 0 for all other values.
578      *
579      * @param length the length
580      * @return the pixel value
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      * Checks if the given node or one of its ancestors is not visible for WebDriver
596      * (opacity of zero, content-visibility hidden or offscreen).
597      * The results are cached for the current run; the ancestors share the cached result.
598      *
599      * @param domNode the node to check
600      * @return true if the node is not visible
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         // walk up until the first transparent element or the first known result;
617         // all the nodes on the way share this result
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      * Checks if the given element itself (the ancestors are not checked) is not visible
644      * for WebDriver (opacity of zero, content-visibility hidden or offscreen).
645      *
646      * @param element the element to check
647      * @param style the computed style of the element
648      * @return true if the element is not visible
649      */
650     private static boolean isTransparentElement(final DomElement element, final ComputedCssStyleDeclaration style) {
651         // WebDriver also hides the content of elements with a size of zero and hidden overflow;
652         // this is not done here because the size of many elements is calculated by
653         // javascript (e.g. GWT layout panels) based on the (estimated) sizes of other elements
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                 // ignore
666             }
667         }
668         return false;
669     }
670 
671     /**
672      * Process {@link HtmlTextArea}.
673      *
674      * @param builder the StringBuilder to add to
675      * @param htmlTextArea the target to process
676      * @param mode the {@link Mode} to use for processing
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      * Process {@link HtmlTable}.
688      *
689      * @param builder the StringBuilder to add to
690      * @param htmlTable the target to process
691      * @param mode the {@link Mode} to use for processing
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         // first thead has to be displayed first and first tfoot has to be displayed last
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         // like WebDriver we use the document order if the footer is before the body
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      * Process the given {@link HtmlTableRow}s; rows are separated by a block separator.
735      *
736      * @param builder the StringBuilder to add to
737      * @param mode the {@link Mode} to use for processing
738      * @param rows the rows
739      * @param first if true this is the first one
740      * @param skipParent1 skip row if the parent is this
741      * @param skipParent2 skip row if the parent is this
742      * @return true if no row was written so far (the new value of first)
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      * Checks if the first footer of the table is placed before the first body (or row)
763      * in the document.
764      *
765      * @param htmlTable the table
766      * @return true if the footer is before the body
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      * Process {@link HtmlSelect}.
782      *
783      * @param builder the StringBuilder to add to
784      * @param htmlSelect the target to process
785      * @param mode the {@link Mode} to use for processing
786      */
787     protected void appendSelect(final HtmlSerializerTextBuilder builder,
788             final HtmlSelect htmlSelect, final Mode mode) {
789         // an empty select has no text and does not separate
790         final List<HtmlOption> options = htmlSelect.getOptions();
791         if (options.isEmpty()) {
792             return;
793         }
794 
795         builder.appendBlockSeparator();
796         boolean leadingNlPending = false;
797         // all options (also the ones inside of an optgroup) are rendered on a separate line;
798         // the white-space style is ignored for the options (see appendOption())
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      * Process {@link HtmlOption}.
813      *
814      * @param builder the StringBuilder to add to
815      * @param htmlOption the target to process
816      * @param mode the {@link Mode} to use for processing
817      */
818     protected void appendOption(final HtmlSerializerTextBuilder builder,
819             final HtmlOption htmlOption, final Mode mode) {
820         // like WebDriver: an option is only shown inside of a select;
821         // but it is still a block
822         if (htmlOption.getEnclosingSelect() == null) {
823             builder.appendBlockSeparator();
824             builder.appendBlockSeparator();
825             return;
826         }
827 
828         // options are rendered by the select (form control); the white-space style
829         // (also the one inherited from the select) is ignored
830         for (final DomNode child : htmlOption.getChildren()) {
831             appendNode(builder, child, Mode.WHITE_SPACE_NORMAL);
832         }
833     }
834 
835     /**
836      * Process {@link HtmlOrderedList}.
837      *
838      * @param builder the StringBuilder to add to
839      * @param htmlOrderedList the OL element
840      * @param mode the {@link Mode} to use for processing
841      */
842     protected void appendOrderedList(final HtmlSerializerTextBuilder builder,
843             final HtmlOrderedList htmlOrderedList, final Mode mode) {
844         appendListItems(builder, htmlOrderedList, mode);
845     }
846 
847     /**
848      * Process {@link HtmlUnorderedList}.
849      *
850      * @param builder the StringBuilder to add to
851      * @param htmlUnorderedList the target to process
852      * @param mode the {@link Mode} to use for processing
853      */
854     protected void appendUnorderedList(final HtmlSerializerTextBuilder builder,
855                     final HtmlUnorderedList htmlUnorderedList, final Mode mode) {
856         appendListItems(builder, htmlUnorderedList, mode);
857     }
858 
859     /**
860      * Process the children of a list (ol, ul); a block level item
861      * with some content is placed on a line of its own.
862      *
863      * @param builder the StringBuilder to add to
864      * @param list the list element
865      * @param mode the {@link Mode} to use for processing
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      * Process {@link HtmlPreformattedText}.
886      *
887      * @param builder the StringBuilder to add to
888      * @param htmlPreformattedText the target to process
889      * @param mode the {@link Mode} to use for processing
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      * Process {@link HtmlSvg}.
902      * The svg itself is inline; Chrome/Edge render the svg text elements as blocks.
903      *
904      * @param builder the StringBuilder to add to
905      * @param htmlSvg the target to process
906      * @param mode the {@link Mode} to use for processing
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      * Checks if the given svg node has some text that is not part of a title or desc element.
924      *
925      * @param node the node to check
926      * @return true if there is some text
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      * Process {@link HtmlInlineFrame}.
947      *
948      * @param builder the StringBuilder to add to
949      * @param htmlInlineFrame the target to process
950      * @param mode the {@link Mode} to use for processing
951      */
952     protected void appendInlineFrame(final HtmlSerializerTextBuilder builder,
953             final HtmlInlineFrame htmlInlineFrame, final Mode mode) {
954         // like WebDriver: the content of the frame is not part of the text,
955         // the iframe is an inline element with the (fallback) text of its children
956         if (isDisplayed(htmlInlineFrame)) {
957             appendChildren(builder, htmlInlineFrame, mode);
958         }
959     }
960 
961     /**
962      * Process {@link DomText}.
963      *
964      * @param builder the StringBuilder to add to
965      * @param domText the target to process
966      * @param mode the {@link Mode} to use for processing
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         // options are not rendered by css; the display style is ignored
977         if (parent == null
978                 || parent instanceof HtmlOption
979                 || isDisplayed(parent)) {
980             String data = domText.getData();
981             // zero width space is not part of the visible text
982             if (data.indexOf('\u200B') > -1) {
983                 data = data.replace("\u200B", "");
984             }
985             builder.append(data, mode);
986         }
987     }
988 
989     /**
990      * Process {@link DomComment}.
991      *
992      * @param builder the StringBuilder to add to
993      * @param domComment the target to process
994      * @param mode the {@link Mode} to use for processing
995      */
996     protected void appendComment(final HtmlSerializerTextBuilder builder,
997             final DomComment domComment, final Mode mode) {
998         // nothing to do
999     }
1000 
1001     /**
1002      * Process {@link HtmlBreak}.
1003      *
1004      * @param builder the StringBuilder to add to
1005      * @param htmlBreak the target to process
1006      * @param mode the {@link Mode} to use for processing
1007      */
1008     protected void appendBreak(final HtmlSerializerTextBuilder builder,
1009             final HtmlBreak htmlBreak, final Mode mode) {
1010         // a leading line break is not part of the visible text
1011         if (builder.isEmpty()) {
1012             return;
1013         }
1014         builder.appendBreak(mode);
1015     }
1016 
1017     /**
1018      * Process {@link HtmlCheckBoxInput}.
1019      *
1020      * @param builder the StringBuilder to add to
1021      * @param htmlCheckBoxInput the target to process
1022      * @param mode the {@link Mode} to use for processing
1023      */
1024     protected void appendCheckBoxInput(final HtmlSerializerTextBuilder builder,
1025                     final HtmlCheckBoxInput htmlCheckBoxInput, final Mode mode) {
1026         // nothing to do
1027     }
1028 
1029     /**
1030      * Process {@link HtmlRadioButtonInput}.
1031      *
1032      * @param builder the StringBuilder to add to
1033      * @param htmlRadioButtonInput the target to process
1034      * @param mode the {@link Mode} to use for processing
1035      */
1036     protected void appendRadioButtonInput(final HtmlSerializerTextBuilder builder,
1037             final HtmlRadioButtonInput htmlRadioButtonInput, final Mode mode) {
1038         // nothing to do
1039     }
1040 
1041     /**
1042      * Determines the white-space mode for the given node; the white-space style of the node
1043      * and the ancestors is checked. The result is cached for the current run.
1044      *
1045      * @param domNode the node
1046      * @param defaultMode the mode used if no white-space style is defined
1047      * @return the {@link Mode}
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         // walk up until the first node defining a white-space style or the first known result;
1060         // all the nodes on the way share this result
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      * Determines the white-space mode for the given node; only the white-space style
1087      * of the node itself is checked.
1088      *
1089      * @param domNode the node
1090      * @param defaultMode the mode used if the node does not define a white-space style
1091      * @return the {@link Mode}
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      * Maps the white-space (and white-space-collapse) style to the {@link Mode}.
1111      *
1112      * @param style the computed style
1113      * @return the {@link Mode} or {@code null} if the style does not define the white-space handling
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             // WebDriver collapses the spaces of break-spaces but keeps the line breaks
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      * Helper to compose the text for the serializer based on several modes.
1143      */
1144     protected static class HtmlSerializerTextBuilder {
1145         /** Mode. */
1146         protected enum Mode {
1147             /**
1148              * The mode for the pre tag.
1149              */
1150             PRE,
1151 
1152             /**
1153              * Sequences of white space are collapsed. Newline characters
1154              * in the source are handled the same as other white space.
1155              * Lines are broken as necessary to fill line boxes.
1156              */
1157             WHITE_SPACE_NORMAL,
1158 
1159             /**
1160              * Sequences of white space are preserved. Lines are only broken
1161              * at newline characters in the source and at {@code <br>} elements.
1162              */
1163             WHITE_SPACE_PRE,
1164 
1165             /**
1166              * Sequences of white space are collapsed. Lines are broken
1167              * at newline characters, at {@code <br>} and as necessary
1168              * to fill line boxes.
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          * Ctor.
1190          */
1191         public HtmlSerializerTextBuilder() {
1192             builder_ = new StringBuilder();
1193             state_ = State.EMPTY;
1194             trimRightPos_ = 0;
1195         }
1196 
1197         /**
1198          * Append the provided content.
1199          * see https://drafts.csswg.org/css-text-3/#white-space
1200          *
1201          * @param content the content to add
1202          * @param mode the {@link Mode}
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                 // a \r is ignored; only at the end it is handled like a \n
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                         // a leading line break is not part of the visible text
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          * Append a block separator.
1332          */
1333         public void appendBlockSeparator() {
1334             // like WebDriver: no line break if the current line contains only (preserved) white space
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          * Checks if the text starting at the given position contains only white space.
1350          *
1351          * @param start the start position
1352          * @return true if there is only white space
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          * Append a block separator.
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          * Append a break.
1404          *
1405          * @param mode the {@link Mode}
1406          */
1407         public void appendBreak(final Mode mode) {
1408             // a break after a block adds an empty line
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          * Append a blank.
1421          */
1422         public void appendBlank() {
1423             builder_.append(' ');
1424             state_ = State.BLANK_AT_END;
1425             trimRightPos_ = builder_.length();
1426         }
1427 
1428         /**
1429          * Append the separator between two table cells; the separator is
1430          * collapsible (not added twice, removed at the end of a line).
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          * Remove all trailing whitespace from the end.
1448          *
1449          * @param mode the {@link Mode}
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          * Returns {@code true} if nothing was added so far.
1475          *
1476          * @return {@code true} if nothing was added so far
1477          */
1478         public boolean isEmpty() {
1479             return state_ == State.EMPTY;
1480         }
1481 
1482         /**
1483          * Returns true if some content was already added.
1484          *
1485          * @return true if some content was already added
1486          */
1487         public boolean wasContentAdded() {
1488             return contentAdded_;
1489         }
1490 
1491         /**
1492          * Resets the contentAdded state to false.
1493          */
1494         public void resetContentAdded() {
1495             contentAdded_ = false;
1496         }
1497 
1498         /**
1499          * Returns the constructed text.
1500          *
1501          * @return the constructed text.
1502          */
1503         public String getText() {
1504             return builder_.substring(0, trimRightPos_);
1505         }
1506     }
1507 }