| 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. | |
| 6 (function() { | |
| 7 var ShadowDOMPolyfill = window.ShadowDOMPolyfill; | |
| 8 if (!ShadowDOMPolyfill) return; | |
| 9 | |
| 10 // TODO(sigmund): remove the userAgent check once 1.6 rolls as stable. | |
| 11 // See: dartbug.com/18463 | |
| 12 if (navigator.dartEnabled || (navigator.userAgent.indexOf('(Dart)') !== -1)) { | |
| 13 console.error("ShadowDOMPolyfill polyfill was loaded in Dartium. This " + | |
| 14 "will not work. This indicates that Dartium's Chrome version is " + | |
| 15 "not compatible with this version of web_components."); | |
| 16 } | |
| 17 | |
| 18 var needsConstructorFix = window.constructor === window.Window; | |
| 19 | |
| 20 // TODO(jmesserly): we need to wrap document somehow (a dart:html hook?) | |
| 21 | |
| 22 // dartNativeDispatchHooksTransformer is described on initHooks() in | |
| 23 // sdk/lib/_internal/lib/native_helper.dart. | |
| 24 if (typeof window.dartNativeDispatchHooksTransformer == 'undefined') | |
| 25 window.dartNativeDispatchHooksTransformer = []; | |
| 26 | |
| 27 window.dartNativeDispatchHooksTransformer.push(function(hooks) { | |
| 28 var NodeList = ShadowDOMPolyfill.wrappers.NodeList; | |
| 29 var ShadowRoot = ShadowDOMPolyfill.wrappers.ShadowRoot; | |
| 30 var unwrapIfNeeded = ShadowDOMPolyfill.unwrapIfNeeded; | |
| 31 var originalGetTag = hooks.getTag; | |
| 32 hooks.getTag = function getTag(obj) { | |
| 33 // TODO(jmesserly): do we still need these? | |
| 34 if (obj instanceof NodeList) return 'NodeList'; | |
| 35 if (obj instanceof ShadowRoot) return 'ShadowRoot'; | |
| 36 if (MutationRecord && (obj instanceof MutationRecord)) | |
| 37 return 'MutationRecord'; | |
| 38 if (MutationObserver && (obj instanceof MutationObserver)) | |
| 39 return 'MutationObserver'; | |
| 40 | |
| 41 // TODO(jmesserly): this prevents incorrect interaction between ShadowDOM | |
| 42 // and dart:html's <template> polyfill. Essentially, ShadowDOM is | |
| 43 // polyfilling native template, but our Dart polyfill fails to detect this | |
| 44 // because the unwrapped node is an HTMLUnknownElement, leading it to | |
| 45 // think the node has no content. | |
| 46 if (obj instanceof HTMLTemplateElement) return 'HTMLTemplateElement'; | |
| 47 | |
| 48 var unwrapped = unwrapIfNeeded(obj); | |
| 49 if (unwrapped && (needsConstructorFix || obj !== unwrapped)) { | |
| 50 // Fix up class names for Firefox, or if using the minified polyfill. | |
| 51 // dart2js prefers .constructor.name, but there are all kinds of cases | |
| 52 // where this will give the wrong answer. | |
| 53 var ctor = obj.constructor | |
| 54 if (ctor === unwrapped.constructor) { | |
| 55 var name = ctor._ShadowDOMPolyfill$cacheTag_; | |
| 56 if (!name) { | |
| 57 name = originalGetTag(unwrapped); | |
| 58 ctor._ShadowDOMPolyfill$cacheTag_ = name; | |
| 59 } | |
| 60 return name; | |
| 61 } | |
| 62 | |
| 63 obj = unwrapped; | |
| 64 } | |
| 65 return originalGetTag(obj); | |
| 66 } | |
| 67 }); | |
| 68 })(); | |
| 69 | |
| 70 // Updates document.registerElement so Dart can see when Javascript custom | 5 // Updates document.registerElement so Dart can see when Javascript custom |
| 71 // elements are created, and wrap them to provide a Dart friendly API. | 6 // elements are created, and wrap them to provide a Dart friendly API. |
| 72 (function (doc) { | 7 (function (doc) { |
| 8 if (window._dart_register_element_interop_support) return; |
| 9 window._dart_register_element_interop_support = true; |
| 10 |
| 73 var upgraders = {}; // upgrader associated with a custom-tag. | 11 var upgraders = {}; // upgrader associated with a custom-tag. |
| 74 var unpatchableTags = {}; // set of custom-tags that can't be patched. | 12 var unpatchableTags = {}; // set of custom-tags that can't be patched. |
| 75 var pendingElements = {}; // will upgrade when/if an upgrader is installed. | 13 var pendingElements = {}; // will upgrade when/if an upgrader is installed. |
| 76 var upgradeOldElements = true; | 14 var upgradeOldElements = true; |
| 77 | 15 |
| 78 var originalRegisterElement = doc.registerElement; | 16 var originalRegisterElement = doc.registerElement; |
| 79 if (!originalRegisterElement) { | 17 if (!originalRegisterElement) { |
| 80 throw new Error('document.registerElement is not present.'); | 18 throw new Error('document.registerElement is not present.'); |
| 81 } | 19 } |
| 82 | 20 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 } | 90 } |
| 153 | 91 |
| 154 function onlyUpgradeNewElements() { | 92 function onlyUpgradeNewElements() { |
| 155 upgradeOldElements = false; | 93 upgradeOldElements = false; |
| 156 pendingElements = null; | 94 pendingElements = null; |
| 157 } | 95 } |
| 158 | 96 |
| 159 // Native custom elements outside the app in Chrome have constructor | 97 // Native custom elements outside the app in Chrome have constructor |
| 160 // names like "x-tag", which need to be translated to the DOM | 98 // names like "x-tag", which need to be translated to the DOM |
| 161 // element they extend. When using the shadow dom polyfill this is | 99 // element they extend. When using the shadow dom polyfill this is |
| 162 // take care of above. | 100 // taken care of in dart_support.js. |
| 163 var ShadowDOMPolyfill = window.ShadowDOMPolyfill; | 101 var ShadowDOMPolyfill = window.ShadowDOMPolyfill; |
| 164 if (!ShadowDOMPolyfill) { | 102 if (!ShadowDOMPolyfill) { |
| 165 // dartNativeDispatchHooksTransformer is described on initHooks() in | 103 // dartNativeDispatchHooksTransformer is described on initHooks() in |
| 166 // sdk/lib/_internal/lib/native_helper.dart. | 104 // sdk/lib/_internal/lib/native_helper.dart. |
| 167 if (typeof window.dartNativeDispatchHooksTransformer == 'undefined') | 105 if (typeof window.dartNativeDispatchHooksTransformer == 'undefined') |
| 168 window.dartNativeDispatchHooksTransformer = []; | 106 window.dartNativeDispatchHooksTransformer = []; |
| 169 | 107 |
| 170 window.dartNativeDispatchHooksTransformer.push(function(hooks) { | 108 window.dartNativeDispatchHooksTransformer.push(function(hooks) { |
| 171 var originalGetUnknownTag = hooks.getUnknownTag; | 109 var originalGetUnknownTag = hooks.getUnknownTag; |
| 172 hooks.getUnknownTag = function(o, tag) { | 110 hooks.getUnknownTag = function(o, tag) { |
| 173 if (/-/.test(tag)) { // "x-tag" | 111 if (/-/.test(tag)) { // "x-tag" |
| 174 var s = Object.prototype.toString.call(o); | 112 var s = Object.prototype.toString.call(o); |
| 175 var match = s.match(/^\[object ([A-Za-z]*Element)\]$/); | 113 var match = s.match(/^\[object ([A-Za-z]*Element)\]$/); |
| 176 if (match) { | 114 if (match) { |
| 177 return match[1]; | 115 return match[1]; |
| 178 » } | 116 } |
| 179 return originalGetUnknownTag(o, tag); | 117 return originalGetUnknownTag(o, tag); |
| 180 } | 118 } |
| 181 }; | 119 }; |
| 182 }); | 120 }); |
| 183 } | 121 } |
| 184 | 122 |
| 185 doc._registerDartTypeUpgrader = registerDartTypeUpgrader; | 123 doc._registerDartTypeUpgrader = registerDartTypeUpgrader; |
| 186 doc._onlyUpgradeNewElements = onlyUpgradeNewElements; | 124 doc._onlyUpgradeNewElements = onlyUpgradeNewElements; |
| 187 doc.registerElement = registerElement; | 125 doc.registerElement = registerElement; |
| 188 })(document); | 126 })(document); |
| OLD | NEW |