Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Side by Side Diff: packages/polymer/lib/src/declaration.dart

Issue 2312183003: Removed Polymer from Observatory deps (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « packages/polymer/lib/src/build/utils.dart ('k') | packages/polymer/lib/src/events.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of polymer;
6
7 /// *Warning* this class is experimental and subject to change.
8 ///
9 /// The data associated with a polymer-element declaration, if it is backed
10 /// by a Dart class instead of a JavaScript prototype.
11 class PolymerDeclaration {
12 /// The one syntax to rule them all.
13 static final BindingDelegate _polymerSyntax = new PolymerExpressions();
14
15 /// The polymer-element for this declaration.
16 final HtmlElement element;
17
18 /// The Dart type corresponding to this custom element declaration.
19 final Type type;
20
21 /// If we extend another custom element, this points to the super declaration.
22 final PolymerDeclaration superDeclaration;
23
24 /// The name of the custom element.
25 final String name;
26
27 /// Map of publish properties. Can be a field or a property getter, but if
28 /// this map contains a getter, is because it also has a corresponding setter.
29 ///
30 /// Note: technically these are always single properties, so we could use a
31 /// Symbol instead of a PropertyPath. However there are lookups between this
32 /// map and [_observe] so it is easier to just track paths.
33 Map<PropertyPath, smoke.Declaration> _publish;
34
35 /// The names of published properties for this polymer-element.
36 Iterable<String> get publishedProperties =>
37 _publish != null ? _publish.keys.map((p) => '$p') : const [];
38
39 /// Same as [_publish] but with lower case names.
40 Map<String, smoke.Declaration> _publishLC;
41
42 Map<PropertyPath, List<Symbol>> _observe;
43
44 /// Name and expression for each computed property.
45 Map<Symbol, String> _computed = {};
46
47 Map<String, Object> _instanceAttributes;
48
49 /// A set of properties that should be automatically reflected to attributes.
50 /// Typically this is used for CSS styling. If none, this variable will be
51 /// left as null.
52 Set<String> _reflect;
53
54 List<Element> _sheets;
55 List<Element> get sheets => _sheets;
56
57 List<Element> _styles;
58 List<Element> get styles => _styles;
59
60 // The default syntax for polymer-elements.
61 PolymerExpressions syntax = _polymerSyntax;
62
63 DocumentFragment get templateContent {
64 final template = fetchTemplate();
65 return template != null ? templateBind(template).content : null;
66 }
67
68 /// Maps event names and their associated method in the element class.
69 final Map<String, String> _eventDelegates = {};
70
71 /// Expected events per element node.
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?)
74 Expando<Set<String>> _templateDelegates;
75
76 String get extendee =>
77 superDeclaration != null ? superDeclaration.name : null;
78
79 /// The root URI for assets.
80 Uri _rootUri;
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'),
101 const Symbol('id'),
102 const Symbol('hidden'),
103 const Symbol('style'),
104 const Symbol('title'),
105 const Symbol('classes')
106 ]);
107
108 bool _checkPropertyBlacklist(Symbol name) {
109 if (_PROPERTY_NAME_BLACKLIST.contains(name)) {
110 print('Cannot define property "$name" for element "${this.name}" '
111 'because it has the same name as an HTMLElement property, and not '
112 'all browsers support overriding that. Consider giving it a '
113 'different name. ');
114 return true;
115 }
116 return false;
117 }
118
119 // Dart note: since polymer-element is handled in JS now, we have a simplified
120 // flow for registering. We don't need to wait for the supertype or the code
121 // to be noticed.
122 PolymerDeclaration(this.element, this.name, this.type, this.superDeclaration);
123
124 void register() {
125 // more declarative features
126 desugar();
127 // register our custom element
128 registerType(name);
129
130 // NOTE: skip in Dart because we don't have mutable global scope.
131 // reference constructor in a global named by 'constructor' attribute
132 // publishConstructor();
133 }
134
135 /// Implement various declarative features.
136 // Dart note: this merges "buildPrototype" "desugarBeforeChaining" and
137 // "desugarAfterChaining", because we don't have prototypes.
138 void desugar() {
139
140 // back reference declaration element
141 _declarations[name] = this;
142
143 // transcribe `attributes` declarations onto own prototype's `publish`
144 publishAttributes(superDeclaration);
145
146 publishProperties();
147
148 inferObservers();
149
150 // desugar compound observer syntax, e.g. @ObserveProperty('a b c')
151 explodeObservers();
152
153 createPropertyAccessors();
154 // install mdv delegate on template
155 installBindingDelegate(fetchTemplate());
156 // install external stylesheets as if they are inline
157 installSheets();
158 // adjust any paths in dom from imports
159 resolveElementPaths(element);
160 // compile list of attributes to copy to instances
161 accumulateInstanceAttributes();
162 // parse on-* delegates declared on `this` element
163 parseHostEvents();
164 // install a helper method this.resolvePath to aid in
165 // setting resource urls. e.g.
166 // this.$.image.src = this.resolvePath('images/foo.png')
167 initResolvePath();
168 // under ShadowDOMPolyfill, transforms to approximate missing CSS features
169 _shimShadowDomStyling(templateContent, name, extendee);
170
171 // TODO(jmesserly): this feels unnatrual in Dart. Since we have convenient
172 // lazy static initialization, can we get by without it?
173 if (smoke.hasStaticMethod(type, #registerCallback)) {
174 smoke.invoke(type, #registerCallback, [this]);
175 }
176 }
177
178 void registerType(String name) {
179 var baseTag;
180 var decl = this;
181 while (decl != null) {
182 baseTag = decl.element.attributes['extends'];
183 decl = decl.superDeclaration;
184 }
185 document.registerElement(name, type, extendsTag: baseTag);
186 }
187
188 // from declaration/mdv.js
189 Element fetchTemplate() => element.querySelector('template');
190
191 void installBindingDelegate(Element template) {
192 if (template != null) {
193 templateBind(template).bindingDelegate = this.syntax;
194 }
195 }
196
197 // from declaration/path.js
198 void resolveElementPaths(Node node) => PolymerJs.resolveElementPaths(node);
199
200 // Dart note: renamed from "addResolvePathApi".
201 void initResolvePath() {
202 // let assetpath attribute modify the resolve path
203 var assetPath = element.attributes['assetpath'];
204 if (assetPath == null) assetPath = '';
205 var base = Uri.parse(element.ownerDocument.baseUri);
206 _rootUri = base.resolve(assetPath);
207 }
208
209 String resolvePath(String urlPath, [baseUrlOrString]) {
210 Uri base;
211 if (baseUrlOrString == null) {
212 // Dart note: this enforces the same invariant as JS, where you need to
213 // call addResolvePathApi first.
214 if (_rootUri == null) {
215 throw new StateError('call initResolvePath before calling resolvePath');
216 }
217 base = _rootUri;
218 } else if (baseUrlOrString is Uri) {
219 base = baseUrlOrString;
220 } else {
221 base = Uri.parse(baseUrlOrString);
222 }
223 return base.resolve(urlPath).toString();
224 }
225
226 void publishAttributes(PolymerDeclaration superDecl) {
227 // get properties to publish
228 if (superDecl != null) {
229 // Dart note: even though we walk the type hierarchy in
230 // _getPublishedProperties, this will additionally include any names
231 // published via the `attributes` attribute.
232 if (superDecl._publish != null) {
233 _publish = new Map.from(superDecl._publish);
234 }
235 if (superDecl._reflect != null) {
236 _reflect = new Set.from(superDecl._reflect);
237 }
238 }
239
240 _getPublishedProperties(type);
241
242 // merge names from 'attributes' attribute into the '_publish' object
243 var attrs = element.attributes['attributes'];
244 if (attrs != null) {
245 // names='a b c' or names='a,b,c'
246 // record each name for publishing
247 for (var attr in attrs.split(_ATTRIBUTES_REGEX)) {
248 // remove excess ws
249 attr = attr.trim();
250
251 // if the user hasn't specified a value, we want to use the
252 // default, unless a superclass has already chosen one
253 if (attr == '') continue;
254
255 var decl, path;
256 var property = smoke.nameToSymbol(attr);
257 if (property != null) {
258 path = new PropertyPath([property]);
259 if (_publish != null && _publish.containsKey(path)) {
260 continue;
261 }
262 decl = smoke.getDeclaration(type, property);
263 }
264
265 if (property == null || decl == null || decl.isMethod || decl.isFinal) {
266 window.console.warn('property for attribute $attr of polymer-element '
267 'name=$name not found.');
268 continue;
269 }
270 if (_publish == null) _publish = {};
271 _publish[path] = decl;
272 }
273 }
274
275 // NOTE: the following is not possible in Dart; fields must be declared.
276 // install 'attributes' as properties on the prototype,
277 // but don't override
278 }
279
280 void _getPublishedProperties(Type type) {
281 var options = const smoke.QueryOptions(
282 includeInherited: true,
283 includeUpTo: HtmlElement,
284 withAnnotations: const [PublishedProperty]);
285 for (var decl in smoke.query(type, options)) {
286 if (decl.isFinal) continue;
287 if (_checkPropertyBlacklist(decl.name)) continue;
288 if (_publish == null) _publish = {};
289 _publish[new PropertyPath([decl.name])] = decl;
290
291 // Should we reflect the property value to the attribute automatically?
292 if (decl.annotations
293 .where((a) => a is PublishedProperty)
294 .any((a) => a.reflect)) {
295 if (_reflect == null) _reflect = new Set();
296 _reflect.add(smoke.symbolToName(decl.name));
297 }
298 }
299 }
300
301 void accumulateInstanceAttributes() {
302 // inherit instance attributes
303 _instanceAttributes = new Map<String, Object>();
304 if (superDeclaration != null) {
305 _instanceAttributes.addAll(superDeclaration._instanceAttributes);
306 }
307
308 // merge attributes from element
309 element.attributes.forEach((name, value) {
310 if (isInstanceAttribute(name)) {
311 _instanceAttributes[name] = value;
312 }
313 });
314 }
315
316 static bool isInstanceAttribute(name) {
317 // do not clone these attributes onto instances
318 final blackList = const {
319 'name': 1,
320 'extends': 1,
321 'constructor': 1,
322 'noscript': 1,
323 'assetpath': 1,
324 'cache-csstext': 1,
325 // add ATTRIBUTES_ATTRIBUTE to the blacklist
326 'attributes': 1,
327 };
328
329 return !blackList.containsKey(name) && !name.startsWith('on-');
330 }
331
332 /// Extracts events from the element tag attributes.
333 void parseHostEvents() {
334 addAttributeDelegates(_eventDelegates);
335 }
336
337 void addAttributeDelegates(Map<String, String> delegates) {
338 element.attributes.forEach((name, value) {
339 if (_hasEventPrefix(name)) {
340 var start = value.indexOf('{{');
341 var end = value.lastIndexOf('}}');
342 if (start >= 0 && end >= 0) {
343 delegates[_removeEventPrefix(name)] =
344 value.substring(start + 2, end).trim();
345 }
346 }
347 });
348 }
349
350 String urlToPath(String url) {
351 if (url == null) return '';
352 return (url.split('/')
353 ..removeLast()
354 ..add('')).join('/');
355 }
356
357 // Dart note: loadStyles, convertSheetsToStyles, copySheetAttribute and
358 // findLoadableStyles are not ported because they're handled by Polymer JS
359 // before we get into [register].
360
361 /// Install external stylesheets loaded in <element> elements into the
362 /// element's template.
363 void installSheets() {
364 cacheSheets();
365 cacheStyles();
366 installLocalSheets();
367 installGlobalStyles();
368 }
369
370 void cacheSheets() {
371 _sheets = findNodes(_SHEET_SELECTOR);
372 for (var s in sheets) s.remove();
373 }
374
375 void cacheStyles() {
376 _styles = findNodes('$_STYLE_SELECTOR[$_SCOPE_ATTR]');
377 for (var s in styles) s.remove();
378 }
379
380 /// Takes external stylesheets loaded in an `<element>` element and moves
381 /// their content into a style element inside the `<element>`'s template.
382 /// The sheet is then removed from the `<element>`. This is done only so
383 /// that if the element is loaded in the main document, the sheet does
384 /// not become active.
385 /// Note, ignores sheets with the attribute 'polymer-scope'.
386 void installLocalSheets() {
387 var sheets =
388 this.sheets.where((s) => !s.attributes.containsKey(_SCOPE_ATTR));
389 var content = templateContent;
390 if (content != null) {
391 var cssText = new StringBuffer();
392 for (var sheet in sheets) {
393 cssText
394 ..write(_cssTextFromSheet(sheet))
395 ..write('\n');
396 }
397 if (cssText.length > 0) {
398 var style = element.ownerDocument.createElement('style')
399 ..text = '$cssText';
400
401 content.insertBefore(style, content.firstChild);
402 }
403 }
404 }
405
406 List<Element> findNodes(String selector, [bool matcher(Element e)]) {
407 var nodes = element.querySelectorAll(selector).toList();
408 var content = templateContent;
409 if (content != null) {
410 nodes = nodes..addAll(content.querySelectorAll(selector));
411 }
412 if (matcher != null) return nodes.where(matcher).toList();
413 return nodes;
414 }
415
416 /// Promotes external stylesheets and style elements with the attribute
417 /// polymer-scope='global' into global scope.
418 /// This is particularly useful for defining @keyframe rules which
419 /// currently do not function in scoped or shadow style elements.
420 /// (See wkb.ug/72462)
421 // TODO(sorvell): remove when wkb.ug/72462 is addressed.
422 void installGlobalStyles() {
423 var style = styleForScope(_STYLE_GLOBAL_SCOPE);
424 Polymer.applyStyleToScope(style, document.head);
425 }
426
427 String cssTextForScope(String scopeDescriptor) {
428 var cssText = new StringBuffer();
429 // handle stylesheets
430 var selector = '[$_SCOPE_ATTR=$scopeDescriptor]';
431 matcher(s) => s.matches(selector);
432
433 for (var sheet in sheets.where(matcher)) {
434 cssText
435 ..write(_cssTextFromSheet(sheet))
436 ..write('\n\n');
437 }
438 // handle cached style elements
439 for (var style in styles.where(matcher)) {
440 cssText
441 ..write(style.text)
442 ..write('\n\n');
443 }
444 return cssText.toString();
445 }
446
447 StyleElement styleForScope(String scopeDescriptor) {
448 var cssText = cssTextForScope(scopeDescriptor);
449 return cssTextToScopeStyle(cssText, scopeDescriptor);
450 }
451
452 StyleElement cssTextToScopeStyle(String cssText, String scopeDescriptor) {
453 if (cssText == '') return null;
454
455 return new StyleElement()
456 ..text = cssText
457 ..attributes[_STYLE_SCOPE_ATTRIBUTE] = '$name-$scopeDescriptor';
458 }
459
460 /// Fetch a list of all *Changed methods so we can observe the associated
461 /// properties.
462 void inferObservers() {
463 for (var decl in smoke.query(type, _changedMethodQueryOptions)) {
464 // TODO(jmesserly): now that we have a better system, should we
465 // deprecate *Changed methods?
466 if (_observe == null) _observe = new HashMap();
467 var name = smoke.symbolToName(decl.name);
468 name = name.substring(0, name.length - 7);
469 if (!_canObserveProperty(decl.name)) continue;
470 _observe[new PropertyPath(name)] = [decl.name];
471 }
472 }
473
474 /// Fetch a list of all methods annotated with [ObserveProperty] so we can
475 /// observe the associated properties.
476 void explodeObservers() {
477 var options = const smoke.QueryOptions(
478 includeFields: false,
479 includeProperties: false,
480 includeMethods: true,
481 includeInherited: true,
482 includeUpTo: HtmlElement,
483 withAnnotations: const [ObserveProperty]);
484 for (var decl in smoke.query(type, options)) {
485 for (var meta in decl.annotations) {
486 if (meta is! ObserveProperty) continue;
487 if (_observe == null) _observe = new HashMap();
488 for (String name in meta.names) {
489 _observe.putIfAbsent(new PropertyPath(name), () => []).add(decl.name);
490 }
491 }
492 }
493 }
494
495 void publishProperties() {
496 // Dart note: _publish was already populated by publishAttributes
497 if (_publish != null) _publishLC = _lowerCaseMap(_publish);
498 }
499
500 Map<String, dynamic> _lowerCaseMap(Map<PropertyPath, dynamic> properties) {
501 final map = new Map<String, dynamic>();
502 properties.forEach((PropertyPath path, value) {
503 map['$path'.toLowerCase()] = value;
504 });
505 return map;
506 }
507
508 void createPropertyAccessors() {
509 // Dart note: since we don't have a prototype in Dart, most of the work of
510 // createPolymerAccessors is done lazily on the first access of properties.
511 // Here we just extract the information from annotations and store it as
512 // properties on the declaration.
513
514 // Dart Note: The js side makes computed properties read only, and does
515 // special logic right here for them. For us they are automatically read
516 // only unless you define a setter for them, so we left that out.
517 var options = const smoke.QueryOptions(
518 includeInherited: true,
519 includeUpTo: HtmlElement,
520 withAnnotations: const [ComputedProperty]);
521 var existing = {};
522 for (var decl in smoke.query(type, options)) {
523 var name = decl.name;
524 if (_checkPropertyBlacklist(name)) continue;
525 var meta = decl.annotations.firstWhere((e) => e is ComputedProperty);
526 var prev = existing[name];
527 // The definition of a child class takes priority.
528 if (prev == null || smoke.isSubclassOf(decl.type, prev.type)) {
529 _computed[name] = meta.expression;
530 existing[name] = decl;
531 }
532 }
533 }
534 }
535
536 /// maps tag names to prototypes
537 final Map _typesByName = new Map<String, Type>();
538
539 Type _getRegisteredType(String name) => _typesByName[name];
540
541 /// Dart Note: instanceOfType not implemented for dart, its not needed.
542
543 /// track document.register'ed tag names and their declarations
544 final Map _declarations = new Map<String, PolymerDeclaration>();
545
546 bool _isRegistered(String name) => _declarations.containsKey(name);
547 PolymerDeclaration _getDeclaration(String name) => _declarations[name];
548
549 /// Using Polymer's web_components/src/ShadowCSS.js passing the style tag's
550 /// content.
551 void _shimShadowDomStyling(
552 DocumentFragment template, String name, String extendee) {
553 if (_ShadowCss == null || !_hasShadowDomPolyfill) return;
554
555 _ShadowCss.callMethod('shimStyling', [template, name, extendee]);
556 }
557
558 final bool _hasShadowDomPolyfill = js.context.hasProperty('ShadowDOMPolyfill');
559 final JsObject _ShadowCss =
560 _WebComponents != null ? _WebComponents['ShadowCSS'] : null;
561
562 const _STYLE_SELECTOR = 'style';
563 const _SHEET_SELECTOR = 'link[rel=stylesheet]';
564 const _STYLE_GLOBAL_SCOPE = 'global';
565 const _SCOPE_ATTR = 'polymer-scope';
566 const _STYLE_SCOPE_ATTRIBUTE = 'element';
567 const _STYLE_CONTROLLER_SCOPE = 'controller';
568
569 String _cssTextFromSheet(LinkElement sheet) {
570 if (sheet == null) return '';
571
572 // In deploy mode we should never do a sync XHR; link rel=stylesheet will
573 // be inlined into a <style> tag by ImportInliner.
574 if (_deployMode) return '';
575
576 // TODO(jmesserly): sometimes the href property is wrong after deployment.
577 var href = sheet.href;
578 if (href == '') href = sheet.attributes["href"];
579
580 // TODO(jmesserly): it seems like polymer-js is always polyfilling
581 // HTMLImports, because their code depends on "__resource" to work, so I
582 // don't see how it can work with native HTML Imports. We use a sync-XHR
583 // under the assumption that the file is likely to have been already
584 // downloaded and cached by HTML Imports.
585 try {
586 return (new HttpRequest()
587 ..open('GET', href, async: false)
588 ..send()).responseText;
589 } on DomException catch (e, t) {
590 _sheetLog.fine('failed to XHR stylesheet text href="$href" error: '
591 '$e, trace: $t');
592 return '';
593 }
594 }
595
596 final Logger _sheetLog = new Logger('polymer.stylesheet');
597
598 final smoke.QueryOptions _changedMethodQueryOptions = new smoke.QueryOptions(
599 includeFields: false,
600 includeProperties: false,
601 includeMethods: true,
602 includeInherited: true,
603 includeUpTo: HtmlElement,
604 matches: _isObserverMethod);
605
606 bool _isObserverMethod(Symbol symbol) {
607 String name = smoke.symbolToName(symbol);
608 if (name == null) return false;
609 return name.endsWith('Changed') && name != 'attributeChanged';
610 }
611
612 final _ATTRIBUTES_REGEX = new RegExp(r'\s|,');
613
614 final JsObject _WebComponents = js.context['WebComponents'];
OLDNEW
« no previous file with comments | « packages/polymer/lib/src/build/utils.dart ('k') | packages/polymer/lib/src/events.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698