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

Side by Side Diff: sdk/lib/html/dart2js/html_dart2js.dart

Issue 14651030: Version 0.5.7.1 . (Closed) Base URL: http://dart.googlecode.com/svn/trunk/dart/
Patch Set: Created 7 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | sdk/lib/html/dartium/html_dartium.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /// The Dart HTML library. 1 /// The Dart HTML library.
2 library dart.dom.html; 2 library dart.dom.html;
3 3
4 import 'dart:async'; 4 import 'dart:async';
5 import 'dart:collection'; 5 import 'dart:collection';
6 import 'dart:_collection-dev' hide Symbol; 6 import 'dart:_collection-dev' hide Symbol;
7 import 'dart:html_common'; 7 import 'dart:html_common';
8 import 'dart:indexed_db'; 8 import 'dart:indexed_db';
9 import 'dart:isolate'; 9 import 'dart:isolate';
10 import 'dart:json' as json; 10 import 'dart:json' as json;
(...skipping 7698 matching lines...) Expand 10 before | Expand all | Expand 10 after
7709 * 7709 *
7710 * Currently this does not support propagation through Shadow DOMs. 7710 * Currently this does not support propagation through Shadow DOMs.
7711 */ 7711 */
7712 @Experimental 7712 @Experimental
7713 get model => _model; 7713 get model => _model;
7714 7714
7715 @Experimental 7715 @Experimental
7716 void set model(value) { 7716 void set model(value) {
7717 _ensureTemplate(); 7717 _ensureTemplate();
7718 7718
7719 var syntax = TemplateElement.syntax[attributes['syntax']];
7719 _model = value; 7720 _model = value;
7720 _addBindings(this, model); 7721 _addBindings(this, model, syntax);
7721 } 7722 }
7722 7723
7723 // TODO(jmesserly): const set would be better 7724 // TODO(jmesserly): const set would be better
7724 static const _TABLE_TAGS = const { 7725 static const _TABLE_TAGS = const {
7725 'caption': null, 7726 'caption': null,
7726 'col': null, 7727 'col': null,
7727 'colgroup': null, 7728 'colgroup': null,
7728 'tbody': null, 7729 'tbody': null,
7729 'td': null, 7730 'td': null,
7730 'tfoot': null, 7731 'tfoot': null,
(...skipping 7034 matching lines...) Expand 10 before | Expand all | Expand 10 after
14765 /** Unbinds the attribute [name]. */ 14766 /** Unbinds the attribute [name]. */
14766 @Experimental 14767 @Experimental
14767 void unbind(String name) {} 14768 void unbind(String name) {}
14768 14769
14769 /** Unbinds all bound attributes. */ 14770 /** Unbinds all bound attributes. */
14770 @Experimental 14771 @Experimental
14771 void unbindAll() {} 14772 void unbindAll() {}
14772 14773
14773 TemplateInstance _templateInstance; 14774 TemplateInstance _templateInstance;
14774 14775
14775 // TODO(arv): Consider storing all "NodeRareData" on a single object?
14776 int __instanceTerminatorCount;
14777 int get _instanceTerminatorCount {
14778 if (__instanceTerminatorCount == null) return 0;
14779 return __instanceTerminatorCount;
14780 }
14781 set _instanceTerminatorCount(int value) {
14782 if (value == 0) value = null;
14783 __instanceTerminatorCount = value;
14784 }
14785
14786 /** Gets the template instance that instantiated this node, if any. */ 14776 /** Gets the template instance that instantiated this node, if any. */
14787 @Experimental 14777 @Experimental
14788 TemplateInstance get templateInstance => 14778 TemplateInstance get templateInstance =>
14789 _templateInstance != null ? _templateInstance : 14779 _templateInstance != null ? _templateInstance :
14790 (parent != null ? parent.templateInstance : null); 14780 (parent != null ? parent.templateInstance : null);
14791 14781
14792 14782
14793 static const int ATTRIBUTE_NODE = 2; 14783 static const int ATTRIBUTE_NODE = 2;
14794 14784
14795 static const int CDATA_SECTION_NODE = 4; 14785 static const int CDATA_SECTION_NODE = 4;
(...skipping 10181 matching lines...) Expand 10 before | Expand all | Expand 10 after
24977 * 24967 *
24978 * TemplateElement.syntax['MySyntax'] = new MySyntax(); 24968 * TemplateElement.syntax['MySyntax'] = new MySyntax();
24979 * 24969 *
24980 * See <https://github.com/toolkitchen/mdv/blob/master/docs/syntax.md> for more 24970 * See <https://github.com/toolkitchen/mdv/blob/master/docs/syntax.md> for more
24981 * information about Custom Syntax. 24971 * information about Custom Syntax.
24982 */ 24972 */
24983 // TODO(jmesserly): if this is just one method, a function type would make it 24973 // TODO(jmesserly): if this is just one method, a function type would make it
24984 // more Dart-friendly. 24974 // more Dart-friendly.
24985 @Experimental 24975 @Experimental
24986 abstract class CustomBindingSyntax { 24976 abstract class CustomBindingSyntax {
24977 /**
24978 * This syntax method allows for a custom interpretation of the contents of
24979 * mustaches (`{{` ... `}}`).
24980 *
24981 * When a template is inserting an instance, it will invoke this method for
24982 * each mustache which is encountered. The function is invoked with four
24983 * arguments:
24984 *
24985 * - [model]: The data context for which this instance is being created.
24986 * - [path]: The text contents (trimmed of outer whitespace) of the mustache.
24987 * - [name]: The context in which the mustache occurs. Within element
24988 * attributes, this will be the name of the attribute. Within text,
24989 * this will be 'text'.
24990 * - [node]: A reference to the node to which this binding will be created.
24991 *
24992 * If the method wishes to handle binding, it is required to return an object
24993 * which has at least a `value` property that can be observed. If it does,
24994 * then MDV will call [Node.bind on the node:
24995 *
24996 * node.bind(name, retval, 'value');
24997 *
24998 * If the 'getBinding' does not wish to override the binding, it should return
24999 * null.
25000 */
24987 // TODO(jmesserly): I had to remove type annotations from "name" and "node" 25001 // TODO(jmesserly): I had to remove type annotations from "name" and "node"
24988 // Normally they are String and Node respectively. But sometimes it will pass 25002 // Normally they are String and Node respectively. But sometimes it will pass
24989 // (int name, CompoundBinding node). That seems very confusing; we may want 25003 // (int name, CompoundBinding node). That seems very confusing; we may want
24990 // to change this API. 25004 // to change this API.
24991 getBinding(model, String path, name, node); 25005 getBinding(model, String path, name, node) => null;
25006
25007 /**
25008 * This syntax method allows a syntax to provide an alterate model than the
25009 * one the template would otherwise use when producing an instance.
25010 *
25011 * When a template is about to create an instance, it will invoke this method
25012 * The function is invoked with two arguments:
25013 *
25014 * - [template]: The template element which is about to create and insert an
25015 * instance.
25016 * - [model]: The data context for which this instance is being created.
25017 *
25018 * The template element will always use the return value of `getInstanceModel`
25019 * as the model for the new instance. If the syntax does not wish to override
25020 * the value, it should simply return the `model` value it was passed.
25021 */
25022 getInstanceModel(Element template, model) => model;
25023
25024 /**
25025 * This syntax method allows a syntax to provide an alterate expansion of
25026 * the [template] contents. When the template wants to create an instance,
25027 * it will call this method with the template element.
25028 *
25029 * By default this will call `template.createInstance()`.
25030 */
25031 getInstanceFragment(Element template) => template.createInstance();
24992 } 25032 }
24993 25033
24994 /** The callback used in the [CompoundBinding.combinator] field. */ 25034 /** The callback used in the [CompoundBinding.combinator] field. */
24995 @Experimental 25035 @Experimental
24996 typedef Object CompoundBindingCombinator(Map objects); 25036 typedef Object CompoundBindingCombinator(Map objects);
24997 25037
24998 /** Information about the instantiated template. */ 25038 /** Information about the instantiated template. */
24999 @Experimental 25039 @Experimental
25000 class TemplateInstance { 25040 class TemplateInstance {
25001 // TODO(rafaelw): firstNode & lastNode should be read-synchronous 25041 // TODO(rafaelw): firstNode & lastNode should be read-synchronous
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
25349 templateDescendents.forEach(bootstrap); 25389 templateDescendents.forEach(bootstrap);
25350 } 25390 }
25351 25391
25352 final String _allTemplatesSelectors = 'template, option[template], ' + 25392 final String _allTemplatesSelectors = 'template, option[template], ' +
25353 Element._TABLE_TAGS.keys.map((k) => "$k[template]").join(", "); 25393 Element._TABLE_TAGS.keys.map((k) => "$k[template]").join(", ");
25354 25394
25355 void _addBindings(Node node, model, [CustomBindingSyntax syntax]) { 25395 void _addBindings(Node node, model, [CustomBindingSyntax syntax]) {
25356 if (node is Element) { 25396 if (node is Element) {
25357 _addAttributeBindings(node, model, syntax); 25397 _addAttributeBindings(node, model, syntax);
25358 } else if (node is Text) { 25398 } else if (node is Text) {
25359 _parseAndBind(node, node.text, 'text', model, syntax); 25399 _parseAndBind(node, 'text', node.text, model, syntax);
25360 } 25400 }
25361 25401
25362 for (var c = node.$dom_firstChild; c != null; c = c.nextNode) { 25402 for (var c = node.$dom_firstChild; c != null; c = c.nextNode) {
25363 _addBindings(c, model, syntax); 25403 _addBindings(c, model, syntax);
25364 } 25404 }
25365 } 25405 }
25366 25406
25367 25407
25368 void _addAttributeBindings(Element element, model, syntax) { 25408 void _addAttributeBindings(Element element, model, syntax) {
25369 element.attributes.forEach((name, value) { 25409 element.attributes.forEach((name, value) {
25370 if (value == '' && (name == 'bind' || name == 'repeat')) { 25410 if (value == '' && (name == 'bind' || name == 'repeat')) {
25371 value = '{{}}'; 25411 value = '{{}}';
25372 } 25412 }
25373 _parseAndBind(element, value, name, model, syntax); 25413 _parseAndBind(element, name, value, model, syntax);
25374 }); 25414 });
25375 } 25415 }
25376 25416
25377 void _parseAndBind(Node node, String text, String name, model, 25417 void _parseAndBind(Node node, String name, String text, model,
25378 CustomBindingSyntax syntax) { 25418 CustomBindingSyntax syntax) {
25379 25419
25380 var tokens = _parseMustacheTokens(text); 25420 var tokens = _parseMustacheTokens(text);
25381 if (tokens.length == 0 || (tokens.length == 1 && tokens[0].isText)) { 25421 if (tokens.length == 0 || (tokens.length == 1 && tokens[0].isText)) {
25382 return; 25422 return;
25383 } 25423 }
25384 25424
25385 if (tokens.length == 1 && tokens[0].isBinding) { 25425 if (tokens.length == 1 && tokens[0].isBinding) {
25386 _bindOrDelegate(node, name, model, tokens[0].value, syntax); 25426 _bindOrDelegate(node, name, model, tokens[0].value, syntax);
25387 return; 25427 return;
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
25502 var templateIterator = child._templateIterator; 25542 var templateIterator = child._templateIterator;
25503 if (templateIterator != null) { 25543 if (templateIterator != null) {
25504 templateIterator.abandon(); 25544 templateIterator.abandon();
25505 child._templateIterator = null; 25545 child._templateIterator = null;
25506 } 25546 }
25507 } 25547 }
25508 child.remove(); 25548 child.remove();
25509 _removeAllBindingsRecursively(child); 25549 _removeAllBindingsRecursively(child);
25510 } 25550 }
25511 25551
25512 class _InstanceCursor {
25513 final Element _template;
25514 Node _terminator;
25515 Node _previousTerminator;
25516 int _previousIndex = -1;
25517 int _index = 0;
25518
25519 _InstanceCursor(this._template, [index]) {
25520 _terminator = _template;
25521 if (index != null) {
25522 while (index-- > 0) {
25523 next();
25524 }
25525 }
25526 }
25527
25528 void next() {
25529 _previousTerminator = _terminator;
25530 _previousIndex = _index;
25531 _index++;
25532
25533 while (_index > _terminator._instanceTerminatorCount) {
25534 _index -= _terminator._instanceTerminatorCount;
25535 _terminator = _terminator.nextNode;
25536 if (_terminator is Element && _terminator.tagName == 'TEMPLATE') {
25537 _index += _instanceCount(_terminator);
25538 }
25539 }
25540 }
25541
25542 void abandon() {
25543 assert(_instanceCount(_template) > 0);
25544 assert(_terminator._instanceTerminatorCount > 0);
25545 assert(_index > 0);
25546
25547 _terminator._instanceTerminatorCount--;
25548 _index--;
25549 }
25550
25551 void insert(fragment) {
25552 assert(_template.parentNode != null);
25553
25554 _previousTerminator = _terminator;
25555 _previousIndex = _index;
25556 _index++;
25557
25558 _terminator = fragment.$dom_lastChild;
25559 if (_terminator == null) _terminator = _previousTerminator;
25560 _template.parentNode.insertBefore(fragment, _previousTerminator.nextNode);
25561
25562 _terminator._instanceTerminatorCount++;
25563 if (_terminator != _previousTerminator) {
25564 while (_previousTerminator._instanceTerminatorCount >
25565 _previousIndex) {
25566 _previousTerminator._instanceTerminatorCount--;
25567 _terminator._instanceTerminatorCount++;
25568 }
25569 }
25570 }
25571
25572 void remove() {
25573 assert(_previousIndex != -1);
25574 assert(_previousTerminator != null &&
25575 (_previousIndex > 0 || _previousTerminator == _template));
25576 assert(_terminator != null && _index > 0);
25577 assert(_template.parentNode != null);
25578 assert(_instanceCount(_template) > 0);
25579
25580 if (_previousTerminator == _terminator) {
25581 assert(_index == _previousIndex + 1);
25582 _terminator._instanceTerminatorCount--;
25583 _terminator = _template;
25584 _previousTerminator = null;
25585 _previousIndex = -1;
25586 return;
25587 }
25588
25589 _terminator._instanceTerminatorCount--;
25590
25591 var parent = _template.parentNode;
25592 while (_previousTerminator.nextNode != _terminator) {
25593 _removeTemplateChild(parent, _previousTerminator.nextNode);
25594 }
25595 _removeTemplateChild(parent, _terminator);
25596
25597 _terminator = _previousTerminator;
25598 _index = _previousIndex;
25599 _previousTerminator = null;
25600 _previousIndex = -1; // 0?
25601 }
25602 }
25603
25604 25552
25605 class _TemplateIterator { 25553 class _TemplateIterator {
25606 final Element _templateElement; 25554 final Element _templateElement;
25607 int instanceCount = 0; 25555 final List<Node> terminators = [];
25556 final CompoundBinding inputs;
25608 List iteratedValue; 25557 List iteratedValue;
25609 bool observing = false;
25610 final CompoundBinding inputs;
25611 25558
25612 StreamSubscription _sub; 25559 StreamSubscription _sub;
25613 StreamSubscription _valueBinding; 25560 StreamSubscription _valueBinding;
25614 25561
25615 _TemplateIterator(this._templateElement) 25562 _TemplateIterator(this._templateElement)
25616 : inputs = new CompoundBinding(resolveInputs) { 25563 : inputs = new CompoundBinding(resolveInputs) {
25617 25564
25618 _valueBinding = new PathObserver(inputs, 'value').bindSync(valueChanged); 25565 _valueBinding = new PathObserver(inputs, 'value').bindSync(valueChanged);
25619 } 25566 }
25620 25567
(...skipping 22 matching lines...) Expand all
25643 if (value is Observable) { 25590 if (value is Observable) {
25644 _sub = value.changes.listen(_handleChanges); 25591 _sub = value.changes.listen(_handleChanges);
25645 } 25592 }
25646 25593
25647 int len = iteratedValue.length; 25594 int len = iteratedValue.length;
25648 if (len > 0) { 25595 if (len > 0) {
25649 _handleChanges([new ListChangeRecord(0, addedCount: len)]); 25596 _handleChanges([new ListChangeRecord(0, addedCount: len)]);
25650 } 25597 }
25651 } 25598 }
25652 25599
25653 // TODO(jmesserly): port MDV v3. 25600 Node getTerminatorAt(int index) {
25654 getInstanceModel(model, syntax) => model; 25601 if (index == -1) return _templateElement;
25655 getInstanceFragment(syntax) => _templateElement.createInstance(); 25602 var terminator = terminators[index];
25603 if (terminator is! Element) return terminator;
25604
25605 var subIterator = terminator._templateIterator;
25606 if (subIterator == null) return terminator;
25607
25608 return subIterator.getTerminatorAt(subIterator.terminators.length - 1);
25609 }
25610
25611 void insertInstanceAt(int index, Node fragment) {
25612 var previousTerminator = getTerminatorAt(index - 1);
25613 var terminator = fragment.$dom_lastChild;
25614 if (terminator == null) terminator = previousTerminator;
25615
25616 terminators.insert(index, terminator);
25617 var parent = _templateElement.parentNode;
25618 parent.insertBefore(fragment, previousTerminator.nextNode);
25619 }
25620
25621 void removeInstanceAt(int index) {
25622 var previousTerminator = getTerminatorAt(index - 1);
25623 var terminator = getTerminatorAt(index);
25624 terminators.removeAt(index);
25625
25626 var parent = _templateElement.parentNode;
25627 while (terminator != previousTerminator) {
25628 var node = terminator;
25629 terminator = node.previousNode;
25630 _removeTemplateChild(parent, node);
25631 }
25632 }
25633
25634 void removeAllInstances() {
25635 if (terminators.length == 0) return;
25636
25637 var previousTerminator = _templateElement;
25638 var terminator = getTerminatorAt(terminators.length - 1);
25639 terminators.length = 0;
25640
25641 var parent = _templateElement.parentNode;
25642 while (terminator != previousTerminator) {
25643 var node = terminator;
25644 terminator = node.previousNode;
25645 _removeTemplateChild(parent, node);
25646 }
25647 }
25648
25649 void clear() {
25650 unobserve();
25651 removeAllInstances();
25652 iteratedValue = null;
25653 }
25654
25655 getInstanceModel(model, syntax) {
25656 if (syntax != null) {
25657 return syntax.getInstanceModel(_templateElement, model);
25658 }
25659 return model;
25660 }
25661
25662 getInstanceFragment(syntax) {
25663 if (syntax != null) {
25664 return syntax.getInstanceFragment(_templateElement);
25665 }
25666 return _templateElement.createInstance();
25667 }
25656 25668
25657 void _handleChanges(List<ListChangeRecord> splices) { 25669 void _handleChanges(List<ListChangeRecord> splices) {
25658 var syntax = TemplateElement.syntax[_templateElement.attributes['syntax']]; 25670 var syntax = TemplateElement.syntax[_templateElement.attributes['syntax']];
25659 25671
25660 for (var splice in splices) { 25672 for (var splice in splices) {
25661 if (splice is! ListChangeRecord) continue; 25673 if (splice is! ListChangeRecord) continue;
25662 25674
25663 for (int i = 0; i < splice.removedCount; i++) { 25675 for (int i = 0; i < splice.removedCount; i++) {
25664 var cursor = new _InstanceCursor(_templateElement, splice.index + 1); 25676 removeInstanceAt(splice.index);
25665 cursor.remove();
25666 instanceCount--;
25667 } 25677 }
25668 25678
25669 for (var addIndex = splice.index; 25679 for (var addIndex = splice.index;
25670 addIndex < splice.index + splice.addedCount; 25680 addIndex < splice.index + splice.addedCount;
25671 addIndex++) { 25681 addIndex++) {
25672 25682
25673 var model = getInstanceModel(iteratedValue[addIndex], syntax); 25683 var model = getInstanceModel(iteratedValue[addIndex], syntax);
25684
25674 var fragment = getInstanceFragment(syntax); 25685 var fragment = getInstanceFragment(syntax);
25675 25686
25676 _addBindings(fragment, model, syntax); 25687 _addBindings(fragment, model, syntax);
25677 _addTemplateInstanceRecord(fragment, model); 25688 _addTemplateInstanceRecord(fragment, model);
25678 25689
25679 var cursor = new _InstanceCursor(_templateElement, addIndex); 25690 insertInstanceAt(addIndex, fragment);
25680 cursor.insert(fragment);
25681 instanceCount++;
25682 } 25691 }
25683 } 25692 }
25684 } 25693 }
25685 25694
25686 void unobserve() { 25695 void unobserve() {
25687 if (_sub == null) return; 25696 if (_sub == null) return;
25688 _sub.cancel(); 25697 _sub.cancel();
25689 _sub = null; 25698 _sub = null;
25690 } 25699 }
25691 25700
25692 void clear() {
25693 unobserve();
25694
25695 iteratedValue = null;
25696 if (instanceCount == 0) return;
25697
25698 for (var i = 0; i < instanceCount; i++) {
25699 var cursor = new _InstanceCursor(_templateElement, 1);
25700 cursor.remove();
25701 }
25702
25703 instanceCount = 0;
25704 }
25705
25706 void abandon() { 25701 void abandon() {
25707 unobserve(); 25702 unobserve();
25708 _valueBinding.cancel(); 25703 _valueBinding.cancel();
25709 inputs.dispose(); 25704 inputs.dispose();
25710 } 25705 }
25711 } 25706 }
25712
25713 int _instanceCount(Element element) {
25714 var templateIterator = element._templateIterator;
25715 return templateIterator != null ? templateIterator.instanceCount : 0;
25716 }
25717 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 25707 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
25718 // for details. All rights reserved. Use of this source code is governed by a 25708 // for details. All rights reserved. Use of this source code is governed by a
25719 // BSD-style license that can be found in the LICENSE file. 25709 // BSD-style license that can be found in the LICENSE file.
25720 25710
25721 25711
25722 class _HttpRequestUtils { 25712 class _HttpRequestUtils {
25723 25713
25724 // Helper for factory HttpRequest.get 25714 // Helper for factory HttpRequest.get
25725 static HttpRequest get(String url, 25715 static HttpRequest get(String url,
25726 onComplete(HttpRequest request), 25716 onComplete(HttpRequest request),
(...skipping 1208 matching lines...) Expand 10 before | Expand all | Expand 10 after
26935 _position = nextPosition; 26925 _position = nextPosition;
26936 return true; 26926 return true;
26937 } 26927 }
26938 _current = null; 26928 _current = null;
26939 _position = _array.length; 26929 _position = _array.length;
26940 return false; 26930 return false;
26941 } 26931 }
26942 26932
26943 T get current => _current; 26933 T get current => _current;
26944 } 26934 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/html/dartium/html_dartium.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698