Clean up child checks in ContainerNode.
We can simplify the checks given that there's fewer node types now. This does make the error messages from Range a little worse, but it's weird that Range is doing its own error checking anyway. I also took this as an opportunity to add a bunch of DOM tests. R=ojan@chromium.org Review URL: https://codereview.chromium.org/732203004
This commit is contained in:
9
engine/src/flutter/tests/dom/appendChild-expected.txt
Normal file
9
engine/src/flutter/tests/dom/appendChild-expected.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
Running 5 tests
|
||||
ok 1 appendChild should throw with invalid arguments
|
||||
ok 2 appendChild should insert children
|
||||
ok 3 appendChild should insert children with a fragment
|
||||
ok 4 appendChild should throw when inserting a tree scope
|
||||
ok 5 appendChild should throw when appending to a text
|
||||
5 tests
|
||||
5 pass
|
||||
0 fail
|
||||
76
engine/src/flutter/tests/dom/appendChild.sky
Normal file
76
engine/src/flutter/tests/dom/appendChild.sky
Normal file
@@ -0,0 +1,76 @@
|
||||
<sky>
|
||||
<import src="../resources/chai.sky" />
|
||||
<import src="../resources/mocha.sky" />
|
||||
<import src="../resources/dom-utils.sky" as="DomUtils" />
|
||||
<script>
|
||||
describe("appendChild", function() {
|
||||
var childElementCount = DomUtils.childElementCount;
|
||||
var childNodeCount = DomUtils.childNodeCount;
|
||||
|
||||
it("should throw with invalid arguments", function() {
|
||||
var parent = document.createElement("div");
|
||||
assert.throws(function() {
|
||||
parent.appendChild();
|
||||
});
|
||||
assert.throws(function() {
|
||||
parent.appendChild(null);
|
||||
});
|
||||
assert.throws(function() {
|
||||
parent.appendChild({tagName: "div"});
|
||||
});
|
||||
});
|
||||
|
||||
it("should insert children", function() {
|
||||
var parent = document.createElement("div");
|
||||
var child1 = parent.appendChild(document.createElement("div"));
|
||||
var child2 = parent.appendChild(new Text(" text "));
|
||||
var child3 = parent.appendChild(new Text(" "));
|
||||
var child4 = parent.appendChild(document.createElement("div"));
|
||||
assert.equal(child1.parentNode, parent);
|
||||
assert.equal(child2.parentNode, parent);
|
||||
assert.equal(child3.parentNode, parent);
|
||||
assert.equal(child4.parentNode, parent);
|
||||
assert.equal(childNodeCount(parent), 4);
|
||||
assert.equal(childElementCount(parent), 2);
|
||||
});
|
||||
|
||||
it("should insert children with a fragment", function() {
|
||||
var fragment = document.createDocumentFragment();
|
||||
var child1 = fragment.appendChild(document.createElement("div"));
|
||||
var child2 = fragment.appendChild(new Text(" text "));
|
||||
var child3 = fragment.appendChild(new Text(" "));
|
||||
var child4 = fragment.appendChild(document.createElement("div"));
|
||||
var parent = document.createElement("div");
|
||||
parent.appendChild(fragment);
|
||||
assert.equal(child1.parentNode, parent);
|
||||
assert.equal(child2.parentNode, parent);
|
||||
assert.equal(child3.parentNode, parent);
|
||||
assert.equal(child4.parentNode, parent);
|
||||
assert.equal(childNodeCount(parent), 4);
|
||||
assert.equal(childElementCount(parent), 2);
|
||||
});
|
||||
|
||||
it("should throw when inserting a tree scope", function() {
|
||||
var parent = document.createElement("div");
|
||||
var doc = new Document();
|
||||
var shadowRoot = document.createElement("span").createShadowRoot();
|
||||
assert.throws(function() {
|
||||
parent.appendChild(doc);
|
||||
});
|
||||
assert.throws(function() {
|
||||
parent.appendChild(shadowRoot);
|
||||
});
|
||||
assert.throws(function() {
|
||||
doc.appendChild(fragment);
|
||||
});
|
||||
});
|
||||
|
||||
it("should throw when appending to a text", function() {
|
||||
var parent = new Text();
|
||||
assert.throws(function() {
|
||||
parent.appendChild(document.createElement("div"));
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</sky>
|
||||
@@ -0,0 +1,11 @@
|
||||
Running 7 tests
|
||||
ok 1 Document should allow replacing the document element
|
||||
ok 2 Document should allow replacing a text child with an element
|
||||
ok 3 Document should allow replacing the document element with text
|
||||
ok 4 Document should allow inserting text with a fragment
|
||||
ok 5 Document should allow replacing the document element with a fragment
|
||||
ok 6 Document should throw when inserting multiple elements
|
||||
ok 7 Document should throw when inserting multiple elements with a fragment
|
||||
7 tests
|
||||
7 pass
|
||||
0 fail
|
||||
103
engine/src/flutter/tests/dom/document-child-mutations.sky
Normal file
103
engine/src/flutter/tests/dom/document-child-mutations.sky
Normal file
@@ -0,0 +1,103 @@
|
||||
<sky>
|
||||
<import src="../resources/chai.sky" />
|
||||
<import src="../resources/mocha.sky" />
|
||||
<import src="../resources/dom-utils.sky" as="DomUtils" />
|
||||
<script>
|
||||
describe("Document", function() {
|
||||
var doc;
|
||||
|
||||
var childElementCount = DomUtils.childElementCount;
|
||||
var childNodeCount = DomUtils.childNodeCount;
|
||||
|
||||
beforeEach(function() {
|
||||
doc = new Document();
|
||||
});
|
||||
|
||||
it("should allow replacing the document element", function() {
|
||||
var oldChild = doc.appendChild(doc.createElement("div"));
|
||||
assert.equal(childElementCount(doc), 1);
|
||||
var newChild = doc.createElement("div");
|
||||
doc.replaceChild(newChild, oldChild);
|
||||
assert.equal(childElementCount(doc), 1);
|
||||
assert.equal(newChild.parentNode, doc);
|
||||
assert.isNull(oldChild.parentNode);
|
||||
});
|
||||
|
||||
it("should allow replacing a text child with an element", function() {
|
||||
var oldChild = doc.appendChild(new Text("text here"));
|
||||
assert.equal(childElementCount(doc), 0);
|
||||
assert.equal(childNodeCount(doc), 1);
|
||||
var newChild = doc.createElement("div");
|
||||
doc.replaceChild(newChild, oldChild);
|
||||
assert.equal(childElementCount(doc), 1);
|
||||
assert.equal(childNodeCount(doc), 1);
|
||||
assert.equal(newChild.parentNode, doc);
|
||||
assert.isNull(oldChild.parentNode);
|
||||
});
|
||||
|
||||
it("should allow replacing the document element with text", function() {
|
||||
var oldChild = doc.appendChild(doc.createElement("div"));
|
||||
assert.equal(childElementCount(doc), 1);
|
||||
var newChild = new Text(" text ");
|
||||
doc.replaceChild(newChild, oldChild);
|
||||
assert.equal(childElementCount(doc), 0);
|
||||
assert.equal(childNodeCount(doc), 1);
|
||||
assert.equal(newChild.parentNode, doc);
|
||||
assert.isNull(oldChild.parentNode);
|
||||
});
|
||||
|
||||
it("should allow inserting text with a fragment", function() {
|
||||
var fragment = doc.createDocumentFragment();
|
||||
fragment.appendChild(new Text(" text "));
|
||||
fragment.appendChild(new Text(" text "));
|
||||
assert.equal(childNodeCount(doc), 0);
|
||||
doc.appendChild(fragment);
|
||||
assert.equal(childElementCount(doc), 0);
|
||||
assert.equal(childNodeCount(doc), 2);
|
||||
});
|
||||
|
||||
it("should allow replacing the document element with a fragment", function() {
|
||||
var oldChild = doc.appendChild(doc.createElement("div"));
|
||||
assert.equal(childElementCount(doc), 1);
|
||||
var fragment = doc.createDocumentFragment();
|
||||
fragment.appendChild(new Text(" text "));
|
||||
var newChild = fragment.appendChild(doc.createElement("div"));
|
||||
fragment.appendChild(new Text(" "));
|
||||
doc.replaceChild(fragment, oldChild);
|
||||
assert.equal(childElementCount(doc), 1);
|
||||
assert.equal(childNodeCount(doc), 3);
|
||||
assert.equal(newChild.parentNode, doc);
|
||||
assert.isNull(oldChild.parentNode);
|
||||
});
|
||||
|
||||
it("should throw when inserting multiple elements", function() {
|
||||
doc.appendChild(doc.createElement("div"));
|
||||
var oldChild = doc.appendChild(new Text(" text "));
|
||||
assert.equal(childElementCount(doc), 1);
|
||||
var newChild = doc.createElement("div");
|
||||
assert.throws(function() {
|
||||
doc.replaceChild(newChild, 0);
|
||||
});
|
||||
assert.throws(function() {
|
||||
doc.insertBefore(newChild, oldChild);
|
||||
});
|
||||
});
|
||||
|
||||
it("should throw when inserting multiple elements with a fragment", function() {
|
||||
var oldChild = doc.appendChild(doc.createElement("div"));
|
||||
assert.equal(childElementCount(doc), 1);
|
||||
var fragment = doc.createDocumentFragment();
|
||||
fragment.appendChild(new Text(" text "));
|
||||
fragment.appendChild(doc.createElement("div"));
|
||||
fragment.appendChild(doc.createElement("div"));
|
||||
fragment.appendChild(new Text(" "));
|
||||
assert.throws(function() {
|
||||
doc.replaceChild(fragment, 0);
|
||||
});
|
||||
assert.throws(function() {
|
||||
doc.insertBefore(fragment, oldChild);
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</sky>
|
||||
10
engine/src/flutter/tests/dom/replaceChild-expected.txt
Normal file
10
engine/src/flutter/tests/dom/replaceChild-expected.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
Running 6 tests
|
||||
ok 1 replaceChild should throw with invalid arguments
|
||||
ok 2 replaceChild should replace elements
|
||||
ok 3 replaceChild should replace text
|
||||
ok 4 replaceChild should replace children with a fragment
|
||||
ok 5 replaceChild should throw when inserting a tree scope
|
||||
ok 6 replaceChild should throw when appending to a text
|
||||
6 tests
|
||||
6 pass
|
||||
0 fail
|
||||
90
engine/src/flutter/tests/dom/replaceChild.sky
Normal file
90
engine/src/flutter/tests/dom/replaceChild.sky
Normal file
@@ -0,0 +1,90 @@
|
||||
<sky>
|
||||
<import src="../resources/chai.sky" />
|
||||
<import src="../resources/mocha.sky" />
|
||||
<import src="../resources/dom-utils.sky" as="DomUtils" />
|
||||
<script>
|
||||
describe("replaceChild", function() {
|
||||
var childElementCount = DomUtils.childElementCount;
|
||||
var childNodeCount = DomUtils.childNodeCount;
|
||||
|
||||
it("should throw with invalid arguments", function() {
|
||||
var parent = document.createElement("div");
|
||||
assert.throws(function() {
|
||||
parent.replaceChild();
|
||||
});
|
||||
assert.throws(function() {
|
||||
parent.replaceChild(null, null);
|
||||
});
|
||||
assert.throws(function() {
|
||||
parent.replaceChild({tagName: "div"});
|
||||
});
|
||||
assert.throws(function() {
|
||||
parent.replaceChild(null, document.createElement("div"));
|
||||
});
|
||||
assert.throws(function() {
|
||||
parent.replaceChild(document.createElement("div"), {tagName: "div"});
|
||||
});
|
||||
});
|
||||
|
||||
it("should replace elements", function() {
|
||||
var parent = document.createElement("div");
|
||||
var oldChild = parent.appendChild(document.createElement("div"));
|
||||
var newChild = document.createElement("div");
|
||||
parent.replaceChild(newChild, oldChild);
|
||||
assert.isNull(oldChild.parentNode);
|
||||
assert.equal(newChild.parentNode, parent);
|
||||
});
|
||||
|
||||
it("should replace text", function() {
|
||||
var parent = document.createElement("div");
|
||||
var oldChild = parent.appendChild(new Text(" it's a text "));
|
||||
var newChild = document.createElement("div");
|
||||
parent.replaceChild(newChild, oldChild);
|
||||
assert.isNull(oldChild.parentNode);
|
||||
assert.equal(newChild.parentNode, parent);
|
||||
});
|
||||
|
||||
it("should replace children with a fragment", function() {
|
||||
var fragment = document.createDocumentFragment();
|
||||
var child1 = fragment.appendChild(document.createElement("div"));
|
||||
var child2 = fragment.appendChild(new Text(" text "));
|
||||
var child3 = fragment.appendChild(new Text(" "));
|
||||
var child4 = fragment.appendChild(document.createElement("div"));
|
||||
var parent = document.createElement("div");
|
||||
var oldChild = parent.appendChild(document.createElement("div"));
|
||||
var lastChild = parent.appendChild(document.createElement("div"));
|
||||
parent.replaceChild(fragment, oldChild);
|
||||
assert.equal(child1.parentNode, parent);
|
||||
assert.equal(child2.parentNode, parent);
|
||||
assert.equal(child3.parentNode, parent);
|
||||
assert.equal(child4.parentNode, parent);
|
||||
assert.isNull(oldChild.parentNode);
|
||||
assert.equal(childNodeCount(parent), 5);
|
||||
assert.equal(childElementCount(parent), 3);
|
||||
assert.equal(parent.lastChild, lastChild);
|
||||
});
|
||||
|
||||
it("should throw when inserting a tree scope", function() {
|
||||
var parent = document.createElement("div");
|
||||
var doc = new Document();
|
||||
var shadowRoot = document.createElement("span").createShadowRoot();
|
||||
assert.throws(function() {
|
||||
parent.replaceChild(doc);
|
||||
});
|
||||
assert.throws(function() {
|
||||
parent.replaceChild(shadowRoot);
|
||||
});
|
||||
assert.throws(function() {
|
||||
doc.replaceChild(fragment);
|
||||
});
|
||||
});
|
||||
|
||||
it("should throw when appending to a text", function() {
|
||||
var parent = new Text();
|
||||
assert.throws(function() {
|
||||
parent.replaceChild(document.createElement("div"), null);
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</sky>
|
||||
Reference in New Issue
Block a user