OLD | NEW |
(Empty) | |
| 1 /** Support code for the tests in this directory. */ |
| 2 library support; |
| 3 |
| 4 import 'dart:async'; |
| 5 import 'dart:io'; |
| 6 import 'dart:collection'; |
| 7 import 'package:path/path.dart' as path; |
| 8 import 'package:html5lib/src/treebuilder.dart'; |
| 9 import 'package:html5lib/dom.dart'; |
| 10 import 'package:html5lib/dom_parsing.dart'; |
| 11 |
| 12 typedef TreeBuilder TreeBuilderFactory(bool namespaceHTMLElements); |
| 13 |
| 14 Map _treeTypes; |
| 15 Map<String, TreeBuilderFactory> get treeTypes { |
| 16 if (_treeTypes == null) { |
| 17 // TODO(jmesserly): add DOM here once it's implemented |
| 18 _treeTypes = { "simpletree": (useNs) => new TreeBuilder(useNs) }; |
| 19 } |
| 20 return _treeTypes; |
| 21 } |
| 22 |
| 23 final testDataDir = path.join(path.dirname(new Options().script), 'data'); |
| 24 |
| 25 Iterable<String> getDataFiles(String subdirectory) { |
| 26 var dir = new Directory(path.join(testDataDir, subdirectory)); |
| 27 return dir.listSync().where((f) => f is File).map((f) => f.path); |
| 28 } |
| 29 |
| 30 // TODO(jmesserly): make this class simpler. We could probably split on |
| 31 // "\n#" instead of newline and remove a lot of code. |
| 32 class TestData extends IterableBase<Map> { |
| 33 final String _text; |
| 34 final String newTestHeading; |
| 35 |
| 36 TestData(String filename, [this.newTestHeading = "data"]) |
| 37 // Note: can't use readAsLinesSync here because it splits on \r |
| 38 : _text = new File(filename).readAsStringSync(); |
| 39 |
| 40 // Note: in Python this was a generator, but since we can't do that in Dart, |
| 41 // it's easier to convert it into an upfront computation. |
| 42 Iterator<Map> get iterator => _getData().iterator; |
| 43 |
| 44 List<Map> _getData() { |
| 45 var data = <String, String>{}; |
| 46 var key = null; |
| 47 var result = <Map>[]; |
| 48 var lines = _text.split('\n'); |
| 49 int numLines = lines.length; |
| 50 // Remove trailing newline to match Python |
| 51 if (lines.last == '') { |
| 52 lines.removeLast(); |
| 53 } |
| 54 for (var line in lines) { |
| 55 var heading = sectionHeading(line); |
| 56 if (heading != null) { |
| 57 if (data.length > 0 && heading == newTestHeading) { |
| 58 // Remove trailing newline |
| 59 data[key] = data[key].substring(0, data[key].length - 1); |
| 60 result.add(normaliseOutput(data)); |
| 61 data = <String, String>{}; |
| 62 } |
| 63 key = heading; |
| 64 data[key] = ""; |
| 65 } else if (key != null) { |
| 66 data[key] = '${data[key]}$line\n'; |
| 67 } |
| 68 } |
| 69 |
| 70 if (data.length > 0) { |
| 71 result.add(normaliseOutput(data)); |
| 72 } |
| 73 return result; |
| 74 } |
| 75 |
| 76 /** |
| 77 * If the current heading is a test section heading return the heading, |
| 78 * otherwise return null. |
| 79 */ |
| 80 static String sectionHeading(String line) { |
| 81 return line.startsWith("#") ? line.substring(1).trim() : null; |
| 82 } |
| 83 |
| 84 static Map normaliseOutput(Map data) { |
| 85 // Remove trailing newlines |
| 86 data.forEach((key, value) { |
| 87 if (value.endsWith("\n")) { |
| 88 data[key] = value.substring(0, value.length - 1); |
| 89 } |
| 90 }); |
| 91 return data; |
| 92 } |
| 93 } |
| 94 |
| 95 /** |
| 96 * Serialize the [document] into the html5 test data format. |
| 97 */ |
| 98 testSerializer(Document document) { |
| 99 return (new TestSerializer()..visit(document)).toString(); |
| 100 } |
| 101 |
| 102 /** Serializes the DOM into test format. See [testSerializer]. */ |
| 103 class TestSerializer extends TreeVisitor { |
| 104 final StringBuffer _str; |
| 105 int _indent = 0; |
| 106 String _spaces = ''; |
| 107 |
| 108 TestSerializer() : _str = new StringBuffer(); |
| 109 |
| 110 String toString() => _str.toString(); |
| 111 |
| 112 int get indent => _indent; |
| 113 |
| 114 set indent(int value) { |
| 115 if (_indent == value) return; |
| 116 |
| 117 var arr = new List<int>(value); |
| 118 for (int i = 0; i < value; i++) { |
| 119 arr[i] = 32; |
| 120 } |
| 121 _spaces = new String.fromCharCodes(arr); |
| 122 _indent = value; |
| 123 } |
| 124 |
| 125 void _newline() { |
| 126 if (_str.length > 0) _str.write('\n'); |
| 127 _str.write('|$_spaces'); |
| 128 } |
| 129 |
| 130 visitNodeFallback(Node node) { |
| 131 _newline(); |
| 132 _str.write(node); |
| 133 visitChildren(node); |
| 134 } |
| 135 |
| 136 visitChildren(Node node) { |
| 137 indent += 2; |
| 138 for (var child in node.nodes) visit(child); |
| 139 indent -= 2; |
| 140 } |
| 141 |
| 142 visitDocument(Document node) { |
| 143 indent += 1; |
| 144 for (var child in node.nodes) visit(child); |
| 145 indent -= 1; |
| 146 } |
| 147 |
| 148 visitElement(Element node) { |
| 149 _newline(); |
| 150 _str.write(node); |
| 151 if (node.attributes.length > 0) { |
| 152 indent += 2; |
| 153 var keys = new List.from(node.attributes.keys); |
| 154 keys.sort((x, y) => x.compareTo(y)); |
| 155 for (var key in keys) { |
| 156 var v = node.attributes[key]; |
| 157 if (key is AttributeName) { |
| 158 AttributeName attr = key; |
| 159 key = "${attr.prefix} ${attr.name}"; |
| 160 } |
| 161 _newline(); |
| 162 _str.write('$key="$v"'); |
| 163 } |
| 164 indent -= 2; |
| 165 } |
| 166 visitChildren(node); |
| 167 } |
| 168 } |
OLD | NEW |