Search in sources :

Example 11 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class Selection method collapseToStart.

/**
 * Moves the focus of the selection to the same point at the anchor. The anchor does not move.
 */
@JsxFunction
public void collapseToStart() {
    final Range first = getFirstRange();
    if (first != null) {
        final List<Range> ranges = getRanges();
        ranges.clear();
        ranges.add(first);
        first.collapse(true);
    }
    type_ = TYPE_CARET;
}
Also used : SimpleRange(com.gargoylesoftware.htmlunit.html.impl.SimpleRange) Range(org.w3c.dom.ranges.Range) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 12 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class TextRange method parentElement.

/**
 * Retrieves the parent element for the given text range.
 * The parent element is the element that completely encloses the text in the range.
 * If the text range spans text in more than one element, this method returns the smallest element that encloses
 * all the elements. When you insert text into a range that spans multiple elements, the text is placed in the
 * parent element rather than in any of the contained elements.
 *
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536654.aspx">MSDN doc</a>
 * @return the parent element object if successful, or null otherwise.
 */
@JsxFunction
public Node parentElement() {
    final org.w3c.dom.Node parent = range_.getCommonAncestorContainer();
    if (null == parent) {
        if (null == range_.getStartContainer() || null == range_.getEndContainer()) {
            try {
                final Window window = (Window) getParentScope();
                final HtmlPage page = (HtmlPage) window.getDomNodeOrDie();
                return (Node) getScriptableFor(page.getBody());
            } catch (final Exception e) {
            // ok bad luck
            }
        }
        return null;
    }
    return (Node) getScriptableFor(parent);
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 13 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class FileReader method readAsArrayBuffer.

/**
 * Reads the contents of the specified {@link Blob} or {@link File}.
 * @param object the {@link Blob} or {@link File} from which to read
 * @throws IOException if an error occurs
 */
@JsxFunction
public void readAsArrayBuffer(final Object object) throws IOException {
    readyState_ = LOADING;
    if (object instanceof Blob) {
        final byte[] bytes = ((Blob) object).getBytes();
        final NativeArrayBuffer buffer = new NativeArrayBuffer(bytes.length);
        System.arraycopy(bytes, 0, buffer.getBuffer(), 0, bytes.length);
        buffer.setParentScope(getParentScope());
        buffer.setPrototype(ScriptableObject.getClassPrototype(getWindow(), buffer.getClassName()));
        result_ = buffer;
    }
    readyState_ = DONE;
    final Event event = new Event(this, Event.TYPE_LOAD);
    fireEvent(event);
}
Also used : Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) NativeArrayBuffer(net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 14 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class FileReader method readAsText.

/**
 * Reads the contents of the specified {@link Blob} or {@link File}.
 * When the read operation is complete, the readyState is changed to DONE,
 * the loaded event is triggered, and the result attribute contains the
 * contents of the file as a text string.
 * @param object the {@link Blob} or {@link File} from which to read
 * @param encoding the encoding
 * @throws IOException if an error occurs
 */
@JsxFunction
public void readAsText(final Object object, final Object encoding) throws IOException {
    readyState_ = LOADING;
    Charset charset = StandardCharsets.UTF_8;
    if (encoding != null && !Undefined.isUndefined(encoding)) {
        final String encAsString = Context.toString(encoding);
        if (StringUtils.isNotBlank(encAsString)) {
            try {
                charset = Charsets.toCharset(encAsString.trim().toLowerCase(Locale.ROOT));
            } catch (final UnsupportedCharsetException e) {
                if (LOG.isWarnEnabled()) {
                    LOG.warn("FileReader readAsText was called with an unsupported encoding '" + encoding + "'. Using UTF-8 instead.");
                }
            }
        }
    }
    if (object instanceof Blob) {
        result_ = new String(((Blob) object).getBytes(), charset);
    }
    readyState_ = DONE;
    final Event event = new Event(this, Event.TYPE_LOAD);
    fireEvent(event);
}
Also used : UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Charset(java.nio.charset.Charset) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 15 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class TextRange method moveEnd.

/**
 * Changes the end 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 moveEnd(final String unit, final Object count) {
    if (!"character".equals(unit)) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("moveEnd('" + 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_.getEndOffset(), input.getText().length());
        range_.setEnd(input, range_.getEndOffset() + c);
    }
    return c;
}
Also used : SelectableTextInput(com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)133 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)30 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)20 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)19 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)16 URL (java.net.URL)14 IOException (java.io.IOException)11 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)11 WebClient (com.gargoylesoftware.htmlunit.WebClient)10 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)10 SimpleRange (com.gargoylesoftware.htmlunit.html.impl.SimpleRange)10 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)10 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)8 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)8 MessageEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent)8 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)8 NameValuePair (com.gargoylesoftware.htmlunit.util.NameValuePair)8 Range (org.w3c.dom.ranges.Range)8 HtmlAttributeChangeEvent (com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent)7 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)7