| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of _js_helper; | 5 part of _js_helper; |
| 6 | 6 |
| 7 | 7 |
| 8 // TODO(ngeoffray): stop using this method once our optimizers can | 8 // TODO(ngeoffray): stop using this method once our optimizers can |
| 9 // change str1.contains(str2) into str1.indexOf(str2) != -1. | 9 // change str1.contains(str2) into str1.indexOf(str2) != -1. |
| 10 bool contains(String userAgent, String name) { | 10 bool contains(String userAgent, String name) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 void propertySet(var object, String property, var value) { | 34 void propertySet(var object, String property, var value) { |
| 35 JS('var', '#[#] = #', object, property, value); | 35 JS('var', '#[#] = #', object, property, value); |
| 36 } | 36 } |
| 37 | 37 |
| 38 getPropertyFromPrototype(var object, String name) { | 38 getPropertyFromPrototype(var object, String name) { |
| 39 return JS('var', 'Object.getPrototypeOf(#)[#]', object, name); | 39 return JS('var', 'Object.getPrototypeOf(#)[#]', object, name); |
| 40 } | 40 } |
| 41 | 41 |
| 42 /** | 42 /** |
| 43 * Returns a String tag identifying the type of the native object, or `null`. | |
| 44 * The tag is not the name of the type, but usually the name of the JavaScript | |
| 45 * constructor function. Initialized by [initHooks]. | |
| 46 */ | |
| 47 Function getTagFunction; | |
| 48 | |
| 49 /** | |
| 50 * If a lookup via [getTagFunction] on an object [object] that has [tag] fails, | |
| 51 * this function is called to provide an alternate tag. This allows us to fail | |
| 52 * gracefully if we can make a good guess, for example, when browsers add novel | |
| 53 * kinds of HTMLElement that we have never heard of. Initialized by | |
| 54 * [initHooks]. | |
| 55 */ | |
| 56 Function alternateTagFunction; | |
| 57 | |
| 58 /** | |
| 59 * Returns the prototype for the JavaScript constructor named by an input tag. | |
| 60 * Returns `null` if there is no such constructor, or if pre-patching of the | |
| 61 * constructor is to be avoided. Initialized by [initHooks]. | |
| 62 */ | |
| 63 Function prototypeForTagFunction; | |
| 64 | |
| 65 | |
| 66 String toStringForNativeObject(var obj) { | |
| 67 // TODO(sra): Is this code dead? | |
| 68 // [getTagFunction] might be uninitialized, but in usual usage, toString has | |
| 69 // been called via an interceptor and initialized it. | |
| 70 String name = getTagFunction == null | |
| 71 ? '<Unknown>' | |
| 72 : JS('String', '#', getTagFunction(obj)); | |
| 73 return 'Instance of $name'; | |
| 74 } | |
| 75 | |
| 76 int hashCodeForNativeObject(object) => Primitives.objectHashCode(object); | |
| 77 | |
| 78 /** | |
| 79 * Sets a JavaScript property on an object. | 43 * Sets a JavaScript property on an object. |
| 80 */ | 44 */ |
| 81 void defineProperty(var obj, String property, var value) { | 45 void defineProperty(var obj, String property, var value) { |
| 82 JS('void', | 46 JS('void', |
| 83 'Object.defineProperty(#, #, ' | 47 'Object.defineProperty(#, #, ' |
| 84 '{value: #, enumerable: false, writable: true, configurable: true})', | 48 '{value: #, enumerable: false, writable: true, configurable: true})', |
| 85 obj, | 49 obj, |
| 86 property, | 50 property, |
| 87 value); | 51 value); |
| 88 } | 52 } |
| 89 | |
| 90 | |
| 91 // Is [obj] an instance of a Dart-defined class? | |
| 92 bool isDartObject(obj) { | |
| 93 // Some of the extra parens here are necessary. | |
| 94 return JS('bool', '((#) instanceof (#))', obj, JS_DART_OBJECT_CONSTRUCTOR()); | |
| 95 } | |
| 96 | |
| 97 /** | |
| 98 * A JavaScript object mapping tags to the constructors of interceptors. | |
| 99 * This is a JavaScript object with no prototype. | |
| 100 * | |
| 101 * Example: 'HTMLImageElement' maps to the ImageElement class constructor. | |
| 102 */ | |
| 103 get interceptorsByTag => JS_EMBEDDED_GLOBAL('=Object', INTERCEPTORS_BY_TAG); | |
| 104 | |
| 105 /** | |
| 106 * A JavaScript object mapping tags to `true` or `false`. | |
| 107 * | |
| 108 * Example: 'HTMLImageElement' maps to `true` since, as there are no subclasses | |
| 109 * of ImageElement, it is a leaf class in the native class hierarchy. | |
| 110 */ | |
| 111 get leafTags => JS_EMBEDDED_GLOBAL('=Object', LEAF_TAGS); | |
| 112 | |
| 113 String findDispatchTagForInterceptorClass(interceptorClassConstructor) { | |
| 114 return JS('', r'#.#', | |
| 115 interceptorClassConstructor, NATIVE_SUPERCLASS_TAG_NAME); | |
| 116 } | |
| 117 | |
| 118 /** | |
| 119 * Cache of dispatch records for instances. This is a JavaScript object used as | |
| 120 * a map. Keys are instance tags, e.g. "!SomeThing". The cache permits the | |
| 121 * sharing of one dispatch record between multiple instances. | |
| 122 */ | |
| 123 var dispatchRecordsForInstanceTags; | |
| 124 | |
| 125 /** | |
| 126 * Cache of interceptors indexed by uncacheable tags, e.g. "~SomeThing". | |
| 127 * This is a JavaScript object used as a map. | |
| 128 */ | |
| 129 var interceptorsForUncacheableTags; | |
| 130 | |
| 131 | |
| 132 lookupInterceptor(String tag) { | |
| 133 return propertyGet(interceptorsByTag, tag); | |
| 134 } | |
| 135 | |
| 136 | |
| 137 // Dispatch tag marks are optional prefixes for a dispatch tag that direct how | |
| 138 // the interceptor for the tag may be cached. | |
| 139 | |
| 140 /// No caching permitted. | |
| 141 const UNCACHED_MARK = '~'; | |
| 142 | |
| 143 /// Dispatch record must be cached per instance | |
| 144 const INSTANCE_CACHED_MARK = '!'; | |
| 145 | |
| 146 /// Dispatch record is cached on immediate prototype. | |
| 147 const LEAF_MARK = '-'; | |
| 148 | |
| 149 /// Dispatch record is cached on immediate prototype with a prototype | |
| 150 /// verification to prevent the interceptor being associated with a subclass | |
| 151 /// before a dispatch record is cached on the subclass. | |
| 152 const INTERIOR_MARK = '+'; | |
| 153 | |
| 154 /// A 'discriminator' function is to be used. TBD. | |
| 155 const DISCRIMINATED_MARK = '*'; | |
| 156 | |
| 157 | |
| 158 /** | |
| 159 * Returns the interceptor for a native object, or returns `null` if not found. | |
| 160 * | |
| 161 * A dispatch record is cached according to the specification of the dispatch | |
| 162 * tag for [obj]. | |
| 163 */ | |
| 164 lookupAndCacheInterceptor(obj) { | |
| 165 assert(!isDartObject(obj)); | |
| 166 String tag = getTagFunction(obj); | |
| 167 | |
| 168 // Fast path for instance (and uncached) tags because the lookup is repeated | |
| 169 // for each instance (or getInterceptor call). | |
| 170 var record = propertyGet(dispatchRecordsForInstanceTags, tag); | |
| 171 if (record != null) return patchInstance(obj, record); | |
| 172 var interceptor = propertyGet(interceptorsForUncacheableTags, tag); | |
| 173 if (interceptor != null) return interceptor; | |
| 174 | |
| 175 // This lookup works for derived dispatch tags because we add them all in | |
| 176 // [initNativeDispatch]. | |
| 177 var interceptorClass = lookupInterceptor(tag); | |
| 178 if (interceptorClass == null) { | |
| 179 tag = alternateTagFunction(obj, tag); | |
| 180 if (tag != null) { | |
| 181 // Fast path for instance and uncached tags again. | |
| 182 record = propertyGet(dispatchRecordsForInstanceTags, tag); | |
| 183 if (record != null) return patchInstance(obj, record); | |
| 184 interceptor = propertyGet(interceptorsForUncacheableTags, tag); | |
| 185 if (interceptor != null) return interceptor; | |
| 186 | |
| 187 interceptorClass = lookupInterceptor(tag); | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 if (interceptorClass == null) { | |
| 192 // This object is not known to Dart. There could be several reasons for | |
| 193 // that, including (but not limited to): | |
| 194 // | |
| 195 // * A bug in native code (hopefully this is caught during development). | |
| 196 // * An unknown DOM object encountered. | |
| 197 // * JavaScript code running in an unexpected context. For example, on | |
| 198 // node.js. | |
| 199 return null; | |
| 200 } | |
| 201 | |
| 202 interceptor = JS('', '#.prototype', interceptorClass); | |
| 203 | |
| 204 var mark = JS('String|Null', '#[0]', tag); | |
| 205 | |
| 206 if (mark == INSTANCE_CACHED_MARK) { | |
| 207 record = makeLeafDispatchRecord(interceptor); | |
| 208 propertySet(dispatchRecordsForInstanceTags, tag, record); | |
| 209 return patchInstance(obj, record); | |
| 210 } | |
| 211 | |
| 212 if (mark == UNCACHED_MARK) { | |
| 213 propertySet(interceptorsForUncacheableTags, tag, interceptor); | |
| 214 return interceptor; | |
| 215 } | |
| 216 | |
| 217 if (mark == LEAF_MARK) { | |
| 218 return patchProto(obj, makeLeafDispatchRecord(interceptor)); | |
| 219 } | |
| 220 | |
| 221 if (mark == INTERIOR_MARK) { | |
| 222 return patchInteriorProto(obj, interceptor); | |
| 223 } | |
| 224 | |
| 225 if (mark == DISCRIMINATED_MARK) { | |
| 226 // TODO(sra): Use discriminator of tag. | |
| 227 throw new UnimplementedError(tag); | |
| 228 } | |
| 229 | |
| 230 // [tag] was not explicitly an interior or leaf tag, so | |
| 231 var isLeaf = JS('bool', '(#[#]) === true', leafTags, tag); | |
| 232 if (isLeaf) { | |
| 233 return patchProto(obj, makeLeafDispatchRecord(interceptor)); | |
| 234 } else { | |
| 235 return patchInteriorProto(obj, interceptor); | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 patchInstance(obj, record) { | |
| 240 setDispatchProperty(obj, record); | |
| 241 return dispatchRecordInterceptor(record); | |
| 242 } | |
| 243 | |
| 244 patchProto(obj, record) { | |
| 245 setDispatchProperty(JS('', 'Object.getPrototypeOf(#)', obj), record); | |
| 246 return dispatchRecordInterceptor(record); | |
| 247 } | |
| 248 | |
| 249 patchInteriorProto(obj, interceptor) { | |
| 250 var proto = JS('', 'Object.getPrototypeOf(#)', obj); | |
| 251 var record = makeDispatchRecord(interceptor, proto, null, null); | |
| 252 setDispatchProperty(proto, record); | |
| 253 return interceptor; | |
| 254 } | |
| 255 | |
| 256 | |
| 257 makeLeafDispatchRecord(interceptor) { | |
| 258 var fieldName = JS_IS_INDEXABLE_FIELD_NAME(); | |
| 259 bool indexability = JS('bool', r'!!#[#]', interceptor, fieldName); | |
| 260 return makeDispatchRecord(interceptor, false, null, indexability); | |
| 261 } | |
| 262 | |
| 263 makeDefaultDispatchRecord(tag, interceptorClass, proto) { | |
| 264 var interceptor = JS('', '#.prototype', interceptorClass); | |
| 265 var isLeaf = JS('bool', '(#[#]) === true', leafTags, tag); | |
| 266 if (isLeaf) { | |
| 267 return makeLeafDispatchRecord(interceptor); | |
| 268 } else { | |
| 269 return makeDispatchRecord(interceptor, proto, null, null); | |
| 270 } | |
| 271 } | |
| 272 | |
| 273 /** | |
| 274 * [proto] should have no shadowing prototypes that are not also assigned a | |
| 275 * dispatch rescord. | |
| 276 */ | |
| 277 setNativeSubclassDispatchRecord(proto, interceptor) { | |
| 278 setDispatchProperty(proto, makeLeafDispatchRecord(interceptor)); | |
| 279 } | |
| 280 | |
| 281 String constructorNameFallback(object) { | |
| 282 return JS('String', '#(#)', _constructorNameFallback, object); | |
| 283 } | |
| 284 | |
| 285 | |
| 286 var initNativeDispatchFlag; // null or true | |
| 287 | |
| 288 void initNativeDispatch() { | |
| 289 if (true == initNativeDispatchFlag) return; | |
| 290 initNativeDispatchFlag = true; | |
| 291 initNativeDispatchContinue(); | |
| 292 } | |
| 293 | |
| 294 void initNativeDispatchContinue() { | |
| 295 | |
| 296 dispatchRecordsForInstanceTags = JS('', 'Object.create(null)'); | |
| 297 interceptorsForUncacheableTags = JS('', 'Object.create(null)'); | |
| 298 | |
| 299 initHooks(); | |
| 300 | |
| 301 // Try to pro-actively patch prototypes of DOM objects. For each of our known | |
| 302 // tags `TAG`, if `window.TAG` is a (constructor) function, set the dispatch | |
| 303 // property if the function's prototype to a dispatch record. | |
| 304 var map = interceptorsByTag; | |
| 305 var tags = JS('JSMutableArray', 'Object.getOwnPropertyNames(#)', map); | |
| 306 | |
| 307 if (JS('bool', 'typeof window != "undefined"')) { | |
| 308 var context = JS('=Object', 'window'); | |
| 309 var fun = JS('=Object', 'function () {}'); | |
| 310 for (int i = 0; i < tags.length; i++) { | |
| 311 var tag = tags[i]; | |
| 312 var proto = prototypeForTagFunction(tag); | |
| 313 if (proto != null) { | |
| 314 var interceptorClass = JS('', '#[#]', map, tag); | |
| 315 var record = makeDefaultDispatchRecord(tag, interceptorClass, proto); | |
| 316 if (record != null) { | |
| 317 setDispatchProperty(proto, record); | |
| 318 // Ensure the modified prototype is still fast by assigning it to | |
| 319 // the prototype property of a function object. | |
| 320 JS('', '#.prototype = #', fun, proto); | |
| 321 } | |
| 322 } | |
| 323 } | |
| 324 } | |
| 325 | |
| 326 // [interceptorsByTag] maps 'plain' dispatch tags. Add all the derived | |
| 327 // dispatch tags to simplify lookup of derived tags. | |
| 328 for (int i = 0; i < tags.length; i++) { | |
| 329 var tag = JS('String', '#[#]', tags, i); | |
| 330 if (JS('bool', '/^[A-Za-z_]/.test(#)', tag)) { | |
| 331 var interceptorClass = propertyGet(map, tag); | |
| 332 propertySet(map, INSTANCE_CACHED_MARK + tag, interceptorClass); | |
| 333 propertySet(map, UNCACHED_MARK + tag, interceptorClass); | |
| 334 propertySet(map, LEAF_MARK + tag, interceptorClass); | |
| 335 propertySet(map, INTERIOR_MARK + tag, interceptorClass); | |
| 336 propertySet(map, DISCRIMINATED_MARK + tag, interceptorClass); | |
| 337 } | |
| 338 } | |
| 339 } | |
| 340 | |
| 341 | |
| 342 /** | |
| 343 * Initializes [getTagFunction] and [alternateTagFunction]. | |
| 344 * | |
| 345 * These functions are 'hook functions', collectively 'hooks'. They initialized | |
| 346 * by applying a series of hooks transformers. Built-in hooks transformers deal | |
| 347 * with various known browser behaviours. | |
| 348 * | |
| 349 * Each hook tranformer takes a 'hooks' input which is a JavaScript object | |
| 350 * containing the hook functions, and returns the same or a new object with | |
| 351 * replacements. The replacements can wrap the originals to provide alternate | |
| 352 * or modified behaviour. | |
| 353 * | |
| 354 * { getTag: function(obj) {...}, | |
| 355 * getUnknownTag: function(obj, tag) {...}, | |
| 356 * prototypeForTag: function(tag) {...}, | |
| 357 * discriminator: function(tag) {...}, | |
| 358 * } | |
| 359 * | |
| 360 * * getTag(obj) returns the dispatch tag, or `null`. | |
| 361 * * getUnknownTag(obj, tag) returns a tag when [getTag] fails. | |
| 362 * * prototypeForTag(tag) returns the prototype of the constructor for tag, | |
| 363 * or `null` if not available or prepatching is undesirable. | |
| 364 * * discriminator(tag) returns a function TBD. | |
| 365 * | |
| 366 * The web site can adapt a dart2js application by loading code ahead of the | |
| 367 * dart2js application that defines hook transformers to be after the built in | |
| 368 * ones. Code defining a transformer HT should use the following pattern to | |
| 369 * ensure multiple transformers can be composed: | |
| 370 * | |
| 371 * (dartNativeDispatchHooksTransformer = | |
| 372 * window.dartNativeDispatchHooksTransformer || []).push(HT); | |
| 373 * | |
| 374 * | |
| 375 * TODO: Implement and describe dispatch tags and their caching methods. | |
| 376 */ | |
| 377 void initHooks() { | |
| 378 // The initial simple hooks: | |
| 379 var hooks = JS('', '#()', _baseHooks); | |
| 380 | |
| 381 // Customize for browsers where `object.constructor.name` fails: | |
| 382 var _fallbackConstructorHooksTransformer = | |
| 383 JS('', '#(#)', _fallbackConstructorHooksTransformerGenerator, | |
| 384 _constructorNameFallback); | |
| 385 hooks = applyHooksTransformer(_fallbackConstructorHooksTransformer, hooks); | |
| 386 | |
| 387 // Customize for browsers: | |
| 388 hooks = applyHooksTransformer(_firefoxHooksTransformer, hooks); | |
| 389 hooks = applyHooksTransformer(_ieHooksTransformer, hooks); | |
| 390 hooks = applyHooksTransformer(_operaHooksTransformer, hooks); | |
| 391 hooks = applyHooksTransformer(_safariHooksTransformer, hooks); | |
| 392 | |
| 393 hooks = applyHooksTransformer(_fixDocumentHooksTransformer, hooks); | |
| 394 | |
| 395 // TODO(sra): Update ShadowDOM polyfil to use | |
| 396 // [dartNativeDispatchHooksTransformer] and remove this hook. | |
| 397 hooks = applyHooksTransformer(_dartExperimentalFixupGetTagHooksTransformer, | |
| 398 hooks); | |
| 399 | |
| 400 // Apply global hooks. | |
| 401 // | |
| 402 // If defined, dartNativeDispatchHookdTransformer should be a single function | |
| 403 // of a JavaScript Array of functions. | |
| 404 | |
| 405 if (JS('bool', 'typeof dartNativeDispatchHooksTransformer != "undefined"')) { | |
| 406 var transformers = JS('', 'dartNativeDispatchHooksTransformer'); | |
| 407 if (JS('bool', 'typeof # == "function"', transformers)) { | |
| 408 transformers = [transformers]; | |
| 409 } | |
| 410 if (JS('bool', '#.constructor == Array', transformers)) { | |
| 411 for (int i = 0; i < JS('int', '#.length', transformers); i++) { | |
| 412 var transformer = JS('', '#[#]', transformers, i); | |
| 413 if (JS('bool', 'typeof # == "function"', transformer)) { | |
| 414 hooks = applyHooksTransformer(transformer, hooks); | |
| 415 } | |
| 416 } | |
| 417 } | |
| 418 } | |
| 419 | |
| 420 var getTag = JS('', '#.getTag', hooks); | |
| 421 var getUnknownTag = JS('', '#.getUnknownTag', hooks); | |
| 422 var prototypeForTag = JS('', '#.prototypeForTag', hooks); | |
| 423 | |
| 424 getTagFunction = (o) => JS('String|Null', '#(#)', getTag, o); | |
| 425 alternateTagFunction = | |
| 426 (o, String tag) => JS('String|Null', '#(#, #)', getUnknownTag, o, tag); | |
| 427 prototypeForTagFunction = | |
| 428 (String tag) => JS('', '#(#)', prototypeForTag, tag); | |
| 429 } | |
| 430 | |
| 431 applyHooksTransformer(transformer, hooks) { | |
| 432 var newHooks = JS('=Object|Null', '#(#)', transformer, hooks); | |
| 433 return JS('', '# || #', newHooks, hooks); | |
| 434 } | |
| 435 | |
| 436 // JavaScript code fragments. | |
| 437 // | |
| 438 // This is a temporary place for the JavaScript code. | |
| 439 // | |
| 440 // TODO(sra): These code fragments are not minified. They could be generated by | |
| 441 // the code emitter, or JS_CONST could be improved to parse entire functions and | |
| 442 // take care of the minification. | |
| 443 | |
| 444 const _baseHooks = const JS_CONST(r''' | |
| 445 function() { | |
| 446 function typeNameInChrome(o) { | |
| 447 var constructor = o.constructor; | |
| 448 if (constructor) { | |
| 449 var name = constructor.name; | |
| 450 if (name) return name; | |
| 451 } | |
| 452 var s = Object.prototype.toString.call(o); | |
| 453 return s.substring(8, s.length - 1); | |
| 454 } | |
| 455 function getUnknownTag(object, tag) { | |
| 456 // This code really belongs in [getUnknownTagGenericBrowser] but having it | |
| 457 // here allows [getUnknownTag] to be tested on d8. | |
| 458 if (/^HTML[A-Z].*Element$/.test(tag)) { | |
| 459 // Check that it is not a simple JavaScript object. | |
| 460 var name = Object.prototype.toString.call(object); | |
| 461 if (name == "[object Object]") return null; | |
| 462 return "HTMLElement"; | |
| 463 } | |
| 464 } | |
| 465 function getUnknownTagGenericBrowser(object, tag) { | |
| 466 if (self.HTMLElement && object instanceof HTMLElement) return "HTMLElement"; | |
| 467 return getUnknownTag(object, tag); | |
| 468 } | |
| 469 function prototypeForTag(tag) { | |
| 470 if (typeof window == "undefined") return null; | |
| 471 if (typeof window[tag] == "undefined") return null; | |
| 472 var constructor = window[tag]; | |
| 473 if (typeof constructor != "function") return null; | |
| 474 return constructor.prototype; | |
| 475 } | |
| 476 function discriminator(tag) { return null; } | |
| 477 | |
| 478 var isBrowser = typeof navigator == "object"; | |
| 479 | |
| 480 return { | |
| 481 getTag: typeNameInChrome, | |
| 482 getUnknownTag: isBrowser ? getUnknownTagGenericBrowser : getUnknownTag, | |
| 483 prototypeForTag: prototypeForTag, | |
| 484 discriminator: discriminator }; | |
| 485 }'''); | |
| 486 | |
| 487 | |
| 488 /** | |
| 489 * Returns the name of the constructor function for browsers where | |
| 490 * `object.constructor.name` is not reliable. | |
| 491 * | |
| 492 * This function is split out of [_fallbackConstructorHooksTransformerGenerator] | |
| 493 * as it is called from both the dispatch hooks and via | |
| 494 * [constructorNameFallback] from objectToString. | |
| 495 */ | |
| 496 const _constructorNameFallback = const JS_CONST(r''' | |
| 497 function getTagFallback(o) { | |
| 498 var constructor = o.constructor; | |
| 499 if (typeof constructor == "function") { | |
| 500 var name = constructor.name; | |
| 501 // If the name is a non-empty string, we use that as the type name of this | |
| 502 // object. There are various cases where that does not work, so we have to | |
| 503 // detect them and fall through to the toString() based implementation. | |
| 504 | |
| 505 if (typeof name == "string" && | |
| 506 | |
| 507 // Sometimes the string is empty. This test also catches minified | |
| 508 // shadow dom polyfil wrapper for Window on Firefox where the faked | |
| 509 // constructor name does not 'stick'. The shortest real DOM object | |
| 510 // names have three characters (e.g. URL, CSS). | |
| 511 name.length > 2 && | |
| 512 | |
| 513 // On Firefox we often get "Object" as the constructor name, even for | |
| 514 // more specialized DOM objects. | |
| 515 name !== "Object" && | |
| 516 | |
| 517 // This can happen in Opera. | |
| 518 name !== "Function.prototype") { | |
| 519 return name; | |
| 520 } | |
| 521 } | |
| 522 var s = Object.prototype.toString.call(o); | |
| 523 return s.substring(8, s.length - 1); | |
| 524 }'''); | |
| 525 | |
| 526 | |
| 527 const _fallbackConstructorHooksTransformerGenerator = const JS_CONST(r''' | |
| 528 function(getTagFallback) { | |
| 529 return function(hooks) { | |
| 530 // If we are not in a browser, assume we are in d8. | |
| 531 // TODO(sra): Recognize jsshell. | |
| 532 if (typeof navigator != "object") return hooks; | |
| 533 | |
| 534 var ua = navigator.userAgent; | |
| 535 // TODO(antonm): remove a reference to DumpRenderTree. | |
| 536 if (ua.indexOf("DumpRenderTree") >= 0) return hooks; | |
| 537 if (ua.indexOf("Chrome") >= 0) { | |
| 538 // Confirm constructor name is usable for dispatch. | |
| 539 function confirm(p) { | |
| 540 return typeof window == "object" && window[p] && window[p].name == p; | |
| 541 } | |
| 542 if (confirm("Window") && confirm("HTMLElement")) return hooks; | |
| 543 } | |
| 544 | |
| 545 hooks.getTag = getTagFallback; | |
| 546 }; | |
| 547 }'''); | |
| 548 | |
| 549 | |
| 550 const _ieHooksTransformer = const JS_CONST(r''' | |
| 551 function(hooks) { | |
| 552 var userAgent = typeof navigator == "object" ? navigator.userAgent : ""; | |
| 553 if (userAgent.indexOf("Trident/") == -1) return hooks; | |
| 554 | |
| 555 var getTag = hooks.getTag; | |
| 556 | |
| 557 var quickMap = { | |
| 558 "BeforeUnloadEvent": "Event", | |
| 559 "DataTransfer": "Clipboard", | |
| 560 "HTMLDDElement": "HTMLElement", | |
| 561 "HTMLDTElement": "HTMLElement", | |
| 562 "HTMLPhraseElement": "HTMLElement", | |
| 563 "Position": "Geoposition" | |
| 564 }; | |
| 565 | |
| 566 function getTagIE(o) { | |
| 567 var tag = getTag(o); | |
| 568 var newTag = quickMap[tag]; | |
| 569 if (newTag) return newTag; | |
| 570 // Patches for types which report themselves as Objects. | |
| 571 if (tag == "Object") { | |
| 572 if (window.DataView && (o instanceof window.DataView)) return "DataView"; | |
| 573 } | |
| 574 return tag; | |
| 575 } | |
| 576 | |
| 577 function prototypeForTagIE(tag) { | |
| 578 var constructor = window[tag]; | |
| 579 if (constructor == null) return null; | |
| 580 return constructor.prototype; | |
| 581 } | |
| 582 | |
| 583 hooks.getTag = getTagIE; | |
| 584 hooks.prototypeForTag = prototypeForTagIE; | |
| 585 }'''); | |
| 586 | |
| 587 const _fixDocumentHooksTransformer = const JS_CONST(r''' | |
| 588 function(hooks) { | |
| 589 var getTag = hooks.getTag; | |
| 590 var prototypeForTag = hooks.prototypeForTag; | |
| 591 function getTagFixed(o) { | |
| 592 var tag = getTag(o); | |
| 593 if (tag == "Document") { | |
| 594 // Some browsers and the polymer polyfill call both HTML and XML documents | |
| 595 // "Document", so we check for the xmlVersion property, which is the empty | |
| 596 // string on HTML documents. Since both dart:html classes Document and | |
| 597 // HtmlDocument share the same type, we must patch the instances and not | |
| 598 // the prototype. | |
| 599 if (!!o.xmlVersion) return "!Document"; | |
| 600 return "!HTMLDocument"; | |
| 601 } | |
| 602 return tag; | |
| 603 } | |
| 604 | |
| 605 function prototypeForTagFixed(tag) { | |
| 606 if (tag == "Document") return null; // Do not pre-patch Document. | |
| 607 return prototypeForTag(tag); | |
| 608 } | |
| 609 | |
| 610 hooks.getTag = getTagFixed; | |
| 611 hooks.prototypeForTag = prototypeForTagFixed; | |
| 612 }'''); | |
| 613 | |
| 614 const _firefoxHooksTransformer = const JS_CONST(r''' | |
| 615 function(hooks) { | |
| 616 var userAgent = typeof navigator == "object" ? navigator.userAgent : ""; | |
| 617 if (userAgent.indexOf("Firefox") == -1) return hooks; | |
| 618 | |
| 619 var getTag = hooks.getTag; | |
| 620 | |
| 621 var quickMap = { | |
| 622 "BeforeUnloadEvent": "Event", | |
| 623 "DataTransfer": "Clipboard", | |
| 624 "GeoGeolocation": "Geolocation", | |
| 625 "Location": "!Location", // Fixes issue 18151 | |
| 626 "WorkerMessageEvent": "MessageEvent", | |
| 627 "XMLDocument": "!Document"}; | |
| 628 | |
| 629 function getTagFirefox(o) { | |
| 630 var tag = getTag(o); | |
| 631 return quickMap[tag] || tag; | |
| 632 } | |
| 633 | |
| 634 hooks.getTag = getTagFirefox; | |
| 635 }'''); | |
| 636 | |
| 637 | |
| 638 const _operaHooksTransformer = const JS_CONST(r''' | |
| 639 function(hooks) { return hooks; } | |
| 640 '''); | |
| 641 | |
| 642 | |
| 643 const _safariHooksTransformer = const JS_CONST(r''' | |
| 644 function(hooks) { return hooks; } | |
| 645 '''); | |
| 646 | |
| 647 | |
| 648 const _dartExperimentalFixupGetTagHooksTransformer = const JS_CONST(r''' | |
| 649 function(hooks) { | |
| 650 if (typeof dartExperimentalFixupGetTag != "function") return hooks; | |
| 651 hooks.getTag = dartExperimentalFixupGetTag(hooks.getTag); | |
| 652 }'''); | |
| OLD | NEW |