OLD | NEW |
1 /** | 1 /** |
2 * This library contains extra APIs that aren't in the DOM, but are useful | 2 * This library contains extra APIs that aren't in the DOM, but are useful |
3 * when interacting with the parse tree. For example, [SourceSpan] and | 3 * when interacting with the parse tree. For example, [SourceSpan] and |
4 * [TreeVisitor]. | 4 * [TreeVisitor]. |
5 */ | 5 */ |
6 library dom_parsing; | 6 library dom_parsing; |
7 | 7 |
8 import 'dart:math'; | 8 import 'dart:math'; |
9 import 'dart:utf' show codepointsToString; | 9 import 'dart:utf' show codepointsToString; |
10 import 'dom.dart'; | 10 import 'dom.dart'; |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 } | 126 } |
127 | 127 |
128 /** Gets the 0-based column in the file for this offset. */ | 128 /** Gets the 0-based column in the file for this offset. */ |
129 int getColumn(int line, int offset) { | 129 int getColumn(int line, int offset) { |
130 return offset - _lineStarts[line]; | 130 return offset - _lineStarts[line]; |
131 } | 131 } |
132 | 132 |
133 /** Gets the text at the given offsets. */ | 133 /** Gets the text at the given offsets. */ |
134 String getText(int start, [int end]) { | 134 String getText(int start, [int end]) { |
135 if (_decodedChars == null) { | 135 if (_decodedChars == null) { |
136 throw new UnsupportedOperationException('getText is only supported ' | 136 throw new UnsupportedError('getText is only supported ' |
137 'if parser.generateSpans is true.'); | 137 'if parser.generateSpans is true.'); |
138 } | 138 } |
139 | 139 |
140 return codepointsToString(_decodedChars.getRange(start, end - start)); | 140 return codepointsToString(_decodedChars.getRange(start, end - start)); |
141 } | 141 } |
142 | 142 |
143 /** | 143 /** |
144 * Create a pretty string representation from a character position | 144 * Create a pretty string representation from a character position |
145 * in the file. | 145 * in the file. |
146 */ | 146 */ |
147 String getLocationMessage(String filename, String message, int start, | 147 String getLocationMessage(String filename, String message, int start, |
148 [int end, bool includeText = true, bool useColors = false]) { | 148 [int end, bool includeText = true, bool useColors = false]) { |
149 | 149 |
150 // Color constants used for generating messages. | 150 // Color constants used for generating messages. |
151 // TODO(jmesserly): it would be more useful to pass in an object that | 151 // TODO(jmesserly): it would be more useful to pass in an object that |
152 // controls how the errors are printed. This method is a bit too smart. | 152 // controls how the errors are printed. This method is a bit too smart. |
153 final String RED_COLOR = '\u001b[31m'; | 153 final String RED_COLOR = '\u001b[31m'; |
154 final String NO_COLOR = '\u001b[0m'; | 154 final String NO_COLOR = '\u001b[0m'; |
155 | 155 |
156 var line = getLine(start); | 156 var line = getLine(start); |
157 var column = getColumn(line, start); | 157 var column = getColumn(line, start); |
158 | 158 |
159 var msg = '$filename:${line + 1}:${column + 1}: $message'; | 159 var msg = '$filename:${line + 1}:${column + 1}: $message'; |
160 if (!includeText) return msg; | 160 if (!includeText) return msg; |
161 | 161 |
162 if (_decodedChars == null) { | 162 if (_decodedChars == null) { |
163 throw new UnsupportedOperationException('includeText is only supported ' | 163 throw new UnsupportedError('includeText is only supported ' |
164 'if parser.generateSpans is true.'); | 164 'if parser.generateSpans is true.'); |
165 } | 165 } |
166 | 166 |
167 var buf = new StringBuffer(msg); | 167 var buf = new StringBuffer(msg); |
168 buf.add('\n'); | 168 buf.add('\n'); |
169 var textLine; | 169 var textLine; |
170 | 170 |
171 // +1 for 0-indexing, +1 again to avoid the last line of the file | 171 // +1 for 0-indexing, +1 again to avoid the last line of the file |
172 if ((line + 2) < _lineStarts.length) { | 172 if ((line + 2) < _lineStarts.length) { |
173 textLine = getText(_lineStarts[line], _lineStarts[line + 1]); | 173 textLine = getText(_lineStarts[line], _lineStarts[line + 1]); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 /** A simple tree visitor for the DOM nodes. */ | 205 /** A simple tree visitor for the DOM nodes. */ |
206 class TreeVisitor { | 206 class TreeVisitor { |
207 visit(Node node) { | 207 visit(Node node) { |
208 switch (node.nodeType) { | 208 switch (node.nodeType) { |
209 case Node.ELEMENT_NODE: return visitElement(node); | 209 case Node.ELEMENT_NODE: return visitElement(node); |
210 case Node.TEXT_NODE: return visitText(node); | 210 case Node.TEXT_NODE: return visitText(node); |
211 case Node.COMMENT_NODE: return visitComment(node); | 211 case Node.COMMENT_NODE: return visitComment(node); |
212 case Node.DOCUMENT_FRAGMENT_NODE: return visitDocumentFragment(node); | 212 case Node.DOCUMENT_FRAGMENT_NODE: return visitDocumentFragment(node); |
213 case Node.DOCUMENT_NODE: return visitDocument(node); | 213 case Node.DOCUMENT_NODE: return visitDocument(node); |
214 case Node.DOCUMENT_TYPE_NODE: return visitDocumentType(node); | 214 case Node.DOCUMENT_TYPE_NODE: return visitDocumentType(node); |
215 default: throw new UnsupportedOperationException( | 215 default: throw new UnsupportedError('DOM node type ${node.nodeType}'); |
216 'DOM node type ${node.nodeType}'); | |
217 } | 216 } |
218 } | 217 } |
219 | 218 |
220 visitChildren(Node node) { | 219 visitChildren(Node node) { |
221 for (var child in node.nodes) visit(child); | 220 for (var child in node.nodes) visit(child); |
222 } | 221 } |
223 | 222 |
224 /** | 223 /** |
225 * The fallback handler if the more specific visit method hasn't been | 224 * The fallback handler if the more specific visit method hasn't been |
226 * overriden. Only use this from a subclass of [TreeVisitor], otherwise | 225 * overriden. Only use this from a subclass of [TreeVisitor], otherwise |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 bool isVoidElement(String tagName) { | 359 bool isVoidElement(String tagName) { |
361 switch (tagName) { | 360 switch (tagName) { |
362 case "area": case "base": case "br": case "col": case "command": | 361 case "area": case "base": case "br": case "col": case "command": |
363 case "embed": case "hr": case "img": case "input": case "keygen": | 362 case "embed": case "hr": case "img": case "input": case "keygen": |
364 case "link": case "meta": case "param": case "source": case "track": | 363 case "link": case "meta": case "param": case "source": case "track": |
365 case "wbr": | 364 case "wbr": |
366 return true; | 365 return true; |
367 } | 366 } |
368 return false; | 367 return false; |
369 } | 368 } |
OLD | NEW |