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