Search in sources :

Example 1 with DomCharacterData

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

the class XMLDOMCharacterData method appendData.

/**
 * Appends the supplied string to the existing string data.
 * @param data the data that is to be appended to the existing string
 */
@JsxFunction
public void appendData(final String data) {
    if (data == null || "null".equals(data)) {
        throw Context.reportRuntimeError("Type mismatch.");
    }
    final DomCharacterData domCharacterData = getDomNodeOrDie();
    domCharacterData.appendData(data);
}
Also used : DomCharacterData(com.gargoylesoftware.htmlunit.html.DomCharacterData) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 2 with DomCharacterData

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

the class XMLDOMCharacterData method insertData.

/**
 * Inserts a string at the specified offset.
 * @param offset the offset, in characters, at which to insert the supplied string data
 * @param data the data that is to be inserted into the existing string
 */
@JsxFunction
public void insertData(final int offset, final String data) {
    if (data == null || "null".equals(data)) {
        throw Context.reportRuntimeError("Type mismatch.");
    }
    if (data.isEmpty()) {
        return;
    }
    if (offset < 0) {
        throw Context.reportRuntimeError("The offset must be 0 or a positive number that is not greater than the " + "number of characters in the data.");
    }
    final DomCharacterData domCharacterData = getDomNodeOrDie();
    if (offset > domCharacterData.getLength()) {
        throw Context.reportRuntimeError("The offset must be 0 or a positive number that is not greater than the " + "number of characters in the data.");
    }
    domCharacterData.insertData(offset, data);
}
Also used : DomCharacterData(com.gargoylesoftware.htmlunit.html.DomCharacterData) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 3 with DomCharacterData

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

the class CharacterData method deleteData.

/**
 * Delete characters from character data.
 * @param offset the position of the first character to be deleted
 * @param count the number of characters to be deleted
 */
@JsxFunction
public void deleteData(final int offset, final int count) {
    if (offset < 0) {
        throw Context.reportRuntimeError("Provided offset: " + offset + " is less than zero.");
    }
    if (getBrowserVersion().hasFeature(JS_DOM_CDATA_DELETE_THROWS_NEGATIVE_COUNT)) {
        if (count < 0) {
            throw Context.reportRuntimeError("Provided count: " + count + " is less than zero.");
        }
        if (count == 0) {
            return;
        }
    }
    final DomCharacterData domCharacterData = getDomCharacterDataOrDie();
    if (offset > domCharacterData.getLength()) {
        throw Context.reportRuntimeError("Provided offset: " + offset + " is greater than length.");
    }
    domCharacterData.deleteData(offset, count);
}
Also used : DomCharacterData(com.gargoylesoftware.htmlunit.html.DomCharacterData) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 4 with DomCharacterData

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

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

the class XMLDOMCharacterData method deleteData.

/**
 * Deletes specified data.
 * @param offset the offset, in characters, at which to start deleting string data
 * @param count the number of characters to delete
 */
@JsxFunction
public void deleteData(final int offset, final int count) {
    if (offset < 0) {
        throw Context.reportRuntimeError("The offset must be 0 or a positive number that is not greater than the " + "number of characters in the data.");
    }
    if (count < 0) {
        throw Context.reportRuntimeError("The offset must be 0 or a positive number that is not greater than the " + "number of characters in the data.");
    }
    if (count == 0) {
        return;
    }
    final DomCharacterData domCharacterData = getDomNodeOrDie();
    if (offset > domCharacterData.getLength()) {
        throw Context.reportRuntimeError("The offset must be 0 or a positive number that is not greater than the " + "number of characters in the data.");
    }
    domCharacterData.deleteData(offset, count);
}
Also used : DomCharacterData(com.gargoylesoftware.htmlunit.html.DomCharacterData) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

DomCharacterData (com.gargoylesoftware.htmlunit.html.DomCharacterData)7 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)5 DomAttr (com.gargoylesoftware.htmlunit.html.DomAttr)1 DomComment (com.gargoylesoftware.htmlunit.html.DomComment)1 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)1 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)1 JsxSetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)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