tor-browser

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

htmlconstructor_autonomous_tests.js (3506B)


      1 promises.push(
      2  test_with_new_window(testWindow => {
      3    // Test calling the HTMLElement constructor.
      4    (() => {
      5      SimpleTest.doesThrow(() => {
      6        testWindow.HTMLElement();
      7      }, "calling the HTMLElement constructor should throw a TypeError");
      8    })();
      9 
     10    // Test constructing a HTMLELement.
     11    (() => {
     12      SimpleTest.doesThrow(() => {
     13        new testWindow.HTMLElement();
     14      }, "constructing a HTMLElement should throw a TypeError");
     15    })();
     16 
     17    // Test constructing a custom element with defining HTMLElement as entry.
     18    (() => {
     19      testWindow.customElements.define(
     20        "x-defining-html-element",
     21        testWindow.HTMLElement
     22      );
     23      SimpleTest.doesThrow(() => {
     24        new testWindow.HTMLElement();
     25      }, "constructing a custom element with defining HTMLElement as registry " + "entry should throw a TypeError");
     26    })();
     27 
     28    // Test calling a custom element constructor and constructing an autonomous
     29    // custom element.
     30    (() => {
     31      let num_constructor_invocations = 0;
     32      class X extends testWindow.HTMLElement {
     33        constructor() {
     34          super();
     35          num_constructor_invocations++;
     36        }
     37      }
     38      testWindow.customElements.define("x-element", X);
     39      SimpleTest.doesThrow(() => {
     40        X();
     41      }, "calling an autonomous custom element constructor should throw a TypeError");
     42 
     43      let element = new X();
     44      SimpleTest.is(
     45        Object.getPrototypeOf(Cu.waiveXrays(element)),
     46        X.prototype,
     47        "constructing an autonomous custom element; " +
     48          "the element should be a registered constructor"
     49      );
     50      SimpleTest.is(
     51        element.localName,
     52        "x-element",
     53        "constructing an autonomous custom element; " +
     54          'the element tag name should be "x-element"'
     55      );
     56      SimpleTest.is(
     57        element.namespaceURI,
     58        "http://www.w3.org/1999/xhtml",
     59        "constructing an autonomous custom element; " +
     60          "the element should be in the HTML namespace"
     61      );
     62      SimpleTest.is(
     63        element.prefix,
     64        null,
     65        "constructing an autonomous custom element; " +
     66          "the element name should not have a prefix"
     67      );
     68      SimpleTest.is(
     69        element.ownerDocument,
     70        testWindow.document,
     71        "constructing an autonomous custom element; " +
     72          "the element should be owned by the registry's associated " +
     73          "document"
     74      );
     75      SimpleTest.is(
     76        num_constructor_invocations,
     77        1,
     78        "constructing an autonomous custom element; " +
     79          "the constructor should have been invoked once"
     80      );
     81    })();
     82 
     83    // Test if prototype is no an object.
     84    (() => {
     85      function ElementWithNonObjectPrototype() {
     86        let o = Reflect.construct(testWindow.HTMLElement, [], new.target);
     87        SimpleTest.is(
     88          Object.getPrototypeOf(Cu.waiveXrays(o)),
     89          window.HTMLElement.prototype,
     90          "constructing an autonomous custom element; " +
     91            "if prototype is not object, fallback from NewTarget's realm"
     92        );
     93      }
     94 
     95      // Prototype have to be an object during define(), otherwise define will
     96      // throw an TypeError exception.
     97      ElementWithNonObjectPrototype.prototype = {};
     98      testWindow.customElements.define(
     99        "x-non-object-prototype",
    100        ElementWithNonObjectPrototype
    101      );
    102 
    103      ElementWithNonObjectPrototype.prototype = "string";
    104      new ElementWithNonObjectPrototype();
    105    })();
    106  })
    107 );