Search in sources :

Example 76 with JsxFunction

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

the class FileReader method readAsDataURL.

/**
 * 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 readAsDataURL(final Object object) throws IOException {
    readyState_ = LOADING;
    result_ = DataURLConnection.DATA_PREFIX;
    final byte[] bytes = ((Blob) object).getBytes();
    final String value = new String(new Base64().encode(bytes), StandardCharsets.US_ASCII);
    final BrowserVersion browserVersion = getBrowserVersion();
    String contentType = ((Blob) object).getType();
    if (StringUtils.isEmpty(contentType) && !browserVersion.hasFeature(JS_FILEREADER_EMPTY_NULL)) {
        contentType = MimeType.APPLICATION_OCTET_STREAM;
    }
    if (object instanceof File) {
        final java.io.File file = ((File) object).getFile();
        if (value.isEmpty()) {
            contentType = URLConnection.guessContentTypeFromName(file.getName());
        } else {
            contentType = Files.probeContentType(file.toPath());
        // this is a bit weak, on linux we get different results
        // e.g. 'application/octet-stream' for a file with random bits
        // instead of null on windows
        }
    }
    if (browserVersion.hasFeature(JS_FILEREADER_EMPTY_NULL)) {
        if (value.isEmpty()) {
            result_ = "null";
        } else {
            if (contentType != null) {
                result_ += contentType;
            }
            result_ += ";base64," + value;
        }
    } else {
        final boolean includeConentType = browserVersion.hasFeature(JS_FILEREADER_CONTENT_TYPE);
        if (!value.isEmpty() || includeConentType) {
            if (contentType == null) {
                contentType = MimeType.APPLICATION_OCTET_STREAM;
            }
            result_ += contentType + ";base64," + value;
        }
    }
    readyState_ = DONE;
    final Event event = new Event(this, Event.TYPE_LOAD);
    fireEvent(event);
}
Also used : Base64(org.apache.commons.codec.binary.Base64) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 77 with JsxFunction

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

the class Element method getElementsByTagName.

/**
 * Returns all the descendant elements with the specified tag name.
 * @param tagName the name to search for
 * @return all the descendant elements with the specified tag name
 */
@JsxFunction
public HTMLCollection getElementsByTagName(final String tagName) {
    if (elementsByTagName_ == null) {
        elementsByTagName_ = new HashMap<>();
    }
    final String searchTagName;
    final boolean caseSensitive;
    final DomNode dom = getDomNodeOrNull();
    if (dom == null) {
        searchTagName = tagName.toLowerCase(Locale.ROOT);
        caseSensitive = false;
    } else {
        final SgmlPage page = dom.getPage();
        if (page != null && page.hasCaseSensitiveTagNames()) {
            searchTagName = tagName;
            caseSensitive = true;
        } else {
            searchTagName = tagName.toLowerCase(Locale.ROOT);
            caseSensitive = false;
        }
    }
    HTMLCollection collection = elementsByTagName_.get(searchTagName);
    if (collection != null) {
        return collection;
    }
    final DomNode node = getDomNodeOrDie();
    if ("*".equals(tagName)) {
        collection = new HTMLCollection(node, false) {

            @Override
            protected boolean isMatching(final DomNode nodeToMatch) {
                return true;
            }
        };
    } else {
        collection = new HTMLCollection(node, false) {

            @Override
            protected boolean isMatching(final DomNode nodeToMatch) {
                if (caseSensitive) {
                    return searchTagName.equals(nodeToMatch.getNodeName());
                }
                return searchTagName.equalsIgnoreCase(nodeToMatch.getNodeName());
            }
        };
    }
    elementsByTagName_.put(tagName, collection);
    return collection;
}
Also used : ProxyDomNode(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HTMLCollection(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCollection) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 78 with JsxFunction

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

the class Element method getElementsByClassName.

/**
 * Returns all the descendant elements with the specified class.
 * @param className the name to search for
 * @return all the descendant elements with the specified class name
 */
@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public HTMLCollection getElementsByClassName(final String className) {
    final DomElement elt = getDomNodeOrDie();
    final String[] classNames = CLASS_NAMES_SPLIT_PATTERN.split(className, 0);
    return new HTMLCollection(elt, true) {

        @Override
        protected boolean isMatching(final DomNode node) {
            if (!(node instanceof HtmlElement)) {
                return false;
            }
            String classAttribute = ((HtmlElement) node).getAttributeDirect("class");
            if (ATTRIBUTE_NOT_DEFINED == classAttribute) {
                // probably better performance as most of elements won't have a class attribute
                return false;
            }
            classAttribute = " " + classAttribute + " ";
            for (final String aClassName : classNames) {
                if (!classAttribute.contains(" " + aClassName + " ")) {
                    return false;
                }
            }
            return true;
        }
    };
}
Also used : ProxyDomNode(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HTMLCollection(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCollection) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 79 with JsxFunction

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

the class Element method insertAdjacentElement.

/**
 * Inserts the given element into the element at the location.
 * @param where specifies where to insert the element, using one of the following values (case-insensitive):
 *        beforebegin, afterbegin, beforeend, afterend
 * @param insertedElement the element to be inserted
 * @return an element object
 *
 * @see <a href="http://msdn.microsoft.com/en-us/library/ie/ms536451.aspx">MSDN</a>
 */
@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public Object insertAdjacentElement(final String where, final Object insertedElement) {
    if (insertedElement instanceof Node) {
        final DomNode childNode = ((Node) insertedElement).getDomNodeOrDie();
        final Object[] values = getInsertAdjacentLocation(where);
        final DomNode node = (DomNode) values[0];
        final boolean append = ((Boolean) values[1]).booleanValue();
        if (append) {
            node.appendChild(childNode);
        } else {
            node.insertBefore(childNode);
        }
        return insertedElement;
    }
    throw Context.reportRuntimeError("Passed object is not an element: " + insertedElement);
}
Also used : ProxyDomNode(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) ProxyDomNode(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) Node(com.gargoylesoftware.htmlunit.javascript.host.dom.Node) FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 80 with JsxFunction

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

the class Element method insertAdjacentHTML.

/**
 * Parses the given text as HTML or XML and inserts the resulting nodes into the tree in the position given by the
 * position argument.
 * @param position specifies where to insert the nodes, using one of the following values (case-insensitive):
 *        <code>beforebegin</code>, <code>afterbegin</code>, <code>beforeend</code>, <code>afterend</code>
 * @param text the text to parse
 *
 * @see <a href="http://www.w3.org/TR/DOM-Parsing/#methods-2">W3C Spec</a>
 * @see <a href="http://domparsing.spec.whatwg.org/#dom-element-insertadjacenthtml">WhatWG Spec</a>
 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element.insertAdjacentHTML"
 *      >Mozilla Developer Network</a>
 * @see <a href="http://msdn.microsoft.com/en-us/library/ie/ms536452.aspx">MSDN</a>
 */
@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public void insertAdjacentHTML(final String position, final String text) {
    final Object[] values = getInsertAdjacentLocation(position);
    final DomNode domNode = (DomNode) values[0];
    final boolean append = ((Boolean) values[1]).booleanValue();
    // add the new nodes
    final DomNode proxyDomNode = new ProxyDomNode(domNode.getPage(), domNode, append);
    parseHtmlSnippet(proxyDomNode, text);
}
Also used : ProxyDomNode(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject) ProxyDomNode(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode) 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