Search in sources :

Example 1 with SimpleRange

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();
}
Also used : HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) SimpleRange(com.gargoylesoftware.htmlunit.html.impl.SimpleRange) SelectableTextInput(com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput)

Example 2 with SimpleRange

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);
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) AlertHandler(com.gargoylesoftware.htmlunit.AlertHandler) SimpleRange(com.gargoylesoftware.htmlunit.html.impl.SimpleRange)

Example 3 with SimpleRange

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";
        }
    }
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) SimpleRange(com.gargoylesoftware.htmlunit.html.impl.SimpleRange) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)

Example 4 with SimpleRange

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;
}
Also used : SimpleRange(com.gargoylesoftware.htmlunit.html.impl.SimpleRange) Range(org.w3c.dom.ranges.Range) SimpleRange(com.gargoylesoftware.htmlunit.html.impl.SimpleRange) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 5 with SimpleRange

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;
}
Also used : SimpleRange(com.gargoylesoftware.htmlunit.html.impl.SimpleRange) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

SimpleRange (com.gargoylesoftware.htmlunit.html.impl.SimpleRange)6 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)3 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)2 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)2 Range (org.w3c.dom.ranges.Range)2 AlertHandler (com.gargoylesoftware.htmlunit.AlertHandler)1 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)1 CollectingAlertHandler (com.gargoylesoftware.htmlunit.CollectingAlertHandler)1 MockWebConnection (com.gargoylesoftware.htmlunit.MockWebConnection)1 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)1 SelectableTextInput (com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput)1 JsxSetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)1 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)1 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)1 ArrayList (java.util.ArrayList)1 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)1