tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

parser-sets-attributes-and-children.html (4993B)


      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4 <title>Custom Elements: Changes to the HTML parser</title>
      5 <meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org">
      6 <meta name="assert" content="HTML parser must set the attributes and append the children on a custom element">
      7 <link rel="help" href="https://html.spec.whatwg.org/#create-an-element-for-the-token">
      8 <link rel="help" href="https://html.spec.whatwg.org/multipage/parsing.html#insert-a-foreign-element">
      9 <link rel="help" href="https://dom.spec.whatwg.org/#concept-create-element">
     10 <script src="/resources/testharness.js"></script>
     11 <script src="/resources/testharnessreport.js"></script>
     12 <script src="../resources/custom-elements-helpers.js"></script>
     13 </head>
     14 <body>
     15 <div id="log"></div>
     16 <script>
     17 
     18 var numberOfAttributesInConstructor = 0;
     19 var numberOfChildNodesInConstructor = 0;
     20 var numberOfChildNodesInAttributeChangedCallback = 0;
     21 var numberOfChildNodesInConnectedCallback = 0;
     22 var attributesChangedCalls = [];
     23 
     24 class MyCustomElement extends HTMLElement {
     25    constructor(...args) {
     26        super(...args);
     27        numberOfAttributesInConstructor = this.attributes.length;
     28        numberOfChildNodesInConstructor = this.childNodes.length;
     29    }
     30 
     31    attributeChangedCallback(...args) {
     32        attributesChangedCalls.push(create_attribute_changed_callback_log(this, ...args));
     33        numberOfChildNodesInAttributeChangedCallback = this.childNodes.length;
     34    }
     35 
     36    static get observedAttributes() {
     37        return ['id', 'class'];
     38    }
     39 
     40    connectedCallback() {
     41        numberOfChildNodesInConnectedCallback = this.childNodes.length;
     42    }
     43 };
     44 customElements.define('my-custom-element', MyCustomElement);
     45 
     46 </script>
     47 <my-custom-element id="custom-element-id" class="class1 class2">hello <b>world</b></my-custom-element>
     48 <script>
     49 
     50 var customElement = document.querySelector('my-custom-element');
     51 
     52 test(function () {
     53    assert_equals(customElement.getAttribute('id'), 'custom-element-id', 'HTML parser must preserve the id attribute');
     54    assert_equals(customElement.id, 'custom-element-id', 'HTML parser must preserve the semantics of reflect for the id attribute');
     55    assert_equals(customElement.getAttribute('class'), 'class1 class2', 'HTML parser must preserve the class attribute');
     56    assert_equals(customElement.classList.length, 2, 'HTML parser must initialize classList on custom elements');
     57    assert_equals(customElement.classList[0], 'class1', 'HTML parser must initialize classList on custom elements');
     58    assert_equals(customElement.classList[1], 'class2', 'HTML parser must initialize classList on custom elements');
     59 }, 'HTML parser must set the attributes');
     60 
     61 test(function () {
     62    assert_equals(customElement.childNodes.length, 2, 'HTML parser must append child nodes');
     63    assert_true(customElement.firstChild instanceof Text, 'HTML parser must append Text node child to a custom element');
     64    assert_equals(customElement.firstChild.data, 'hello ', 'HTML parser must append Text node child to a custom element');
     65    assert_true(customElement.lastChild instanceof HTMLElement, 'HTML parser must append a builtin element child to a custom element');
     66    assert_true(customElement.lastChild.firstChild instanceof Text, 'HTML parser must preserve grandchild nodes of a custom element');
     67    assert_equals(customElement.lastChild.firstChild.data, 'world', 'HTML parser must preserve grandchild nodes of a custom element');
     68 }, 'HTML parser must append child nodes');
     69 
     70 test(function () {
     71    assert_equals(numberOfAttributesInConstructor, 0, 'HTML parser must not set attributes on a custom element before invoking the constructor');
     72    assert_equals(numberOfChildNodesInConstructor, 0, 'HTML parser must not append child nodes to a custom element before invoking the constructor');
     73 }, 'HTML parser must set the attributes or append children before calling constructor');
     74 
     75 test(function () {
     76    // https://html.spec.whatwg.org/multipage/parsing.html#insert-a-foreign-element
     77    // 3.3. Pop the element queue from the custom element reactions
     78    // stack, and invoke custom element reactions in that queue.
     79    assert_equals(numberOfChildNodesInConnectedCallback, 0);
     80 }, 'HTML parser should call connectedCallback before appending child nodes.');
     81 
     82 test(function () {
     83    assert_equals(attributesChangedCalls.length, 2);
     84    assert_attribute_log_entry(attributesChangedCalls[0], {name: 'id', oldValue: null, newValue: 'custom-element-id', namespace: null});
     85    assert_attribute_log_entry(attributesChangedCalls[1], {name: 'class', oldValue: null, newValue: 'class1 class2', namespace: null});
     86    // https://html.spec.whatwg.org/multipage/parsing.html#create-an-element-for-the-token
     87    // 9.2. Invoke custom element reactions in queue.
     88    assert_equals(numberOfChildNodesInAttributeChangedCallback, 0,
     89                  'attributeChangedCallback should be called ' +
     90                      'before appending a child');
     91 }, 'HTML parser must enqueue attributeChanged reactions');
     92 
     93 </script>
     94 </body>
     95 </html>