Chromium Code Reviews| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 obj = unwrapped; | 61 obj = unwrapped; |
| 62 } | 62 } |
| 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 = {}; | 71 var upgraders = {}; // upgrader associated with a custom-tag. |
| 72 var unpatchableTags = {}; // set of custom-tags that can't be patched. | |
| 72 var originalRegisterElement = doc.registerElement; | 73 var originalRegisterElement = doc.registerElement; |
| 73 if (!originalRegisterElement) { | 74 if (!originalRegisterElement) { |
| 74 throw new Error('document.registerElement is not present.'); | 75 throw new Error('document.registerElement is not present.'); |
| 75 } | 76 } |
| 76 | 77 |
| 78 function reportError(name) { | |
| 79 console.error("Couldn't patch prototype to notify Dart when " + name + | |
| 80 " elements are created. This can be fixed by making the " + | |
| 81 "createdCallback in " + name + " a configurable property."); | |
| 82 } | |
| 83 | |
| 77 function registerElement(name, options) { | 84 function registerElement(name, options) { |
| 78 var proto, extendsOption; | 85 var proto, extendsOption; |
| 79 if (options !== undefined) { | 86 if (options !== undefined) { |
| 80 proto = options.prototype; | 87 proto = options.prototype; |
| 81 } else { | 88 } else { |
| 82 proto = Object.create(HTMLElement.prototype); | 89 proto = Object.create(HTMLElement.prototype); |
| 83 options = {protoptype: proto}; | 90 options = {protoptype: proto}; |
| 84 } | 91 } |
| 85 | 92 |
| 86 var original = proto.createdCallback; | 93 var original = proto.createdCallback; |
| 87 var newCallback = function() { | 94 var newCallback = function() { |
| 88 original.call(this); | 95 original.call(this); |
| 89 var name = this.getAttribute('is') || this.localName; | 96 var name = this.getAttribute('is') || this.localName; |
| 90 var upgrader = upgraders[name.toLowerCase()]; | 97 var upgrader = upgraders[name.toLowerCase()]; |
| 91 if (upgrader) upgrader(this); | 98 if (upgrader) upgrader(this); |
| 92 }; | 99 }; |
| 93 | 100 |
| 94 var descriptor = Object.getOwnPropertyDescriptor(proto, 'createdCallback'); | 101 var descriptor = Object.getOwnPropertyDescriptor(proto, 'createdCallback'); |
| 95 if (!descriptor || descriptor.writable) { | 102 if (!descriptor || descriptor.writable) { |
| 96 proto.createdCallback = newCallback; | 103 proto.createdCallback = newCallback; |
| 97 } else if (descriptor.configurable) { | 104 } else if (descriptor.configurable) { |
| 98 descriptor['value'] = newCallback; | 105 descriptor['value'] = newCallback; |
| 99 Object.defineProperty(proto, 'createdCallback', descriptor); | 106 Object.defineProperty(proto, 'createdCallback', descriptor); |
| 100 } else { | 107 } else { |
| 101 console.error("Couldn't patch prototype to notify Dart when " + name + | 108 unpatchableTags[name] = true; |
| 102 " elements are created. This can be fixed by making the " + | 109 if (!!upgraders[name]) reportError(name); |
|
Jennifer Messerly
2014/06/12 00:51:05
I don't think !! is needed here.
Siggi Cherem (dart-lang)
2014/06/12 01:03:23
Done.
| |
| 103 "createdCallback in " + name + " a configurable property."); | |
| 104 } | 110 } |
| 105 return originalRegisterElement.call(this, name, options); | 111 return originalRegisterElement.call(this, name, options); |
| 106 } | 112 } |
| 107 | 113 |
| 108 function registerDartTypeUpgrader(name, upgrader) { | 114 function registerDartTypeUpgrader(name, upgrader) { |
| 109 if (!upgrader) return; | 115 if (!upgrader) return; |
| 110 name = name.toLowerCase(); | 116 name = name.toLowerCase(); |
| 111 var existing = upgraders[name]; | 117 var existing = upgraders[name]; |
| 112 if (existing) { | 118 if (existing) { |
| 113 console.error('Already have a Dart type associated with ' + name); | 119 console.error('Already have a Dart type associated with ' + name); |
| 114 return; | 120 return; |
| 115 } | 121 } |
| 116 upgraders[name] = upgrader; | 122 upgraders[name] = upgrader; |
| 123 if (unpatchableTags[name]) reportError(name); | |
| 117 } | 124 } |
| 118 | 125 |
| 119 | 126 |
| 120 // Native custom elements outside the app in Chrome have constructor | 127 // Native custom elements outside the app in Chrome have constructor |
| 121 // names like "x-tag", which need to be translated to the DOM | 128 // names like "x-tag", which need to be translated to the DOM |
| 122 // element they extend. When using the shadow dom polyfill this is | 129 // element they extend. When using the shadow dom polyfill this is |
| 123 // take care of above. | 130 // take care of above. |
| 124 var ShadowDOMPolyfill = window.ShadowDOMPolyfill; | 131 var ShadowDOMPolyfill = window.ShadowDOMPolyfill; |
| 125 if (!ShadowDOMPolyfill) { | 132 if (!ShadowDOMPolyfill) { |
| 126 // dartNativeDispatchHooksTransformer is described on initHooks() in | 133 // dartNativeDispatchHooksTransformer is described on initHooks() in |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 139 } | 146 } |
| 140 return originalGetUnknownTag(o, tag); | 147 return originalGetUnknownTag(o, tag); |
| 141 } | 148 } |
| 142 }; | 149 }; |
| 143 }); | 150 }); |
| 144 } | 151 } |
| 145 | 152 |
| 146 doc._registerDartTypeUpgrader = registerDartTypeUpgrader; | 153 doc._registerDartTypeUpgrader = registerDartTypeUpgrader; |
| 147 doc.registerElement = registerElement; | 154 doc.registerElement = registerElement; |
| 148 })(document); | 155 })(document); |
| OLD | NEW |