use of com.gargoylesoftware.htmlunit.html.HtmlBody in project htmlunit by HtmlUnit.
the class HTMLElement method getOffsetParentInternal.
private Object getOffsetParentInternal(final boolean returnNullIfFixed) {
DomNode currentElement = getDomNodeOrDie();
if (currentElement.getParentNode() == null) {
return null;
}
final HTMLElement htmlElement = currentElement.getScriptableObject();
if (returnNullIfFixed && "fixed".equals(htmlElement.getStyle().getStyleAttribute(StyleAttributes.Definition.POSITION, true))) {
return null;
}
final ComputedCSSStyleDeclaration style = htmlElement.getWindow().getComputedStyle(htmlElement, null);
final String position = style.getPositionWithInheritance();
final boolean staticPos = "static".equals(position);
while (currentElement != null) {
final DomNode parentNode = currentElement.getParentNode();
if (parentNode instanceof HtmlBody || (staticPos && parentNode instanceof HtmlTableDataCell) || (staticPos && parentNode instanceof HtmlTable)) {
return parentNode.getScriptableObject();
}
if (parentNode != null && parentNode.getScriptableObject() instanceof HTMLElement) {
final HTMLElement parentElement = parentNode.getScriptableObject();
final ComputedCSSStyleDeclaration parentStyle = parentElement.getWindow().getComputedStyle(parentElement, null);
final String parentPosition = parentStyle.getPositionWithInheritance();
if (!"static".equals(parentPosition)) {
return parentNode.getScriptableObject();
}
}
currentElement = currentElement.getParentNode();
}
return null;
}
use of com.gargoylesoftware.htmlunit.html.HtmlBody in project htmlunit by HtmlUnit.
the class HtmlUnitXPathTest method xpathFromElement.
/**
* Test evaluation relative from elements other than the whole page.
* @throws Exception if test fails
*/
@Test
public void xpathFromElement() throws Exception {
final String content = "<html><head><title>Test page</title></head>\n" + "<body><a href='foo.html' id='myLink'>foo</a></body>\n" + "</html>";
final HtmlPage page = loadPage(content);
final HtmlBody body = page.getFirstByXPath("/html/body");
assertEquals(page.getHtmlElementById("myLink"), body.getFirstByXPath("./a"));
}
use of com.gargoylesoftware.htmlunit.html.HtmlBody in project htmlunit by HtmlUnit.
the class HtmlSerializerNormalizedText method appendDomNode.
/**
* Process {@link HtmlHiddenInput}.
*
* @param builder the StringBuilder to add to
* @param domNode the target to process
*/
protected void appendDomNode(final HtmlSerializerTextBuilder builder, final DomNode domNode) {
boolean block = false;
if (!(domNode instanceof HtmlBody)) {
final SgmlPage page = domNode.getPage();
final WebWindow window = page.getEnclosingWindow();
if (page != null && // TODO
page.getWebClient().isJavaScriptEngineEnabled() && window.getWebClient().getOptions().isCssEnabled()) {
if (domNode instanceof DomElement) {
final String display = window.getComputedStyle((DomElement) domNode, null).getDisplay();
block = "block".equals(display);
}
} else if (domNode instanceof HtmlElement) {
block = DisplayStyle.BLOCK == ((HtmlElement) domNode).getDefaultStyleDisplay();
}
}
if (block) {
builder.appendBlockSeparator();
}
appendChildren(builder, domNode);
if (block) {
builder.appendBlockSeparator();
}
}
use of com.gargoylesoftware.htmlunit.html.HtmlBody in project htmlunit by HtmlUnit.
the class HtmlUnitNekoDOMBuilder method startElement.
/**
* {@inheritDoc}
*/
@Override
public void startElement(String namespaceURI, final String localName, final String qName, final Attributes atts) throws SAXException {
if (snippetStartNodeOverwritten_) {
snippetStartNodeOverwritten_ = false;
return;
}
handleCharacters();
final String tagLower = localName.toLowerCase(Locale.ROOT);
if (page_.isParsingHtmlSnippet() && ("html".equals(tagLower) || "body".equals(tagLower))) {
return;
}
if ("head".equals(tagLower)) {
if (headParsed_ == HeadParsed.YES || page_.isParsingHtmlSnippet()) {
return;
}
headParsed_ = lastTagWasSynthesized_ ? HeadParsed.SYNTHESIZED : HeadParsed.YES;
}
if (namespaceURI != null) {
namespaceURI = namespaceURI.trim();
} else // add a head if none was there
if (headParsed_ == HeadParsed.NO && ("body".equals(tagLower) || "frameset".equals(tagLower))) {
final ElementFactory factory = htmlParser_.getElementFactory(page_, null, "head", insideSvg_, false);
final DomElement newElement = factory.createElement(page_, "head", null);
appendChild(currentNode_, newElement);
headParsed_ = HeadParsed.SYNTHESIZED;
}
// If we're adding a body element, keep track of any temporary synthetic ones
// that we may have had to create earlier (for document.write(), for example).
HtmlBody oldBody = null;
if ("body".equals(qName) && page_.getBody() instanceof HtmlBody) {
oldBody = (HtmlBody) page_.getBody();
}
// end tag.
if ("form".equals(tagLower)) {
formWaitingForLostChildren_ = null;
}
// Add the new node.
if (!(page_ instanceof XHtmlPage) && Html.XHTML_NAMESPACE.equals(namespaceURI)) {
namespaceURI = null;
}
final ElementFactory factory = htmlParser_.getElementFactory(page_, namespaceURI, qName, insideSvg_, false);
if (factory == HtmlUnitNekoHtmlParser.SVG_FACTORY) {
namespaceURI = Html.SVG_NAMESPACE;
}
final DomElement newElement = factory.createElementNS(page_, namespaceURI, qName, atts, true);
newElement.setStartLocation(locator_.getLineNumber(), locator_.getColumnNumber());
// parse can't replace everything as it does not buffer elements while parsing
addNodeToRightParent(currentNode_, newElement);
if ("svg".equals(tagLower)) {
insideSvg_ = true;
}
// remove the old body and move its children to the real body element we just added.
if (oldBody != null) {
oldBody.quietlyRemoveAndMoveChildrenTo(newElement);
}
if ("body".equals(tagLower)) {
body_ = (HtmlElement) newElement;
} else if ("meta".equals(tagLower) && page_.hasFeature(META_X_UA_COMPATIBLE)) {
final HtmlMeta meta = (HtmlMeta) newElement;
if ("X-UA-Compatible".equals(meta.getHttpEquivAttribute())) {
final String content = meta.getContentAttribute();
if (content.startsWith("IE=")) {
final String mode = content.substring(3).trim();
final int version = page_.getWebClient().getBrowserVersion().getBrowserVersionNumeric();
try {
int value = Integer.parseInt(mode);
if (value > version) {
value = version;
}
((HTMLDocument) page_.getScriptableObject()).forceDocumentMode(value);
} catch (final Exception e) {
// ignore
}
}
}
} else if (createdByJavascript_ && "script".equals(tagLower)) {
final ScriptElement script = (ScriptElement) newElement;
script.markAsCreatedByDomParser();
}
currentNode_ = newElement;
stack_.push(currentNode_);
}
Aggregations