| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, 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 /** | |
| 6 * Tools for HTML manipulation. | |
| 7 */ | |
| 8 library html.tools; | |
| 9 | |
| 10 import 'package:html/dom.dart' as dom; | |
| 11 | |
| 12 /** | |
| 13 * Make a deep copy of the given HTML nodes. | |
| 14 */ | |
| 15 List<dom.Node> cloneHtmlNodes(List<dom.Node> nodes) => | |
| 16 nodes.map((dom.Node node) => node.clone(true)).toList(); | |
| 17 | |
| 18 /** | |
| 19 * Return true if the given iterable contains only whitespace text nodes. | |
| 20 */ | |
| 21 bool containsOnlyWhitespace(Iterable<dom.Node> nodes) { | |
| 22 for (dom.Node node in nodes) { | |
| 23 if (!isWhitespaceNode(node)) { | |
| 24 return false; | |
| 25 } | |
| 26 } | |
| 27 return true; | |
| 28 } | |
| 29 | |
| 30 /** | |
| 31 * Get the text contents of the element, ignoring all markup. | |
| 32 */ | |
| 33 String innerText(dom.Element parent) { | |
| 34 StringBuffer buffer = new StringBuffer(); | |
| 35 void recurse(dom.Element parent) { | |
| 36 for (dom.Node child in parent.nodes) { | |
| 37 if (child is dom.Text) { | |
| 38 buffer.write(child.text); | |
| 39 } else if (child is dom.Element) { | |
| 40 recurse(child); | |
| 41 } | |
| 42 } | |
| 43 } | |
| 44 recurse(parent); | |
| 45 return buffer.toString(); | |
| 46 } | |
| 47 | |
| 48 /** | |
| 49 * Return true if the given node is a text node containing only whitespace, or | |
| 50 * a comment. | |
| 51 */ | |
| 52 bool isWhitespaceNode(dom.Node node) { | |
| 53 if (node is dom.Element) { | |
| 54 return false; | |
| 55 } else if (node is dom.Text) { | |
| 56 return node.text.trim().isEmpty; | |
| 57 } | |
| 58 // Treat all other types of nodes (e.g. comments) as whitespace. | |
| 59 return true; | |
| 60 } | |
| 61 | |
| 62 /** | |
| 63 * Create an HTML element with the given name, attributes, and child nodes. | |
| 64 */ | |
| 65 dom.Element makeElement( | |
| 66 String name, Map<dynamic, String> attributes, List<dom.Node> children) { | |
| 67 dom.Element result = new dom.Element.tag(name); | |
| 68 result.attributes.addAll(attributes); | |
| 69 for (dom.Node child in children) { | |
| 70 result.append(child); | |
| 71 } | |
| 72 return result; | |
| 73 } | |
| 74 | |
| 75 /** | |
| 76 * Mixin class for generating HTML. | |
| 77 */ | |
| 78 class HtmlGenerator { | |
| 79 List<dom.Node> _html; | |
| 80 | |
| 81 /** | |
| 82 * Add the given [node] to the HTML output. | |
| 83 */ | |
| 84 void add(dom.Node node) { | |
| 85 _html.add(node); | |
| 86 } | |
| 87 | |
| 88 /** | |
| 89 * Add the given [nodes] to the HTML output. | |
| 90 */ | |
| 91 void addAll(Iterable<dom.Node> nodes) { | |
| 92 for (dom.Node node in nodes) { | |
| 93 add(node); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 /** | |
| 98 * Execute [callback], collecting any code that is output using [write], | |
| 99 * [writeln], [add], [addAll] or [element], and return the result as a list | |
| 100 * of HTML nodes. | |
| 101 */ | |
| 102 List<dom.Node> collectHtml(void callback()) { | |
| 103 List<dom.Node> oldHtml = _html; | |
| 104 try { | |
| 105 _html = <dom.Node>[]; | |
| 106 if (callback != null) { | |
| 107 callback(); | |
| 108 } | |
| 109 return _html; | |
| 110 } finally { | |
| 111 _html = oldHtml; | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 /** | |
| 116 * Execute [callback], wrapping its output in an element with the given | |
| 117 * [name] and [attributes]. | |
| 118 */ | |
| 119 void element(String name, Map<dynamic, String> attributes, | |
| 120 [void callback()]) { | |
| 121 add(makeElement(name, attributes, collectHtml(callback))); | |
| 122 } | |
| 123 | |
| 124 /** | |
| 125 * Output text without ending the current line. | |
| 126 */ | |
| 127 void write(String text) { | |
| 128 _html.add(new dom.Text(text)); | |
| 129 } | |
| 130 | |
| 131 /** | |
| 132 * Output text, ending the current line. | |
| 133 */ | |
| 134 void writeln([Object obj = '']) { | |
| 135 write('$obj\n'); | |
| 136 } | |
| 137 } | |
| OLD | NEW |