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 (function() { | 5 (function() { |
6 var ShadowDOMPolyfill = window.ShadowDOMPolyfill; | 6 var ShadowDOMPolyfill = window.ShadowDOMPolyfill; |
7 if (!ShadowDOMPolyfill) return; | 7 if (!ShadowDOMPolyfill) return; |
8 | 8 |
9 if (navigator.userAgent.indexOf('(Dart)') !== -1) { | 9 if (navigator.userAgent.indexOf('(Dart)') !== -1) { |
10 console.error("ShadowDOMPolyfill polyfill was loaded in Dartium. This " + | 10 console.error("ShadowDOMPolyfill polyfill was loaded in Dartium. This " + |
11 "will not work. This indicates that Dartium's Chrome version is " + | 11 "will not work. This indicates that Dartium's Chrome version is " + |
12 "not compatible with this version of web_components."); | 12 "not compatible with this version of web_components."); |
13 } | 13 } |
14 | 14 |
15 var needsConstructorFix = window.constructor === window.Window; | 15 var needsConstructorFix = window.constructor === window.Window; |
16 | 16 |
17 // TODO(jmesserly): we need to wrap document somehow (a dart:html hook?) | 17 // TODO(jmesserly): we need to wrap document somehow (a dart:html hook?) |
18 window.dartExperimentalFixupGetTag = function(originalGetTag) { | 18 |
19 // dartNativeDispatchHooksTransformer is described on initHooks() in | |
20 // sdk/lib/_internal/lib/native_helper.dart. | |
21 if (typeof window.dartNativeDispatchHooksTransformer == 'undefined') | |
22 window.dartNativeDispatchHooksTransformer = []; | |
23 | |
24 window.dartNativeDispatchHooksTransformer.push(function(hooks) { | |
19 var NodeList = ShadowDOMPolyfill.wrappers.NodeList; | 25 var NodeList = ShadowDOMPolyfill.wrappers.NodeList; |
20 var ShadowRoot = ShadowDOMPolyfill.wrappers.ShadowRoot; | 26 var ShadowRoot = ShadowDOMPolyfill.wrappers.ShadowRoot; |
21 var unwrapIfNeeded = ShadowDOMPolyfill.unwrapIfNeeded; | 27 var unwrapIfNeeded = ShadowDOMPolyfill.unwrapIfNeeded; |
22 function getTag(obj) { | 28 var originalGetTag = hooks.getTag; |
29 hooks.getTag = function getTag(obj) { | |
23 // TODO(jmesserly): do we still need these? | 30 // TODO(jmesserly): do we still need these? |
24 if (obj instanceof NodeList) return 'NodeList'; | 31 if (obj instanceof NodeList) return 'NodeList'; |
25 if (obj instanceof ShadowRoot) return 'ShadowRoot'; | 32 if (obj instanceof ShadowRoot) return 'ShadowRoot'; |
26 if (MutationRecord && (obj instanceof MutationRecord)) | 33 if (MutationRecord && (obj instanceof MutationRecord)) |
27 return 'MutationRecord'; | 34 return 'MutationRecord'; |
28 if (MutationObserver && (obj instanceof MutationObserver)) | 35 if (MutationObserver && (obj instanceof MutationObserver)) |
29 return 'MutationObserver'; | 36 return 'MutationObserver'; |
30 | 37 |
31 // TODO(jmesserly): this prevents incorrect interaction between ShadowDOM | 38 // TODO(jmesserly): this prevents incorrect interaction between ShadowDOM |
32 // and dart:html's <template> polyfill. Essentially, ShadowDOM is | 39 // and dart:html's <template> polyfill. Essentially, ShadowDOM is |
33 // polyfilling native template, but our Dart polyfill fails to detect this | 40 // polyfilling native template, but our Dart polyfill fails to detect this |
34 // because the unwrapped node is an HTMLUnknownElement, leading it to | 41 // because the unwrapped node is an HTMLUnknownElement, leading it to |
35 // think the node has no content. | 42 // think the node has no content. |
36 if (obj instanceof HTMLTemplateElement) return 'HTMLTemplateElement'; | 43 if (obj instanceof HTMLTemplateElement) return 'HTMLTemplateElement'; |
37 | 44 |
38 var unwrapped = unwrapIfNeeded(obj); | 45 var unwrapped = unwrapIfNeeded(obj); |
39 if (unwrapped && (needsConstructorFix || obj !== unwrapped)) { | 46 if (unwrapped && (needsConstructorFix || obj !== unwrapped)) { |
40 // Fix up class names for Firefox, or if using the minified polyfill. | 47 // Fix up class names for Firefox, or if using the minified polyfill. |
41 // dart2js prefers .constructor.name, but there are all kinds of cases | 48 // dart2js prefers .constructor.name, but there are all kinds of cases |
42 // where this will give the wrong answer. | 49 // where this will give the wrong answer. |
43 var ctor = obj.constructor | 50 var ctor = obj.constructor |
44 if (ctor === unwrapped.constructor) { | 51 if (ctor === unwrapped.constructor) { |
45 var name = ctor._ShadowDOMPolyfill$cacheTag_; | 52 var name = ctor._ShadowDOMPolyfill$cacheTag_; |
46 if (!name) { | 53 if (!name) { |
47 name = Object.prototype.toString.call(unwrapped); | 54 name = originalGetTag(unwrapped); |
Jennifer Messerly
2014/05/13 21:01:31
Interesting! Do we still need ctor._ShadowDOMPolyf
Siggi Cherem (dart-lang)
2014/05/13 21:06:58
+1, lgtm, if we don't need the caching, I'm happy
Siggi Cherem (dart-lang)
2014/05/13 21:55:21
FYI - we chatted more about it offline and decided
Jennifer Messerly
2014/05/13 22:12:36
interesting. yeah, seems okay, but makes me wonder
| |
48 name = name.substring(8, name.length - 1); | |
49 ctor._ShadowDOMPolyfill$cacheTag_ = name; | 55 ctor._ShadowDOMPolyfill$cacheTag_ = name; |
50 } | 56 } |
51 return name; | 57 return name; |
52 } | 58 } |
53 | 59 |
54 obj = unwrapped; | 60 obj = unwrapped; |
55 } | 61 } |
56 return originalGetTag(obj); | 62 return originalGetTag(obj); |
57 } | 63 } |
58 | 64 }); |
59 return getTag; | |
60 }; | |
61 })(); | 65 })(); |
OLD | NEW |