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