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

Side by Side Diff: pkg/analysis_server/tool/spec/html_tools.dart

Issue 725143004: Format and sort analyzer and analysis_server packages. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month 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
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /** 5 /**
6 * Tools for HTML manipulation. 6 * Tools for HTML manipulation.
7 */ 7 */
8 library html.tools; 8 library html.tools;
9 9
10 import 'package:html5lib/dom.dart' as dom; 10 import 'package:html5lib/dom.dart' as dom;
11 11
12 /** 12 /**
13 * Make a deep copy of the given HTML nodes. 13 * Make a deep copy of the given HTML nodes.
14 */ 14 */
15 List<dom.Node> cloneHtmlNodes(List<dom.Node> nodes) => nodes.map((dom.Node node) 15 List<dom.Node> cloneHtmlNodes(List<dom.Node> nodes) =>
16 => node.clone(true)).toList(); 16 nodes.map((dom.Node node) => node.clone(true)).toList();
17 17
18 /** 18 /**
19 * Create an HTML element with the given name, attributes, and child nodes. 19 * Return true if the given iterable contains only whitespace text nodes.
20 */ 20 */
21 dom.Element makeElement(String name, Map<dynamic, String> 21 bool containsOnlyWhitespace(Iterable<dom.Node> nodes) {
22 attributes, List<dom.Node> children) { 22 for (dom.Node node in nodes) {
23 dom.Element result = new dom.Element.tag(name); 23 if (!isWhitespaceNode(node)) {
24 result.attributes.addAll(attributes); 24 return false;
25 for (dom.Node child in children) { 25 }
26 result.append(child);
27 } 26 }
28 return result; 27 return true;
29 } 28 }
30 29
31 /** 30 /**
32 * Get the text contents of the element, ignoring all markup. 31 * Get the text contents of the element, ignoring all markup.
33 */ 32 */
34 String innerText(dom.Element parent) { 33 String innerText(dom.Element parent) {
35 StringBuffer buffer = new StringBuffer(); 34 StringBuffer buffer = new StringBuffer();
36 void recurse(dom.Element parent) { 35 void recurse(dom.Element parent) {
37 for (dom.Node child in parent.nodes) { 36 for (dom.Node child in parent.nodes) {
38 if (child is dom.Text) { 37 if (child is dom.Text) {
(...skipping 15 matching lines...) Expand all
54 if (node is dom.Element) { 53 if (node is dom.Element) {
55 return false; 54 return false;
56 } else if (node is dom.Text) { 55 } else if (node is dom.Text) {
57 return node.text.trim().isEmpty; 56 return node.text.trim().isEmpty;
58 } 57 }
59 // Treat all other types of nodes (e.g. comments) as whitespace. 58 // Treat all other types of nodes (e.g. comments) as whitespace.
60 return true; 59 return true;
61 } 60 }
62 61
63 /** 62 /**
64 * Return true if the given iterable contains only whitespace text nodes. 63 * Create an HTML element with the given name, attributes, and child nodes.
65 */ 64 */
66 bool containsOnlyWhitespace(Iterable<dom.Node> nodes) { 65 dom.Element makeElement(String name, Map<dynamic, String> attributes,
67 for (dom.Node node in nodes) { 66 List<dom.Node> children) {
68 if (!isWhitespaceNode(node)) { 67 dom.Element result = new dom.Element.tag(name);
69 return false; 68 result.attributes.addAll(attributes);
70 } 69 for (dom.Node child in children) {
70 result.append(child);
71 } 71 }
72 return true; 72 return result;
73 } 73 }
74 74
75 /** 75 /**
76 * Mixin class for generating HTML. 76 * Mixin class for generating HTML.
77 */ 77 */
78 class HtmlGenerator { 78 class HtmlGenerator {
79 List<dom.Node> _html; 79 List<dom.Node> _html;
80 80
81 /** 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 /**
82 * Execute [callback], collecting any code that is output using [write], 98 * Execute [callback], collecting any code that is output using [write],
83 * [writeln], [add], or [addAll], and return the result as a list of HTML 99 * [writeln], [add], or [addAll], and return the result as a list of HTML
84 * nodes. 100 * nodes.
85 */ 101 */
86 List<dom.Node> collectHtml(void callback()) { 102 List<dom.Node> collectHtml(void callback()) {
87 List<dom.Node> oldHtml = _html; 103 List<dom.Node> oldHtml = _html;
88 try { 104 try {
89 _html = <dom.Node>[]; 105 _html = <dom.Node>[];
90 if (callback != null) { 106 if (callback != null) {
91 callback(); 107 callback();
92 } 108 }
93 return _html; 109 return _html;
94 } finally { 110 } finally {
95 _html = oldHtml; 111 _html = oldHtml;
96 } 112 }
97 } 113 }
98 114
99 /** 115 /**
100 * Add the given [node] to the HTML output. 116 * Execute [callback], wrapping its output in an element with the given
117 * [name] and [attributes].
101 */ 118 */
102 void add(dom.Node node) { 119 void element(String name, Map<dynamic, String> attributes, [void
103 _html.add(node); 120 callback()]) {
121 add(makeElement(name, attributes, collectHtml(callback)));
104 } 122 }
105 123
106 /** 124 /**
107 * Add the given [nodes] to the HTML output.
108 */
109 void addAll(Iterable<dom.Node> nodes) {
110 for (dom.Node node in nodes) {
111 add(node);
112 }
113 }
114
115 /**
116 * Output text without ending the current line. 125 * Output text without ending the current line.
117 */ 126 */
118 void write(String text) { 127 void write(String text) {
119 _html.add(new dom.Text(text)); 128 _html.add(new dom.Text(text));
120 } 129 }
121 130
122 /** 131 /**
123 * Output text, ending the current line. 132 * Output text, ending the current line.
124 */ 133 */
125 void writeln([Object obj = '']) { 134 void writeln([Object obj = '']) {
126 write('$obj\n'); 135 write('$obj\n');
127 } 136 }
128
129 /**
130 * Execute [callback], wrapping its output in an element with the given
131 * [name] and [attributes].
132 */
133 void element(String name, Map<dynamic, String> attributes, [void callback()]) {
134 add(makeElement(name, attributes, collectHtml(callback)));
135 }
136 } 137 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/tool/spec/generate_all.dart ('k') | pkg/analysis_server/tool/spec/implied_types.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698