OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Teaches dart2js about the wrapping that is done by the Shadow DOM polyfill. | 5 // Teaches dart2js about the wrapping that is done by the Shadow DOM polyfill. |
6 (function() { | 6 (function() { |
7 var ShadowDOMPolyfill = window.ShadowDOMPolyfill; | 7 var ShadowDOMPolyfill = window.ShadowDOMPolyfill; |
8 if (!ShadowDOMPolyfill) return; | 8 if (!ShadowDOMPolyfill) return; |
9 | 9 |
10 if (navigator.userAgent.indexOf('(Dart)') !== -1) { | 10 if (navigator.userAgent.indexOf('(Dart)') !== -1) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 return originalGetTag(obj); | 63 return originalGetTag(obj); |
64 } | 64 } |
65 }); | 65 }); |
66 })(); | 66 })(); |
67 | 67 |
68 // Updates document.registerElement so Dart can see when Javascript custom | 68 // Updates document.registerElement so Dart can see when Javascript custom |
69 // elements are created, and wrap them to provide a Dart friendly API. | 69 // elements are created, and wrap them to provide a Dart friendly API. |
70 (function (doc) { | 70 (function (doc) { |
71 var upgraders = {}; // upgrader associated with a custom-tag. | 71 var upgraders = {}; // upgrader associated with a custom-tag. |
72 var unpatchableTags = {}; // set of custom-tags that can't be patched. | 72 var unpatchableTags = {}; // set of custom-tags that can't be patched. |
| 73 var pendingElements = {}; // will upgrade when/if an upgrader is installed. |
| 74 var upgradeOldElements = true; |
| 75 |
73 var originalRegisterElement = doc.registerElement; | 76 var originalRegisterElement = doc.registerElement; |
74 if (!originalRegisterElement) { | 77 if (!originalRegisterElement) { |
75 throw new Error('document.registerElement is not present.'); | 78 throw new Error('document.registerElement is not present.'); |
76 } | 79 } |
77 | 80 |
78 function reportError(name) { | 81 function reportError(name) { |
79 console.error("Couldn't patch prototype to notify Dart when " + name + | 82 console.error("Couldn't patch prototype to notify Dart when " + name + |
80 " elements are created. This can be fixed by making the " + | 83 " elements are created. This can be fixed by making the " + |
81 "createdCallback in " + name + " a configurable property."); | 84 "createdCallback in " + name + " a configurable property."); |
82 } | 85 } |
83 | 86 |
84 function registerElement(name, options) { | 87 function registerElement(name, options) { |
85 var proto, extendsOption; | 88 var proto, extendsOption; |
86 if (options !== undefined) { | 89 if (options !== undefined) { |
87 proto = options.prototype; | 90 proto = options.prototype; |
88 } else { | 91 } else { |
89 proto = Object.create(HTMLElement.prototype); | 92 proto = Object.create(HTMLElement.prototype); |
90 options = {protoptype: proto}; | 93 options = {protoptype: proto}; |
91 } | 94 } |
92 | 95 |
93 var original = proto.createdCallback; | 96 var original = proto.createdCallback; |
94 var newCallback = function() { | 97 var newCallback = function() { |
95 original.call(this); | 98 original.call(this); |
96 var name = this.getAttribute('is') || this.localName; | 99 var name = (this.getAttribute('is') || this.localName).toLowerCase(); |
97 var upgrader = upgraders[name.toLowerCase()]; | 100 var upgrader = upgraders[name]; |
98 if (upgrader) upgrader(this); | 101 if (upgrader) { |
| 102 upgrader(this); |
| 103 } else if (upgradeOldElements) { |
| 104 // Save this element in case we can upgrade it later when an upgrader is |
| 105 // registered. |
| 106 var list = pendingElements[name]; |
| 107 if (!list) { |
| 108 list = pendingElements[name] = []; |
| 109 } |
| 110 list.push(this); |
| 111 } |
99 }; | 112 }; |
100 | 113 |
101 var descriptor = Object.getOwnPropertyDescriptor(proto, 'createdCallback'); | 114 var descriptor = Object.getOwnPropertyDescriptor(proto, 'createdCallback'); |
102 if (!descriptor || descriptor.writable) { | 115 if (!descriptor || descriptor.writable) { |
103 proto.createdCallback = newCallback; | 116 proto.createdCallback = newCallback; |
104 } else if (descriptor.configurable) { | 117 } else if (descriptor.configurable) { |
105 descriptor['value'] = newCallback; | 118 descriptor['value'] = newCallback; |
106 Object.defineProperty(proto, 'createdCallback', descriptor); | 119 Object.defineProperty(proto, 'createdCallback', descriptor); |
107 } else { | 120 } else { |
108 unpatchableTags[name] = true; | 121 unpatchableTags[name] = true; |
109 if (upgraders[name]) reportError(name); | 122 if (upgraders[name]) reportError(name); |
110 } | 123 } |
111 return originalRegisterElement.call(this, name, options); | 124 return originalRegisterElement.call(this, name, options); |
112 } | 125 } |
113 | 126 |
114 function registerDartTypeUpgrader(name, upgrader) { | 127 function registerDartTypeUpgrader(name, upgrader) { |
115 if (!upgrader) return; | 128 if (!upgrader) return; |
116 name = name.toLowerCase(); | 129 name = name.toLowerCase(); |
117 var existing = upgraders[name]; | 130 var existing = upgraders[name]; |
118 if (existing) { | 131 if (existing) { |
119 console.error('Already have a Dart type associated with ' + name); | 132 console.error('Already have a Dart type associated with ' + name); |
120 return; | 133 return; |
121 } | 134 } |
122 upgraders[name] = upgrader; | 135 upgraders[name] = upgrader; |
123 if (unpatchableTags[name]) reportError(name); | 136 if (unpatchableTags[name]) reportError(name); |
| 137 if (upgradeOldElements) { |
| 138 // Upgrade elements that were created before the upgrader was registered. |
| 139 var list = pendingElements[name]; |
| 140 if (list) { |
| 141 for (var i = 0; i < list.length; i++) { |
| 142 upgrader(list[i]); |
| 143 } |
| 144 } |
| 145 delete pendingElements[name]; |
| 146 } else { |
| 147 console.warn("Didn't expect more Dart types to be registered. '" + name |
| 148 + "' elements that already exist in the page might not be wrapped."); |
| 149 } |
124 } | 150 } |
125 | 151 |
| 152 function onlyUpgradeNewElements() { |
| 153 upgradeOldElements = false; |
| 154 pendingElements = null; |
| 155 } |
126 | 156 |
127 // Native custom elements outside the app in Chrome have constructor | 157 // Native custom elements outside the app in Chrome have constructor |
128 // names like "x-tag", which need to be translated to the DOM | 158 // names like "x-tag", which need to be translated to the DOM |
129 // element they extend. When using the shadow dom polyfill this is | 159 // element they extend. When using the shadow dom polyfill this is |
130 // take care of above. | 160 // take care of above. |
131 var ShadowDOMPolyfill = window.ShadowDOMPolyfill; | 161 var ShadowDOMPolyfill = window.ShadowDOMPolyfill; |
132 if (!ShadowDOMPolyfill) { | 162 if (!ShadowDOMPolyfill) { |
133 // dartNativeDispatchHooksTransformer is described on initHooks() in | 163 // dartNativeDispatchHooksTransformer is described on initHooks() in |
134 // sdk/lib/_internal/lib/native_helper.dart. | 164 // sdk/lib/_internal/lib/native_helper.dart. |
135 if (typeof window.dartNativeDispatchHooksTransformer == 'undefined') | 165 if (typeof window.dartNativeDispatchHooksTransformer == 'undefined') |
136 window.dartNativeDispatchHooksTransformer = []; | 166 window.dartNativeDispatchHooksTransformer = []; |
137 | 167 |
138 window.dartNativeDispatchHooksTransformer.push(function(hooks) { | 168 window.dartNativeDispatchHooksTransformer.push(function(hooks) { |
139 var originalGetUnknownTag = hooks.getUnknownTag; | 169 var originalGetUnknownTag = hooks.getUnknownTag; |
140 hooks.getUnknownTag = function(o, tag) { | 170 hooks.getUnknownTag = function(o, tag) { |
141 if (/-/.test(tag)) { // "x-tag" | 171 if (/-/.test(tag)) { // "x-tag" |
142 var s = Object.prototype.toString.call(o); | 172 var s = Object.prototype.toString.call(o); |
143 var match = s.match(/^\[object ([A-Za-z]*Element)\]$/); | 173 var match = s.match(/^\[object ([A-Za-z]*Element)\]$/); |
144 if (match) { | 174 if (match) { |
145 return match[1]; | 175 return match[1]; |
146 } | 176 } |
147 return originalGetUnknownTag(o, tag); | 177 return originalGetUnknownTag(o, tag); |
148 } | 178 } |
149 }; | 179 }; |
150 }); | 180 }); |
151 } | 181 } |
152 | 182 |
153 doc._registerDartTypeUpgrader = registerDartTypeUpgrader; | 183 doc._registerDartTypeUpgrader = registerDartTypeUpgrader; |
| 184 doc._onlyUpgradeNewElements = onlyUpgradeNewElements; |
154 doc.registerElement = registerElement; | 185 doc.registerElement = registerElement; |
155 })(document); | 186 })(document); |
OLD | NEW |