summaryrefslogtreecommitdiff
path: root/dom/html/test/test_bug1081037.html
blob: 9d878258023db7e5ed37a583e86921cebb57a779 (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
130
131
132
133
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1081037
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1081037</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript">

  /** Test for Bug 1081037 **/

function shouldThrow(fun, msg, ex, todo) {
  try {
    fun();
    ok(todo, msg);
  } catch (e) {
    ok(new RegExp(ex).test(e), msg + " (thrown:" + e + ")")
  }
}

var Foo = document.registerElement('x-foo', {
  prototype: {bar: 5}
});

Foo.prototype.bar = 6;
var foo = new Foo();
is(foo.bar, 6, "prototype of the ctor returned from registerElement works");

var protoDesc = Object.getOwnPropertyDescriptor(Foo, "prototype");
is(protoDesc.configurable, false, "proto should be non-configurable");
is(protoDesc.enumerable, false, "proto should be non-enumerable");
is(protoDesc.writable, false, "proto should be non-writable");

// TODO: FIXME!
shouldThrow(function() {
              document.registerElement('x-foo2', {
                prototype: Foo.prototype
              });
            },
            "if proto is an interface prototype object, registerElement should throw",
            "not supported",
            /* todo = */ true);

var nonConfigReadonlyProto = Object.create(HTMLElement.prototype,
  { constructor: { configurable: false, writable: false, value: 42 } });

shouldThrow(function() {
              document.registerElement('x-nonconfig-readonly', {
                prototype: nonConfigReadonlyProto
              });
            },
            "non-configurable and not-writable constructor property",
            "not supported");


// this is not defined in current spec:
var readonlyProto = Object.create(HTMLElement.prototype,
  { constructor: { configurable: true, writable: false, value: 42 } });

var Readonly = document.registerElement('x-nonconfig-readonly', {
  prototype: readonlyProto
});

is(Readonly.prototype, readonlyProto, "configurable readonly constructor property");

var handler = {
  getOwnPropertyDescriptor: function(target, name) {
    return name == "constructor" ? undefined : Object.getOwnPropertyDescriptor(target,name);
  },
  defineProperty: function(target, name, propertyDescriptor) {
     if (name == "constructor") {
      throw "spec this";
    }

    return Object.defineProperty(target, name, propertyDescriptor);
  },
  has: function(target, name) {
    if (name == "constructor") {
      return false;
    }
    return name in target;
  }
};
var proxy = new Proxy({}, handler);

shouldThrow(function() {
              document.registerElement('x-proxymagic', {
                prototype: proxy
              });
            },
            "proxy magic",
            "spec this");

var getOwn = 0;
var defineProp = 0;
var handler2 = {
  getOwnPropertyDescriptor: function(target, name) {
    if (name == "constructor") {
      getOwn++;
    }
    return Object.getOwnPropertyDescriptor(target,name);
  },
  defineProperty: function(target, name, propertyDescriptor) {
    if (name == "constructor") {
      defineProp++;
    }
    return Object.defineProperty(target, name, propertyDescriptor);
  }
};
var proxy2 = new Proxy({}, handler2);

document.registerElement('x-proxymagic2', {
  prototype: proxy2
});

is(getOwn, 1, "number of getOwnPropertyDescriptor calls from registerElement: " + getOwn);
is(defineProp, 1, "number of defineProperty calls from registerElement: " + defineProp);

  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1081037">Mozilla Bug 1081037</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>