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 (function() { | 6 (function() { |
6 var ShadowDOMPolyfill = window.ShadowDOMPolyfill; | 7 var ShadowDOMPolyfill = window.ShadowDOMPolyfill; |
7 if (!ShadowDOMPolyfill) return; | 8 if (!ShadowDOMPolyfill) return; |
8 | 9 |
9 if (navigator.userAgent.indexOf('(Dart)') !== -1) { | 10 if (navigator.userAgent.indexOf('(Dart)') !== -1) { |
10 console.error("ShadowDOMPolyfill polyfill was loaded in Dartium. This " + | 11 console.error("ShadowDOMPolyfill polyfill was loaded in Dartium. This " + |
11 "will not work. This indicates that Dartium's Chrome version is " + | 12 "will not work. This indicates that Dartium's Chrome version is " + |
12 "not compatible with this version of web_components."); | 13 "not compatible with this version of web_components."); |
13 } | 14 } |
14 | 15 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 } | 57 } |
57 return name; | 58 return name; |
58 } | 59 } |
59 | 60 |
60 obj = unwrapped; | 61 obj = unwrapped; |
61 } | 62 } |
62 return originalGetTag(obj); | 63 return originalGetTag(obj); |
63 } | 64 } |
64 }); | 65 }); |
65 })(); | 66 })(); |
| 67 |
| 68 // Updates document.registerElement so Dart can see when Javascript custom |
| 69 // elements are created, and wrap them to provide a Dart friendly API. |
| 70 (function (doc) { |
| 71 var upgraders = {}; |
| 72 var originalRegisterElement = doc.registerElement; |
| 73 if (!originalRegisterElement) { |
| 74 throw new Error('document.registerElement is not present.'); |
| 75 } |
| 76 |
| 77 function registerElement(name, options) { |
| 78 var proto, extendsOption; |
| 79 if (options !== undefined) { |
| 80 proto = options.prototype; |
| 81 } else { |
| 82 proto = Object.create(HTMLElement.prototype); |
| 83 options = {protoptype: proto}; |
| 84 } |
| 85 |
| 86 var original = proto.createdCallback; |
| 87 var newCallback = function() { |
| 88 original.call(this); |
| 89 var name = this.getAttribute('is') || this.localName; |
| 90 var upgrader = upgraders[name.toLowerCase()]; |
| 91 if (upgrader) upgrader(this); |
| 92 }; |
| 93 |
| 94 var descriptor = Object.getOwnPropertyDescriptor(proto, 'createdCallback'); |
| 95 if (!descriptor || descriptor.writable) { |
| 96 proto.createdCallback = newCallback; |
| 97 } else if (descriptor.configurable) { |
| 98 descriptor['value'] = newCallback; |
| 99 Object.defineProperty(proto, 'createdCallback', descriptor); |
| 100 } else { |
| 101 console.error("Couldn't patch prototype to notify Dart when " + name + |
| 102 " elements are created. This can be fixed by making the " + |
| 103 "createdCallback in " + name + " a configurable property."); |
| 104 } |
| 105 return originalRegisterElement.call(this, name, options); |
| 106 } |
| 107 |
| 108 function registerDartTypeUpgrader(name, upgrader) { |
| 109 if (!upgrader) return; |
| 110 name = name.toLowerCase(); |
| 111 var existing = upgraders[name]; |
| 112 if (existing) { |
| 113 console.error('Already have a Dart type associated with ' + name); |
| 114 return; |
| 115 } |
| 116 upgraders[name] = upgrader; |
| 117 } |
| 118 |
| 119 doc._registerDartTypeUpgrader = registerDartTypeUpgrader; |
| 120 doc.registerElement = registerElement; |
| 121 })(document); |
OLD | NEW |