Search in sources :

Example 1 with HtmlRt

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

the class Document method createElement.

/**
 * Creates a new element with the given tag name.
 *
 * @param tagName the tag name
 * @return the new HTML element, or NOT_FOUND if the tag is not supported
 */
@JsxFunction
public Object createElement(String tagName) {
    Object result = NOT_FOUND;
    try {
        if (tagName.contains("<") || tagName.contains(">")) {
            if (LOG.isInfoEnabled()) {
                LOG.info("createElement: Provided string '" + tagName + "' contains an invalid character; '<' and '>' are not allowed");
            }
            throw Context.reportRuntimeError("String contains an invalid character");
        } else if (tagName.length() > 0 && tagName.charAt(0) == '<' && tagName.endsWith(">")) {
            tagName = tagName.substring(1, tagName.length() - 1);
            final Matcher matcher = TAG_NAME_PATTERN.matcher(tagName);
            if (!matcher.matches()) {
                if (LOG.isInfoEnabled()) {
                    LOG.info("createElement: Provided string '" + tagName + "' contains an invalid character");
                }
                throw Context.reportRuntimeError("String contains an invalid character");
            }
        }
        final SgmlPage page = getPage();
        org.w3c.dom.Node element = page.createElement(tagName);
        if (element instanceof HtmlInput) {
            ((HtmlInput) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlImage) {
            ((HtmlImage) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlRp) {
            ((HtmlRp) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlRt) {
            ((HtmlRt) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlUnknownElement) {
            ((HtmlUnknownElement) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlSvg) {
            element = UnknownElementFactory.instance.createElementNS(page, "", "svg", null);
            ((HtmlUnknownElement) element).markAsCreatedByJavascript();
        }
        final Object jsElement = getScriptableFor(element);
        if (jsElement == NOT_FOUND) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("createElement(" + tagName + ") cannot return a result as there isn't a JavaScript object for the element " + element.getClass().getName());
            }
        } else {
            result = jsElement;
        }
    } catch (final ElementNotFoundException e) {
    // Just fall through - result is already set to NOT_FOUND
    }
    return result;
}
Also used : HtmlImage(com.gargoylesoftware.htmlunit.html.HtmlImage) Matcher(java.util.regex.Matcher) ElementNotFoundException(com.gargoylesoftware.htmlunit.ElementNotFoundException) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject) HtmlRp(com.gargoylesoftware.htmlunit.html.HtmlRp) HtmlUnknownElement(com.gargoylesoftware.htmlunit.html.HtmlUnknownElement) HtmlSvg(com.gargoylesoftware.htmlunit.html.HtmlSvg) HtmlRt(com.gargoylesoftware.htmlunit.html.HtmlRt) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

ElementNotFoundException (com.gargoylesoftware.htmlunit.ElementNotFoundException)1 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)1 HtmlImage (com.gargoylesoftware.htmlunit.html.HtmlImage)1 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)1 HtmlRp (com.gargoylesoftware.htmlunit.html.HtmlRp)1 HtmlRt (com.gargoylesoftware.htmlunit.html.HtmlRt)1 HtmlSvg (com.gargoylesoftware.htmlunit.html.HtmlSvg)1 HtmlUnknownElement (com.gargoylesoftware.htmlunit.html.HtmlUnknownElement)1 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)1 Matcher (java.util.regex.Matcher)1 FunctionObject (net.sourceforge.htmlunit.corejs.javascript.FunctionObject)1 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)1