Search in sources :

Example 1 with DomComment

use of com.gargoylesoftware.htmlunit.html.DomComment 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 2 with DomComment

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

the class HtmlUnitNekoDOMBuilder method comment.

// LexicalHandler methods
/**
 * {@inheritDoc}
 */
@Override
public void comment(final char[] ch, final int start, final int length) {
    handleCharacters();
    final String data = new String(ch, start, length);
    final DomComment comment = new DomComment(page_, data);
    appendChild(currentNode_, comment);
}
Also used : DomComment(com.gargoylesoftware.htmlunit.html.DomComment)

Example 3 with DomComment

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

the class Element method printNode.

protected void printNode(final StringBuilder builder, final DomNode node, final boolean html) {
    if (node instanceof DomComment) {
        if (html) {
            // Remove whitespace sequences.
            final String s = PRINT_NODE_PATTERN.matcher(node.getNodeValue()).replaceAll(" ");
            builder.append("<!--").append(s).append("-->");
        }
    } else if (node instanceof DomCharacterData) {
        // Remove whitespace sequences, possibly escape XML characters.
        String s = node.getNodeValue();
        if (html) {
            s = com.gargoylesoftware.htmlunit.util.StringUtils.escapeXmlChars(s);
        }
        builder.append(s);
    } else if (html) {
        final DomElement element = (DomElement) node;
        final Element scriptObject = node.getScriptableObject();
        final String tag = element.getTagName();
        Element htmlElement = null;
        if (scriptObject instanceof HTMLElement) {
            htmlElement = scriptObject;
        }
        builder.append('<').append(tag);
        // Add the attributes. IE does not use quotes, FF does.
        for (final DomAttr attr : element.getAttributesMap().values()) {
            if (!attr.getSpecified()) {
                continue;
            }
            final String name = attr.getName();
            final String value = PRINT_NODE_QUOTE_PATTERN.matcher(attr.getValue()).replaceAll("&quot;");
            builder.append(' ').append(name).append('=');
            builder.append('\"');
            builder.append(value);
            builder.append('\"');
        }
        builder.append('>');
        // Add the children.
        final boolean isHtml = html && !(scriptObject instanceof HTMLScriptElement) && !(scriptObject instanceof HTMLStyleElement);
        printChildren(builder, node, isHtml);
        if (null == htmlElement || !htmlElement.isEndTagForbidden()) {
            builder.append("</").append(tag).append('>');
        }
    } else {
        if (node instanceof HtmlElement) {
            final HtmlElement element = (HtmlElement) node;
            if ("p".equals(element.getTagName())) {
                if (getBrowserVersion().hasFeature(JS_INNER_HTML_LF)) {
                    // \r\n because it's to implement something IE specific
                    builder.append('\n');
                } else {
                    int i = builder.length() - 1;
                    while (i >= 0 && Character.isWhitespace(builder.charAt(i))) {
                        i--;
                    }
                    builder.setLength(i + 1);
                    builder.append('\n');
                }
            }
            if (!"script".equals(element.getTagName())) {
                printChildren(builder, node, html);
            }
        }
    }
}
Also used : DomComment(com.gargoylesoftware.htmlunit.html.DomComment) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HTMLStyleElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement) HTMLScriptElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLScriptElement) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HTMLScriptElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLScriptElement) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLTemplateElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTemplateElement) HTMLStyleElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) DomCharacterData(com.gargoylesoftware.htmlunit.html.DomCharacterData)

Example 4 with DomComment

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

the class XmlUtils method copy.

/**
 * Copy all children from 'source' to 'dest', within the context of the specified page.
 * @param page the page which the nodes belong to
 * @param source the node to copy from
 * @param dest the node to copy to
 * @param handleXHTMLAsHTML if true elements from the XHTML namespace are handled as HTML elements instead of
 *     DOM elements
 */
private static void copy(final SgmlPage page, final Node source, final DomNode dest, final boolean handleXHTMLAsHTML, final Map<Integer, List<String>> attributesOrderMap) {
    final NodeList nodeChildren = source.getChildNodes();
    for (int i = 0; i < nodeChildren.getLength(); i++) {
        final Node child = nodeChildren.item(i);
        switch(child.getNodeType()) {
            case Node.ELEMENT_NODE:
                final DomNode childXml = createFrom(page, child, handleXHTMLAsHTML, attributesOrderMap);
                dest.appendChild(childXml);
                copy(page, child, childXml, handleXHTMLAsHTML, attributesOrderMap);
                break;
            case Node.TEXT_NODE:
                dest.appendChild(new DomText(page, child.getNodeValue()));
                break;
            case Node.CDATA_SECTION_NODE:
                dest.appendChild(new DomCDataSection(page, child.getNodeValue()));
                break;
            case Node.COMMENT_NODE:
                dest.appendChild(new DomComment(page, child.getNodeValue()));
                break;
            case Node.PROCESSING_INSTRUCTION_NODE:
                dest.appendChild(new DomProcessingInstruction(page, child.getNodeName(), child.getNodeValue()));
                break;
            default:
                if (LOG.isWarnEnabled()) {
                    LOG.warn("NodeType " + child.getNodeType() + " (" + child.getNodeName() + ") is not yet supported.");
                }
        }
    }
}
Also used : DomCDataSection(com.gargoylesoftware.htmlunit.html.DomCDataSection) DomComment(com.gargoylesoftware.htmlunit.html.DomComment) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomText(com.gargoylesoftware.htmlunit.html.DomText) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DeferredNode(org.apache.xerces.dom.DeferredNode) DomProcessingInstruction(com.gargoylesoftware.htmlunit.html.DomProcessingInstruction)

Aggregations

DomComment (com.gargoylesoftware.htmlunit.html.DomComment)4 DomAttr (com.gargoylesoftware.htmlunit.html.DomAttr)2 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2 DomProcessingInstruction (com.gargoylesoftware.htmlunit.html.DomProcessingInstruction)2 DomText (com.gargoylesoftware.htmlunit.html.DomText)2 DomCDataSection (com.gargoylesoftware.htmlunit.html.DomCDataSection)1 DomCharacterData (com.gargoylesoftware.htmlunit.html.DomCharacterData)1 DomDocumentType (com.gargoylesoftware.htmlunit.html.DomDocumentType)1 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1 ElementFactory (com.gargoylesoftware.htmlunit.html.ElementFactory)1 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)1 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)1 HTMLScriptElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLScriptElement)1 HTMLStyleElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement)1 HTMLTemplateElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTemplateElement)1 LinkedHashMap (java.util.LinkedHashMap)1 DeferredNode (org.apache.xerces.dom.DeferredNode)1 Attr (org.w3c.dom.Attr)1 DocumentType (org.w3c.dom.DocumentType)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1