Annotation of 2001/DOM-Test-Suite/ecmascript/DOMTestCase.js, revision 1.3
1.1 dom-ts-4 1: /*
2: Copyright (c) 2001 World Wide Web Consortium,
3: (Massachusetts Institute of Technology, Institut National de
4: Recherche en Informatique et en Automatique, Keio University). All
5: Rights Reserved. This program is distributed under the W3C's Software
6: Intellectual Property License. This program is distributed in the
7: hope that it will be useful, but WITHOUT ANY WARRANTY; without even
8: the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9: PURPOSE.
10: See W3C License http://www.w3.org/Consortium/Legal/ for more details.
11: */
12:
1.3 ! dom-ts-4 13: function assertTrue(descr, actual) {
! 14: assert(descr,actual);
! 15: }
! 16:
! 17: function assertFalse(descr, actual) {
! 18: assert(descr, !actual);
! 19: }
! 20:
! 21: function assertSize(descr, expected, actual) {
! 22: var actualSize;
! 23: actualSize = actual.length;
! 24: assertEquals(descr, expected, actualSize);
! 25: }
! 26:
! 27: function assertEqualsCollection(descr, expected, actual) {
! 28: //
! 29: // if they aren't the same size, they aren't equal
! 30: assertEquals(descr, expected.length, actual.length);
! 31: //
! 32: // if there length is the same, then every entry in the expected list
! 33: // must appear once and only once in the actual list
! 34: var expectedLen = expected.length;
! 35: var expectedValue;
! 36: var actualLen = actual.length;
! 37: var i;
! 38: var j;
! 39: var matches;
! 40: for(i = 0; i < expectedLen; i++) {
! 41: matches = 0;
! 42: expectedValue = expected[i];
! 43: for(j = 0; j < actualLen; j++) {
! 44: if(expectedValue == actual[j]) {
! 45: matches++;
! 46: }
! 47: }
! 48: if(matches == 0) {
! 49: assert(descr + ": No match found for " + expectedValue,false);
! 50: }
! 51: if(matches > 1) {
! 52: assert(descr + ": Multiple matches found for " + expectedValue, false);
! 53: }
! 54: }
! 55: }
! 56:
! 57:
! 58: function assertEqualsList(descr, expected, actual) {
! 59: //
! 60: // if they aren't the same size, they aren't equal
! 61: assertEquals(descr, expected.length, actual.length);
! 62: //
! 63: var actualLen = actual.length;
! 64: var i;
! 65: for(i = 0; i < actualLen; i++) {
! 66: if(expected[i] != actual[i]) {
! 67: assertEquals(descr, expected[i], actual[i]);
! 68: }
! 69: }
! 70: }
! 71:
! 72: function assertInstanceOf(descr, type, obj) {
! 73: if(type == "Attr") {
! 74: assertEquals(descr,2,obj.nodeType);
! 75: var specd = obj.specified;
! 76: }
! 77: }
! 78:
! 79: function assertSame(descr, expected, actual) {
! 80: if(expected != actual) {
! 81: assertEquals(descr, expected.nodeType, actual.nodeType);
! 82: assertEquals(descr, expected.nodeValue, actual.nodeValue);
! 83: }
! 84: }
! 85:
! 86: var factory = null;
! 87:
! 88:
! 89: function MozApplyParserAttributes(parser, attrNames, attrValues) {
! 90: }
! 91:
! 92: var mozStaffDocs = new Array();
! 93:
! 94: var docsStarted = 0;
! 95: var docsLoaded = 0;
! 96:
! 97: function documentLoaded(e) {
! 98: docsLoaded++;
! 99: }
! 100:
! 101:
! 102: //
! 103: // get a document ready for the next test
! 104: //
! 105: function MozAdvanceLoad(sourceURL) {
! 106: var doc = document.implementation.createDocument("","temp",null);
! 107: doc.load(sourceURL);
! 108: for(var i = 0; i <= mozStaffDocs.length; i++) {
! 109: if(i == mozStaffDocs.length || mozStaffDocs[i] == null) {
! 110: mozStaffDocs[i] = doc;
! 111: break;
! 112: }
! 113: }
! 114: }
! 115:
! 116: function MozDocumentBuilder_load(sourceURL, willBeModified) {
! 117: if(sourceURL == 'staff.xml' && this.attrNames == null && this.attrValues == null) {
! 118: var doc = null;
! 119: for(var i = 0; i < mozStaffDocs.length; i++) {
! 120: doc = mozStaffDocs[i];
! 121: if(doc != null && doc.documentElement.nodeName == "staff") {
! 122: if(willBeModified) {
! 123: mozStaffDocs[i] = null;
! 124: MozAdvanceLoad(sourceURL);
! 125: }
! 126: return doc;
! 127: }
! 128: }
! 129: }
! 130: doc = document.implementation.createDocument("","temp",null);
! 131: //
! 132: // never seems to fire
! 133: //
! 134: doc.addEventListener("load",documentLoaded,false);
! 135: //
! 136: // need to add listener and wait
! 137: docsStarted++;
! 138: doc.load(sourceURL);
! 139: for(var i = 0; i < 5; i++) {
! 140: if(docsLoaded >= docsStarted) break;
! 141: alert("Load attempt " + docsStarted.toString() + ": Press OK continue.");
! 142: if(doc.documentElement.nodeName != "temp") break;
! 143: }
! 144:
! 145: if(sourceURL == "staff.xml") {
! 146: if(willBeModified) {
! 147: MozAdvanceLoad(sourceURL);
! 148: }
! 149: else {
! 150: mozStaffDocs[mozStaffDocs.length] = doc;
! 151: }
! 152: }
! 153: return doc;
! 154: }
! 155:
! 156: function MozDocumentBuilder_getDOMImplementation() {
! 157: return document.implementation;
! 158: }
! 159:
! 160: function MozDocumentBuilder_isDOMExceptionCode(ex, code) {
! 161: return true;
! 162: }
! 163:
! 164: function MozDocumentBuilder_getImplementationAttribute(attr) {
! 165: return false;
! 166: }
! 167:
! 168: function MozDocumentBuilder(attrNames, attrValues) {
! 169: this.attrNames = attrNames;
! 170: this.attrValues = attrValues;
! 171: this.load = MozDocumentBuilder_load;
! 172: this.isDOMExceptionCode = MozDocumentBuilder_isDOMExceptionCode;
! 173: this.getDOMImplementation = MozDocumentBuilder_getDOMImplementation;
! 174: this.getImplementationAttribute = MozDocumentBuilder_getImplementationAttribute;
! 175: }
! 176:
! 177: var mozDefaultBuilder = null;
! 178:
! 179: function MozDocumentBuilderFactory_newDocumentBuilder(attrNames, attrValues) {
! 180: if(attrNames == null && attrValues == null) {
! 181: return mozDefaultBuilder;
! 182: }
! 183: return new MozDocumentBuilder(attrNames, attrValues);
! 184: }
! 185:
! 186: function MozDocumentBuilderFactory() {
! 187: this.newDocumentBuilder = MozDocumentBuilderFactory_newDocumentBuilder;
! 188: }
! 189:
! 190: if(navigator.appName.indexOf("Netscape") != -1) {
! 191: mozDefaultBuilder = new MozDocumentBuilder(null,null);
! 192: factory = new MozDocumentBuilderFactory();
! 193: //
! 194: // preload a couple of staff.xml documents
! 195: //
! 196: MozAdvanceLoad("staff.xml");
! 197: MozAdvanceLoad("staff.xml");
! 198: }
! 199:
! 200:
! 201:
1.1 dom-ts-4 202: function IE5ApplyParserAttributes(parser, attrNames, attrValues) {
203: if(attrNames != null) {
204: var i;
205: for(i = 0; i < attrNames.length; i++) {
206: if(attrNames[i] == "expandEntityReferences") {
207: if(attrValues[i] == true) {
208: throw "MSXML does not support expanding entity references";
209: }
210: }
211: if(attrNames[i] == "validate") {
212: parser.validateOnParse = attrValues[i];
213: }
214: if(attrNames[i] == "ignoreElementContentWhitespace") {
215: parser.preserveWhiteSpace = !attrValues[i];
216: }
217: }
218: }
219: }
220:
221: function IE5DocumentBuilder_load(sourceURL, willBeModified) {
1.2 dom-ts-4 222: var actualURL = sourceURL;
1.1 dom-ts-4 223: if(!this.parser.load(actualURL)) {
224: throw this.parser.parseError.reason;
225: }
226: //
227: // if the first child of the document is a PI representing
228: // the XML Declaration, remove it from the tree.
229: //
230: // According to the DOM FAQ, this behavior is not wrong,
231: // but the tests are written assumming that it is not there.
232: //
233: var xmlDecl = this.parser.firstChild;
234: if(xmlDecl.nodeType == 7 && xmlDecl.target.toLowerCase() == "xml") {
235: this.parser.removeChild(xmlDecl);
236: }
237: return this.parser;
238: }
239:
240: function IE5DocumentBuilder_getDOMImplementation() {
241: return this.parser.domImplementation;
242: }
243:
244: function IE5DocumentBuilder_isDOMExceptionCode(ex, code) {
1.2 dom-ts-4 245: var retval;
246: switch(code) {
247: //
248: // INDEX_SIZE_ERR
249: case 1:
250: retval = (ex.number == -2147024809);
251: break;
252:
253: //
254: // HIERARCHY_REQUEST_ERR
255: case 3:
256: retval = (ex.number == -2147467259);
257: break;
258:
259:
260: //
261: // INVALID_CHARACTER_ERR
262: case 5:
263: retval = (ex.description.search(".*may not contain.*") >= 0);
264: break;
265:
266: //
267: // NO_MODIFICATION_ALLOWED_ERR
268: case 7:
1.1 dom-ts-4 269: retval = (ex.description.search(".*read.*only.*") >= 0);
1.2 dom-ts-4 270: break;
271:
272: //
273: // NOT_FOUND_ERR
274: //
275: case 8:
276: retval = (ex.number == -2147024809 || ex.number == -2147467259);
277: break;
278:
279: //
280: // INUSE_ATTRIBUTE_ERR
281: case 10:
282: retval = (ex.description.search(".*must be removed.*") >= 0);
283: break;
1.1 dom-ts-4 284: }
285: return retval;
286: }
287:
288: function IE5DocumentBuilder_getImplementationAttribute(attr) {
289: if(attr == "ignoringElementContentWhitespace") {
290: return !this.parser.preserveWhiteSpace;
291: }
292: return false;
293: }
294:
295: function IE5DocumentBuilder(attrNames, attrValues) {
296: this.attrNames = attrNames;
297: this.attrValues = attrValues;
298: this.load = IE5DocumentBuilder_load;
299: this.isDOMExceptionCode = IE5DocumentBuilder_isDOMExceptionCode;
300: this.getDOMImplementation = IE5DocumentBuilder_getDOMImplementation;
301: this.getImplementationAttribute = IE5DocumentBuilder_getImplementationAttribute;
302: this.parser = new ActiveXObject("MSXML2.DOMDocument.3.0");
303: this.parser.async = false;
304: this.parser.preserveWhiteSpace = true;
305: IE5ApplyParserAttributes(this.parser,this.attrNames, this.attrValues);
306: }
307:
308: var IE5DefaultBuilder = new IE5DocumentBuilder(null,null);
309:
310: function IE5DocumentBuilderFactory_newDocumentBuilder(attrNames, attrValues) {
311: if(attrNames == null && attrValues == null) {
312: return IE5DefaultBuilder;
313: }
314: return new IE5DocumentBuilder(attrNames, attrValues);
315: }
316:
317: function IE5DocumentBuilderFactory() {
318: this.newDocumentBuilder = IE5DocumentBuilderFactory_newDocumentBuilder;
319: }
320:
1.3 ! dom-ts-4 321: if(factory == null && navigator.appName.indexOf("Microsoft") != -1) {
! 322: factory = new IE5DocumentBuilderFactory();
1.1 dom-ts-4 323: }
324:
Webmaster