1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/webcomponents/test_document_register.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,175 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=783129 1.8 +--> 1.9 +<head> 1.10 + <title>Test for document.registerElement using custom prototype</title> 1.11 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.12 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.13 +</head> 1.14 +<body> 1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=783129">Bug 783129</a> 1.16 +<div> 1.17 +<x-unresolved id="unresolved"></x-unresolved> 1.18 +</div> 1.19 + 1.20 +<script> 1.21 + 1.22 +function testRegisterExtend(tag, extend, proto, expectException) { 1.23 + try { 1.24 + document.registerElement(tag, { prototype: proto, extends: extend }); 1.25 + ok(!expectException, "Registered " + tag + " extending " + extend + " containing " + proto + " in proto chain."); 1.26 + } catch (ex) { 1.27 + ok(expectException, "Did not register " + tag + " extending " + extend + " containing " + proto + " in proto chain."); 1.28 + } 1.29 +} 1.30 + 1.31 +function testRegisterSimple(tag, proto, expectException) { 1.32 + try { 1.33 + document.registerElement(tag, { prototype: proto }); 1.34 + ok(!expectException, "Registered " + tag + " containing " + proto + " in proto chain."); 1.35 + } catch (ex) { 1.36 + ok(expectException, "Did not register " + tag + " containing " + proto + " in proto chain."); 1.37 + } 1.38 +} 1.39 + 1.40 +function startTest() { 1.41 + // Test registering some simple prototypes. 1.42 + testRegisterSimple("x-html-obj-elem", Object.create(HTMLElement.prototype), false); 1.43 + testRegisterSimple("x-html-obj-p", Object.create(HTMLParagraphElement.prototype), false); 1.44 + 1.45 + // If prototype is an interface prototype object for any interface object, 1.46 + // registration will throw. 1.47 + testRegisterSimple("x-html-elem", HTMLElement.prototype, true); 1.48 + testRegisterSimple("x-html-select", HTMLSelectElement.prototype, true); 1.49 + testRegisterSimple("some-elem", HTMLElement.prototype, true); 1.50 + testRegisterSimple("x-html-p", HTMLParagraphElement.prototype, true); 1.51 + testRegisterSimple("x-html-span", HTMLSpanElement.prototype, true); 1.52 + testRegisterSimple("x-svg-proto", SVGElement.prototype, true); 1.53 + 1.54 + // Make sure the prototype on unresolved elements is HTMLElement not HTMLUnknownElement. 1.55 + var unresolved = document.getElementById("unresolved"); 1.56 + is(unresolved.__proto__, HTMLElement.prototype, "Unresolved custom elements should have HTMLElement as prototype."); 1.57 + 1.58 + var anotherUnresolved = document.createElement("maybe-custom-element"); 1.59 + is(anotherUnresolved.__proto__, HTMLElement.prototype, "Unresolved custom elements should have HTMLElement as prototype."); 1.60 + 1.61 + // Registering without a prototype should automatically create one inheriting from HTMLElement. 1.62 + testRegisterSimple("x-elem-no-proto", null, false); 1.63 + var simpleElem = document.createElement("x-elem-no-proto"); 1.64 + is(simpleElem.__proto__.__proto__, HTMLElement.prototype, "Default prototype should inherit from HTMLElement"); 1.65 + 1.66 + var simpleProto = Object.create(HTMLElement.prototype); 1.67 + testRegisterSimple("x-elem-simple-proto", simpleProto, false); 1.68 + var simpleProtoElem = document.createElement("x-elem-simple-proto"); 1.69 + is(simpleProtoElem.__proto__, simpleProto, "Custom element should use registered prototype."); 1.70 + var anotherSimpleElem = document.createElementNS("http://www.w3.org/1999/xhtml", "x-elem-simple-proto"); 1.71 + is(anotherSimpleElem.__proto__, simpleProto, "Custom element should use registered prototype."); 1.72 + 1.73 + // Test registering some invalid prototypes. 1.74 + testRegisterSimple("x-invalid-number", 42, true); 1.75 + testRegisterSimple("x-invalid-boolean", false, true); 1.76 + testRegisterSimple("x-invalid-float", 1.0, true); 1.77 + // Can not register with a prototype that inherits from SVGElement 1.78 + // without extending an existing element type. 1.79 + testRegisterSimple("x-html-obj-svg", Object.create(SVGElement.prototype), true); 1.80 + // A prototype with a non-configurable "constructor" property must throw. 1.81 + var nonConfigProto = Object.create(HTMLElement.prototype, 1.82 + { constructor: { configurable: false, value: function() {} } }); 1.83 + testRegisterSimple("x-non-config-proto", nonConfigProto, true); 1.84 + 1.85 + // Test invalid custom element names. 1.86 + testRegisterSimple("invalid", Object.create(HTMLElement.prototype), true); 1.87 + testRegisterSimple("annotation-xml", Object.create(HTMLElement.prototype), true); 1.88 + testRegisterSimple("color-profile", Object.create(HTMLElement.prototype), true); 1.89 + testRegisterSimple("font-face", Object.create(HTMLElement.prototype), true); 1.90 + testRegisterSimple("font-face-src", Object.create(HTMLElement.prototype), true); 1.91 + testRegisterSimple("font-face-uri", Object.create(HTMLElement.prototype), true); 1.92 + testRegisterSimple("font-face-format", Object.create(HTMLElement.prototype), true); 1.93 + testRegisterSimple("font-face-name", Object.create(HTMLElement.prototype), true); 1.94 + testRegisterSimple("missing-glyph", Object.create(HTMLElement.prototype), true); 1.95 + 1.96 + // Test registering elements that extend from an existing element. 1.97 + testRegisterExtend("x-extend-span", "span", Object.create(HTMLElement.prototype), false); 1.98 + testRegisterExtend("x-extend-span-caps", "SPAN", Object.create(HTMLElement.prototype), false); 1.99 + 1.100 + // Test registering elements that extend from a non-existing element. 1.101 + testRegisterExtend("x-extend-span-nonexist", "nonexisting", Object.create(HTMLElement.prototype), true); 1.102 + testRegisterExtend("x-extend-svg-nonexist", "nonexisting", Object.create(SVGElement.prototype), true); 1.103 + 1.104 + // Test registration with duplicate type. 1.105 + testRegisterSimple("x-dupe-me", Object.create(HTMLElement.prototype), false); 1.106 + testRegisterSimple("x-dupe-me", Object.create(HTMLElement.prototype), true); 1.107 + testRegisterSimple("X-DUPE-ME", Object.create(HTMLElement.prototype), true); 1.108 + testRegisterSimple("x-dupe-me", null, true); 1.109 + testRegisterExtend("x-dupe-me", "span", Object.create(HTMLElement.prototype), true); 1.110 + testRegisterExtend("x-dupe-me", "shape", Object.create(SVGElement.prototype), true); 1.111 + 1.112 + testRegisterExtend("x-svg-dupe-me", "circle", Object.create(SVGElement.prototype), false); 1.113 + testRegisterSimple("x-svg-dupe-me", Object.create(HTMLElement.prototype), true); 1.114 + testRegisterSimple("X-SVG-DUPE-ME", Object.create(HTMLElement.prototype), true); 1.115 + testRegisterSimple("x-svg-dupe-me", null, true); 1.116 + testRegisterExtend("x-svg-dupe-me", "span", Object.create(HTMLElement.prototype), true); 1.117 + testRegisterExtend("x-svg-dupe-me", "shape", Object.create(SVGElement.prototype), true); 1.118 + 1.119 + // document.createElement with extended type. 1.120 + var extendedProto = Object.create(HTMLButtonElement.prototype); 1.121 + var buttonConstructor = document.registerElement("x-extended-button", { prototype: extendedProto, extends: "button" }); 1.122 + var extendedButton = document.createElement("button", "x-extended-button"); 1.123 + is(extendedButton.tagName, "BUTTON", "Created element should have local name of BUTTON"); 1.124 + is(extendedButton.__proto__, extendedProto, "Created element should have the prototype of the extended type."); 1.125 + is(extendedButton.getAttribute("is"), "x-extended-button", "The |is| attribute of the created element should be the extended type."); 1.126 + 1.127 + // document.createElementNS with different namespace than definition. 1.128 + var svgButton = document.createElementNS("http://www.w3.org/2000/svg", "button", "x-extended-button"); 1.129 + isnot(svgButton.__proto__, extendedProto, "Definition for element is in html namespace, registration should not apply for SVG elements."); 1.130 + 1.131 + // document.createElementNS with no namespace. 1.132 + var noNamespaceButton = document.createElementNS("", "button", "x-extended-button"); 1.133 + isnot(noNamespaceButton.__proto__, extendedProto, "Definition for element is in html namespace, registration should not apply for elements with no namespace."); 1.134 + 1.135 + // document.createElement with non-existant extended type. 1.136 + var normalButton = document.createElement("button", "x-non-existant"); 1.137 + is(normalButton.__proto__, HTMLButtonElement.prototype, "When the extended type doesn't exist, prototype should not change."); 1.138 + 1.139 + // document.createElement with exteneded type that does not match with local name of element. 1.140 + var normalDiv = document.createElement("div", "x-extended-button"); 1.141 + is(normalDiv.__proto__, HTMLDivElement.prototype, "Prototype should not change when local name of extended type defintion does not match."); 1.142 + 1.143 + // Custom element constructor. 1.144 + var constructedButton = new buttonConstructor(); 1.145 + is(constructedButton.tagName, "BUTTON", "Created element should have local name of BUTTON"); 1.146 + is(constructedButton.__proto__, extendedProto, "Created element should have the prototype of the extended type."); 1.147 + is(constructedButton.getAttribute("is"), "x-extended-button", "The |is| attribute of the created element should be the extended type."); 1.148 + 1.149 + // document.createElementNS with extended type. 1.150 + var svgExtendedProto = Object.create(SVGTextElement.prototype); 1.151 + var svgConstructor = document.registerElement("x-extended-text", { prototype: svgExtendedProto, extends: "text"}); 1.152 + var extendedText = document.createElementNS("http://www.w3.org/2000/svg", "text", "x-extended-text"); 1.153 + is(extendedText.tagName, "text", "Created element should have a local name of |text|."); 1.154 + is(extendedText.__proto__, svgExtendedProto, "Created element have the registered prototype."); 1.155 + is(extendedText.getAttribute("is"), "x-extended-text", "The |is| attribute of the created element should be the extended type."); 1.156 + 1.157 + // document.createElement with different namespace than definition for extended element. 1.158 + var htmlText = document.createElement("text", "x-extended-text"); 1.159 + isnot(htmlText.__proto__, svgExtendedProto, "Definition for element in SVG namespace should not apply to HTML elements."); 1.160 + 1.161 + // Custom element constructor for a SVG element. 1.162 + var constructedText = new svgConstructor(); 1.163 + is(constructedText.tagName, "text", "Created element should have a local name of |text|."); 1.164 + is(constructedText.__proto__, svgExtendedProto, "Created element have the registered prototype."); 1.165 + is(constructedText.getAttribute("is"), "x-extended-text", "The |is| attribute of the created element should be the extended type."); 1.166 + 1.167 + // Try creating an element with a custom element name, but not in the html namespace. 1.168 + var htmlNamespaceProto = Object.create(HTMLElement.prototype); 1.169 + document.registerElement("x-in-html-namespace", { prototype: htmlNamespaceProto }); 1.170 + var wrongNamespaceElem = document.createElementNS("http://www.w3.org/2000/svg", "x-in-html-namespace"); 1.171 + isnot(wrongNamespaceElem.__proto__, htmlNamespaceProto, "Definition for element in html namespace should not apply to SVG elements."); 1.172 +} 1.173 + 1.174 +startTest(); 1.175 + 1.176 +</script> 1.177 +</body> 1.178 +</html>