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);
}
}
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);
}
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);
}
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)));
}
}
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;
}
Aggregations