OLD | NEW |
1 /// A simple tree API that results from parsing html. Intended to be compatible | 1 /// A simple tree API that results from parsing html. Intended to be compatible |
2 /// with dart:html, but it is missing many types and APIs. | 2 /// with dart:html, but it is missing many types and APIs. |
3 library dom; | 3 library dom; |
4 | 4 |
5 // TODO(jmesserly): lots to do here. Originally I wanted to generate this using | 5 // TODO(jmesserly): lots to do here. Originally I wanted to generate this using |
6 // our Blink IDL generator, but another idea is to directly use the excellent | 6 // our Blink IDL generator, but another idea is to directly use the excellent |
7 // http://dom.spec.whatwg.org/ and http://html.spec.whatwg.org/ and just | 7 // http://dom.spec.whatwg.org/ and http://html.spec.whatwg.org/ and just |
8 // implement that. | 8 // implement that. |
9 | 9 |
10 import 'dart:collection'; | 10 import 'dart:collection'; |
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 } | 395 } |
396 | 396 |
397 void _addOuterHtml(StringBuffer str) { | 397 void _addOuterHtml(StringBuffer str) { |
398 str.write(toString()); | 398 str.write(toString()); |
399 } | 399 } |
400 | 400 |
401 DocumentType clone(bool deep) => new DocumentType(name, publicId, systemId); | 401 DocumentType clone(bool deep) => new DocumentType(name, publicId, systemId); |
402 } | 402 } |
403 | 403 |
404 class Text extends Node { | 404 class Text extends Node { |
405 String data; | 405 /// The text node's data, stored as either a String or StringBuffer. |
| 406 /// We support storing a StringBuffer here to support fast [appendData]. |
| 407 /// It will flatten back to a String on read. |
| 408 var _data; |
406 | 409 |
407 Text(this.data) : super._(); | 410 Text(String data) : _data = data != null ? data : '', super._(); |
408 | 411 |
409 int get nodeType => Node.TEXT_NODE; | 412 int get nodeType => Node.TEXT_NODE; |
410 | 413 |
| 414 String get data => _data = _data.toString(); |
| 415 set data(String value) { |
| 416 _data = value != null ? value : ''; |
| 417 } |
| 418 |
411 String toString() => '"$data"'; | 419 String toString() => '"$data"'; |
412 | 420 |
413 void _addOuterHtml(StringBuffer str) => writeTextNodeAsHtml(str, this); | 421 void _addOuterHtml(StringBuffer str) => writeTextNodeAsHtml(str, this); |
414 | 422 |
415 Text clone(bool deep) => new Text(data); | 423 Text clone(bool deep) => new Text(data); |
416 | 424 |
| 425 void appendData(String data) { |
| 426 if (_data is! StringBuffer) _data = new StringBuffer(_data); |
| 427 StringBuffer sb = _data; |
| 428 sb.write(data); |
| 429 } |
| 430 |
417 String get text => data; | 431 String get text => data; |
418 set text(String value) { | 432 set text(String value) { |
419 data = value; | 433 data = value; |
420 } | 434 } |
421 } | 435 } |
422 | 436 |
423 // TODO(jmesserly): Elements should have a pointer back to their document | 437 // TODO(jmesserly): Elements should have a pointer back to their document |
424 class Element extends Node with _ParentNode, _ElementAndDocument { | 438 class Element extends Node with _ParentNode, _ElementAndDocument { |
425 final String namespaceUri; | 439 final String namespaceUri; |
426 | 440 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 set innerHtml(String value) { | 546 set innerHtml(String value) { |
533 nodes.clear(); | 547 nodes.clear(); |
534 // TODO(jmesserly): should be able to get the same effect by adding the | 548 // TODO(jmesserly): should be able to get the same effect by adding the |
535 // fragment directly. | 549 // fragment directly. |
536 nodes.addAll(parseFragment(value, container: localName).nodes); | 550 nodes.addAll(parseFragment(value, container: localName).nodes); |
537 } | 551 } |
538 | 552 |
539 void _addOuterHtml(StringBuffer str) { | 553 void _addOuterHtml(StringBuffer str) { |
540 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#
serializing-html-fragments | 554 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#
serializing-html-fragments |
541 // Element is the most complicated one. | 555 // Element is the most complicated one. |
542 str.write('<${_getSerializationPrefix(namespaceUri)}$localName'); | 556 str.write('<'); |
| 557 str.write(_getSerializationPrefix(namespaceUri)); |
| 558 str.write(localName); |
543 | 559 |
544 if (attributes.length > 0) { | 560 if (attributes.length > 0) { |
545 attributes.forEach((key, v) { | 561 attributes.forEach((key, v) { |
546 // Note: AttributeName.toString handles serialization of attribute | 562 // Note: AttributeName.toString handles serialization of attribute |
547 // namespace, if needed. | 563 // namespace, if needed. |
548 str.write(' $key="${htmlSerializeEscape(v, attributeMode: true)}"'); | 564 str.write(' '); |
| 565 str.write(key); |
| 566 str.write('="'); |
| 567 str.write(htmlSerializeEscape(v, attributeMode: true)); |
| 568 str.write('"'); |
549 }); | 569 }); |
550 } | 570 } |
551 | 571 |
552 str.write('>'); | 572 str.write('>'); |
553 | 573 |
554 if (nodes.length > 0) { | 574 if (nodes.length > 0) { |
555 if (localName == 'pre' || | 575 if (localName == 'pre' || |
556 localName == 'textarea' || | 576 localName == 'textarea' || |
557 localName == 'listing') { | 577 localName == 'listing') { |
558 final first = nodes[0]; | 578 final first = nodes[0]; |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
969 | 989 |
970 class _ConcatTextVisitor extends TreeVisitor { | 990 class _ConcatTextVisitor extends TreeVisitor { |
971 final _str = new StringBuffer(); | 991 final _str = new StringBuffer(); |
972 | 992 |
973 String toString() => _str.toString(); | 993 String toString() => _str.toString(); |
974 | 994 |
975 visitText(Text node) { | 995 visitText(Text node) { |
976 _str.write(node.data); | 996 _str.write(node.data); |
977 } | 997 } |
978 } | 998 } |
OLD | NEW |