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