Morph the APIs for Node, ParentNode, and Element closer to the specs

These still don't match the specs exactly, but they're much closer.

R=ojan@chromium.org, eseidel@chromium.org

Review URL: https://codereview.chromium.org/924203002
This commit is contained in:
Adam Barth
2015-02-13 21:36:53 -08:00
parent 3912ca81ab
commit 051352eec3
9 changed files with 31 additions and 54 deletions

View File

@@ -22,7 +22,7 @@ void main() {
var oldChild = doc.appendChild(doc.createElement("div"));
expect(childElementCount(doc), equals(1));
var newChild = doc.createElement("div");
doc.replaceChild(newChild, oldChild);
oldChild.replaceWith([newChild]);
expect(childElementCount(doc), equals(1));
expect(newChild.parentNode, equals(doc));
expect(oldChild.parentNode, isNull);
@@ -33,7 +33,7 @@ void main() {
expect(childElementCount(doc), equals(0));
expect(childNodeCount(doc), equals(1));
var newChild = doc.createElement("div");
doc.replaceChild(newChild, oldChild);
oldChild.replaceWith([newChild]);
expect(childElementCount(doc), equals(1));
expect(childNodeCount(doc), equals(1));
expect(newChild.parentNode, equals(doc));
@@ -44,7 +44,7 @@ void main() {
var oldChild = doc.appendChild(doc.createElement("div"));
expect(childElementCount(doc), equals(1));
var newChild = new Text(" text ");
doc.replaceChild(newChild, oldChild);
oldChild.replaceWith([newChild]);
expect(childElementCount(doc), equals(0));
expect(childNodeCount(doc), equals(1));
expect(newChild.parentNode, equals(doc));
@@ -68,7 +68,7 @@ void main() {
fragment.appendChild(new Text(" text "));
var newChild = fragment.appendChild(doc.createElement("div"));
fragment.appendChild(new Text(" "));
doc.replaceChild(fragment, oldChild);
oldChild.replaceWith([fragment]);
expect(childElementCount(doc), equals(1));
expect(childNodeCount(doc), equals(3));
expect(newChild.parentNode, equals(doc));

View File

@@ -11,14 +11,13 @@ void main() {
test("should return null for elements not a child of a scope", () {
var doc = new Document();
var element = doc.createElement("div");
expect(element.ownerScope, isNull);
expect(element.owner, isNull);
});
test("should return the document for elements in the document scope", () {
var doc = new Document();
var element = doc.createElement("div");
doc.appendChild(element);
expect(element.ownerScope, equals(element.ownerDocument));
expect(element.ownerScope, equals(doc));
expect(element.owner, equals(doc));
});
test("should return the shadow root for elements in the shadow root scope", () {
var doc = new Document();
@@ -26,26 +25,26 @@ void main() {
var child = doc.createElement("div");
var shadowRoot = host.ensureShadowRoot();
shadowRoot.appendChild(child);
expect(child.ownerScope, equals(shadowRoot));
expect(child.owner, equals(shadowRoot));
});
test("should return self for a shadow root or document", () {
var doc = new Document();
var host = doc.createElement("div");
doc.appendChild(host);
var shadowRoot = host.ensureShadowRoot();
expect(shadowRoot.ownerScope, equals(shadowRoot));
expect(doc.ownerScope, equals(doc));
expect(shadowRoot.owner, equals(shadowRoot));
expect(doc.owner, equals(doc));
});
test("should dynamically update", () {
var doc = new Document();
var host = doc.createElement("div");
var child = doc.createElement("div");
var shadowRoot = host.ensureShadowRoot();
expect(child.ownerScope, isNull);
expect(child.owner, isNull);
shadowRoot.appendChild(child);
expect(child.ownerScope, equals(shadowRoot));
expect(child.owner, equals(shadowRoot));
child.remove();
expect(child.ownerScope, isNull);
expect(child.owner, isNull);
});
}
</script>

View File

@@ -1,9 +1,8 @@
CONSOLE: unittest-suite-wait-for-done
CONSOLE: PASS: should throw with invalid arguments
CONSOLE: PASS: should replace elements
CONSOLE: PASS: should replace text
CONSOLE: PASS: should replace children with a fragment
CONSOLE:
CONSOLE: All 4 tests passed.
CONSOLE: All 3 tests passed.
CONSOLE: unittest-suite-success
DONE

View File

@@ -12,30 +12,11 @@ void main() {
var childElementCount = DomUtils.childElementCount;
var childNodeCount = DomUtils.childNodeCount;
test("should throw with invalid arguments", () {
var parent = document.createElement("div");
expect(() {
parent.replaceChild();
}, throws);
// expect(() {
// parent.replaceChild(null, null);
// }, throws);
expect(() {
parent.replaceChild({tagName: "div"});
}, throws);
// expect(() {
// parent.replaceChild(null, document.createElement("div"));
// }, throws);
expect(() {
parent.replaceChild(document.createElement("div"), {tagName: "div"});
}, throws);
});
test("should replace elements", () {
var parent = document.createElement("div");
var oldChild = parent.appendChild(document.createElement("div"));
var newChild = document.createElement("div");
parent.replaceChild(newChild, oldChild);
oldChild.replaceWith([newChild]);
expect(oldChild.parentNode, isNull);
expect(newChild.parentNode, equals(parent));
});
@@ -44,7 +25,7 @@ void main() {
var parent = document.createElement("div");
var oldChild = parent.appendChild(new Text(" it's a text "));
var newChild = document.createElement("div");
parent.replaceChild(newChild, oldChild);
oldChild.replaceWith([newChild]);
expect(oldChild.parentNode, isNull);
expect(newChild.parentNode, equals(parent));
});
@@ -58,7 +39,7 @@ void main() {
var parent = document.createElement("div");
var oldChild = parent.appendChild(document.createElement("div"));
var lastChild = parent.appendChild(document.createElement("div"));
parent.replaceChild(fragment, oldChild);
oldChild.replaceWith([fragment]);
expect(child1.parentNode, equals(parent));
expect(child2.parentNode, equals(parent));
expect(child3.parentNode, equals(parent));

View File

@@ -20,9 +20,7 @@ void main() {
d.appendChild(new Text('bbb'));
var newHeight = elem.offsetHeight;
while (elem.firstChild != null) {
elem.removeChild(elem.firstChild);
}
elem.removeChildren();
new Timer(Duration.ZERO, expectAsync(() {
expect(elem.offsetHeight, equals(originalHeight));

View File

@@ -26,7 +26,7 @@ void main() {
test("should grow height to 200px", () {
var target = document.querySelectorAll('div').item(1);
target.id = 'high';
target.setAttribute("id", "high");
expect(window.getComputedStyle(target).getPropertyValue("height"),
equals("200px"));
});

View File

@@ -36,13 +36,13 @@ main() {
test("should find elements by id", () {
expect(query("#id5"), isNotNull);
expect(query("#id5").id, equals("id5"));
expect(query("#id5").getAttribute("id"), equals("id5"));
expect(query("#id5").classList.toString(), equals("class5"));
// FIXME(sky): Do we still want to allow multiple id stuff like this?
expect(queryAll("#id5").length, equals(2));
expect(queryAll("#id5").item(0), equals(query("#id5")));
expect(queryAll("#id5").item(1), isNot(equals(query("#id5"))));
expect(queryAll("#id5").item(1).id, equals("id5"));
expect(queryAll("#id5").item(1).getAttribute("id"), equals("id5"));
});
test("should find elements by tag name", () {
@@ -55,7 +55,7 @@ main() {
test("should find an element by compound selector", () {
expect(query("tag-name-6.class6#id6"), isNotNull);
expect(query("tag-name-6.class6#id6").id, equals("id6"));
expect(query("tag-name-6.class6#id6").getAttribute("id"), equals("id6"));
expect(query("tag-name-6.class6#id6").classList.toString(), equals("class6"));
expect(query("tag-name-6.class6#id6").tagName, equals("tag-name-6"));
});
@@ -63,21 +63,21 @@ main() {
test("should find all elements by compound selector", () {
expect(queryAll("tag-name-3.class7"), isNotNull);
expect(queryAll("tag-name-3.class7").length, equals(3));
expect(queryAll("tag-name-3.class7").item(0).id, equals("tag1"));
expect(queryAll("tag-name-3.class7").item(1).id, equals("tag2"));
expect(queryAll("tag-name-3.class7").item(0).getAttribute("id"), equals("tag1"));
expect(queryAll("tag-name-3.class7").item(1).getAttribute("id"), equals("tag2"));
});
test("should find all elements by attribute presence selector", () {
expect(queryAll("[testAttr]"), isNotNull);
expect(queryAll("[testAttr]").length, equals(2));
expect(queryAll("[testAttr]").item(0).id, equals("id5"));
expect(queryAll("[testAttr]").item(1).id, equals("id6"));
expect(queryAll("[testAttr]").item(0).getAttribute("id"), equals("id5"));
expect(queryAll("[testAttr]").item(1).getAttribute("id"), equals("id6"));
});
test("should find all elements by attribute value selector", () {
expect(queryAll("[testAttr='the value']"), isNotNull);
expect(queryAll("[testAttr='the value']").length, equals(1));
expect(queryAll("[testAttr='the value']").item(0).id, equals("id6"));
expect(queryAll("[testAttr='the value']").item(0).getAttribute("id"), equals("id6"));
});
}
</script>

View File

@@ -1,6 +1,6 @@
PASS: <div class= id=id1> order was 1
PASS: <div class=class2 id=> order was 2
PASS: <tag-name-3 class= id=> order was 3
PASS: <div class=class4 class4 id=> order was 4
PASS: <div class=class2 id=null> order was 2
PASS: <tag-name-3 class= id=null> order was 3
PASS: <div class=class4 class4 id=null> order was 4
PASS: <div class=class5 id=id5> order was 5
PASS: <tag-name-6 class=class6 id=id6> order was 6

View File

@@ -26,7 +26,7 @@
var tests = document.getElementById("tests");
var log = document.getElementById("log");
var i = 1;
for (var element = tests.firstElementChild; element != null; element = element.nextElementSibling) {
for (Element element = tests.firstElementChild; element != null; element = element.nextElementSibling) {
var order = int.parse(window.getComputedStyle(element).getPropertyValue("order"));
var div = document.createElement("div");
var text = (order == i) ? "PASS" : "FAIL";
@@ -34,7 +34,7 @@
+ element.tagName
+ " class="
+ element.classList.toString()
+ " id=" + element.id
+ " id=" + element.getAttribute("id").toString()
+ "> order was "
+ order.toString();
if (order != i)