OLD | NEW |
(Empty) | |
| 1 var namespaces = { |
| 2 "html":"http://www.w3.org/1999/xhtml", |
| 3 "mathml":"http://www.w3.org/1998/Math/MathML", |
| 4 "svg":"http://www.w3.org/2000/svg", |
| 5 "xlink":"http://www.w3.org/1999/xlink", |
| 6 "xml":"http://www.w3.org/XML/1998/namespace", |
| 7 "xmlns":"http://www.w3.org/2000/xmlns/" |
| 8 }; |
| 9 |
| 10 var prefixes = {}; |
| 11 for (var prefix in namespaces) { |
| 12 if (namespaces.hasOwnProperty(prefix)) { |
| 13 prefixes[namespaces[prefix]] = prefix; |
| 14 } |
| 15 } |
| 16 prefixes[namespaces["mathml"]] = "math"; |
| 17 |
| 18 function format(format_string) { |
| 19 var insertions = Array.prototype.slice.call(arguments, 1); |
| 20 var regexp = /%s/g; |
| 21 var match_count = 0; |
| 22 var rv = format_string.replace(regexp, function(match) { |
| 23 var rv = insertions[match_count]; |
| 24 match_count++; |
| 25 return rv; |
| 26 }); |
| 27 return rv; |
| 28 } |
| 29 |
| 30 function test_serializer(element) { |
| 31 //element.normalize(); |
| 32 var lines = []; |
| 33 function serialize_element(element, indent) { |
| 34 var indent_spaces = (new Array(indent)).join(" "); |
| 35 switch(element.nodeType) { |
| 36 case Node.DOCUMENT_TYPE_NODE: |
| 37 if (element.name) { |
| 38 if (element.publicId || element.systemId) { |
| 39 var publicId = element.publicId ? element.publicId : ""; |
| 40 var systemId = element.systemId ? element.systemId : ""; |
| 41 lines.push(format("|%s<!DOCTYPE %s \"%s\" \"%s\">", indent_spaces, |
| 42 element.name, publicId, systemId)); |
| 43 } else { |
| 44 lines.push(format("|%s<!DOCTYPE %s>", indent_spaces, |
| 45 element.name)); |
| 46 } |
| 47 } else { |
| 48 lines.push(format("|%s<!DOCTYPE >", indent_spaces)); |
| 49 } |
| 50 break; |
| 51 case Node.DOCUMENT_NODE: |
| 52 lines.push("#document"); |
| 53 break; |
| 54 case Node.DOCUMENT_FRAGMENT_NODE: |
| 55 lines.push("#document-fragment"); |
| 56 break; |
| 57 case Node.PROCESSING_INSTRUCTION_NODE: |
| 58 lines.push(format("|%s<?%s %s>", indent_spaces, element.target, element.
data)); |
| 59 break; |
| 60 case Node.COMMENT_NODE: |
| 61 lines.push(format("|%s<!-- %s -->", indent_spaces, element.nodeValue)); |
| 62 break; |
| 63 case Node.TEXT_NODE: |
| 64 lines.push(format("|%s\"%s\"", indent_spaces, element.nodeValue)); |
| 65 break; |
| 66 case Node.ELEMENT_NODE: |
| 67 if (element.getAttribute("data-skip") !== null) { |
| 68 return; |
| 69 } |
| 70 if (element.namespaceURI !== null && element.namespaceURI !== namespaces
.html) { |
| 71 var name = format("%s %s", prefixes[element.namespaceURI], |
| 72 element.localName); |
| 73 } else { |
| 74 var name = element.localName; |
| 75 } |
| 76 lines.push(format("|%s<%s>", indent_spaces, name)); |
| 77 |
| 78 var attributes = Array.prototype.map.call( |
| 79 element.attributes, |
| 80 function(attr) { |
| 81 var name = (attr.namespaceURI ? prefixes[attr.namespaceURI] + "
" : "") + |
| 82 attr.localName; |
| 83 return [name, attr.value]; |
| 84 }); |
| 85 attributes.sort(function (a, b) { |
| 86 var x = a[0]; |
| 87 var y = b[0]; |
| 88 if (x === y) { |
| 89 return 0; |
| 90 } |
| 91 return x > y ? 1 : -1; |
| 92 }); |
| 93 |
| 94 attributes.forEach( |
| 95 function(attr) { |
| 96 var indent_spaces = (new Array(indent + 2)).join(" "); |
| 97 lines.push(format("|%s%s=\"%s\"", indent_spaces, attr[0], attr[1])); |
| 98 } |
| 99 ); |
| 100 break; |
| 101 } |
| 102 indent += 2; |
| 103 Array.prototype.forEach.call(element.childNodes, |
| 104 function(node) { |
| 105 serialize_element(node, indent); |
| 106 }); |
| 107 } |
| 108 serialize_element(element, 0); |
| 109 return lines.join("\n"); |
| 110 } |
| 111 |
| 112 function print_diffs(test_id, uri_encoded_input, expected, actual, container) { |
| 113 container = container ? container : null; |
| 114 if (actual) { |
| 115 var diffs = mark_diffs(expected, actual); |
| 116 var expected_text = diffs[0]; |
| 117 var actual_text = diffs[1]; |
| 118 } else { |
| 119 var expected_text = expected; |
| 120 var actual_text = ""; |
| 121 } |
| 122 |
| 123 var tmpl = ["div", {"id":"${test_id}"}, |
| 124 ["h2", {}, "${test_id}"], |
| 125 function(vars) { |
| 126 if (vars.container !== null) { |
| 127 return ["div", {"class":"container"}, |
| 128 ["h3", {}, "innerHTML Container"], |
| 129 ["pre", {}, vars.container]]; |
| 130 } else { |
| 131 return null; |
| 132 } |
| 133 }, |
| 134 ["div", {"id":"input_${test_id}"}, ["h3", {}, "Input"], ["pre", {}
, |
| 135 ["code",
{}, decodeURIComponent(uri_encoded_input)]]], |
| 136 ["div", {"id":"expected_${test_id}"}, ["h3", {}, "Expected"], |
| 137 ["pre", {}, ["code", {}, expected_text]]], |
| 138 ["div", {"id":"actual_${test_id}"}, ["h3", {}, "Actual"], |
| 139 ["pre", {}, ["code", {}, actual_text]]] |
| 140 ]; |
| 141 |
| 142 var diff_dom = template.render(tmpl, {test_id:test_id, container:container}); |
| 143 document.body.appendChild(diff_dom); |
| 144 } |
| 145 |
| 146 function runTests(tests) { |
| 147 tests.forEach(function(test){ |
| 148 var expected = decodeURIComponent(test.expected); |
| 149 var t = async_test(document.title + ' - ' + test.name); |
| 150 t.step(function(){ |
| 151 var video = document.createElement('video'); |
| 152 var track = document.createElement('track'); |
| 153 assert_true('src' in track, 'track not supported'); |
| 154 t.test_id = test.name; |
| 155 t.url_encoded_input = test.input; |
| 156 t.expected = expected; |
| 157 track.src = 'data:text/vtt,'+encodeURIComponent('WEBVTT\n\n00:00.000
--> 00:01.000\n')+test.input; |
| 158 track['default'] = true; |
| 159 track.kind = 'subtitles'; |
| 160 track.onload = t.step_func(trackLoaded); |
| 161 track.onerror = t.step_func(trackError); |
| 162 video.appendChild(track); |
| 163 document.body.appendChild(video); |
| 164 }); |
| 165 }); |
| 166 } |
| 167 |
| 168 function trackLoaded(e) { |
| 169 var track = e.target; |
| 170 setTimeout(removeElm, 0, track.parentNode); |
| 171 var cue = track.track.cues[0]; |
| 172 var frag = cue.getCueAsHTML(); |
| 173 var got = test_serializer(frag); |
| 174 if (got !== this.expected) { |
| 175 print_diffs(this.test_id, this.url_encoded_input, this.expected, got); |
| 176 } |
| 177 assert_equals(got, this.expected); |
| 178 this.done(); |
| 179 } |
| 180 |
| 181 function trackError(e) { |
| 182 setTimeout(removeElm, 0, e.target.parentNode); |
| 183 assert_unreached('got error event'); |
| 184 this.done(); |
| 185 } |
| 186 |
| 187 function removeElm(elm) { |
| 188 document.body.removeChild(elm); |
| 189 } |
OLD | NEW |