Skip to content

Commit

Permalink
move some methods up
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Jan 27, 2020
1 parent bc1f58d commit 2d6ec1c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_DOCUMENT_SETTING_DOMAIN_THROWS_FOR_ABOUT_BLANK;
import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_TREEWALKER_EXPAND_ENTITY_REFERENCES_FALSE;
import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_TREEWALKER_FILTER_FUNCTION_ONLY;
import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_XML_GET_ELEMENT_BY_ID__ANY_ELEMENT;
import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.QUERYSELECTORALL_NOT_IN_QUIRKS;
import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
Expand Down Expand Up @@ -59,8 +60,10 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xml.utils.PrefixResolver;
import org.w3c.dom.CDATASection;
import org.w3c.dom.DOMException;
import org.w3c.dom.DocumentType;
import org.w3c.dom.ProcessingInstruction;

import com.gargoylesoftware.css.parser.CSSException;
import com.gargoylesoftware.htmlunit.BrowserVersion;
Expand Down Expand Up @@ -134,6 +137,7 @@
import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument;
import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement;
import com.gargoylesoftware.htmlunit.util.EncodingSniffer;
import com.gargoylesoftware.htmlunit.xml.XmlPage;

import net.sourceforge.htmlunit.corejs.javascript.Callable;
import net.sourceforge.htmlunit.corejs.javascript.Context;
Expand Down Expand Up @@ -4157,4 +4161,48 @@ protected boolean isReadOnlySettable(final String name, final Object value) {
}
return super.isReadOnlySettable(name, value);
}

/**
* Returns the element with the specified ID, as long as it is an HTML element; {@code null} otherwise.
* @param id the ID to search for
* @return the element with the specified ID, as long as it is an HTML element; {@code null} otherwise
*/
@JsxFunction
public Object getElementById(final String id) {
final DomNode domNode = getDomNodeOrDie();
final Object domElement = domNode.getFirstByXPath("//*[@id = \"" + id + "\"]");
if (domElement != null) {
if (!(domNode instanceof XmlPage) || domElement instanceof HtmlElement
|| getBrowserVersion().hasFeature(JS_XML_GET_ELEMENT_BY_ID__ANY_ELEMENT)) {
return ((DomElement) domElement).getScriptableObject();
}
if (LOG.isDebugEnabled()) {
LOG.debug("getElementById(" + id + "): no HTML DOM node found with this ID");
}
}
return null;
}

/**
* Creates a new ProcessingInstruction.
* @param target the target
* @param data the data
* @return the new ProcessingInstruction
*/
@JsxFunction
public Object createProcessingInstruction(final String target, final String data) {
final ProcessingInstruction node = getPage().createProcessingInstruction(target, data);
return getScriptableFor(node);
}

/**
* Creates a new createCDATASection.
* @param data the data
* @return the new CDATASection
*/
@JsxFunction
public Object createCDATASection(final String data) {
final CDATASection node = getPage().createCDATASection(data);
return getScriptableFor(node);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_DOMPARSER_EXCEPTION_ON_ERROR;
import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_DOMPARSER_PARSERERROR_ON_ERROR;
import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_XML_GET_ELEMENTS_BY_TAG_NAME_LOCAL;
import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_XML_GET_ELEMENT_BY_ID__ANY_ELEMENT;
import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;

Expand All @@ -27,9 +26,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.CDATASection;
import org.w3c.dom.ProcessingInstruction;

import com.gargoylesoftware.htmlunit.StringWebResponse;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
Expand Down Expand Up @@ -275,48 +271,4 @@ protected boolean isMatching(final DomNode node) {

return collection;
}

/**
* Returns the element with the specified ID, as long as it is an HTML element; {@code null} otherwise.
* @param id the ID to search for
* @return the element with the specified ID, as long as it is an HTML element; {@code null} otherwise
*/
@JsxFunction
public Object getElementById(final String id) {
final DomNode domNode = getDomNodeOrDie();
final Object domElement = domNode.getFirstByXPath("//*[@id = \"" + id + "\"]");
if (domElement != null) {
if (!(domNode instanceof XmlPage) || domElement instanceof HtmlElement
|| getBrowserVersion().hasFeature(JS_XML_GET_ELEMENT_BY_ID__ANY_ELEMENT)) {
return ((DomElement) domElement).getScriptableObject();
}
if (LOG.isDebugEnabled()) {
LOG.debug("getElementById(" + id + "): no HTML DOM node found with this ID");
}
}
return null;
}

/**
* Creates a new ProcessingInstruction.
* @param target the target
* @param data the data
* @return the new ProcessingInstruction
*/
@JsxFunction
public Object createProcessingInstruction(final String target, final String data) {
final ProcessingInstruction node = getPage().createProcessingInstruction(target, data);
return getScriptableFor(node);
}

/**
* Creates a new createCDATASection.
* @param data the data
* @return the new CDATASection
*/
@JsxFunction
public Object createCDATASection(final String data) {
final CDATASection node = getPage().createCDATASection(data);
return getScriptableFor(node);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7534,15 +7534,10 @@ public void htmlDocument() throws Exception {
FF60 = "async,constructor(),load()",
FF68 = "async,constructor(),load()",
IE = "constructor")
@HtmlUnitNYI(CHROME = "constructor(),createCDATASection(),createProcessingInstruction(),"
+ "getElementById(),getElementsByTagName()",
FF60 = "async,constructor(),createCDATASection(),createProcessingInstruction(),"
+ "getElementById(),getElementsByTagName(),load()",
FF68 = "async,constructor(),createCDATASection(),createProcessingInstruction(),"
+ "getElementById(),getElementsByTagName(),load()",
IE = "constructor,createCDATASection(),createProcessingInstruction(),"
+ "getElementById(),getElementsByTagName()")
//IE expectations are bigger than real IE alert length, test should be changed to store value in textarea
@HtmlUnitNYI(CHROME = "constructor(),getElementsByTagName()",
FF60 = "async,constructor(),getElementsByTagName(),load()",
FF68 = "async,constructor(),getElementsByTagName(),load()",
IE = "constructor,getElementsByTagName()")
public void document() throws Exception {
testString("xmlDocument");
}
Expand Down

0 comments on commit 2d6ec1c

Please sign in to comment.