| 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 htmlTools; |
| 9 |
| 10 import 'package:html5lib/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) => nodes.map((dom.Node node) |
| 16 => node.clone(true)).toList(); |
| 17 |
| 18 /** |
| 19 * Create an HTML element with the given name, attributes, and child nodes. |
| 20 */ |
| 21 dom.Element makeElement(String name, Map<dynamic, String> |
| 22 attributes, List<dom.Node> children) { |
| 23 dom.Element result = new dom.Element.tag(name); |
| 24 result.attributes.addAll(attributes); |
| 25 for (dom.Node child in children) { |
| 26 result.append(child); |
| 27 } |
| 28 return result; |
| 29 } |
| 30 |
| 31 /** |
| 32 * Get the text contents of the element, ignoring all markup. |
| 33 */ |
| 34 String innerText(dom.Element parent) { |
| 35 StringBuffer buffer = new StringBuffer(); |
| 36 void recurse(dom.Element parent) { |
| 37 for (dom.Node child in parent.nodes) { |
| 38 if (child is dom.Text) { |
| 39 buffer.write(child.text); |
| 40 } else if (child is dom.Element) { |
| 41 recurse(child); |
| 42 } |
| 43 } |
| 44 } |
| 45 recurse(parent); |
| 46 return buffer.toString(); |
| 47 } |
| 48 |
| 49 /** |
| 50 * Mixin class for generating HTML. |
| 51 */ |
| 52 class HtmlGenerator { |
| 53 List<dom.Node> _html; |
| 54 |
| 55 /** |
| 56 * Execute [callback], collecting any code that is output using [write], |
| 57 * [writeln], [add], or [addAll], and return the result as a list of HTML |
| 58 * nodes. |
| 59 */ |
| 60 List<dom.Node> collectHtml(void callback()) { |
| 61 List<dom.Node> oldHtml = _html; |
| 62 try { |
| 63 _html = <dom.Node>[]; |
| 64 if (callback != null) { |
| 65 callback(); |
| 66 } |
| 67 return _html; |
| 68 } finally { |
| 69 _html = oldHtml; |
| 70 } |
| 71 } |
| 72 |
| 73 /** |
| 74 * Add the given [node] to the HTML output. |
| 75 */ |
| 76 void add(dom.Node node) { |
| 77 _html.add(node); |
| 78 } |
| 79 |
| 80 /** |
| 81 * Add the given [nodes] to the HTML output. |
| 82 */ |
| 83 void addAll(Iterable<dom.Node> nodes) { |
| 84 for (dom.Node node in nodes) { |
| 85 add(node); |
| 86 } |
| 87 } |
| 88 |
| 89 /** |
| 90 * Output text without ending the current line. |
| 91 */ |
| 92 void write(String text) { |
| 93 _html.add(new dom.Text(text)); |
| 94 } |
| 95 |
| 96 /** |
| 97 * Output text, ending the current line. |
| 98 */ |
| 99 void writeln([Object obj = '']) { |
| 100 write('$obj\n'); |
| 101 } |
| 102 |
| 103 /** |
| 104 * Execute [callback], wrapping its output in an element with the given |
| 105 * [name] and [attributes]. |
| 106 */ |
| 107 void element(String name, Map<dynamic, String> attributes, [void callback()])
{ |
| 108 add(makeElement(name, attributes, collectHtml(callback))); |
| 109 } |
| 110 } |
| OLD | NEW |