View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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.parser.neko;
16  
17  import static org.htmlunit.BrowserVersionFeatures.HTML_COMMAND_TAG;
18  import static org.htmlunit.BrowserVersionFeatures.JS_SCRIPT_IN_TEMPLATE_EXECUTED_ON_ATTACH;
19  
20  import java.io.IOException;
21  import java.io.StringReader;
22  import java.net.URL;
23  import java.nio.charset.Charset;
24  import java.util.ArrayDeque;
25  import java.util.Deque;
26  
27  import org.htmlunit.BrowserVersion;
28  import org.htmlunit.ObjectInstantiationException;
29  import org.htmlunit.WebClient;
30  import org.htmlunit.WebResponse;
31  import org.htmlunit.cyberneko.HTMLConfiguration;
32  import org.htmlunit.cyberneko.HTMLElements;
33  import org.htmlunit.cyberneko.HTMLScanner;
34  import org.htmlunit.cyberneko.HTMLTagBalancingListener;
35  import org.htmlunit.cyberneko.xerces.parsers.AbstractSAXParser;
36  import org.htmlunit.cyberneko.xerces.xni.Augmentations;
37  import org.htmlunit.cyberneko.xerces.xni.QName;
38  import org.htmlunit.cyberneko.xerces.xni.XMLAttributes;
39  import org.htmlunit.cyberneko.xerces.xni.XMLString;
40  import org.htmlunit.cyberneko.xerces.xni.XNIException;
41  import org.htmlunit.cyberneko.xerces.xni.parser.XMLInputSource;
42  import org.htmlunit.cyberneko.xerces.xni.parser.XMLParserConfiguration;
43  import org.htmlunit.html.DomCDataSection;
44  import org.htmlunit.html.DomComment;
45  import org.htmlunit.html.DomDocumentType;
46  import org.htmlunit.html.DomElement;
47  import org.htmlunit.html.DomNode;
48  import org.htmlunit.html.DomText;
49  import org.htmlunit.html.ElementFactory;
50  import org.htmlunit.html.Html;
51  import org.htmlunit.html.HtmlBody;
52  import org.htmlunit.html.HtmlElement;
53  import org.htmlunit.html.HtmlForm;
54  import org.htmlunit.html.HtmlHiddenInput;
55  import org.htmlunit.html.HtmlImage;
56  import org.htmlunit.html.HtmlPage;
57  import org.htmlunit.html.HtmlSvg;
58  import org.htmlunit.html.HtmlTable;
59  import org.htmlunit.html.HtmlTableRow;
60  import org.htmlunit.html.HtmlTemplate;
61  import org.htmlunit.html.ScriptElement;
62  import org.htmlunit.html.SubmittableElement;
63  import org.htmlunit.html.XHtmlPage;
64  import org.htmlunit.html.parser.HTMLParser;
65  import org.htmlunit.html.parser.HTMLParserDOMBuilder;
66  import org.htmlunit.html.parser.HTMLParserListener;
67  import org.htmlunit.javascript.host.html.HTMLBodyElement;
68  import org.htmlunit.util.StringUtils;
69  import org.w3c.dom.Node;
70  import org.xml.sax.Attributes;
71  import org.xml.sax.ContentHandler;
72  import org.xml.sax.Locator;
73  import org.xml.sax.SAXException;
74  import org.xml.sax.ext.LexicalHandler;
75  
76  /**
77   * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
78   *
79   * The parser and DOM builder. This class subclasses Xerces's AbstractSAXParser and implements
80   * the ContentHandler interface. Thus all parser APIs are kept private. The ContentHandler methods
81   * consume SAX events to build the page DOM
82   *
83   * @author Christian Sell
84   * @author David K. Taylor
85   * @author Chris Erskine
86   * @author Ahmed Ashour
87   * @author Marc Guillemot
88   * @author Ethan Glasser-Camp
89   * @author Sudhan Moghe
90   * @author Ronald Brill
91   * @author Frank Danek
92   * @author Carsten Steul
93   * @author Ronny Shapiro
94   * @author Atsushi Nakagawa
95   */
96  final class HtmlUnitNekoDOMBuilder extends AbstractSAXParser
97          implements ContentHandler, LexicalHandler, HTMLTagBalancingListener, HTMLParserDOMBuilder {
98  
99      // cache Neko Elements for performance and memory efficiency
100     private static final HTMLElements HTMLELEMENTS;
101     private static final HTMLElements HTMLELEMENTS_WITH_CMD;
102 
103     static {
104         // continue short code enumeration
105         final short commandShortCode = HTMLElements.UNKNOWN + 1;
106 
107         final HTMLElements.Element command = new HTMLElements.Element(commandShortCode, "COMMAND",
108                 HTMLElements.Element.EMPTY, new short[] {HTMLElements.BODY, HTMLElements.HEAD}, null);
109 
110         HTMLELEMENTS = new HTMLElements();
111 
112         final HTMLElements value = new HTMLElements();
113         value.setElement(command);
114         HTMLELEMENTS_WITH_CMD = value;
115     }
116 
117     private enum HeadParsed { YES, SYNTHESIZED, NO }
118 
119     private final HTMLParser htmlParser_;
120     private final HtmlPage page_;
121 
122     private Locator locator_;
123     private final Deque<DomNode> stack_ = new ArrayDeque<>();
124 
125     /** Did the snippet tried to overwrite the start node? */
126     private boolean snippetStartNodeOverwritten_;
127     private final int initialSize_;
128     private DomNode currentNode_;
129     private final boolean createdByJavascript_;
130     private final XMLString characters_ = new XMLString();
131     private HtmlUnitNekoDOMBuilder.HeadParsed headParsed_ = HeadParsed.NO;
132     private HtmlElement body_;
133     private boolean lastTagWasSynthesized_;
134     private HtmlForm consumingForm_;
135     private boolean formEndingIsAdjusting_;
136     private boolean insideSvg_;
137     private boolean insideTemplate_;
138 
139     private static final String FEATURE_AUGMENTATIONS = "http://cyberneko.org/html/features/augmentations";
140     private static final String FEATURE_PARSE_NOSCRIPT
141         = "http://cyberneko.org/html/features/parse-noscript-content";
142 
143     /**
144      * Parses and then inserts the specified HTML content into the HTML content currently being parsed.
145      * @param html the HTML content to push
146      */
147     @Override
148     public void pushInputString(final String html) {
149         page_.registerParsingStart();
150         page_.registerInlineSnippetParsingStart();
151         try {
152             final WebResponse webResponse = page_.getWebResponse();
153             final Charset charset = webResponse.getContentCharset();
154             final String url = webResponse.getWebRequest().getUrl().toString();
155             final XMLInputSource in = new XMLInputSource(null, url, null, new StringReader(html), charset.name());
156             ((HTMLConfiguration) parserConfiguration_).evaluateInputSource(in);
157         }
158         finally {
159             page_.registerParsingEnd();
160             page_.registerInlineSnippetParsingEnd();
161         }
162     }
163 
164     /**
165      * Creates a new builder for parsing the specified response contents.
166      * @param node the location at which to insert the new content
167      * @param url the page's URL
168      * @param createdByJavascript if true the (script) tag was created by javascript
169      */
170     HtmlUnitNekoDOMBuilder(final HTMLParser htmlParser,
171             final DomNode node, final URL url, final String htmlContent, final boolean createdByJavascript) {
172         super(createConfiguration(node.getPage().getWebClient().getBrowserVersion()));
173 
174         htmlParser_ = htmlParser;
175         page_ = (HtmlPage) node.getPage();
176 
177         currentNode_ = node;
178         for (final Node ancestor : currentNode_.getAncestors()) {
179             stack_.push((DomNode) ancestor);
180         }
181         createdByJavascript_ = createdByJavascript;
182 
183         final WebClient webClient = page_.getWebClient();
184         final HTMLParserListener listener = webClient.getHTMLParserListener();
185         final boolean reportErrors = listener != null;
186         if (reportErrors) {
187             parserConfiguration_.setErrorHandler(new HtmlUnitNekoHTMLErrorHandler(listener, url, htmlContent));
188         }
189 
190         try {
191             setFeature(FEATURE_AUGMENTATIONS, true);
192             setFeature("http://cyberneko.org/html/features/report-errors", reportErrors);
193             setFeature(FEATURE_PARSE_NOSCRIPT, !webClient.isJavaScriptEnabled());
194             setFeature(HTMLScanner.ALLOW_SELFCLOSING_IFRAME, false);
195 
196             setContentHandler(this);
197             setLexicalHandler(this); //comments and CDATA
198         }
199         catch (final SAXException e) {
200             throw new ObjectInstantiationException("unable to create HTML parser", e);
201         }
202         initialSize_ = stack_.size();
203     }
204 
205     /**
206      * Create the configuration depending on the simulated browser
207      * @return the configuration
208      */
209     private static XMLParserConfiguration createConfiguration(final BrowserVersion browserVersion) {
210         // HTMLElements.HTMLElementsWithCache are not thread safe
211         // because the cache is not synchronized
212         // we have to create a new one for each parser run
213 
214         if (browserVersion.hasFeature(HTML_COMMAND_TAG)) {
215             return new HTMLConfiguration(new HTMLElements.HTMLElementsWithCache(HTMLELEMENTS_WITH_CMD));
216         }
217         return new HTMLConfiguration(new HTMLElements.HTMLElementsWithCache(HTMLELEMENTS));
218     }
219 
220     /**
221      * {@inheritDoc}
222      */
223     @Override
224     public void setDocumentLocator(final Locator locator) {
225         locator_ = locator;
226     }
227 
228     /**
229      * {@inheritDoc}
230      */
231     @Override
232     public void startDocument() throws SAXException {
233         // nothing to do
234     }
235 
236     /** {@inheritDoc} */
237     @Override
238     public void startElement(final QName element, final XMLAttributes attributes, final Augmentations augs)
239         throws XNIException {
240         // augs might change so we store only the interesting part
241         lastTagWasSynthesized_ = augs.isSynthesized();
242         super.startElement(element, attributes, augs);
243     }
244 
245     /**
246      * {@inheritDoc}
247      */
248     @Override
249     public void startElement(String namespaceURI, final String localName, final String qName, final Attributes atts)
250         throws SAXException {
251 
252         if (snippetStartNodeOverwritten_) {
253             snippetStartNodeOverwritten_ = false;
254             return;
255         }
256         handleCharacters();
257 
258         final String tagLower = StringUtils.toRootLowerCase(localName);
259         if (page_.isParsingHtmlSnippet() && ("html".equals(tagLower) || "body".equals(tagLower))) {
260             // we have to push the current node on the stack to make sure
261             // the endElement call is able to remove a node from the stack
262             stack_.push(currentNode_);
263             return;
264         }
265 
266         if ("head".equals(tagLower)) {
267             if (headParsed_ == HeadParsed.YES || page_.isParsingHtmlSnippet()) {
268                 // we have to push the current node on the stack to make sure
269                 // the endElement call is able to remove a node from the stack
270                 stack_.push(currentNode_);
271                 return;
272             }
273 
274             headParsed_ = lastTagWasSynthesized_ ? HeadParsed.SYNTHESIZED : HeadParsed.YES;
275         }
276 
277         // If we're adding a body element, keep track of any temporary synthetic ones
278         // that we may have had to create earlier (for document.write(), for example).
279         HtmlBody oldBody = null;
280         final boolean isBodyTag = "body".equals(tagLower);
281         if (isBodyTag) {
282             final HtmlBody body = page_.getBody();
283             if (body != null) {
284                 oldBody = body;
285             }
286         }
287 
288         if (namespaceURI != null) {
289             namespaceURI = namespaceURI.trim();
290         }
291         // Add the new node.
292         if (!(page_ instanceof XHtmlPage) && Html.XHTML_NAMESPACE.equals(namespaceURI)) {
293             namespaceURI = null;
294         }
295 
296         final ElementFactory factory =
297                 htmlParser_.getElementFactory(page_, namespaceURI, qName, insideSvg_, false);
298         if (factory == HtmlUnitNekoHtmlParser.SVG_FACTORY) {
299             namespaceURI = Html.SVG_NAMESPACE;
300         }
301 
302         final DomElement newElement = factory.createElementNS(page_, namespaceURI, qName, atts);
303         newElement.setStartLocation(locator_.getLineNumber(), locator_.getColumnNumber());
304 
305         // parse can't replace everything as it does not buffer elements while parsing
306         addNodeToRightParent(currentNode_, newElement);
307 
308         if (newElement instanceof HtmlSvg) {
309             insideSvg_ = true;
310         }
311         else if (newElement instanceof HtmlTemplate) {
312             insideTemplate_ = true;
313         }
314 
315         // Forms own elements simply by enclosing source-wise rather than DOM parent-child relationship
316         // Forms without a </form> will keep consuming forever
317         else if (newElement instanceof HtmlForm) {
318             consumingForm_ = (HtmlForm) newElement;
319             formEndingIsAdjusting_ = false;
320         }
321         else if (consumingForm_ != null) {
322             // If the current form enclosed a suitable element
323             if (newElement instanceof SubmittableElement) {
324                 // Let these be owned by the form
325                 if (((HtmlElement) newElement).getEnclosingForm() != consumingForm_) {
326                     ((HtmlElement) newElement).setOwningForm(consumingForm_);
327                 }
328             }
329         }
330 
331         // If we had an old synthetic body, and we just added a real body element, quietly
332         // remove the old body and move its children to the real body element we just added.
333         if (oldBody != null) {
334             oldBody.quietlyRemoveAndMoveChildrenTo(newElement);
335         }
336 
337         if (!insideSvg_ && isBodyTag) {
338             body_ = (HtmlElement) newElement;
339         }
340         else if (createdByJavascript_
341                 && newElement instanceof ScriptElement
342                 && (!insideTemplate_
343                         || !page_.getWebClient().getBrowserVersion()
344                                 .hasFeature(JS_SCRIPT_IN_TEMPLATE_EXECUTED_ON_ATTACH))) {
345             final ScriptElement script = (ScriptElement) newElement;
346             script.markAsCreatedByDomParser();
347         }
348 
349         currentNode_ = newElement;
350         stack_.push(currentNode_);
351     }
352 
353     /**
354      * Adds the new node to the right parent that is not necessary the currentNode in case of
355      * malformed HTML code. The method tries to emulate the behavior of Firefox.
356      */
357     private void addNodeToRightParent(final DomNode currentNode, final DomElement newElement) {
358         final String currentNodeName = currentNode.getNodeName();
359         final String newNodeName = newElement.getNodeName();
360 
361         // First ensure table elements are housed correctly
362         if (isTableChild(newNodeName)) {
363             final DomNode parent =
364                     "table".equals(currentNodeName) ? currentNode : findElementOnStack("table");
365             appendChild(parent, newElement);
366             return;
367         }
368         if ("tr".equals(newNodeName)) {
369             final DomNode parent =
370                     isTableChild(currentNodeName) ? currentNode : findElementOnStack("tbody", "thead", "tfoot");
371             appendChild(parent, newElement);
372             return;
373         }
374         if (isTableCell(newNodeName)) {
375             final DomNode parent =
376                     "tr".equals(currentNodeName) ? currentNode : findElementOnStack("tr");
377             appendChild(parent, newElement);
378             return;
379         }
380 
381         // Next ensure non-table elements don't appear in tables
382         if ("table".equals(currentNodeName) || isTableChild(currentNodeName) || "tr".equals(currentNodeName)) {
383             if ("template".equals(newNodeName)) {
384                 currentNode.appendChild(newElement);
385             }
386 
387             // Scripts, forms, and styles are exempt
388             else if (!"colgroup".equals(currentNodeName)
389                     && ("script".equals(newNodeName)
390                         || "form".equals(newNodeName)
391                         || "style".equals(newNodeName))) {
392                 currentNode.appendChild(newElement);
393             }
394 
395             // These are good
396             else if ("col".equals(newNodeName) && "colgroup".equals(currentNodeName)) {
397                 currentNode.appendChild(newElement);
398             }
399             else if ("caption".equals(currentNodeName)) {
400                 currentNode.appendChild(newElement);
401             }
402             else if (newElement instanceof HtmlHiddenInput) {
403                 currentNode.appendChild(newElement);
404             }
405             else {
406                 // Move before the table
407                 final DomNode parent = findElementOnStack("table");
408                 parent.insertBefore(newElement);
409             }
410             return;
411         }
412 
413         if (formEndingIsAdjusting_ && "form".equals(currentNodeName)) {
414             // We cater to HTMLTagBalancer's shortcomings by moving this node out of the <form>
415             appendChild(currentNode.getParentNode(), newElement);
416             return;
417         }
418 
419         // Everything else
420         appendChild(currentNode, newElement);
421     }
422 
423     private DomNode findElementOnStack(final String searchedElementName) {
424         for (final DomNode node : stack_) {
425             if (searchedElementName.equals(node.getNodeName())) {
426                 return node;
427             }
428         }
429 
430         // this is surely wrong but at least it won't throw a NPE
431         return stack_.peek();
432     }
433 
434     private DomNode findElementOnStack(final String... searchedElementNames) {
435         for (final DomNode node : stack_) {
436             for (final String searchedElementName : searchedElementNames) {
437                 if (searchedElementName.equals(node.getNodeName())) {
438                     return node;
439                 }
440             }
441         }
442 
443         // this is surely wrong but at least it won't throw a NPE
444         return stack_.peek();
445     }
446 
447     private static boolean isTableChild(final String nodeName) {
448         if (nodeName == null || nodeName.length() < 5) {
449             return false;
450         }
451 
452         return "thead".equals(nodeName)
453                 || "tbody".equals(nodeName)
454                 || "tfoot".equals(nodeName)
455                 || "caption".equals(nodeName)
456                 || "colgroup".equals(nodeName);
457     }
458 
459     private static boolean isTableCell(final String nodeName) {
460         if (nodeName == null || nodeName.length() != 2) {
461             return false;
462         }
463         return "td".equals(nodeName) || "th".equals(nodeName);
464     }
465 
466     /** {@inheritDoc} */
467     @Override
468     public void endElement(final QName element, final Augmentations augs)
469         throws XNIException {
470         // augs might change so we store only the interesting part
471         lastTagWasSynthesized_ = augs.isSynthesized();
472         super.endElement(element, augs);
473     }
474 
475     /**
476      * {@inheritDoc}
477      */
478     @Override
479     public void endElement(final String namespaceURI, final String localName, final String qName)
480         throws SAXException {
481 
482         final String tagLower = StringUtils.toRootLowerCase(localName);
483 
484         handleCharacters();
485 
486         if (page_.isParsingHtmlSnippet()) {
487             if ("html".equals(tagLower) || "body".equals(tagLower)) {
488                 return;
489             }
490             if (stack_.size() == initialSize_) {
491                 // a <p> inside a <p> is valid for innerHTML processing
492                 // see HTMLParser2Test for more cases
493                 snippetStartNodeOverwritten_ = !StringUtils.equalsChar('p', tagLower);
494                 return;
495             }
496         }
497 
498         if ("svg".equals(tagLower)) {
499             insideSvg_ = false;
500         }
501         else if ("template".equals(tagLower)) {
502             insideTemplate_ = false;
503         }
504 
505         // this only avoids a problem when the stack is empty here
506         // but for this case we made the problem before - the balancing
507         // is broken already
508         if (stack_.isEmpty()) {
509             return;
510         }
511 
512         final DomNode previousNode = stack_.pop(); //remove currentElement from stack
513         previousNode.setEndLocation(locator_.getLineNumber(), locator_.getColumnNumber());
514 
515         if ("form".equals(tagLower) && !lastTagWasSynthesized_) {
516             // We get here if the </form> was on the same DOM tree depth as the <form> that started it,
517             // otherwise HTMLTagBalancer gives us the end through ignoredEndElement()
518             consumingForm_ = null;
519         }
520 
521         if (!stack_.isEmpty()) {
522             currentNode_ = stack_.peek();
523         }
524 
525         final boolean postponed = page_.isParsingInlineHtmlSnippet();
526         previousNode.onAllChildrenAddedToPage(postponed);
527     }
528 
529     /** {@inheritDoc} */
530     @Override
531     public void characters(final char[] ch, final int start, final int length) throws SAXException {
532         characters_.append(ch, start, length);
533     }
534 
535     /** {@inheritDoc} */
536     @Override
537     public void ignorableWhitespace(final char[] ch, final int start, final int length) throws SAXException {
538         characters_.append(ch, start, length);
539     }
540 
541     /**
542      * Picks up the character data accumulated so far and add it to the current element as a text node.
543      */
544     private void handleCharacters() {
545         // make the code easier to read because we remove a nesting level
546         if (characters_.length() == 0) {
547             return;
548         }
549 
550         // Use the normal behavior: append a text node for the accumulated text.
551         final String textValue = characters_.toString();
552         characters_.clear();
553 
554         if (StringUtils.isBlank(textValue)) {
555             appendChild(currentNode_, new DomText(page_, textValue));
556             return;
557         }
558 
559         // malformed HTML: </td>some text</tr> => text comes before the table
560         if (currentNode_ instanceof HtmlTableRow) {
561             final HtmlTableRow row = (HtmlTableRow) currentNode_;
562             final HtmlTable enclosingTable = row.getEnclosingTable();
563             if (enclosingTable != null) { // may be null when called from Range.createContextualFragment
564                 if (enclosingTable.getPreviousSibling() instanceof DomText) {
565                     final DomText domText = (DomText) enclosingTable.getPreviousSibling();
566                     domText.setTextContent(domText.getWholeText() + textValue);
567                 }
568                 else {
569                     enclosingTable.insertBefore(new DomText(page_, textValue));
570                 }
571             }
572         }
573         else if (currentNode_ instanceof HtmlTable) {
574             final HtmlTable enclosingTable = (HtmlTable) currentNode_;
575             if (enclosingTable.getPreviousSibling() instanceof DomText) {
576                 final DomText domText = (DomText) enclosingTable.getPreviousSibling();
577                 domText.setTextContent(domText.getWholeText() + textValue);
578             }
579             else {
580                 enclosingTable.insertBefore(new DomText(page_, textValue));
581             }
582         }
583         else if (currentNode_ instanceof HtmlImage) {
584             currentNode_.getParentNode().appendChild(new DomText(page_, textValue));
585         }
586         else {
587             appendChild(currentNode_, new DomText(page_, textValue));
588         }
589     }
590 
591     /** {@inheritDoc} */
592     @Override
593     public void endDocument() throws SAXException {
594         handleCharacters();
595         if (locator_ != null) {
596             page_.setEndLocation(locator_.getLineNumber(), locator_.getColumnNumber());
597         }
598     }
599 
600     /** {@inheritDoc} */
601     @Override
602     public void startPrefixMapping(final String prefix, final String uri) throws SAXException {
603         // nothing to do
604     }
605 
606     /** {@inheritDoc} */
607     @Override
608     public void endPrefixMapping(final String prefix) throws SAXException {
609         // nothing to do
610     }
611 
612     /** {@inheritDoc} */
613     @Override
614     public void processingInstruction(final String target, final String data) throws SAXException {
615         // nothing to do
616     }
617 
618     /** {@inheritDoc} */
619     @Override
620     public void skippedEntity(final String name) throws SAXException {
621         // nothing to do
622     }
623 
624     // LexicalHandler methods
625 
626     /** {@inheritDoc} */
627     @Override
628     public void comment(final char[] ch, final int start, final int length) {
629         handleCharacters();
630         final String data = new String(ch, start, length);
631         final DomComment comment = new DomComment(page_, data);
632         appendChild(currentNode_, comment);
633     }
634 
635     /** {@inheritDoc} */
636     @Override
637     public void endCDATA() {
638         final String data = characters_.toString();
639         characters_.clear();
640 
641         final DomCDataSection cdataSection = new DomCDataSection(page_, data);
642         appendChild(currentNode_, cdataSection);
643     }
644 
645     /** {@inheritDoc} */
646     @Override
647     public void endDTD() {
648         // nothing to do
649     }
650 
651     /** {@inheritDoc} */
652     @Override
653     public void endEntity(final String name) {
654         // nothing to do
655     }
656 
657     /** {@inheritDoc} */
658     @Override
659     public void startCDATA() {
660         handleCharacters();
661     }
662 
663     /** {@inheritDoc} */
664     @Override
665     public void startDTD(final String name, final String publicId, final String systemId) {
666         final DomDocumentType type = new DomDocumentType(page_, name, publicId, systemId);
667         page_.setDocumentType(type);
668 
669         final Node child;
670         child = type;
671         page_.appendChild(child);
672     }
673 
674     /** {@inheritDoc} */
675     @Override
676     public void startEntity(final String name) {
677         // nothing to do
678     }
679 
680     /**
681      * {@inheritDoc}
682      */
683     @Override
684     public void ignoredEndElement(final QName element, final Augmentations augs) {
685         // HTMLTagBalancer brings us here if </form> was found in the source on a different
686         // DOM tree depth (either above or below) to the <form> that started it
687         if ("form".equals(element.getLocalpart()) && consumingForm_ != null) {
688             consumingForm_ = null;
689 
690             if (findElementOnStack("table", "form") instanceof HtmlTable) {
691                 // The </form> just goes missing for these (really? just tables?)
692             }
693             else {
694                 /*
695                  * This </form> was ignored by HTMLTagBalancer as it generates its own
696                  * </form> at the end of the depth with the starting <form>.
697                  * e.g. This:
698                  * | <form>
699                  * |   <div>
700                  * |     </form> <!--ignored by HTMLTagBalancer-->
701                  * |   </div>
702                  * |   <input>
703                  *
704                  * is turned into:
705                  * | <form>
706                  * |   <div>
707                  * |   </div>
708                  * |   <input>
709                  * | </form> <!--synthesized by HTMLTagBalancer-->
710                  *
711                  * but this isn't suitable for us because </form> shouldn't be ignored but
712                  * rather moved directly behind the tree it's in to instead become:
713                  * | <form>
714                  * |   <div>
715                  * |   </div>
716                  * | </form> <!--moved out of div-->
717                  * | <input> <!--proceeding children are not part of form-->
718                  */
719                 // We cater for this by moving out nodes such as the <input> in the above
720                 // diagram out of the form
721                 formEndingIsAdjusting_ = true;
722             }
723         }
724     }
725 
726     /**
727      * {@inheritDoc}
728      */
729     @Override
730     public void ignoredStartElement(final QName elem, final XMLAttributes attrs, final Augmentations augs) {
731         // when multiple html/body elements are encountered, the attributes of the discarded
732         // elements are used when not previously defined
733         if (attrs != null && body_ != null) {
734             final String lp = elem.getLocalpart();
735             if (lp != null && lp.length() == 4) {
736                 if ("body".equalsIgnoreCase(lp)) {
737                     copyAttributes(body_, attrs);
738                 }
739                 else if ("html".equalsIgnoreCase(lp)) {
740                     final DomNode parent = body_.getParentNode();
741                     if (parent instanceof DomElement) {
742                         copyAttributes((DomElement) parent, attrs);
743                     }
744                 }
745             }
746         }
747     }
748 
749     private static void copyAttributes(final DomElement to, final XMLAttributes attrs) {
750         final int length = attrs.getLength();
751 
752         for (int i = 0; i < length; i++) {
753             final String attrName = StringUtils.toRootLowerCase(attrs.getLocalName(i));
754             if (to.getAttributes().getNamedItem(attrName) == null) {
755                 to.setAttribute(attrName, attrs.getValue(i));
756                 if (attrName.startsWith("on") && to.getPage().getWebClient().isJavaScriptEngineEnabled()
757                         && to.getScriptableObject() instanceof HTMLBodyElement) {
758                     final HTMLBodyElement jsBody = to.getScriptableObject();
759                     jsBody.createEventHandlerFromAttribute(attrName, attrs.getValue(i));
760                 }
761             }
762         }
763     }
764 
765     /**
766      * {@inheritDoc}
767      */
768     @Override
769     public void parse(final XMLInputSource inputSource) throws XNIException, IOException {
770         final HTMLParserDOMBuilder oldBuilder = page_.getDOMBuilder();
771         page_.setDOMBuilder(this);
772         try {
773             super.parse(inputSource);
774         }
775         finally {
776             page_.setDOMBuilder(oldBuilder);
777         }
778     }
779 
780     private static void appendChild(final DomNode parent, final DomNode child) {
781         if (parent instanceof HtmlTemplate) {
782             ((HtmlTemplate) parent).getContent().appendChild(child);
783             return;
784         }
785 
786         parent.appendChild(child);
787     }
788 }