Skip to content

Commit

Permalink
Document.contains(node) fixed
Browse files Browse the repository at this point in the history
Node.contains(node) returns false for null and undefined parameter values
  • Loading branch information
rbri committed May 14, 2021
1 parent 8d72472 commit 305a2b9
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

<body>
<release version="2.50.0" date="Mai xx, 2021" description="Bugfixes, Firefox 88, Chrome/Edge 89">
<action type="fix" dev="rbri">
Document.contains(node) fixed.
</action>
<action type="fix" dev="rbri">
Node.contains(node) returns false for null and undefined parameter values.
</action>
<action type="fix" dev="rbri" issue="284">
Error.stack lines are separated by LF only.
</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4296,4 +4296,13 @@ public Object createCDATASection(final String data) {
public void clear() {
// nothing
}

/**
* {@inheritDoc}
*/
@JsxFunction({CHROME, EDGE, FF, FF78})
@Override
public boolean contains(final Object element) {
return getDocumentElement().contains(element);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,10 @@ public Object getAttributes() {
*/
@JsxFunction({CHROME, EDGE, FF, FF78})
public boolean contains(final Object element) {
if (element == null || Undefined.isUndefined(element)) {
return false;
}

if (!(element instanceof Node)) {
if (getBrowserVersion().hasFeature(JS_NODE_CONTAINS_RETURNS_FALSE_FOR_INVALID_ARG)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2686,7 +2686,39 @@ public void captureEvents() throws Exception {
* @throws Exception if the test fails
*/
@Test
@Alerts("[object Comment]")
@Alerts(DEFAULT = {"true", "false", "true", "true", "true", "false", "false"},
IE = {"-", "-", "-", "-", "-", "-", "-"})
public void contains() throws Exception {
final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html><head><script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " var testnode = document.getElementById('myNode');\n"
+ " log(document.contains ? document.contains(testnode) : '-');\n"

+ " var newnode = document.createComment('some comment');\n"
+ " log(document.contains ? document.contains(newnode) : '-');\n"

+ " log(document.contains ? document.contains(document.documentElement) : '-');\n"
+ " log(document.contains ? document.contains(document.body) : '-');\n"
+ " log(document.contains ? document.contains(document.firstElementChild) : '-');\n"

+ " log(document.contains ? document.contains(null) : '-');\n"
+ " log(document.contains ? document.contains(undefined) : '-');\n"
+ " }\n"
+ "</script></head>\n"
+ "<body onload='test()'>\n"
+ " <div id='myNode'></div>\n"
+ "</body></html>";
loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"[object Comment]", "false"},
IE = {"[object Comment]", "-"})
public void createComment() throws Exception {
final String html = "<html>\n"
+ "<head>\n"
Expand All @@ -2695,6 +2727,7 @@ public void createComment() throws Exception {
+ "function test() {\n"
+ " var elt = document.createComment('some comment');\n"
+ " log(elt);\n"
+ " log(document.contains ? document.contains(elt) : '-');\n"
+ "}\n"
+ "</script>\n"
+ "</head>\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3267,14 +3267,24 @@ public void contains() throws Exception {
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = "exception",
IE = "false")
@Alerts(DEFAULT = {"exception[]", "false", "false"},
IE = {"false", "false", "false"})
public void contains_invalid_argument() throws Exception {
final String html = "<html><body><script>\n"
+ LOG_TITLE_FUNCTION

+ "try {\n"
+ " log(document.body.contains([]));\n"
+ "} catch(e) { log('exception'); }\n"
+ "} catch(e) { log('exception[]'); }\n"

+ "try {\n"
+ " log(document.body.contains(null));\n"
+ "} catch(e) { log('exception null'); }\n"

+ "try {\n"
+ " log(document.body.contains(undefined));\n"
+ "} catch(e) { log('exception undefined'); }\n"

+ "</script></body></html>";

loadPageVerifyTitle2(html);
Expand Down Expand Up @@ -4203,6 +4213,33 @@ public void replaceChildDeclareJavaScript() throws Exception {
loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"false", "<!--some comment-->", "true", "false"},
IE = {"-", "<!--some comment-->", "-", "-"})
public void replaceChildAddNewChildToDocument() throws Exception {
final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html><head><script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " var newnode = document.createComment('some comment');\n"
+ " log(document.contains ? document.contains(newnode) : '-');\n"

+ " var outernode = document.getElementById('myNode');\n"
+ " var oldnode = document.getElementById('inner');\n"
+ " outernode.replaceChild(newnode, oldnode);\n"
+ " log(outernode.innerHTML);\n"
+ " log(document.contains ? document.contains(newnode) : '-');\n"
+ " log(document.contains ? document.contains(oldnode) : '-');\n"
+ " }\n"
+ "</script></head><body onload='test()'>\n"
+ " <div id='myNode'><div id='inner'></div></div>\n"
+ "</body></html>";
loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
Expand Down

0 comments on commit 305a2b9

Please sign in to comment.