dom/tests/mochitest/webcomponents/test_document_register.html

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=783129
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for document.registerElement using custom prototype</title>
michael@0 8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 10 </head>
michael@0 11 <body>
michael@0 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=783129">Bug 783129</a>
michael@0 13 <div>
michael@0 14 <x-unresolved id="unresolved"></x-unresolved>
michael@0 15 </div>
michael@0 16
michael@0 17 <script>
michael@0 18
michael@0 19 function testRegisterExtend(tag, extend, proto, expectException) {
michael@0 20 try {
michael@0 21 document.registerElement(tag, { prototype: proto, extends: extend });
michael@0 22 ok(!expectException, "Registered " + tag + " extending " + extend + " containing " + proto + " in proto chain.");
michael@0 23 } catch (ex) {
michael@0 24 ok(expectException, "Did not register " + tag + " extending " + extend + " containing " + proto + " in proto chain.");
michael@0 25 }
michael@0 26 }
michael@0 27
michael@0 28 function testRegisterSimple(tag, proto, expectException) {
michael@0 29 try {
michael@0 30 document.registerElement(tag, { prototype: proto });
michael@0 31 ok(!expectException, "Registered " + tag + " containing " + proto + " in proto chain.");
michael@0 32 } catch (ex) {
michael@0 33 ok(expectException, "Did not register " + tag + " containing " + proto + " in proto chain.");
michael@0 34 }
michael@0 35 }
michael@0 36
michael@0 37 function startTest() {
michael@0 38 // Test registering some simple prototypes.
michael@0 39 testRegisterSimple("x-html-obj-elem", Object.create(HTMLElement.prototype), false);
michael@0 40 testRegisterSimple("x-html-obj-p", Object.create(HTMLParagraphElement.prototype), false);
michael@0 41
michael@0 42 // If prototype is an interface prototype object for any interface object,
michael@0 43 // registration will throw.
michael@0 44 testRegisterSimple("x-html-elem", HTMLElement.prototype, true);
michael@0 45 testRegisterSimple("x-html-select", HTMLSelectElement.prototype, true);
michael@0 46 testRegisterSimple("some-elem", HTMLElement.prototype, true);
michael@0 47 testRegisterSimple("x-html-p", HTMLParagraphElement.prototype, true);
michael@0 48 testRegisterSimple("x-html-span", HTMLSpanElement.prototype, true);
michael@0 49 testRegisterSimple("x-svg-proto", SVGElement.prototype, true);
michael@0 50
michael@0 51 // Make sure the prototype on unresolved elements is HTMLElement not HTMLUnknownElement.
michael@0 52 var unresolved = document.getElementById("unresolved");
michael@0 53 is(unresolved.__proto__, HTMLElement.prototype, "Unresolved custom elements should have HTMLElement as prototype.");
michael@0 54
michael@0 55 var anotherUnresolved = document.createElement("maybe-custom-element");
michael@0 56 is(anotherUnresolved.__proto__, HTMLElement.prototype, "Unresolved custom elements should have HTMLElement as prototype.");
michael@0 57
michael@0 58 // Registering without a prototype should automatically create one inheriting from HTMLElement.
michael@0 59 testRegisterSimple("x-elem-no-proto", null, false);
michael@0 60 var simpleElem = document.createElement("x-elem-no-proto");
michael@0 61 is(simpleElem.__proto__.__proto__, HTMLElement.prototype, "Default prototype should inherit from HTMLElement");
michael@0 62
michael@0 63 var simpleProto = Object.create(HTMLElement.prototype);
michael@0 64 testRegisterSimple("x-elem-simple-proto", simpleProto, false);
michael@0 65 var simpleProtoElem = document.createElement("x-elem-simple-proto");
michael@0 66 is(simpleProtoElem.__proto__, simpleProto, "Custom element should use registered prototype.");
michael@0 67 var anotherSimpleElem = document.createElementNS("http://www.w3.org/1999/xhtml", "x-elem-simple-proto");
michael@0 68 is(anotherSimpleElem.__proto__, simpleProto, "Custom element should use registered prototype.");
michael@0 69
michael@0 70 // Test registering some invalid prototypes.
michael@0 71 testRegisterSimple("x-invalid-number", 42, true);
michael@0 72 testRegisterSimple("x-invalid-boolean", false, true);
michael@0 73 testRegisterSimple("x-invalid-float", 1.0, true);
michael@0 74 // Can not register with a prototype that inherits from SVGElement
michael@0 75 // without extending an existing element type.
michael@0 76 testRegisterSimple("x-html-obj-svg", Object.create(SVGElement.prototype), true);
michael@0 77 // A prototype with a non-configurable "constructor" property must throw.
michael@0 78 var nonConfigProto = Object.create(HTMLElement.prototype,
michael@0 79 { constructor: { configurable: false, value: function() {} } });
michael@0 80 testRegisterSimple("x-non-config-proto", nonConfigProto, true);
michael@0 81
michael@0 82 // Test invalid custom element names.
michael@0 83 testRegisterSimple("invalid", Object.create(HTMLElement.prototype), true);
michael@0 84 testRegisterSimple("annotation-xml", Object.create(HTMLElement.prototype), true);
michael@0 85 testRegisterSimple("color-profile", Object.create(HTMLElement.prototype), true);
michael@0 86 testRegisterSimple("font-face", Object.create(HTMLElement.prototype), true);
michael@0 87 testRegisterSimple("font-face-src", Object.create(HTMLElement.prototype), true);
michael@0 88 testRegisterSimple("font-face-uri", Object.create(HTMLElement.prototype), true);
michael@0 89 testRegisterSimple("font-face-format", Object.create(HTMLElement.prototype), true);
michael@0 90 testRegisterSimple("font-face-name", Object.create(HTMLElement.prototype), true);
michael@0 91 testRegisterSimple("missing-glyph", Object.create(HTMLElement.prototype), true);
michael@0 92
michael@0 93 // Test registering elements that extend from an existing element.
michael@0 94 testRegisterExtend("x-extend-span", "span", Object.create(HTMLElement.prototype), false);
michael@0 95 testRegisterExtend("x-extend-span-caps", "SPAN", Object.create(HTMLElement.prototype), false);
michael@0 96
michael@0 97 // Test registering elements that extend from a non-existing element.
michael@0 98 testRegisterExtend("x-extend-span-nonexist", "nonexisting", Object.create(HTMLElement.prototype), true);
michael@0 99 testRegisterExtend("x-extend-svg-nonexist", "nonexisting", Object.create(SVGElement.prototype), true);
michael@0 100
michael@0 101 // Test registration with duplicate type.
michael@0 102 testRegisterSimple("x-dupe-me", Object.create(HTMLElement.prototype), false);
michael@0 103 testRegisterSimple("x-dupe-me", Object.create(HTMLElement.prototype), true);
michael@0 104 testRegisterSimple("X-DUPE-ME", Object.create(HTMLElement.prototype), true);
michael@0 105 testRegisterSimple("x-dupe-me", null, true);
michael@0 106 testRegisterExtend("x-dupe-me", "span", Object.create(HTMLElement.prototype), true);
michael@0 107 testRegisterExtend("x-dupe-me", "shape", Object.create(SVGElement.prototype), true);
michael@0 108
michael@0 109 testRegisterExtend("x-svg-dupe-me", "circle", Object.create(SVGElement.prototype), false);
michael@0 110 testRegisterSimple("x-svg-dupe-me", Object.create(HTMLElement.prototype), true);
michael@0 111 testRegisterSimple("X-SVG-DUPE-ME", Object.create(HTMLElement.prototype), true);
michael@0 112 testRegisterSimple("x-svg-dupe-me", null, true);
michael@0 113 testRegisterExtend("x-svg-dupe-me", "span", Object.create(HTMLElement.prototype), true);
michael@0 114 testRegisterExtend("x-svg-dupe-me", "shape", Object.create(SVGElement.prototype), true);
michael@0 115
michael@0 116 // document.createElement with extended type.
michael@0 117 var extendedProto = Object.create(HTMLButtonElement.prototype);
michael@0 118 var buttonConstructor = document.registerElement("x-extended-button", { prototype: extendedProto, extends: "button" });
michael@0 119 var extendedButton = document.createElement("button", "x-extended-button");
michael@0 120 is(extendedButton.tagName, "BUTTON", "Created element should have local name of BUTTON");
michael@0 121 is(extendedButton.__proto__, extendedProto, "Created element should have the prototype of the extended type.");
michael@0 122 is(extendedButton.getAttribute("is"), "x-extended-button", "The |is| attribute of the created element should be the extended type.");
michael@0 123
michael@0 124 // document.createElementNS with different namespace than definition.
michael@0 125 var svgButton = document.createElementNS("http://www.w3.org/2000/svg", "button", "x-extended-button");
michael@0 126 isnot(svgButton.__proto__, extendedProto, "Definition for element is in html namespace, registration should not apply for SVG elements.");
michael@0 127
michael@0 128 // document.createElementNS with no namespace.
michael@0 129 var noNamespaceButton = document.createElementNS("", "button", "x-extended-button");
michael@0 130 isnot(noNamespaceButton.__proto__, extendedProto, "Definition for element is in html namespace, registration should not apply for elements with no namespace.");
michael@0 131
michael@0 132 // document.createElement with non-existant extended type.
michael@0 133 var normalButton = document.createElement("button", "x-non-existant");
michael@0 134 is(normalButton.__proto__, HTMLButtonElement.prototype, "When the extended type doesn't exist, prototype should not change.");
michael@0 135
michael@0 136 // document.createElement with exteneded type that does not match with local name of element.
michael@0 137 var normalDiv = document.createElement("div", "x-extended-button");
michael@0 138 is(normalDiv.__proto__, HTMLDivElement.prototype, "Prototype should not change when local name of extended type defintion does not match.");
michael@0 139
michael@0 140 // Custom element constructor.
michael@0 141 var constructedButton = new buttonConstructor();
michael@0 142 is(constructedButton.tagName, "BUTTON", "Created element should have local name of BUTTON");
michael@0 143 is(constructedButton.__proto__, extendedProto, "Created element should have the prototype of the extended type.");
michael@0 144 is(constructedButton.getAttribute("is"), "x-extended-button", "The |is| attribute of the created element should be the extended type.");
michael@0 145
michael@0 146 // document.createElementNS with extended type.
michael@0 147 var svgExtendedProto = Object.create(SVGTextElement.prototype);
michael@0 148 var svgConstructor = document.registerElement("x-extended-text", { prototype: svgExtendedProto, extends: "text"});
michael@0 149 var extendedText = document.createElementNS("http://www.w3.org/2000/svg", "text", "x-extended-text");
michael@0 150 is(extendedText.tagName, "text", "Created element should have a local name of |text|.");
michael@0 151 is(extendedText.__proto__, svgExtendedProto, "Created element have the registered prototype.");
michael@0 152 is(extendedText.getAttribute("is"), "x-extended-text", "The |is| attribute of the created element should be the extended type.");
michael@0 153
michael@0 154 // document.createElement with different namespace than definition for extended element.
michael@0 155 var htmlText = document.createElement("text", "x-extended-text");
michael@0 156 isnot(htmlText.__proto__, svgExtendedProto, "Definition for element in SVG namespace should not apply to HTML elements.");
michael@0 157
michael@0 158 // Custom element constructor for a SVG element.
michael@0 159 var constructedText = new svgConstructor();
michael@0 160 is(constructedText.tagName, "text", "Created element should have a local name of |text|.");
michael@0 161 is(constructedText.__proto__, svgExtendedProto, "Created element have the registered prototype.");
michael@0 162 is(constructedText.getAttribute("is"), "x-extended-text", "The |is| attribute of the created element should be the extended type.");
michael@0 163
michael@0 164 // Try creating an element with a custom element name, but not in the html namespace.
michael@0 165 var htmlNamespaceProto = Object.create(HTMLElement.prototype);
michael@0 166 document.registerElement("x-in-html-namespace", { prototype: htmlNamespaceProto });
michael@0 167 var wrongNamespaceElem = document.createElementNS("http://www.w3.org/2000/svg", "x-in-html-namespace");
michael@0 168 isnot(wrongNamespaceElem.__proto__, htmlNamespaceProto, "Definition for element in html namespace should not apply to SVG elements.");
michael@0 169 }
michael@0 170
michael@0 171 startTest();
michael@0 172
michael@0 173 </script>
michael@0 174 </body>
michael@0 175 </html>

mercurial