| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 polymer; | 5 part of polymer; |
| 6 | 6 |
| 7 /// *Warning* this class is experimental and subject to change. | 7 /// *Warning* this class is experimental and subject to change. |
| 8 /// | 8 /// |
| 9 /// The data associated with a polymer-element declaration, if it is backed | 9 /// The data associated with a polymer-element declaration, if it is backed |
| 10 /// by a Dart class instead of a JavaScript prototype. | 10 /// by a Dart class instead of a JavaScript prototype. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 // TODO(sigmund): investigate whether we need more than 1 set of local events | 72 // TODO(sigmund): investigate whether we need more than 1 set of local events |
| 73 // per element (why does the js implementation stores 1 per template node?) | 73 // per element (why does the js implementation stores 1 per template node?) |
| 74 Expando<Set<String>> _templateDelegates; | 74 Expando<Set<String>> _templateDelegates; |
| 75 | 75 |
| 76 String get extendee => superDeclaration != null ? | 76 String get extendee => superDeclaration != null ? |
| 77 superDeclaration.name : null; | 77 superDeclaration.name : null; |
| 78 | 78 |
| 79 /// The root URI for assets. | 79 /// The root URI for assets. |
| 80 Uri _rootUri; | 80 Uri _rootUri; |
| 81 | 81 |
| 82 /// List of properties to ignore for observation. |
| 83 static Set<Symbol> _OBSERVATION_BLACKLIST = |
| 84 new HashSet.from(const [#attribute]); |
| 85 |
| 86 static bool _canObserveProperty(Symbol property) => |
| 87 !_OBSERVATION_BLACKLIST.contains(property); |
| 88 |
| 89 /// This list contains some property names that people commonly want to use, |
| 90 /// but won't work because of Chrome/Safari bugs. It isn't an exhaustive |
| 91 /// list. In particular it doesn't contain any property names found on |
| 92 /// subtypes of HTMLElement (e.g. name, value). Rather it attempts to catch |
| 93 /// some common cases. |
| 94 /// |
| 95 /// Dart Note: `class` is left out since its an invalid symbol in dart. This |
| 96 /// means that nobody could make a property by this name anyways though. |
| 97 /// Dart Note: We have added `classes` to this list, which is the dart:html |
| 98 /// equivalent of `classList` but more likely to have conflicts. |
| 99 static Set<Symbol> _PROPERTY_NAME_BLACKLIST = new HashSet.from([ |
| 100 const Symbol('children'), const Symbol('id'), const Symbol('hidden'), |
| 101 const Symbol('style'), const Symbol('title'), const Symbol('classes')]); |
| 102 |
| 103 bool _checkPropertyBlacklist(Symbol name) { |
| 104 if (_PROPERTY_NAME_BLACKLIST.contains(name)) { |
| 105 print('Cannot define property "$name" for element "${this.name}" ' |
| 106 'because it has the same name as an HTMLElement property, and not ' |
| 107 'all browsers support overriding that. Consider giving it a ' |
| 108 'different name. '); |
| 109 return true; |
| 110 } |
| 111 return false; |
| 112 } |
| 113 |
| 82 // Dart note: since polymer-element is handled in JS now, we have a simplified | 114 // Dart note: since polymer-element is handled in JS now, we have a simplified |
| 83 // flow for registering. We don't need to wait for the supertype or the code | 115 // flow for registering. We don't need to wait for the supertype or the code |
| 84 // to be noticed. | 116 // to be noticed. |
| 85 PolymerDeclaration(this.element, this.name, this.type, this.superDeclaration); | 117 PolymerDeclaration(this.element, this.name, this.type, this.superDeclaration); |
| 86 | 118 |
| 87 void register() { | 119 void register() { |
| 88 // more declarative features | 120 // more declarative features |
| 89 desugar(); | 121 desugar(); |
| 90 // register our custom element | 122 // register our custom element |
| 91 registerType(name); | 123 registerType(name); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 // NOTE: the following is not possible in Dart; fields must be declared. | 273 // NOTE: the following is not possible in Dart; fields must be declared. |
| 242 // install 'attributes' as properties on the prototype, | 274 // install 'attributes' as properties on the prototype, |
| 243 // but don't override | 275 // but don't override |
| 244 } | 276 } |
| 245 | 277 |
| 246 void _getPublishedProperties(Type type) { | 278 void _getPublishedProperties(Type type) { |
| 247 var options = const smoke.QueryOptions(includeInherited: true, | 279 var options = const smoke.QueryOptions(includeInherited: true, |
| 248 includeUpTo: HtmlElement, withAnnotations: const [PublishedProperty]); | 280 includeUpTo: HtmlElement, withAnnotations: const [PublishedProperty]); |
| 249 for (var decl in smoke.query(type, options)) { | 281 for (var decl in smoke.query(type, options)) { |
| 250 if (decl.isFinal) continue; | 282 if (decl.isFinal) continue; |
| 283 if (_checkPropertyBlacklist(decl.name)) continue; |
| 251 if (_publish == null) _publish = {}; | 284 if (_publish == null) _publish = {}; |
| 252 _publish[new PropertyPath([decl.name])] = decl; | 285 _publish[new PropertyPath([decl.name])] = decl; |
| 253 | 286 |
| 254 // Should we reflect the property value to the attribute automatically? | 287 // Should we reflect the property value to the attribute automatically? |
| 255 if (decl.annotations | 288 if (decl.annotations |
| 256 .where((a) => a is PublishedProperty) | 289 .where((a) => a is PublishedProperty) |
| 257 .any((a) => a.reflect)) { | 290 .any((a) => a.reflect)) { |
| 258 | 291 |
| 259 if (_reflect == null) _reflect = new Set(); | 292 if (_reflect == null) _reflect = new Set(); |
| 260 _reflect.add(smoke.symbolToName(decl.name)); | 293 _reflect.add(smoke.symbolToName(decl.name)); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 | 449 |
| 417 /// Fetch a list of all *Changed methods so we can observe the associated | 450 /// Fetch a list of all *Changed methods so we can observe the associated |
| 418 /// properties. | 451 /// properties. |
| 419 void inferObservers() { | 452 void inferObservers() { |
| 420 for (var decl in smoke.query(type, _changedMethodQueryOptions)) { | 453 for (var decl in smoke.query(type, _changedMethodQueryOptions)) { |
| 421 // TODO(jmesserly): now that we have a better system, should we | 454 // TODO(jmesserly): now that we have a better system, should we |
| 422 // deprecate *Changed methods? | 455 // deprecate *Changed methods? |
| 423 if (_observe == null) _observe = new HashMap(); | 456 if (_observe == null) _observe = new HashMap(); |
| 424 var name = smoke.symbolToName(decl.name); | 457 var name = smoke.symbolToName(decl.name); |
| 425 name = name.substring(0, name.length - 7); | 458 name = name.substring(0, name.length - 7); |
| 459 if (!_canObserveProperty(decl.name)) continue; |
| 426 _observe[new PropertyPath(name)] = [decl.name]; | 460 _observe[new PropertyPath(name)] = [decl.name]; |
| 427 } | 461 } |
| 428 } | 462 } |
| 429 | 463 |
| 430 /// Fetch a list of all methods annotated with [ObserveProperty] so we can | 464 /// Fetch a list of all methods annotated with [ObserveProperty] so we can |
| 431 /// observe the associated properties. | 465 /// observe the associated properties. |
| 432 void explodeObservers() { | 466 void explodeObservers() { |
| 433 var options = const smoke.QueryOptions(includeFields: false, | 467 var options = const smoke.QueryOptions(includeFields: false, |
| 434 includeProperties: false, includeMethods: true, includeInherited: true, | 468 includeProperties: false, includeMethods: true, includeInherited: true, |
| 435 includeUpTo: HtmlElement, withAnnotations: const [ObserveProperty]); | 469 includeUpTo: HtmlElement, withAnnotations: const [ObserveProperty]); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 463 // Here we just extract the information from annotations and store it as | 497 // Here we just extract the information from annotations and store it as |
| 464 // properties on the declaration. | 498 // properties on the declaration. |
| 465 | 499 |
| 466 // Dart Note: The js side makes computed properties read only, and does | 500 // Dart Note: The js side makes computed properties read only, and does |
| 467 // special logic right here for them. For us they are automatically read | 501 // special logic right here for them. For us they are automatically read |
| 468 // only unless you define a setter for them, so we left that out. | 502 // only unless you define a setter for them, so we left that out. |
| 469 var options = const smoke.QueryOptions(includeInherited: true, | 503 var options = const smoke.QueryOptions(includeInherited: true, |
| 470 includeUpTo: HtmlElement, withAnnotations: const [ComputedProperty]); | 504 includeUpTo: HtmlElement, withAnnotations: const [ComputedProperty]); |
| 471 var existing = {}; | 505 var existing = {}; |
| 472 for (var decl in smoke.query(type, options)) { | 506 for (var decl in smoke.query(type, options)) { |
| 507 var name = decl.name; |
| 508 if (_checkPropertyBlacklist(name)) continue; |
| 473 var meta = decl.annotations.firstWhere((e) => e is ComputedProperty); | 509 var meta = decl.annotations.firstWhere((e) => e is ComputedProperty); |
| 474 var name = decl.name; | |
| 475 var prev = existing[name]; | 510 var prev = existing[name]; |
| 476 // The definition of a child class takes priority. | 511 // The definition of a child class takes priority. |
| 477 if (prev == null || smoke.isSubclassOf(decl.type, prev.type)) { | 512 if (prev == null || smoke.isSubclassOf(decl.type, prev.type)) { |
| 478 _computed[name] = meta.expression; | 513 _computed[name] = meta.expression; |
| 479 existing[name] = decl; | 514 existing[name] = decl; |
| 480 } | 515 } |
| 481 } | 516 } |
| 482 } | 517 } |
| 483 } | 518 } |
| 484 | 519 |
| 485 /// maps tag names to prototypes | 520 /// maps tag names to prototypes |
| 486 final Map _typesByName = new Map<String, Type>(); | 521 final Map _typesByName = new Map<String, Type>(); |
| 487 | 522 |
| 488 Type _getRegisteredType(String name) => _typesByName[name]; | 523 Type _getRegisteredType(String name) => _typesByName[name]; |
| 489 | 524 |
| 490 /// Dart Note: instanceOfType not implemented for dart, its not needed. | 525 /// Dart Note: instanceOfType not implemented for dart, its not needed. |
| 491 | 526 |
| 492 /// track document.register'ed tag names and their declarations | 527 /// track document.register'ed tag names and their declarations |
| 493 final Map _declarations = new Map<String, PolymerDeclaration>(); | 528 final Map _declarations = new Map<String, PolymerDeclaration>(); |
| 494 | 529 |
| 495 bool _isRegistered(String name) => _declarations.containsKey(name); | 530 bool _isRegistered(String name) => _declarations.containsKey(name); |
| 496 PolymerDeclaration _getDeclaration(String name) => _declarations[name]; | 531 PolymerDeclaration _getDeclaration(String name) => _declarations[name]; |
| 497 | 532 |
| 498 /// Using Polymer's platform/src/ShadowCSS.js passing the style tag's content. | 533 /// Using Polymer's web_components/src/ShadowCSS.js passing the style tag's |
| 534 /// content. |
| 499 void _shimShadowDomStyling(DocumentFragment template, String name, | 535 void _shimShadowDomStyling(DocumentFragment template, String name, |
| 500 String extendee) { | 536 String extendee) { |
| 501 if (_ShadowCss == null ||!_hasShadowDomPolyfill) return; | 537 if (_ShadowCss == null ||!_hasShadowDomPolyfill) return; |
| 502 | 538 |
| 503 _ShadowCss.callMethod('shimStyling', [template, name, extendee]); | 539 _ShadowCss.callMethod('shimStyling', [template, name, extendee]); |
| 504 } | 540 } |
| 505 | 541 |
| 506 final bool _hasShadowDomPolyfill = js.context.hasProperty('ShadowDOMPolyfill'); | 542 final bool _hasShadowDomPolyfill = js.context.hasProperty('ShadowDOMPolyfill'); |
| 507 final JsObject _ShadowCss = _Platform != null ? _Platform['ShadowCSS'] : null; | 543 final JsObject _ShadowCss = |
| 544 _WebComponents != null ? _WebComponents['ShadowCSS'] : null; |
| 508 | 545 |
| 509 const _STYLE_SELECTOR = 'style'; | 546 const _STYLE_SELECTOR = 'style'; |
| 510 const _SHEET_SELECTOR = 'link[rel=stylesheet]'; | 547 const _SHEET_SELECTOR = 'link[rel=stylesheet]'; |
| 511 const _STYLE_GLOBAL_SCOPE = 'global'; | 548 const _STYLE_GLOBAL_SCOPE = 'global'; |
| 512 const _SCOPE_ATTR = 'polymer-scope'; | 549 const _SCOPE_ATTR = 'polymer-scope'; |
| 513 const _STYLE_SCOPE_ATTRIBUTE = 'element'; | 550 const _STYLE_SCOPE_ATTRIBUTE = 'element'; |
| 514 const _STYLE_CONTROLLER_SCOPE = 'controller'; | 551 const _STYLE_CONTROLLER_SCOPE = 'controller'; |
| 515 | 552 |
| 516 String _cssTextFromSheet(LinkElement sheet) { | 553 String _cssTextFromSheet(LinkElement sheet) { |
| 517 if (sheet == null) return ''; | 554 if (sheet == null) return ''; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 | 588 |
| 552 bool _isObserverMethod(Symbol symbol) { | 589 bool _isObserverMethod(Symbol symbol) { |
| 553 String name = smoke.symbolToName(symbol); | 590 String name = smoke.symbolToName(symbol); |
| 554 if (name == null) return false; | 591 if (name == null) return false; |
| 555 return name.endsWith('Changed') && name != 'attributeChanged'; | 592 return name.endsWith('Changed') && name != 'attributeChanged'; |
| 556 } | 593 } |
| 557 | 594 |
| 558 | 595 |
| 559 final _ATTRIBUTES_REGEX = new RegExp(r'\s|,'); | 596 final _ATTRIBUTES_REGEX = new RegExp(r'\s|,'); |
| 560 | 597 |
| 561 final JsObject _Platform = js.context['Platform']; | 598 final JsObject _WebComponents = js.context['WebComponents']; |
| 562 final JsObject _Polymer = js.context['Polymer']; | 599 final JsObject _Polymer = js.context['Polymer']; |
| OLD | NEW |