Use document fragments to write multiple nodes

For writer functions that need to generate multiple nodes, the
createDocumentFragment method can be used.
This commit is contained in:
tschaub
2012-10-25 11:56:48 -06:00
parent 7f2ce74dd7
commit e6399a265d
3 changed files with 57 additions and 30 deletions

View File

@@ -166,6 +166,35 @@
"node can be appended to a doc root");
}
function test_createDocumentFragment(t) {
t.plan(3);
var format = new OpenLayers.Format.XML();
var uri = "http://foo.com";
var prefix = "foo";
var localName = "bar";
var qualifiedName = prefix + ":" + localName;
var parent = format.createElementNS(uri, qualifiedName);
var fragment = format.createDocumentFragment();
t.eq(fragment.nodeType, 11, "fragment type");
try {
fragment.appendChild(format.createTextNode("one"));
fragment.appendChild(format.createTextNode("two"));
t.eq(fragment.childNodes.length, 2, "fragment has two child nodes");
} catch (err) {
t.fail("trouble appending text nodes to fragment: " + err.message);
}
try {
parent.appendChild(fragment);
t.eq(parent.childNodes.length, 2, "parent has two child nodes");
} catch (err) {
t.fail("trouble appending fragment to parent: " + err.message);
}
}
function test_Format_XML_createTextNode(t) {
t.plan(10);