use of com.gargoylesoftware.htmlunit.html.HtmlSvg 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;
}
Aggregations