Search in sources :

Example 1 with HtmlMeta

use of com.gargoylesoftware.htmlunit.html.HtmlMeta in project htmlunit by HtmlUnit.

the class HtmlUnitNekoDOMBuilder method startElement.

/**
 * {@inheritDoc}
 */
@Override
public void startElement(String namespaceURI, final String localName, final String qName, final Attributes atts) throws SAXException {
    if (snippetStartNodeOverwritten_) {
        snippetStartNodeOverwritten_ = false;
        return;
    }
    handleCharacters();
    final String tagLower = localName.toLowerCase(Locale.ROOT);
    if (page_.isParsingHtmlSnippet() && ("html".equals(tagLower) || "body".equals(tagLower))) {
        return;
    }
    if ("head".equals(tagLower)) {
        if (headParsed_ == HeadParsed.YES || page_.isParsingHtmlSnippet()) {
            return;
        }
        headParsed_ = lastTagWasSynthesized_ ? HeadParsed.SYNTHESIZED : HeadParsed.YES;
    }
    if (namespaceURI != null) {
        namespaceURI = namespaceURI.trim();
    } else // add a head if none was there
    if (headParsed_ == HeadParsed.NO && ("body".equals(tagLower) || "frameset".equals(tagLower))) {
        final ElementFactory factory = htmlParser_.getElementFactory(page_, null, "head", insideSvg_, false);
        final DomElement newElement = factory.createElement(page_, "head", null);
        appendChild(currentNode_, newElement);
        headParsed_ = HeadParsed.SYNTHESIZED;
    }
    // If we're adding a body element, keep track of any temporary synthetic ones
    // that we may have had to create earlier (for document.write(), for example).
    HtmlBody oldBody = null;
    if ("body".equals(qName) && page_.getBody() instanceof HtmlBody) {
        oldBody = (HtmlBody) page_.getBody();
    }
    // end tag.
    if ("form".equals(tagLower)) {
        formWaitingForLostChildren_ = null;
    }
    // Add the new node.
    if (!(page_ instanceof XHtmlPage) && Html.XHTML_NAMESPACE.equals(namespaceURI)) {
        namespaceURI = null;
    }
    final ElementFactory factory = htmlParser_.getElementFactory(page_, namespaceURI, qName, insideSvg_, false);
    if (factory == HtmlUnitNekoHtmlParser.SVG_FACTORY) {
        namespaceURI = Html.SVG_NAMESPACE;
    }
    final DomElement newElement = factory.createElementNS(page_, namespaceURI, qName, atts, true);
    newElement.setStartLocation(locator_.getLineNumber(), locator_.getColumnNumber());
    // parse can't replace everything as it does not buffer elements while parsing
    addNodeToRightParent(currentNode_, newElement);
    if ("svg".equals(tagLower)) {
        insideSvg_ = true;
    }
    // remove the old body and move its children to the real body element we just added.
    if (oldBody != null) {
        oldBody.quietlyRemoveAndMoveChildrenTo(newElement);
    }
    if ("body".equals(tagLower)) {
        body_ = (HtmlElement) newElement;
    } else if ("meta".equals(tagLower) && page_.hasFeature(META_X_UA_COMPATIBLE)) {
        final HtmlMeta meta = (HtmlMeta) newElement;
        if ("X-UA-Compatible".equals(meta.getHttpEquivAttribute())) {
            final String content = meta.getContentAttribute();
            if (content.startsWith("IE=")) {
                final String mode = content.substring(3).trim();
                final int version = page_.getWebClient().getBrowserVersion().getBrowserVersionNumeric();
                try {
                    int value = Integer.parseInt(mode);
                    if (value > version) {
                        value = version;
                    }
                    ((HTMLDocument) page_.getScriptableObject()).forceDocumentMode(value);
                } catch (final Exception e) {
                // ignore
                }
            }
        }
    } else if (createdByJavascript_ && "script".equals(tagLower)) {
        final ScriptElement script = (ScriptElement) newElement;
        script.markAsCreatedByDomParser();
    }
    currentNode_ = newElement;
    stack_.push(currentNode_);
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlBody(com.gargoylesoftware.htmlunit.html.HtmlBody) XHtmlPage(com.gargoylesoftware.htmlunit.html.XHtmlPage) ScriptElement(com.gargoylesoftware.htmlunit.html.ScriptElement) HtmlMeta(com.gargoylesoftware.htmlunit.html.HtmlMeta) XNIException(org.apache.xerces.xni.XNIException) SAXException(org.xml.sax.SAXException) ObjectInstantiationException(com.gargoylesoftware.htmlunit.ObjectInstantiationException) IOException(java.io.IOException) ElementFactory(com.gargoylesoftware.htmlunit.html.ElementFactory)

Aggregations

ObjectInstantiationException (com.gargoylesoftware.htmlunit.ObjectInstantiationException)1 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)1 ElementFactory (com.gargoylesoftware.htmlunit.html.ElementFactory)1 HtmlBody (com.gargoylesoftware.htmlunit.html.HtmlBody)1 HtmlMeta (com.gargoylesoftware.htmlunit.html.HtmlMeta)1 ScriptElement (com.gargoylesoftware.htmlunit.html.ScriptElement)1 XHtmlPage (com.gargoylesoftware.htmlunit.html.XHtmlPage)1 IOException (java.io.IOException)1 XNIException (org.apache.xerces.xni.XNIException)1 SAXException (org.xml.sax.SAXException)1