Search in sources :

Example 46 with JsxFunction

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

the class MessagePort method postMessage.

/**
 * Posts a message.
 * @param message the object passed to the window
 * @param transfer an optional sequence of Transferable objects
 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage">MDN documentation</a>
 */
@JsxFunction
public void postMessage(final String message, final Object transfer) {
    if (port1_ != null) {
        final Window w = getWindow();
        final WebWindow webWindow = w.getWebWindow();
        final Page page = webWindow.getEnclosedPage();
        final URL currentURL = page.getUrl();
        final MessageEvent event = new MessageEvent();
        final String origin = currentURL.getProtocol() + "://" + currentURL.getHost() + ':' + currentURL.getPort();
        event.initMessageEvent(Event.TYPE_MESSAGE, false, false, message, origin, "", w, transfer);
        event.setParentScope(port1_);
        event.setPrototype(getPrototype(event.getClass()));
        final JavaScriptEngine jsEngine = (JavaScriptEngine) webWindow.getWebClient().getJavaScriptEngine();
        final PostponedAction action = new PostponedAction(page, "MessagePort.postMessage") {

            @Override
            public void execute() throws Exception {
                final ContextFactory cf = jsEngine.getContextFactory();
                cf.call(cx -> port1_.dispatchEvent(event));
            }
        };
        jsEngine.addPostponedAction(action);
    }
}
Also used : WebWindow(com.gargoylesoftware.htmlunit.WebWindow) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) Page(com.gargoylesoftware.htmlunit.Page) PostponedAction(com.gargoylesoftware.htmlunit.javascript.PostponedAction) URL(java.net.URL) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 47 with JsxFunction

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

the class CanvasRenderingContext2D method clip.

/**
 * Creates a new clipping region.
 * @param context the JavaScript context
 * @param thisObj the scriptable
 * @param args the arguments passed into the method
 * @param function the function
 */
@JsxFunction
public static void clip(final Context context, final Scriptable thisObj, final Object[] args, final Function function) {
    if (!(thisObj instanceof CanvasRenderingContext2D)) {
        throw Context.reportRuntimeError("CanvasRenderingContext2D.getImageData() failed - this is not a CanvasRenderingContext2D");
    }
    final CanvasRenderingContext2D canvas = (CanvasRenderingContext2D) thisObj;
    RenderingBackend.WindingRule windingRule = WindingRule.NON_ZERO;
    if (args.length == 1) {
        final String windingRuleParam = ScriptRuntime.toString(args[0]);
        if ("evenodd".contentEquals(windingRuleParam)) {
            windingRule = WindingRule.EVEN_ODD;
        }
        canvas.getRenderingBackend().clip(windingRule, null);
    }
    if (args.length > 1) {
        if (!(args[0] instanceof Path2D)) {
            throw Context.reportRuntimeError("CanvasRenderingContext2D.clip() failed - the first parameter has to be a Path2D");
        }
        final String windingRuleParam = ScriptRuntime.toString(args[1]);
        if ("evenodd".contentEquals(windingRuleParam)) {
            windingRule = WindingRule.EVEN_ODD;
        }
        LOG.info("CanvasRenderingContext2D.clip(path, fillRule) not yet implemented");
    // canvas.getRenderingBackend().clip(windingRule, (Path2D) args[0]);
    }
    canvas.getRenderingBackend().clip(WindingRule.NON_ZERO, null);
}
Also used : AwtRenderingBackend(com.gargoylesoftware.htmlunit.javascript.host.canvas.rendering.AwtRenderingBackend) RenderingBackend(com.gargoylesoftware.htmlunit.javascript.host.canvas.rendering.RenderingBackend) WindingRule(com.gargoylesoftware.htmlunit.javascript.host.canvas.rendering.RenderingBackend.WindingRule) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 48 with JsxFunction

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

the class FormData method getAll.

/**
 * @param name the name of the field to check
 * @return the values found for the give name
 */
@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public Scriptable getAll(final String name) {
    if (StringUtils.isEmpty(name)) {
        return Context.getCurrentContext().newArray(this, 0);
    }
    final List<Object> values = new ArrayList<>();
    for (final NameValuePair pair : requestParameters_) {
        if (name.equals(pair.getName())) {
            values.add(pair.getValue());
        }
    }
    final Object[] stringValues = values.toArray(new Object[0]);
    return Context.getCurrentContext().newArray(this, stringValues);
}
Also used : NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) ArrayList(java.util.ArrayList) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 49 with JsxFunction

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

the class FormData method append.

/**
 * Appends a new value onto an existing key inside a {@code FormData} object,
 * or adds the key if it does not already exist.
 * @param name the name of the field whose data is contained in {@code value}
 * @param value the field's value
 * @param filename the filename reported to the server (optional)
 */
@JsxFunction
public void append(final String name, final Object value, final Object filename) {
    if (value instanceof File) {
        final File file = (File) value;
        String fileName = null;
        String contentType;
        if (filename instanceof String) {
            fileName = (String) filename;
        }
        contentType = file.getType();
        if (StringUtils.isEmpty(contentType)) {
            if (getBrowserVersion().hasFeature(JS_FORM_DATA_CONTENT_TYPE_PLAIN_IF_FILE_TYPE_UNKNOWN)) {
                contentType = MimeType.TEXT_PLAIN;
            } else {
                contentType = MimeType.APPLICATION_OCTET_STREAM;
            }
        }
        requestParameters_.add(new KeyDataPair(name, file.getFile(), fileName, contentType, (Charset) null));
    } else {
        requestParameters_.add(new NameValuePair(name, Context.toString(value)));
    }
}
Also used : NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) Charset(java.nio.charset.Charset) File(com.gargoylesoftware.htmlunit.javascript.host.file.File) KeyDataPair(com.gargoylesoftware.htmlunit.util.KeyDataPair) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 50 with JsxFunction

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

the class XSLTProcessor method transformToDocument.

/**
 * Transforms the node source applying the stylesheet given by the importStylesheet() function.
 * The owner document of the output node owns the returned document fragment.
 *
 * @param source the node to be transformed
 * @return the result of the transformation
 */
@JsxFunction
public XMLDocument transformToDocument(final Node source) {
    final XMLDocument doc = new XMLDocument();
    doc.setPrototype(getPrototype(doc.getClass()));
    doc.setParentScope(getParentScope());
    final Object transformResult = transform(source);
    final org.w3c.dom.Node node;
    if (transformResult instanceof org.w3c.dom.Node) {
        final org.w3c.dom.Node transformedDoc = (org.w3c.dom.Node) transformResult;
        node = transformedDoc.getFirstChild();
    } else {
        node = null;
    }
    final XmlPage page = new XmlPage(node, getWindow().getWebWindow());
    doc.setDomNode(page);
    return doc;
}
Also used : Node(com.gargoylesoftware.htmlunit.javascript.host.dom.Node) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) XmlPage(com.gargoylesoftware.htmlunit.xml.XmlPage) 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