/parser/htmlparser/tests/mochitest/parser_datreader.js

http://github.com/zpao/v8monkey · JavaScript · 227 lines · 138 code · 13 blank · 76 comment · 52 complexity · 11e1e3a202981ae7600a0d87b6a26976 MD5 · raw file

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. * ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is mozilla.org code.
  16. *
  17. * The Initial Developer of the Original Code is Mozilla Foundation.
  18. *
  19. * Portions created by the Initial Developer are Copyright (C) 2007
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * Robert Sayre <sayrer@gmail.com>
  24. * Henri Sivonen <hsivonen@iki.fi>
  25. *
  26. * Alternatively, the contents of this file may be used under the terms of
  27. * either the GNU General Public License Version 2 or later (the "GPL"), or
  28. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29. * in which case the provisions of the GPL or the LGPL are applicable instead
  30. * of those above. If you wish to allow use of your version of this file only
  31. * under the terms of either the GPL or the LGPL, and not to allow others to
  32. * use your version of this file under the terms of the MPL, indicate your
  33. * decision by deleting the provisions above and replace them with the notice
  34. * and other provisions required by the GPL or the LGPL. If you do not delete
  35. * the provisions above, a recipient may use your version of this file under
  36. * the terms of any one of the MPL, the GPL or the LGPL.
  37. *
  38. * ***** END LICENSE BLOCK *****/
  39. /**
  40. * A test suite that runs WHATWG HTML parser tests.
  41. * The tests are from html5lib.
  42. *
  43. * http://html5lib.googlecode.com/
  44. */
  45. /**
  46. * A few utility functions.
  47. */
  48. function log(entry) {
  49. }
  50. function startsWith(s, s2) {
  51. return s.indexOf(s2)==0;
  52. }
  53. function trimString(s) {
  54. return(s.replace(/^\s+/,'').replace(/\s+$/,''));
  55. }
  56. /**
  57. * Parses an individual testcase into an array containing the input
  58. * string, a string representing the expected tree (DOM), and a list
  59. * of error messages.
  60. *
  61. * @param A string containing a single testcase
  62. */
  63. function parseTestcase(testcase) {
  64. var lines = testcase.split("\n");
  65. /* check that the first non-empty, non-comment line is #data */
  66. for each (var line in lines) {
  67. if (!line || startsWith(line, "##")) {
  68. continue;
  69. }
  70. if (line == "#data")
  71. break;
  72. log(lines);
  73. throw "Unknown test format."
  74. }
  75. var input = [];
  76. var output = [];
  77. var errors = [];
  78. var fragment = [];
  79. var currentList = input;
  80. for each (var line in lines) {
  81. if (startsWith(line, "##todo")) {
  82. todo(false, line.substring(6));
  83. continue;
  84. }
  85. if (!(startsWith(line, "#error") ||
  86. startsWith(line, "#document") ||
  87. startsWith(line, "#document-fragment") ||
  88. startsWith(line, "#data"))) {
  89. currentList.push(line);
  90. } else if (line == "#errors") {
  91. currentList = errors;
  92. } else if (line == "#document") {
  93. currentList = output;
  94. } else if (line == "#document-fragment") {
  95. currentList = fragment;
  96. }
  97. }
  98. while (!output[output.length - 1]) {
  99. output.pop(); // zap trailing blank lines
  100. }
  101. //logger.log(input.length, output.length, errors.length);
  102. return [input.join("\n"), output.join("\n"), errors, fragment[0]];
  103. }
  104. /**
  105. * A generator function that accepts a list of strings. Each list
  106. * member corresponds to the contents of a ".dat" file from the
  107. * html5lib test suite.
  108. *
  109. * @param The list of strings
  110. */
  111. function test_parser(testlist) {
  112. for each (var testgroup in testlist) {
  113. var tests = testgroup.split("#data\n");
  114. tests = ["#data\n" + test for each(test in tests) if (test)];
  115. for each (var test in tests) {
  116. yield parseTestcase(test);
  117. }
  118. }
  119. }
  120. /**
  121. * Transforms a DOM document to a string matching the format in
  122. * the test cases.
  123. *
  124. * @param the DOM document
  125. */
  126. function docToTestOutput(doc) {
  127. var walker = doc.createTreeWalker(doc, NodeFilter.SHOW_ALL, null, true);
  128. return addLevels(walker, "", "| ").slice(0,-1); // remove the last newline
  129. }
  130. /**
  131. * Transforms the descendants of an element to a string matching the format
  132. * in the test cases.
  133. *
  134. * @param an element
  135. */
  136. function fragmentToTestOutput(elt) {
  137. var walker = elt.ownerDocument.createTreeWalker(elt, NodeFilter.SHOW_ALL,
  138. function (node) { return elt == node ?
  139. NodeFilter.FILTER_SKIP :
  140. NodeFilter.FILTER_ACCEPT; }, true);
  141. return addLevels(walker, "", "| ").slice(0,-1); // remove the last newline
  142. }
  143. function addLevels(walker, buf, indent) {
  144. if(walker.firstChild()) {
  145. do {
  146. buf += indent;
  147. switch (walker.currentNode.nodeType) {
  148. case Node.ELEMENT_NODE:
  149. buf += "<"
  150. var ns = walker.currentNode.namespaceURI;
  151. if ("http://www.w3.org/1998/Math/MathML" == ns) {
  152. buf += "math ";
  153. } else if ("http://www.w3.org/2000/svg" == ns) {
  154. buf += "svg ";
  155. } else if ("http://www.w3.org/1999/xhtml" != ns) {
  156. buf += "otherns ";
  157. }
  158. buf += walker.currentNode.localName + ">";
  159. if (walker.currentNode.hasAttributes()) {
  160. var valuesByName = {};
  161. var attrs = walker.currentNode.attributes;
  162. for (var i = 0; i < attrs.length; ++i) {
  163. var localName = attrs[i].localName;
  164. if (localName.indexOf("_moz-") == 0) {
  165. // Skip bogus attributes added by the MathML implementation
  166. continue;
  167. }
  168. var name;
  169. var attrNs = attrs[i].namespaceURI;
  170. if (null == attrNs) {
  171. name = localName;
  172. } else if ("http://www.w3.org/XML/1998/namespace" == attrNs) {
  173. name = "xml " + localName;
  174. } else if ("http://www.w3.org/1999/xlink" == attrNs) {
  175. name = "xlink " + localName;
  176. } else if ("http://www.w3.org/2000/xmlns/" == attrNs) {
  177. name = "xmlns " + localName;
  178. } else {
  179. name = "otherns " + localName;
  180. }
  181. valuesByName[name] = attrs[i].value;
  182. }
  183. var keys = Object.keys(valuesByName).sort();
  184. for (var i = 0; i < keys.length; ++i) {
  185. buf += "\n" + indent + " " + keys[i] +
  186. "=\"" + valuesByName[keys[i]] +"\"";
  187. }
  188. }
  189. break;
  190. case Node.DOCUMENT_TYPE_NODE:
  191. buf += "<!DOCTYPE " + walker.currentNode.name;
  192. if (walker.currentNode.publicId || walker.currentNode.systemId) {
  193. buf += " \"";
  194. buf += walker.currentNode.publicId;
  195. buf += "\" \"";
  196. buf += walker.currentNode.systemId;
  197. buf += "\"";
  198. }
  199. buf += ">";
  200. break;
  201. case Node.COMMENT_NODE:
  202. buf += "<!-- " + walker.currentNode.nodeValue + " -->";
  203. break;
  204. case Node.TEXT_NODE:
  205. buf += "\"" + walker.currentNode.nodeValue + "\"";
  206. break;
  207. }
  208. buf += "\n";
  209. buf = addLevels(walker, buf, indent + " ");
  210. } while(walker.nextSibling());
  211. walker.parentNode();
  212. }
  213. return buf;
  214. }