PageRenderTime 34ms CodeModel.GetById 23ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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/**
 41 * A test suite that runs WHATWG HTML parser tests.
 42 * The tests are from html5lib. 
 43 *
 44 * http://html5lib.googlecode.com/
 45 */
 46
 47/**
 48 * A few utility functions.
 49 */
 50function log(entry) {
 51    
 52}
 53
 54function startsWith(s, s2) {
 55  return s.indexOf(s2)==0;
 56}
 57
 58function trimString(s) {
 59  return(s.replace(/^\s+/,'').replace(/\s+$/,''));
 60}
 61
 62/**
 63 * Parses an individual testcase into an array containing the input
 64 * string, a string representing the expected tree (DOM), and a list
 65 * of error messages.
 66 *
 67 * @param A string containing a single testcase
 68 */
 69function parseTestcase(testcase) {
 70  var lines = testcase.split("\n");
 71
 72  /* check that the first non-empty, non-comment line is #data */
 73  for each (var line in lines) {
 74    if (!line || startsWith(line, "##")) {
 75      continue;
 76    }
 77    if (line == "#data")
 78      break;
 79    log(lines);
 80    throw "Unknown test format."
 81  }
 82
 83  var input = [];
 84  var output = [];
 85  var errors = [];
 86  var fragment = [];
 87  var currentList = input;
 88  for each (var line in lines) {
 89    if (startsWith(line, "##todo")) {
 90      todo(false, line.substring(6));
 91      continue;
 92    }
 93    if (!(startsWith(line, "#error") ||
 94          startsWith(line, "#document") ||
 95          startsWith(line, "#document-fragment") ||
 96          startsWith(line, "#data"))) {
 97      currentList.push(line);
 98    } else if (line == "#errors") {
 99      currentList = errors;
100    } else if (line == "#document") {
101      currentList = output;
102    } else if (line == "#document-fragment") {
103      currentList = fragment;
104    }
105  }
106  while (!output[output.length - 1]) {
107    output.pop(); // zap trailing blank lines
108  }
109  //logger.log(input.length, output.length, errors.length);
110  return [input.join("\n"), output.join("\n"), errors, fragment[0]];
111}
112
113/**
114 * A generator function that accepts a list of strings. Each list
115 * member corresponds to the contents of a ".dat" file from the
116 * html5lib test suite.
117 *
118 * @param The list of strings
119 */
120function test_parser(testlist) {
121  for each (var testgroup in testlist) {
122    var tests = testgroup.split("#data\n");
123    tests = ["#data\n" + test for each(test in tests) if (test)];
124    for each (var test in tests) {
125      yield parseTestcase(test);
126    }
127  }
128}
129
130/**
131 * Transforms a DOM document to a string matching the format in 
132 * the test cases.
133 *
134 * @param the DOM document
135 */
136function docToTestOutput(doc) {
137  var walker = doc.createTreeWalker(doc, NodeFilter.SHOW_ALL, null, true);
138  return addLevels(walker, "", "| ").slice(0,-1); // remove the last newline
139}
140
141/**
142 * Transforms the descendants of an element to a string matching the format
143 * in the test cases.
144 *
145 * @param an element
146 */
147function fragmentToTestOutput(elt) {
148  var walker = elt.ownerDocument.createTreeWalker(elt, NodeFilter.SHOW_ALL, 
149    function (node) { return elt == node ? 
150                        NodeFilter.FILTER_SKIP : 
151                        NodeFilter.FILTER_ACCEPT; }, true);
152  return addLevels(walker, "", "| ").slice(0,-1); // remove the last newline
153}
154
155function addLevels(walker, buf, indent) {
156  if(walker.firstChild()) {
157    do {
158      buf += indent;
159      switch (walker.currentNode.nodeType) {
160        case Node.ELEMENT_NODE:
161          buf += "<"
162          var ns = walker.currentNode.namespaceURI;
163          if ("http://www.w3.org/1998/Math/MathML" == ns) {
164            buf += "math ";
165          } else if ("http://www.w3.org/2000/svg" == ns) {
166            buf += "svg ";
167          } else if ("http://www.w3.org/1999/xhtml" != ns) {
168            buf += "otherns ";
169          }
170          buf += walker.currentNode.localName + ">";
171          if (walker.currentNode.hasAttributes()) {
172            var valuesByName = {};
173            var attrs = walker.currentNode.attributes;
174            for (var i = 0; i < attrs.length; ++i) {
175              var localName = attrs[i].localName;
176              if (localName.indexOf("_moz-") == 0) {
177                // Skip bogus attributes added by the MathML implementation
178                continue;
179              }
180              var name;
181              var attrNs = attrs[i].namespaceURI;
182              if (null == attrNs) {
183                name = localName;
184              } else if ("http://www.w3.org/XML/1998/namespace" == attrNs) {
185                name = "xml " + localName;
186              } else if ("http://www.w3.org/1999/xlink" == attrNs) {
187                name = "xlink " + localName;
188              } else if ("http://www.w3.org/2000/xmlns/" == attrNs) {
189                name = "xmlns " + localName;
190              } else {
191                name = "otherns " + localName;
192              }
193              valuesByName[name] = attrs[i].value;
194            }
195            var keys = Object.keys(valuesByName).sort();
196            for (var i = 0; i < keys.length; ++i) {
197              buf += "\n" + indent + "  " + keys[i] + 
198                     "=\"" + valuesByName[keys[i]] +"\"";
199            }
200          }
201          break;
202        case Node.DOCUMENT_TYPE_NODE:
203          buf += "<!DOCTYPE " + walker.currentNode.name;
204          if (walker.currentNode.publicId || walker.currentNode.systemId) {
205            buf += " \"";
206            buf += walker.currentNode.publicId;
207            buf += "\" \"";
208            buf += walker.currentNode.systemId;
209            buf += "\"";
210          }
211          buf += ">";
212          break;
213        case Node.COMMENT_NODE:
214          buf += "<!-- " + walker.currentNode.nodeValue + " -->";
215          break;
216        case Node.TEXT_NODE:
217          buf += "\"" + walker.currentNode.nodeValue + "\"";
218          break;
219      }
220      buf += "\n";
221      buf = addLevels(walker, buf, indent + "  ");
222    } while(walker.nextSibling());
223    walker.parentNode();
224  }
225  return buf;
226}
227