Search in sources :

Example 36 with SgmlPage

use of com.gargoylesoftware.htmlunit.SgmlPage in project htmlunit by HtmlUnit.

the class SimpleRange method cloneContents.

/**
 * {@inheritDoc}
 */
@Override
public DomDocumentFragment cloneContents() throws DOMException {
    // Clone the common ancestor.
    final DomNode ancestor = (DomNode) getCommonAncestorContainer();
    if (ancestor == null) {
        return new DomDocumentFragment(null);
    }
    final DomNode ancestorClone = ancestor.cloneNode(true);
    // Find the start container and end container clones.
    DomNode startClone = null;
    DomNode endClone = null;
    final DomNode start = (DomNode) startContainer_;
    final DomNode end = (DomNode) endContainer_;
    if (start == ancestor) {
        startClone = ancestorClone;
    }
    if (end == ancestor) {
        endClone = ancestorClone;
    }
    final Iterable<DomNode> descendants = ancestor.getDescendants();
    if (startClone == null || endClone == null) {
        final Iterator<DomNode> i = descendants.iterator();
        final Iterator<DomNode> ci = ancestorClone.getDescendants().iterator();
        while (i.hasNext()) {
            final DomNode e = i.next();
            final DomNode ce = ci.next();
            if (start == e) {
                startClone = ce;
            } else if (end == e) {
                endClone = ce;
                break;
            }
        }
    }
    // Remove everything following the selection end from the clones.
    if (endClone == null) {
        throw Context.reportRuntimeError("Unable to find end node clone.");
    }
    deleteAfter(endClone, endOffset_);
    for (DomNode n = endClone; n != null; n = n.getParentNode()) {
        while (n.getNextSibling() != null) {
            n.getNextSibling().remove();
        }
    }
    // Remove everything prior to the selection start from the clones.
    if (startClone == null) {
        throw Context.reportRuntimeError("Unable to find start node clone.");
    }
    deleteBefore(startClone, startOffset_);
    for (DomNode n = startClone; n != null; n = n.getParentNode()) {
        while (n.getPreviousSibling() != null) {
            n.getPreviousSibling().remove();
        }
    }
    final SgmlPage page = ancestor.getPage();
    final DomDocumentFragment fragment = new DomDocumentFragment(page);
    if (start == end) {
        fragment.appendChild(ancestorClone);
    } else {
        for (final DomNode n : ancestorClone.getChildNodes()) {
            fragment.appendChild(n);
        }
    }
    return fragment;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomDocumentFragment(com.gargoylesoftware.htmlunit.html.DomDocumentFragment) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage)

Example 37 with SgmlPage

use of com.gargoylesoftware.htmlunit.SgmlPage in project htmlunit by HtmlUnit.

the class HtmlUnitScriptable method initParentScope.

/**
 * Initialize the parent scope of a newly created scriptable.
 * @param domNode the DOM node for the script object
 * @param scriptable the script object to initialize
 */
protected void initParentScope(final DomNode domNode, final HtmlUnitScriptable scriptable) {
    final SgmlPage page = domNode.getPage();
    final WebWindow enclosingWindow = page.getEnclosingWindow();
    if (enclosingWindow != null && enclosingWindow.getEnclosedPage() == page) {
        scriptable.setParentScope(enclosingWindow.getScriptableObject());
    } else {
        scriptable.setParentScope(ScriptableObject.getTopLevelScope(page.getScriptableObject()));
    }
}
Also used : SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) WebWindow(com.gargoylesoftware.htmlunit.WebWindow)

Example 38 with SgmlPage

use of com.gargoylesoftware.htmlunit.SgmlPage 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)

Aggregations

SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)38 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)10 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)7 DomDocumentFragment (com.gargoylesoftware.htmlunit.html.DomDocumentFragment)5 DomText (com.gargoylesoftware.htmlunit.html.DomText)5 Page (com.gargoylesoftware.htmlunit.Page)4 ScriptResult (com.gargoylesoftware.htmlunit.ScriptResult)4 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)3 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)3 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)3 JsxSetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)3 Node (com.gargoylesoftware.htmlunit.javascript.host.dom.Node)3 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)3 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)2 HtmlOption (com.gargoylesoftware.htmlunit.html.HtmlOption)2 JsxConstructor (com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)2 DocumentFragment (com.gargoylesoftware.htmlunit.javascript.host.dom.DocumentFragment)2 MouseEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent)2 PointerEvent (com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent)2