use of com.gargoylesoftware.htmlunit.html.impl.SimpleRange in project htmlunit by HtmlUnit.
the class HtmlPage method setFocusedElement.
/**
* Moves the focus to the specified element. This will trigger any relevant JavaScript
* event handlers.
*
* @param newElement the element that will receive the focus, use {@code null} to remove focus from any element
* @param windowActivated - whether the enclosing window got focus resulting in specified element getting focus
* @return true if the specified element now has the focus
* @see #getFocusedElement()
*/
public boolean setFocusedElement(final DomElement newElement, final boolean windowActivated) {
if (elementWithFocus_ == newElement && !windowActivated) {
// nothing to do
return true;
}
final DomElement oldFocusedElement = elementWithFocus_;
elementWithFocus_ = null;
if (getWebClient().isJavaScriptEnabled()) {
final Object o = getScriptableObject();
if (o instanceof HTMLDocument) {
((HTMLDocument) o).setActiveElement(null);
}
}
if (!windowActivated) {
if (hasFeature(EVENT_FOCUS_IN_FOCUS_OUT_BLUR)) {
if (oldFocusedElement != null) {
oldFocusedElement.fireEvent(Event.TYPE_FOCUS_OUT);
}
if (newElement != null) {
newElement.fireEvent(Event.TYPE_FOCUS_IN);
}
}
if (oldFocusedElement != null) {
oldFocusedElement.removeFocus();
oldFocusedElement.fireEvent(Event.TYPE_BLUR);
if (hasFeature(EVENT_FOCUS_FOCUS_IN_BLUR_OUT)) {
oldFocusedElement.fireEvent(Event.TYPE_FOCUS_OUT);
}
}
}
elementWithFocus_ = newElement;
// might be changed by another thread
if (newElement instanceof SelectableTextInput && hasFeature(PAGE_SELECTION_RANGE_FROM_SELECTABLE_TEXT_INPUT)) {
final SelectableTextInput sti = (SelectableTextInput) newElement;
setSelectionRange(new SimpleRange(sti, sti.getSelectionStart(), sti, sti.getSelectionEnd()));
}
if (newElement != null) {
if (getWebClient().isJavaScriptEnabled()) {
final Object o = getScriptableObject();
if (o instanceof HTMLDocument) {
final Object e = newElement.getScriptableObject();
if (e instanceof HTMLElement) {
((HTMLDocument) o).setActiveElement((HTMLElement) e);
}
}
}
newElement.focus();
newElement.fireEvent(Event.TYPE_FOCUS);
if (hasFeature(EVENT_FOCUS_FOCUS_IN_BLUR_OUT)) {
newElement.fireEvent(Event.TYPE_FOCUS_IN);
}
}
// element will not have the focus because its page has gone away.
return this == getEnclosingWindow().getEnclosedPage();
}
use of com.gargoylesoftware.htmlunit.html.impl.SimpleRange in project htmlunit by HtmlUnit.
the class Selection2Test method test.
private void test(final String action, final String x, final String alert) throws Exception {
final String html = "<html>\n" + "<body onload='test()'>\n" + " <span id='s1'>abc</span><span id='s2'>xyz</span><span id='s3'>foo</span>\n" + " <input type='button' id='b' onclick=\"" + action + ";test();\" value='click'></input>\n" + "<script>\n" + " var selection = document.selection; // IE\n" + " if(!selection) selection = window.getSelection(); // FF\n" + " var s1 = document.getElementById('s1');\n" + " var s2 = document.getElementById('s2');\n" + " function test() {\n" + " try {\n" + " var x = " + x + ";\n" + " alert(" + alert + ");\n" + " } catch (e) {\n" + " alert('unsupported action');\n" + " }\n" + " }\n" + "</script>\n" + "</body></html>";
final WebClient client = getWebClient();
final MockWebConnection conn = new MockWebConnection();
conn.setDefaultResponse(html);
client.setWebConnection(conn);
final List<String> actual = new ArrayList<>();
final AlertHandler alertHandler = new CollectingAlertHandler(actual);
client.setAlertHandler(alertHandler);
final HtmlPage page = client.getPage(URL_FIRST);
final DomNode s1Text = page.getHtmlElementById("s1").getFirstChild();
final DomNode s2Text = page.getHtmlElementById("s2").getFirstChild();
final HtmlInput input = page.getHtmlElementById("b");
final org.w3c.dom.ranges.Range range = new SimpleRange();
range.setStart(s1Text, 2);
range.setEnd(s2Text, 1);
page.setSelectionRange(range);
input.click();
assertEquals(getExpectedAlerts(), actual);
}
use of com.gargoylesoftware.htmlunit.html.impl.SimpleRange in project htmlunit by HtmlUnit.
the class Document method setDesignMode.
/**
* Sets a value which indicates whether or not the document can be edited.
* @param mode a value which indicates whether or not the document can be edited
*/
@JsxSetter
public void setDesignMode(final String mode) {
final BrowserVersion browserVersion = getBrowserVersion();
final boolean inherit = browserVersion.hasFeature(JS_DOCUMENT_DESIGN_MODE_INHERIT);
if (inherit) {
if (!"on".equalsIgnoreCase(mode) && !"off".equalsIgnoreCase(mode) && !"inherit".equalsIgnoreCase(mode)) {
throw Context.reportRuntimeError("Invalid document.designMode value '" + mode + "'.");
}
if ("on".equalsIgnoreCase(mode)) {
designMode_ = "on";
} else if ("off".equalsIgnoreCase(mode)) {
designMode_ = "off";
} else if ("inherit".equalsIgnoreCase(mode)) {
designMode_ = "inherit";
}
} else {
if ("on".equalsIgnoreCase(mode)) {
designMode_ = "on";
final SgmlPage page = getPage();
if (page != null && page.isHtmlPage() && getBrowserVersion().hasFeature(JS_DOCUMENT_SELECTION_RANGE_COUNT)) {
final HtmlPage htmlPage = (HtmlPage) page;
final DomNode child = htmlPage.getBody().getFirstChild();
final DomNode rangeNode = child == null ? htmlPage.getBody() : child;
htmlPage.setSelectionRange(new SimpleRange(rangeNode, 0));
}
} else if ("off".equalsIgnoreCase(mode)) {
designMode_ = "off";
}
}
}
use of com.gargoylesoftware.htmlunit.html.impl.SimpleRange in project htmlunit by HtmlUnit.
the class Selection method collapse.
/**
* Collapses the current selection to a single point. The document is not modified.
* @param parentNode the caret location will be within this node
* @param offset the caret will be placed this number of characters from the beginning of the parentNode's text
*/
@JsxFunction
public void collapse(final Node parentNode, final int offset) {
final List<Range> ranges = getRanges();
ranges.clear();
ranges.add(new SimpleRange(parentNode.getDomNodeOrDie(), offset));
type_ = TYPE_CARET;
}
use of com.gargoylesoftware.htmlunit.html.impl.SimpleRange in project htmlunit by HtmlUnit.
the class Selection method addRange.
/**
* Adds a range to the selection.
* @param range the range to add
*/
@JsxFunction
public void addRange(final com.gargoylesoftware.htmlunit.javascript.host.dom.Range range) {
final SimpleRange rg = range.toW3C();
getRanges().add(rg);
if (TYPE_CARET.equals(type_) && rg.getCollapsed()) {
return;
}
type_ = TYPE_RANGE;
}
Aggregations