Java Code Examples for net.sourceforge.htmlunit.corejs.javascript.Context#toNumber()
The following examples show how to use
net.sourceforge.htmlunit.corejs.javascript.Context#toNumber() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HTMLTableRowElement.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Deletes the cell at the specified index in the element's cells collection. If the index * is -1 (or while simulating IE, when there is no index specified), then the last cell is deleted. * @see <a href="http://msdn.microsoft.com/en-us/library/ms536406.aspx">MSDN Documentation</a> * @see <a href="http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/html.html#ID-11738598">W3C DOM Level2</a> * @param index specifies the cell to delete. */ @JsxFunction public void deleteCell(final Object index) { int position = -1; if (!Undefined.isUndefined(index)) { position = (int) Context.toNumber(index); } else if (getBrowserVersion().hasFeature(JS_TABLE_ROW_DELETE_CELL_REQUIRES_INDEX)) { throw Context.reportRuntimeError("No enough arguments"); } final HtmlTableRow htmlRow = (HtmlTableRow) getDomNodeOrDie(); if (position == -1) { position = htmlRow.getCells().size() - 1; } final boolean indexValid = position >= -1 && position <= htmlRow.getCells().size(); if (!indexValid) { throw Context.reportRuntimeError("Index or size is negative or greater than the allowed amount"); } htmlRow.getCell(position).remove(); }
Example 2
Source File: HTMLTableRowElement.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Deletes the cell at the specified index in the element's cells collection. If the index * is -1 (or while simulating IE, when there is no index specified), then the last cell is deleted. * @see <a href="http://msdn.microsoft.com/en-us/library/ms536406.aspx">MSDN Documentation</a> * @see <a href="http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/html.html#ID-11738598">W3C DOM Level2</a> * @param index specifies the cell to delete. */ @JsxFunction public void deleteCell(final Object index) { int position = -1; if (index != Undefined.instance) { position = (int) Context.toNumber(index); } else if (getBrowserVersion().hasFeature(JS_TABLE_ROW_DELETE_CELL_REQUIRES_INDEX)) { throw Context.reportRuntimeError("No enough arguments"); } final HtmlTableRow htmlRow = (HtmlTableRow) getDomNodeOrDie(); if (position == -1) { position = htmlRow.getCells().size() - 1; } final boolean indexValid = position >= -1 && position <= htmlRow.getCells().size(); if (!indexValid) { throw Context.reportRuntimeError("Index or size is negative or greater than the allowed amount"); } htmlRow.getCell(position).remove(); }
Example 3
Source File: Document.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
private static org.w3c.dom.traversal.NodeFilter createFilterWrapper(final Scriptable filter, final boolean filterFunctionOnly) { org.w3c.dom.traversal.NodeFilter filterWrapper = null; if (filter != null) { filterWrapper = new org.w3c.dom.traversal.NodeFilter() { @Override public short acceptNode(final org.w3c.dom.Node n) { final Object[] args = new Object[] {((DomNode) n).getScriptableObject()}; final Object response; if (filter instanceof Callable) { response = ((Callable) filter).call(Context.getCurrentContext(), filter, filter, args); } else { if (filterFunctionOnly) { throw Context.reportRuntimeError("only a function is allowed as filter"); } response = ScriptableObject.callMethod(filter, "acceptNode", args); } return (short) Context.toNumber(response); } }; } return filterWrapper; }
Example 4
Source File: HTMLTableColElement.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Sets the value of the {@code span} property. * @param span the value of the {@code span} property */ @JsxSetter public void setSpan(final Object span) { final double d = Context.toNumber(span); int i = (int) d; if (i < 1) { if (getBrowserVersion().hasFeature(JS_TABLE_SPAN_THROWS_EXCEPTION_IF_INVALID)) { final Exception e = new Exception("Cannot set the span property to invalid value: " + span); Context.throwAsScriptRuntimeEx(e); } else { i = 1; } } getDomNodeOrDie().setAttribute("span", Integer.toString(i)); }
Example 5
Source File: RowContainer.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Inserts a new row at the specified index in the element's row collection. If the index * is -1 or there is no index specified, then the row is appended at the end of the * element's row collection. * @see <a href="http://msdn.microsoft.com/en-us/library/ms536457.aspx">MSDN Documentation</a> * @param index specifies where to insert the row in the rows collection. * The default value is -1, which appends the new row to the end of the rows collection * @return the newly-created row */ @JsxFunction public Object insertRow(final Object index) { int rowIndex = -1; if (index != Undefined.instance) { rowIndex = (int) Context.toNumber(index); } final HTMLCollection rows = (HTMLCollection) getRows(); final int rowCount = rows.getLength(); final int r; if (rowIndex == -1 || rowIndex == rowCount) { r = Math.max(0, rowCount); } else { r = rowIndex; } if (r < 0 || r > rowCount) { throw Context.reportRuntimeError("Index or size is negative or greater than the allowed amount " + "(index: " + rowIndex + ", " + rowCount + " rows)"); } return insertRow(r); }
Example 6
Source File: HTMLCollection.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Returns the item or items corresponding to the specified index or key. * @param index the index or key corresponding to the element or elements to return * @return the element or elements corresponding to the specified index or key * @see <a href="http://msdn.microsoft.com/en-us/library/ms536460.aspx">MSDN doc</a> */ @Override public Object item(final Object index) { if (index instanceof String && getBrowserVersion().hasFeature(HTMLCOLLECTION_ITEM_SUPPORTS_ID_SEARCH_ALSO)) { final String name = (String) index; final Object result = namedItem(name); return result; } int idx = 0; final Double doubleValue = Context.toNumber(index); if (!doubleValue.isNaN()) { idx = doubleValue.intValue(); } final Object object = get(idx, this); if (object == NOT_FOUND) { return null; } return object; }
Example 7
Source File: HTMLTableRowElement.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Inserts a new cell at the specified index in the element's cells collection. If the index * is -1 or there is no index specified, then the cell is appended at the end of the * element's cells collection. * @see <a href="http://msdn.microsoft.com/en-us/library/ms536455.aspx">MSDN Documentation</a> * @param index specifies where to insert the cell in the tr. * The default value is -1, which appends the new cell to the end of the cells collection * @return the newly-created cell */ @JsxFunction public Object insertCell(final Object index) { int position = -1; if (index != Undefined.instance) { position = (int) Context.toNumber(index); } final HtmlTableRow htmlRow = (HtmlTableRow) getDomNodeOrDie(); final boolean indexValid = position >= -1 && position <= htmlRow.getCells().size(); if (indexValid) { final DomElement newCell = ((HtmlPage) htmlRow.getPage()).createElement("td"); if (position == -1 || position == htmlRow.getCells().size()) { htmlRow.appendChild(newCell); } else { htmlRow.getCell(position).insertBefore(newCell); } return getScriptableFor(newCell); } throw Context.reportRuntimeError("Index or size is negative or greater than the allowed amount"); }
Example 8
Source File: PointerEvent.java From htmlunit with Apache License 2.0 | 6 votes |
private static Object getValue(final ScriptableObject object, final String name, final Object defaulValue) { Object value = object.get(name); if (value != null) { if (defaulValue instanceof String) { value = String.valueOf(value); } else if (defaulValue instanceof Double) { value = Context.toNumber(value); } else if (defaulValue instanceof Number) { value = (int) Context.toNumber(value); } else { value = Context.toBoolean(value); } } else { value = defaulValue; } return value; }
Example 9
Source File: TextRange.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Changes the start position of the range. * @param unit specifies the units to move * @param count the number of units to move * @return the number of units moved */ @JsxFunction public int moveStart(final String unit, final Object count) { if (!"character".equals(unit)) { if (LOG.isWarnEnabled()) { LOG.warn("moveStart('" + unit + "') is not yet supported"); } return 0; } int c = 1; if (!Undefined.isUndefined(count)) { c = (int) Context.toNumber(count); } if (range_.getStartContainer() == range_.getEndContainer() && range_.getStartContainer() instanceof SelectableTextInput) { final SelectableTextInput input = (SelectableTextInput) range_.getStartContainer(); c = constrainMoveBy(c, range_.getStartOffset(), input.getText().length()); range_.setStart(input, range_.getStartOffset() + c); } return c; }
Example 10
Source File: Document.java From htmlunit with Apache License 2.0 | 6 votes |
private static org.w3c.dom.traversal.NodeFilter createFilterWrapper(final Scriptable filter, final boolean filterFunctionOnly) { org.w3c.dom.traversal.NodeFilter filterWrapper = null; if (filter != null) { filterWrapper = new org.w3c.dom.traversal.NodeFilter() { @Override public short acceptNode(final org.w3c.dom.Node n) { final Object[] args = new Object[] {((DomNode) n).getScriptableObject()}; final Object response; if (filter instanceof Callable) { response = ((Callable) filter).call(Context.getCurrentContext(), filter, filter, args); } else { if (filterFunctionOnly) { throw Context.reportRuntimeError("only a function is allowed as filter"); } response = ScriptableObject.callMethod(filter, "acceptNode", args); } return (short) Context.toNumber(response); } }; } return filterWrapper; }
Example 11
Source File: XMLDOMDocument.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Creates a node using the supplied type, name, and namespace. * @param type a value that uniquely identifies the node type * @param name the value for the new node's <code>nodeName</code> property * @param namespaceURI the namespace URI. * If specified, the node is created in the context of the namespaceURI parameter * with the prefix specified on the node name. * If the name parameter does not have a prefix, this is treated as the default namespace. * @return the newly created node */ @JsxFunction public Object createNode(final Object type, final String name, final Object namespaceURI) { switch ((short) Context.toNumber(type)) { case Node.ELEMENT_NODE: return createElementNS((String) namespaceURI, name); case Node.ATTRIBUTE_NODE: return createAttribute(name); default: throw Context.reportRuntimeError("xmlDoc.createNode(): Unsupported type " + (short) Context.toNumber(type)); } }
Example 12
Source File: Window.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Remove the callback from the list of animationFrames. * @param requestId the ID value returned by the call to window.requestAnimationFrame() * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/cancelAnimationFrame">MDN Doc</a> */ @JsxFunction public void cancelAnimationFrame(final Object requestId) { final int id = (int) Context.toNumber(requestId); final Iterator<AnimationFrame> frames = animationFrames_.iterator(); while (frames.hasNext()) { final Window.AnimationFrame animationFrame = frames.next(); if (animationFrame.id_ == id) { frames.remove(); } } }
Example 13
Source File: HTMLCollection.java From htmlunit with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override protected Object getWithPreemptionByName(final String name, final List<DomNode> elements) { final List<DomNode> matchingElements = new ArrayList<>(); final boolean searchName = isGetWithPreemptionSearchName(); for (final DomNode next : elements) { if (next instanceof DomElement && (searchName || next instanceof HtmlInput || next instanceof HtmlForm)) { final String nodeName = ((DomElement) next).getAttributeDirect("name"); if (name.equals(nodeName)) { matchingElements.add(next); } } } if (matchingElements.isEmpty()) { if (getBrowserVersion().hasFeature(HTMLCOLLECTION_ITEM_SUPPORTS_DOUBLE_INDEX_ALSO)) { final Double doubleValue = Context.toNumber(name); if (!doubleValue.isNaN()) { final Object object = get(doubleValue.intValue(), this); if (object != NOT_FOUND) { return object; } } } return NOT_FOUND; } else if (matchingElements.size() == 1) { return getScriptableForElement(matchingElements.get(0)); } // many elements => build a sub collection final DomNode domNode = getDomNodeOrNull(); final HTMLCollection collection = new HTMLCollection(domNode, matchingElements); collection.setAvoidObjectDetection(true); return collection; }
Example 14
Source File: HTMLCollection.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override protected Object getWithPreemptionByName(final String name, final List<DomNode> elements) { final List<DomNode> matchingElements = new ArrayList<>(); final boolean searchName = isGetWithPreemptionSearchName(); for (final DomNode next : elements) { if (next instanceof DomElement && (searchName || next instanceof HtmlInput || next instanceof HtmlForm)) { final String nodeName = ((DomElement) next).getAttributeDirect("name"); if (name.equals(nodeName)) { matchingElements.add(next); } } } if (matchingElements.isEmpty()) { if (getBrowserVersion().hasFeature(HTMLCOLLECTION_ITEM_SUPPORTS_DOUBLE_INDEX_ALSO)) { final Double doubleValue = Context.toNumber(name); if (!doubleValue.isNaN()) { final Object object = get(doubleValue.intValue(), this); if (object != NOT_FOUND) { return object; } } } return NOT_FOUND; } else if (matchingElements.size() == 1) { return getScriptableForElement(matchingElements.get(0)); } // many elements => build a sub collection final DomNode domNode = getDomNodeOrNull(); final HTMLCollection collection = new HTMLCollection(domNode, matchingElements); collection.setAvoidObjectDetection(true); return collection; }
Example 15
Source File: XMLDOMDocument.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Creates a node using the supplied type, name, and namespace. * @param type a value that uniquely identifies the node type * @param name the value for the new node's <code>nodeName</code> property * @param namespaceURI the namespace URI. * If specified, the node is created in the context of the namespaceURI parameter * with the prefix specified on the node name. * If the name parameter does not have a prefix, this is treated as the default namespace. * @return the newly created node */ @JsxFunction public Object createNode(final Object type, final String name, final Object namespaceURI) { switch ((short) Context.toNumber(type)) { case Node.ELEMENT_NODE: return createElementNS((String) namespaceURI, name); case Node.ATTRIBUTE_NODE: return createAttribute(name); default: throw Context.reportRuntimeError("xmlDoc.createNode(): Unsupported type " + (short) Context.toNumber(type)); } }
Example 16
Source File: CanvasRenderingContext2D.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Sets the {@code lineWidth} property. * @param lineWidth the {@code lineWidth} property */ @JsxSetter public void setLineWidth(final Object lineWidth) { if (!Undefined.isUndefined(lineWidth)) { final double width = Context.toNumber(lineWidth); if (!Double.isNaN(width)) { getRenderingBackend().setLineWidth((int) width); } } }
Example 17
Source File: HTMLFontElement.java From HtmlUnit-Android with Apache License 2.0 | 4 votes |
/** * Gets the {@code size} attribute. * @return the {@code size} attribute */ @JsxGetter public int getSize() { return (int) Context.toNumber(getDomNodeOrDie().getAttributeDirect("size")); }
Example 18
Source File: HTMLElement.java From HtmlUnit-Android with Apache License 2.0 | 4 votes |
/** * Returns the value of the tabIndex attribute. * @return the value of the tabIndex attribute */ @JsxGetter public int getTabIndex() { return (int) Context.toNumber(getDomNodeOrDie().getAttributeDirect("tabindex")); }
Example 19
Source File: HTMLFontElement.java From htmlunit with Apache License 2.0 | 4 votes |
/** * Gets the {@code size} attribute. * @return the {@code size} attribute */ @JsxGetter public int getSize() { return (int) Context.toNumber(getDomNodeOrDie().getAttributeDirect("size")); }
Example 20
Source File: HTMLBaseFontElement.java From HtmlUnit-Android with Apache License 2.0 | 4 votes |
/** * Gets the {@code size} attribute. * @return the {@code size} attribute */ @JsxGetter public int getSize() { final HtmlBaseFont base = (HtmlBaseFont) getDomNodeOrDie(); return (int) Context.toNumber(base.getSizeAttribute()); }