summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/webcomponents/test_custom_element_define.html
blob: 7ab5ed44aad5997d98349a11789f8d435caf487d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=783129
-->
<head>
  <title>Test for customElements.define</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=783129">Bug 783129</a>
<div>
<x-unresolved id="unresolved"></x-unresolved>
</div>

<script>

function testDefineExtend(tag, extend, definition, expectException) {
  try {
    customElements.define(tag, definition, { extends: extend });
    ok(!expectException, "Defined " + tag + " extending " + extend + ".");
  } catch (ex) {
    ok(expectException, "Did not define " + tag + " extending " + extend + ".");
  }
}

function testDefineSimple(tag, definition, expectException) {
  try {
    customElements.define(tag, definition);
    ok(!expectException, "Defined " + tag + " extending HTMLElement.");
  } catch (ex) {
    ok(expectException, "Did not define " + tag + " extending HTMLElement.");
  }
}

function startTest() {
  // Test defining some simple definition.
  testDefineSimple("x-html-obj-elem", class extends HTMLElement {}, false);
  testDefineSimple("x-html-obj-p", class extends HTMLParagraphElement {}, false);

  // Make sure the prototype on unresolved elements is HTMLElement not HTMLUnknownElement.
  var unresolved = document.getElementById("unresolved");
  is(unresolved.__proto__, HTMLElement.prototype, "Unresolved custom elements should have HTMLElement as prototype.");

  var anotherUnresolved = document.createElement("maybe-custom-element");
  is(anotherUnresolved.__proto__, HTMLElement.prototype, "Unresolved custom elements should have HTMLElement as prototype.");

  // Test defining some invalid definition.
  testDefineSimple("x-invalid-number", 42, true);
  testDefineSimple("x-invalid-boolean", false, true);
  testDefineSimple("x-invalid-float", 1.0, true);

  // Test invalid custom element names.
  testDefineSimple("invalid", class extends HTMLElement {}, true);
  testDefineSimple("annotation-xml", class extends HTMLElement {}, true);
  testDefineSimple("color-profile", class extends HTMLElement {}, true);
  testDefineSimple("font-face", class extends HTMLElement {}, true);
  testDefineSimple("font-face-src", class extends HTMLElement {}, true);
  testDefineSimple("font-face-uri", class extends HTMLElement {}, true);
  testDefineSimple("font-face-format", class extends HTMLElement {}, true);
  testDefineSimple("font-face-name", class extends HTMLElement {}, true);
  testDefineSimple("missing-glyph", class extends HTMLElement {}, true);

  // Test defining elements that extend from an existing element.
  testDefineExtend("x-extend-span", "span", class extends HTMLElement {}, false);
  testDefineExtend("x-extend-span-caps", "SPAN", class extends HTMLElement {}, true);

  // Test defining elements that extend from a non-existing element.
  testDefineExtend("x-extend-span-nonexist", "nonexisting", class extends HTMLElement {}, true);

  // Test registration with duplicate type.
  testDefineSimple("x-dupe-me", class extends HTMLElement {}, false);
  testDefineSimple("x-dupe-me", class extends HTMLElement {}, true);
  testDefineSimple("X-DUPE-ME", class extends HTMLElement {}, true);
  testDefineExtend("x-dupe-me", "span", class extends HTMLElement {}, true);

  // customElements.define with extended type.
  class ExtendButton extends HTMLButtonElement {};
  customElements.define("x-extended-button", ExtendButton, { extends: "button" });
  var extendedButton = document.createElement("button", {is: "x-extended-button"});
  is(extendedButton.tagName, "BUTTON", "Created element should have local name of BUTTON");
  is(extendedButton.__proto__, ExtendButton.prototype, "Created element should have the prototype of the extended type.");
  is(extendedButton.getAttribute("is"), null, "The |is| attribute of the created element should not be the extended type.");
  is(extendedButton.type, "submit", "Created element should be a button with type of \"submit\"");

  // Custom element constructor.
  var constructedButton = new ExtendButton();
  is(constructedButton.tagName, "BUTTON", "Created element should have local name of BUTTON");
  is(constructedButton.__proto__, ExtendButton.prototype, "Created element should have the prototype of the extended type.");

  // Try creating an element with a custom element name, but not in the html namespace.
  class XInHTMLNamespace extends HTMLElement {};
  customElements.define("x-in-html-namespace", XInHTMLNamespace);
  var wrongNamespaceElem = document.createElementNS("http://www.w3.org/2000/svg", "x-in-html-namespace");
  isnot(wrongNamespaceElem.__proto__, XInHTMLNamespace.prototype, "Definition for element in html namespace should not apply to SVG elements.");

  var div = document.createElement("div");
  div.appendChild(extendedButton);
  is(div.innerHTML, '<button is="x-extended-button"></button>', "'is value' should be serialized.");

  const de = SpecialPowers.Ci.nsIDocumentEncoder;
  var htmlencoder = SpecialPowers.Cu.createDocumentEncoder("text/html");
  htmlencoder.init(document, "text/html", de.OutputLFLineBreak);
  htmlencoder.setCharset("UTF-8");
  htmlencoder.setContainerNode(div);
  is(htmlencoder.encodeToString(), '<button is="x-extended-button"></button>',
     "'is value' should be serialized (html).");

  var xhtmlencoder = SpecialPowers.Cu.createDocumentEncoder("application/xhtml+xml");
  xhtmlencoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak);
  xhtmlencoder.setCharset("UTF-8");
  xhtmlencoder.setContainerNode(div);
  is(xhtmlencoder.encodeToString(), '<button is="x-extended-button" xmlns="http://www.w3.org/1999/xhtml"></button>',
     "'is value' should be serialized (xhtml).");

  var xmlencoder = SpecialPowers.Cu.createDocumentEncoder("text/xml");
  xmlencoder.init(document, "text/xml", de.OutputLFLineBreak);
  xmlencoder.setCharset("UTF-8");
  xmlencoder.setContainerNode(div);
  is(xmlencoder.encodeToString(), '<button is="x-extended-button" xmlns="http://www.w3.org/1999/xhtml"></button>',
     "'is value' should be serialized (xml).");
}

startTest();

</script>
</body>
</html>