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