michael@0:
michael@0: // The xml serializer uses the default line break of the plateform.
michael@0: // So we need to know the value of this default line break, in order
michael@0: // to build correctly the reference strings for tests.
michael@0: // This variable will contain this value.
michael@0: var LB;
michael@0:
michael@0: function run_test() {
michael@0:
michael@0: if(("@mozilla.org/windows-registry-key;1" in C) || ("nsILocalFileOS2" in I))
michael@0: LB = "\r\n";
michael@0: else
michael@0: LB = "\n";
michael@0:
michael@0: for (var i = 0; i < tests.length && tests[i]; ++i) {
michael@0: tests[i].call();
michael@0: }
michael@0: }
michael@0:
michael@0: var tests = [
michael@0: test1,
michael@0: test2,
michael@0: test3,
michael@0: test4,
michael@0: test5,
michael@0: test6,
michael@0: test7,
michael@0: test8,
michael@0: test9,
michael@0: test10,
michael@0: null
michael@0: ];
michael@0:
michael@0: function testString(str) {
michael@0: do_check_eq(roundtrip(str), str);
michael@0: }
michael@0:
michael@0: function test1() {
michael@0: // Basic round-tripping which we expect to hand back the same text
michael@0: // as we passed in (not strictly required for correctness in some of
michael@0: // those cases, but best for readability of serializer output)
michael@0: testString('');
michael@0: testString('');
michael@0: testString('');
michael@0: testString('');
michael@0: testString('')
michael@0: testString('')
michael@0: testString('')
michael@0: testString('')
michael@0: testString('')
michael@0: testString('')
michael@0: testString('')
michael@0: }
michael@0:
michael@0: function test2() {
michael@0: // Test setting of "xmlns" attribute in the null namespace
michael@0:
michael@0: // XXXbz are these tests needed? What should happen here? These
michael@0: // may be bogus.
michael@0:
michael@0: // Setting random "xmlns" attribute
michael@0: var doc = ParseXML('');
michael@0: doc.documentElement.setAttribute("xmlns", "ns2");
michael@0: do_check_serialize(doc);
michael@0: }
michael@0:
michael@0: function test3() {
michael@0: // Test basic appending of kids. Again, we're making assumptions
michael@0: // about how our serializer will serialize simple DOMs.
michael@0: var doc = ParseXML('');
michael@0: var root = doc.documentElement;
michael@0: var child = doc.createElementNS("ns2", "child");
michael@0: root.appendChild(child);
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc),
michael@0: '');
michael@0:
michael@0: doc = ParseXML('');
michael@0: root = doc.documentElement;
michael@0: child = doc.createElementNS("ns2", "prefix:child");
michael@0: root.appendChild(child);
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc),
michael@0: '');
michael@0:
michael@0: doc = ParseXML('');
michael@0: root = doc.documentElement;
michael@0: child = doc.createElementNS("ns2", "prefix:child");
michael@0: root.appendChild(child);
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc),
michael@0: ''+
michael@0: '');
michael@0:
michael@0: }
michael@0:
michael@0: function test4() {
michael@0: // setAttributeNS tests
michael@0:
michael@0: var doc = ParseXML('');
michael@0: var root = doc.documentElement;
michael@0: root.setAttributeNS("ns1", "prefix:local", "val");
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc),
michael@0: '');
michael@0:
michael@0: doc = ParseXML('');
michael@0: root = doc.documentElement;
michael@0: root.setAttributeNS("ns1", "local", "val");
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc),
michael@0: '');
michael@0:
michael@0: doc = ParseXML('');
michael@0: root = doc.documentElement;
michael@0: root.setAttributeNS("ns2", "local", "val");
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc),
michael@0: '');
michael@0:
michael@0: // Handling of prefix-generation for non-null-namespace attributes
michael@0: // which have the same namespace as the current default namespace
michael@0: // (bug 301260).
michael@0: doc = ParseXML('');
michael@0: root = doc.documentElement;
michael@0: root.setAttributeNS("ns1", "local", "val");
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc),
michael@0: '');
michael@0:
michael@0: // Tree-walking test
michael@0: doc = ParseXML(''+
michael@0: ''+
michael@0: '');
michael@0: root = doc.documentElement;
michael@0: // Have to QI here -- no classinfo flattening in xpcshell, apparently
michael@0: var node = root.firstChild.firstChild.QueryInterface(nsIDOMElement);
michael@0: node.setAttributeNS("ns4", "l1", "v1");
michael@0: node.setAttributeNS("ns4", "p2:l2", "v2");
michael@0: node.setAttributeNS("", "l3", "v3");
michael@0: node.setAttributeNS("ns3", "l4", "v4");
michael@0: node.setAttributeNS("ns3", "p5:l5", "v5");
michael@0: node.setAttributeNS("ns3", "a:l6", "v6");
michael@0: node.setAttributeNS("ns2", "l7", "v7");
michael@0: node.setAttributeNS("ns2", "p8:l8", "v8");
michael@0: node.setAttributeNS("ns2", "b:l9", "v9");
michael@0: node.setAttributeNS("ns2", "a:l10", "v10");
michael@0: node.setAttributeNS("ns1", "a:l11", "v11");
michael@0: node.setAttributeNS("ns1", "b:l12", "v12");
michael@0: node.setAttributeNS("ns1", "l13", "v13");
michael@0: do_check_serialize(doc);
michael@0: // Note: we end up with "a2" as the prefix on "l11" and "l12" because we use
michael@0: // "a1" earlier, and discard it in favor of something we get off the
michael@0: // namespace stack, apparently
michael@0: do_check_eq(SerializeXML(doc),
michael@0: ''+
michael@0: ''+
michael@0: '');
michael@0: }
michael@0:
michael@0: function test5() {
michael@0: // Handling of kids in the null namespace when the default is a
michael@0: // different namespace (bug 301260).
michael@0: var doc = ParseXML('')
michael@0: var child = doc.createElement('child');
michael@0: doc.documentElement.appendChild(child);
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc),
michael@0: '');
michael@0: }
michael@0:
michael@0: function test6() {
michael@0: // Handling of not using a namespace prefix (or default namespace!)
michael@0: // that's not bound to our namespace in our scope (bug 301260).
michael@0: var doc = ParseXML('');
michael@0: var root = doc.documentElement;
michael@0: var child1 = doc.createElementNS("ns2", "prefix:child1");
michael@0: var child2 = doc.createElementNS("ns1", "prefix:child2");
michael@0: child1.appendChild(child2);
michael@0: root.appendChild(child1);
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc),
michael@0: ''+
michael@0: '');
michael@0:
michael@0: doc = ParseXML('');
michael@0: root = doc.documentElement;
michael@0: child1 = root.firstChild;
michael@0: child2 = doc.createElementNS("ns1", "prefix:child2");
michael@0: child1.appendChild(child2);
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc),
michael@0: ''+
michael@0: '');
michael@0:
michael@0: doc = ParseXML(''+
michael@0: '');
michael@0: root = doc.documentElement;
michael@0: child1 = root.firstChild;
michael@0: child2 = doc.createElementNS("ns1", "prefix:child2");
michael@0: child1.appendChild(child2);
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc),
michael@0: ''+
michael@0: '');
michael@0:
michael@0:
michael@0: doc = ParseXML('');
michael@0: root = doc.documentElement;
michael@0: child1 = doc.createElementNS("ns2", "child1");
michael@0: child2 = doc.createElementNS("ns1", "child2");
michael@0: child1.appendChild(child2);
michael@0: root.appendChild(child1);
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc),
michael@0: ''+
michael@0: '');
michael@0: }
michael@0:
michael@0: function test7() {
michael@0: // Handle xmlns attribute declaring a default namespace on a non-namespaced
michael@0: // element (bug 326994).
michael@0: var doc = ParseXML('')
michael@0: var root = doc.documentElement;
michael@0: root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",
michael@0: "http://www.w3.org/1999/xhtml");
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc), '');
michael@0:
michael@0: doc = ParseXML('')
michael@0: root = doc.documentElement;
michael@0: root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",
michael@0: "http://www.w3.org/1999/xhtml");
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc), '');
michael@0:
michael@0: doc = ParseXML('' +
michael@0: '')
michael@0: root = doc.documentElement;
michael@0:
michael@0: // No interface flattening in xpcshell
michael@0: var child1 = root.firstChild.QueryInterface(nsIDOMElement);
michael@0: child1.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",
michael@0: "http://www.w3.org/1999/xhtml");
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc),
michael@0: '' +
michael@0: '');
michael@0:
michael@0: doc = ParseXML('' +
michael@0: '' +
michael@0: '' +
michael@0: '')
michael@0: root = doc.documentElement;
michael@0: // No interface flattening in xpcshell
michael@0: child1 = root.firstChild.QueryInterface(nsIDOMElement);
michael@0: var child2 = child1.firstChild.QueryInterface(nsIDOMElement);
michael@0: child1.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",
michael@0: "http://www.w3.org/1999/xhtml");
michael@0: child2.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "");
michael@0: do_check_serialize(doc);
michael@0: do_check_eq(SerializeXML(doc),
michael@0: '' +
michael@0: '');
michael@0: }
michael@0:
michael@0: function test8() {
michael@0: // Test behavior of serializing with a given charset.
michael@0: var str1 = ''+LB+'';
michael@0: var str2 = ''+LB+'';
michael@0: var doc1 = ParseXML(str1);
michael@0: var doc2 = ParseXML(str2);
michael@0:
michael@0: var p = Pipe();
michael@0: DOMSerializer().serializeToStream(doc1, p.outputStream, "ISO-8859-1");
michael@0: p.outputStream.close();
michael@0: do_check_eq(ScriptableInput(p).read(-1), str1);
michael@0:
michael@0: p = Pipe();
michael@0: DOMSerializer().serializeToStream(doc2, p.outputStream, "ISO-8859-1");
michael@0: p.outputStream.close();
michael@0: do_check_eq(ScriptableInput(p).read(-1), str1);
michael@0:
michael@0: p = Pipe();
michael@0: DOMSerializer().serializeToStream(doc1, p.outputStream, "UTF8");
michael@0: p.outputStream.close();
michael@0: do_check_eq(ScriptableInput(p).read(-1), str2);
michael@0:
michael@0: p = Pipe();
michael@0: DOMSerializer().serializeToStream(doc2, p.outputStream, "UTF8");
michael@0: p.outputStream.close();
michael@0: do_check_eq(ScriptableInput(p).read(-1), str2);
michael@0: }
michael@0:
michael@0: function test9() {
michael@0: // Test behavior of serializing between given charsets, using
michael@0: // ISO-8859-1-representable text.
michael@0: var contents = '' +
michael@0: '\u00BD + \u00BE == \u00BD\u00B2 + \u00BC + \u00BE' +
michael@0: '';
michael@0: var str1 = ''+ LB + contents;
michael@0: var str2 = ''+ LB + contents;
michael@0: var str3 = ''+ LB + contents;
michael@0: var doc1 = ParseXML(str1);
michael@0: var doc2 = ParseXML(str2);
michael@0: var doc3 = ParseXML(str3);
michael@0:
michael@0: checkSerialization(doc1, "ISO-8859-1", str1);
michael@0: checkSerialization(doc2, "ISO-8859-1", str1);
michael@0: checkSerialization(doc3, "ISO-8859-1", str1);
michael@0:
michael@0: checkSerialization(doc1, "UTF8", str2);
michael@0: checkSerialization(doc2, "UTF8", str2);
michael@0: checkSerialization(doc3, "UTF8", str2);
michael@0:
michael@0: checkSerialization(doc1, "UTF-16", str3);
michael@0: checkSerialization(doc2, "UTF-16", str3);
michael@0: checkSerialization(doc3, "UTF-16", str3);
michael@0: }
michael@0:
michael@0: function test10() {
michael@0: // Test behavior of serializing between given charsets, using
michael@0: // Unicode characters (XXX but only BMP ones because I don't know
michael@0: // how to create one with non-BMP characters, either with JS strings
michael@0: // or using DOM APIs).
michael@0: var contents = '' +
michael@0: 'AZaz09 \u007F ' + // U+000000 to U+00007F
michael@0: '\u0080 \u0398 \u03BB \u0725 ' + // U+000080 to U+0007FF
michael@0: '\u0964 \u0F5F \u20AC \uFFFB' + // U+000800 to U+00FFFF
michael@0: '';
michael@0: var str1 = ''+ LB + contents;
michael@0: var str2 = ''+ LB + contents;
michael@0: var doc1 = ParseXML(str1);
michael@0: var doc2 = ParseXML(str2);
michael@0:
michael@0: checkSerialization(doc1, "UTF8", str1);
michael@0: checkSerialization(doc2, "UTF8", str1);
michael@0:
michael@0: checkSerialization(doc1, "UTF-16", str2);
michael@0: checkSerialization(doc2, "UTF-16", str2);
michael@0: }
michael@0:
michael@0: function checkSerialization(doc, toCharset, expectedString) {
michael@0: var p = Pipe();
michael@0: DOMSerializer().serializeToStream(doc, p.outputStream, toCharset);
michael@0: p.outputStream.close();
michael@0:
michael@0: var cin = C["@mozilla.org/intl/converter-input-stream;1"]
michael@0: .createInstance(I.nsIConverterInputStream);
michael@0: cin.init(p.inputStream, toCharset, 1024, 0x0);
michael@0:
michael@0: // compare the first expectedString.length characters for equality
michael@0: var outString = {};
michael@0: var count = cin.readString(expectedString.length, outString);
michael@0: do_check_true(count == expectedString.length);
michael@0: do_check_true(outString.value == expectedString);
michael@0:
michael@0: // if there's anything more in the stream, it's a bug
michael@0: do_check_eq(0, cin.readString(1, outString));
michael@0: do_check_eq(outString.value, "");
michael@0: }