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