Search in sources :

Example 1 with ElementFactory

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

the class HTMLOptionsCollection method setLength.

/**
 * Changes the number of options: removes options if the new length
 * is less than the current one else add new empty options to reach the
 * new length.
 * @param newLength the new length property value
 */
@JsxSetter
public void setLength(final int newLength) {
    if (newLength < 0) {
        if (getBrowserVersion().hasFeature(JS_SELECT_OPTIONS_IGNORE_NEGATIVE_LENGTH)) {
            return;
        }
        throw Context.reportRuntimeError("Length is negative");
    }
    final int currentLength = htmlSelect_.getOptionSize();
    if (currentLength > newLength) {
        htmlSelect_.setOptionSize(newLength);
    } else {
        final SgmlPage page = htmlSelect_.getPage();
        final ElementFactory factory = page.getWebClient().getPageCreator().getHtmlParser().getFactory(HtmlOption.TAG_NAME);
        for (int i = currentLength; i < newLength; i++) {
            final HtmlOption option = (HtmlOption) factory.createElement(page, HtmlOption.TAG_NAME, null);
            htmlSelect_.appendOption(option);
        }
    }
}
Also used : SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption) ElementFactory(com.gargoylesoftware.htmlunit.html.ElementFactory) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)

Example 2 with ElementFactory

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

the class XmlUtils method createFrom.

private static DomNode createFrom(final SgmlPage page, final Node source, final boolean handleXHTMLAsHTML, final Map<Integer, List<String>> attributesOrderMap) {
    if (source.getNodeType() == Node.TEXT_NODE) {
        return new DomText(page, source.getNodeValue());
    }
    if (source.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        return new DomProcessingInstruction(page, source.getNodeName(), source.getNodeValue());
    }
    if (source.getNodeType() == Node.COMMENT_NODE) {
        return new DomComment(page, source.getNodeValue());
    }
    if (source.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
        final DocumentType documentType = (DocumentType) source;
        return new DomDocumentType(page, documentType.getName(), documentType.getPublicId(), documentType.getSystemId());
    }
    final String ns = source.getNamespaceURI();
    String localName = source.getLocalName();
    if (handleXHTMLAsHTML && Html.XHTML_NAMESPACE.equals(ns)) {
        final ElementFactory factory = page.getWebClient().getPageCreator().getHtmlParser().getFactory(localName);
        return factory.createElementNS(page, ns, localName, namedNodeMapToSaxAttributes(source.getAttributes(), attributesOrderMap, source));
    }
    final NamedNodeMap nodeAttributes = source.getAttributes();
    if (page != null && page.isHtmlPage()) {
        localName = localName.toUpperCase(Locale.ROOT);
    }
    final String qualifiedName;
    if (source.getPrefix() == null) {
        qualifiedName = localName;
    } else {
        qualifiedName = source.getPrefix() + ':' + localName;
    }
    final String namespaceURI = source.getNamespaceURI();
    if (Html.SVG_NAMESPACE.equals(namespaceURI)) {
        return page.getWebClient().getPageCreator().getHtmlParser().getSvgFactory().createElementNS(page, namespaceURI, qualifiedName, namedNodeMapToSaxAttributes(nodeAttributes, attributesOrderMap, source));
    }
    final Map<String, DomAttr> attributes = new LinkedHashMap<>();
    for (int i = 0; i < nodeAttributes.getLength(); i++) {
        final int orderedIndex = getIndex(nodeAttributes, attributesOrderMap, source, i);
        final Attr attribute = (Attr) nodeAttributes.item(orderedIndex);
        final String attributeNamespaceURI = attribute.getNamespaceURI();
        final String attributeQualifiedName;
        if (attribute.getPrefix() == null) {
            attributeQualifiedName = attribute.getLocalName();
        } else {
            attributeQualifiedName = attribute.getPrefix() + ':' + attribute.getLocalName();
        }
        final String value = attribute.getNodeValue();
        final boolean specified = attribute.getSpecified();
        final DomAttr xmlAttribute = new DomAttr(page, attributeNamespaceURI, attributeQualifiedName, value, specified);
        attributes.put(attribute.getNodeName(), xmlAttribute);
    }
    return new DomElement(namespaceURI, qualifiedName, page, attributes);
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) DomDocumentType(com.gargoylesoftware.htmlunit.html.DomDocumentType) DocumentType(org.w3c.dom.DocumentType) Attr(org.w3c.dom.Attr) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) DomDocumentType(com.gargoylesoftware.htmlunit.html.DomDocumentType) LinkedHashMap(java.util.LinkedHashMap) DomComment(com.gargoylesoftware.htmlunit.html.DomComment) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) DomText(com.gargoylesoftware.htmlunit.html.DomText) DomProcessingInstruction(com.gargoylesoftware.htmlunit.html.DomProcessingInstruction) ElementFactory(com.gargoylesoftware.htmlunit.html.ElementFactory)

Example 3 with ElementFactory

use of com.gargoylesoftware.htmlunit.html.ElementFactory 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)

Example 4 with ElementFactory

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

the class HtmlUnitNekoHTMLErrorHandler method getElementFactory.

/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Returns the pre-registered element factory corresponding to the specified tag, or an UnknownElementFactory.
 * @param page the page
 * @param namespaceURI the namespace URI
 * @param qualifiedName the qualified name
 * @param insideSvg is the node inside an SVG node or not
 * @param svgSupport true if called from javascript createElementNS
 * @return the pre-registered element factory corresponding to the specified tag, or an UnknownElementFactory
 */
@Override
public ElementFactory getElementFactory(final SgmlPage page, final String namespaceURI, final String qualifiedName, final boolean insideSvg, final boolean svgSupport) {
    if (insideSvg) {
        return SVG_FACTORY;
    }
    if (namespaceURI == null || namespaceURI.isEmpty() || Html.XHTML_NAMESPACE.equals(namespaceURI) || Html.SVG_NAMESPACE.equals(namespaceURI) || !qualifiedName.contains(":")) {
        String tagName = qualifiedName;
        final int index = tagName.indexOf(':');
        if (index == -1) {
            tagName = tagName.toLowerCase(Locale.ROOT);
        } else {
            tagName = tagName.substring(index + 1);
        }
        final ElementFactory factory;
        if (svgSupport && !"svg".equals(tagName) && Html.SVG_NAMESPACE.equals(namespaceURI)) {
            factory = SVG_FACTORY;
        } else {
            factory = ELEMENT_FACTORIES.get(tagName);
        }
        if (factory != null) {
            return factory;
        }
    }
    return UnknownElementFactory.instance;
}
Also used : UnknownElementFactory(com.gargoylesoftware.htmlunit.html.UnknownElementFactory) SvgElementFactory(com.gargoylesoftware.htmlunit.svg.SvgElementFactory) DefaultElementFactory(com.gargoylesoftware.htmlunit.html.DefaultElementFactory) ElementFactory(com.gargoylesoftware.htmlunit.html.ElementFactory)

Aggregations

ElementFactory (com.gargoylesoftware.htmlunit.html.ElementFactory)4 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2 ObjectInstantiationException (com.gargoylesoftware.htmlunit.ObjectInstantiationException)1 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)1 DefaultElementFactory (com.gargoylesoftware.htmlunit.html.DefaultElementFactory)1 DomAttr (com.gargoylesoftware.htmlunit.html.DomAttr)1 DomComment (com.gargoylesoftware.htmlunit.html.DomComment)1 DomDocumentType (com.gargoylesoftware.htmlunit.html.DomDocumentType)1 DomProcessingInstruction (com.gargoylesoftware.htmlunit.html.DomProcessingInstruction)1 DomText (com.gargoylesoftware.htmlunit.html.DomText)1 HtmlBody (com.gargoylesoftware.htmlunit.html.HtmlBody)1 HtmlMeta (com.gargoylesoftware.htmlunit.html.HtmlMeta)1 HtmlOption (com.gargoylesoftware.htmlunit.html.HtmlOption)1 ScriptElement (com.gargoylesoftware.htmlunit.html.ScriptElement)1 UnknownElementFactory (com.gargoylesoftware.htmlunit.html.UnknownElementFactory)1 XHtmlPage (com.gargoylesoftware.htmlunit.html.XHtmlPage)1 JsxSetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)1 SvgElementFactory (com.gargoylesoftware.htmlunit.svg.SvgElementFactory)1 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1