use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction 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;
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class Document method createEvent.
/**
* Implementation of the {@link org.w3c.dom.events.DocumentEvent} interface's
* {@link org.w3c.dom.events.DocumentEvent#createEvent(String)} method. The method creates an
* uninitialized event of the specified type.
*
* @see <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-DocumentEvent">DocumentEvent</a>
* @param eventType the event type to create
* @return an event object for the specified type
* @throws DOMException if the event type is not supported (will have a type of
* DOMException.NOT_SUPPORTED_ERR)
*/
@JsxFunction
public Event createEvent(final String eventType) throws DOMException {
Class<? extends Event> clazz = SUPPORTED_DOM2_EVENT_TYPE_MAP.get(eventType);
if (clazz == null) {
clazz = SUPPORTED_DOM3_EVENT_TYPE_MAP.get(eventType);
if (CloseEvent.class == clazz && getBrowserVersion().hasFeature(EVENT_ONCLOSE_DOCUMENT_CREATE_NOT_SUPPORTED)) {
clazz = null;
}
}
if (clazz == null && ("Events".equals(eventType) || "HashChangeEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_HASHCHANGEEVENT) || "BeforeUnloadEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_BEFOREUNLOADEVENT) || "MouseWheelEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_MOUSEWHEELEVENT) || "PointerEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_POINTEREVENT) || "PopStateEvent".equals(eventType) || "ProgressEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_PROGRESSEVENT) || "FocusEvent".equals(eventType) || "WheelEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_WHEELEVENT))) {
clazz = SUPPORTED_VENDOR_EVENT_TYPE_MAP.get(eventType);
if (PopStateEvent.class == clazz && getBrowserVersion().hasFeature(EVENT_ONPOPSTATE_DOCUMENT_CREATE_NOT_SUPPORTED)) {
clazz = null;
}
}
if (clazz == null) {
Context.throwAsScriptRuntimeEx(new DOMException(DOMException.NOT_SUPPORTED_ERR, "Event Type is not supported: " + eventType));
// to stop eclipse warning
return null;
}
try {
final Event event = clazz.newInstance();
event.setParentScope(getWindow());
event.setPrototype(getPrototype(clazz));
event.eventCreated();
return event;
} catch (final InstantiationException | IllegalAccessException e) {
throw Context.reportRuntimeError("Failed to instantiate event: class ='" + clazz.getName() + "' for event type of '" + eventType + "': " + e.getMessage());
}
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class Document method createDocumentFragment.
/**
* Creates a new document fragment.
* @return a newly created document fragment
*/
@JsxFunction
public Object createDocumentFragment() {
final DomDocumentFragment fragment = getDomNodeOrDie().getPage().createDocumentFragment();
final DocumentFragment node = new DocumentFragment();
node.setParentScope(getParentScope());
node.setPrototype(getPrototype(node.getClass()));
node.setDomNode(fragment);
return getScriptableFor(fragment);
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class StyleMedia method matchMedium.
/**
* Returns whether the specified media is supported by the object that displays the document object.
* @param media the media query
* @return whether the specified media is supported or not
*/
@JsxFunction
public boolean matchMedium(final String media) {
final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
final MediaListImpl mediaList = CSSStyleSheet.parseMedia(errorHandler, media);
return CSSStyleSheet.isActive(this, mediaList);
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class Range method compareBoundaryPoints.
/**
* Compares the boundary points of two Ranges.
* @param how a constant describing the comparison method
* @param sourceRange the Range to compare boundary points with this range
* @return -1, 0, or 1, indicating whether the corresponding boundary-point of range is respectively before,
* equal to, or after the corresponding boundary-point of sourceRange.
*/
@JsxFunction
public Object compareBoundaryPoints(final int how, final Range sourceRange) {
final Node nodeForThis;
final int offsetForThis;
final int containingMoficator;
if (START_TO_START == how || END_TO_START == how) {
nodeForThis = startContainer_;
offsetForThis = startOffset_;
containingMoficator = 1;
} else {
nodeForThis = endContainer_;
offsetForThis = endOffset_;
containingMoficator = -1;
}
final Node nodeForOther;
final int offsetForOther;
if (START_TO_END == how || START_TO_START == how) {
nodeForOther = sourceRange.startContainer_;
offsetForOther = sourceRange.startOffset_;
} else {
nodeForOther = sourceRange.endContainer_;
offsetForOther = sourceRange.endOffset_;
}
if (nodeForThis == nodeForOther) {
if (offsetForThis < offsetForOther) {
return Integer.valueOf(-1);
} else if (offsetForThis > offsetForOther) {
return Integer.valueOf(1);
}
return Integer.valueOf(0);
}
final byte nodeComparision = (byte) nodeForThis.compareDocumentPosition(nodeForOther);
if ((nodeComparision & Node.DOCUMENT_POSITION_CONTAINED_BY) != 0) {
return Integer.valueOf(-1 * containingMoficator);
} else if ((nodeComparision & Node.DOCUMENT_POSITION_PRECEDING) != 0) {
return Integer.valueOf(-1);
}
// TODO: handle other cases!
return Integer.valueOf(1);
}
Aggregations