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