Annotation of 2001/DOM-Test-Suite/ecmascript/DOMTestCase.js, revision 1.4

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) {
1.4     ! dom-ts-4  165:     if(attr == "expandEntityReferences") {
        !           166:         return true;
        !           167:     }
1.3       dom-ts-4  168:     return false;
                    169:   }
                    170: 
                    171:   function MozDocumentBuilder(attrNames, attrValues) {
                    172:     this.attrNames = attrNames;
                    173:     this.attrValues = attrValues;
                    174:     this.load = MozDocumentBuilder_load;
                    175:     this.isDOMExceptionCode = MozDocumentBuilder_isDOMExceptionCode;
                    176:     this.getDOMImplementation = MozDocumentBuilder_getDOMImplementation;
                    177:     this.getImplementationAttribute = MozDocumentBuilder_getImplementationAttribute;
1.4     ! dom-ts-4  178:     //
        !           179:     //   check if expandEntityReferences is false
        !           180:     //     and throw an excpetion since that behavior is not supported
        !           181:     //
        !           182:     if(attrNames != null) {
        !           183:         for(var i = 0; i < attrNames.length; i++) {
        !           184:             if(attrNames[i] == "expandEntityReferences" && attrValues[i] == false) {
        !           185:                 throw "Mozilla doesn't support entity preservation";
        !           186:             }
        !           187:         }
        !           188:     }
1.3       dom-ts-4  189:   }
                    190: 
                    191:   var mozDefaultBuilder = null;
                    192: 
                    193:   function MozDocumentBuilderFactory_newDocumentBuilder(attrNames, attrValues) {
                    194:     if(attrNames == null && attrValues == null) {
                    195:         return mozDefaultBuilder;
                    196:     }
                    197:     return new MozDocumentBuilder(attrNames, attrValues);
                    198:   }
                    199: 
                    200:   function MozDocumentBuilderFactory() {
                    201:     this.newDocumentBuilder = MozDocumentBuilderFactory_newDocumentBuilder;
                    202:   }
                    203: 
                    204:   if(navigator.appName.indexOf("Netscape") != -1) {
                    205:     mozDefaultBuilder = new MozDocumentBuilder(null,null);
                    206:     factory = new MozDocumentBuilderFactory();
                    207:     //
                    208:     //  preload a couple of staff.xml documents
                    209:     //
                    210:     MozAdvanceLoad("staff.xml");
                    211:     MozAdvanceLoad("staff.xml");
                    212:   }
                    213:     
                    214: 
                    215: 
1.1       dom-ts-4  216:   function IE5ApplyParserAttributes(parser, attrNames, attrValues) {
                    217:        if(attrNames != null) {
                    218:            var i;
                    219:                for(i = 0; i < attrNames.length; i++) {
                    220:                        if(attrNames[i] == "expandEntityReferences") {
                    221:                                if(attrValues[i] == true) {
                    222:                                        throw "MSXML does not support expanding entity references";
                    223:                                }
                    224:                        }
                    225:                        if(attrNames[i] == "validate") {
                    226:                                parser.validateOnParse = attrValues[i];
                    227:                        }
                    228:                        if(attrNames[i] == "ignoreElementContentWhitespace") {
                    229:                                parser.preserveWhiteSpace = !attrValues[i];
                    230:                        }
                    231:                }
                    232:         }
                    233:   }
                    234: 
                    235:   function IE5DocumentBuilder_load(sourceURL, willBeModified) {
1.2       dom-ts-4  236:        var actualURL = sourceURL;
1.1       dom-ts-4  237:        if(!this.parser.load(actualURL)) {
                    238:                throw this.parser.parseError.reason;
                    239:        }
                    240:     //
                    241:     //   if the first child of the document is a PI representing
                    242:     //      the XML Declaration, remove it from the tree.
                    243:     //
                    244:     //   According to the DOM FAQ, this behavior is not wrong,
                    245:     //      but the tests are written assumming that it is not there.
                    246:     //
                    247:     var xmlDecl = this.parser.firstChild;
                    248:     if(xmlDecl.nodeType == 7 && xmlDecl.target.toLowerCase() == "xml") {
                    249:         this.parser.removeChild(xmlDecl);
                    250:     }
                    251:        return this.parser;
                    252:   }
                    253: 
                    254:    function IE5DocumentBuilder_getDOMImplementation() {
                    255:         return this.parser.domImplementation;
                    256:    }
                    257: 
                    258:   function IE5DocumentBuilder_isDOMExceptionCode(ex, code) {
1.2       dom-ts-4  259:        var retval;
                    260:     switch(code) {
                    261:         //
                    262:         //  INDEX_SIZE_ERR
                    263:         case 1:
                    264:         retval = (ex.number == -2147024809);
                    265:         break;
                    266: 
                    267:         //
                    268:         //  HIERARCHY_REQUEST_ERR
                    269:         case 3:
                    270:         retval = (ex.number == -2147467259);
                    271:         break;
                    272: 
                    273: 
                    274:         //
                    275:         //  INVALID_CHARACTER_ERR
                    276:         case 5:
                    277:         retval = (ex.description.search(".*may not contain.*") >= 0);
                    278:         break;
                    279: 
                    280:         //
                    281:         //   NO_MODIFICATION_ALLOWED_ERR
                    282:         case 7:
1.1       dom-ts-4  283:                retval = (ex.description.search(".*read.*only.*") >= 0);
1.2       dom-ts-4  284:         break;
                    285: 
                    286:         //
                    287:         //   NOT_FOUND_ERR
                    288:         //
                    289:         case 8:
                    290:         retval = (ex.number == -2147024809 || ex.number == -2147467259);
                    291:         break;
                    292: 
                    293:         //
                    294:         //   INUSE_ATTRIBUTE_ERR
                    295:         case 10:
                    296:         retval = (ex.description.search(".*must be removed.*") >= 0);
                    297:         break;
1.1       dom-ts-4  298:        }
                    299:        return retval;
                    300:   }
                    301: 
                    302:   function IE5DocumentBuilder_getImplementationAttribute(attr) {
                    303:     if(attr == "ignoringElementContentWhitespace") {
                    304:         return !this.parser.preserveWhiteSpace;
                    305:     }
                    306:     return false;
                    307:   }
                    308: 
                    309:   function IE5DocumentBuilder(attrNames, attrValues) {
                    310:     this.attrNames = attrNames;
                    311:     this.attrValues = attrValues;
                    312:     this.load = IE5DocumentBuilder_load;
                    313:     this.isDOMExceptionCode = IE5DocumentBuilder_isDOMExceptionCode;
                    314:     this.getDOMImplementation = IE5DocumentBuilder_getDOMImplementation;
                    315:     this.getImplementationAttribute = IE5DocumentBuilder_getImplementationAttribute;
                    316:     this.parser = new ActiveXObject("MSXML2.DOMDocument.3.0");
                    317:     this.parser.async = false;
                    318:     this.parser.preserveWhiteSpace = true;
                    319:     IE5ApplyParserAttributes(this.parser,this.attrNames, this.attrValues);    
                    320:   }
                    321: 
                    322:   var IE5DefaultBuilder = new IE5DocumentBuilder(null,null);
                    323: 
                    324:   function IE5DocumentBuilderFactory_newDocumentBuilder(attrNames, attrValues) {
                    325:     if(attrNames == null && attrValues == null) {
                    326:         return IE5DefaultBuilder;
                    327:     }
                    328:     return new IE5DocumentBuilder(attrNames, attrValues);
                    329:   }
                    330: 
                    331:   function IE5DocumentBuilderFactory() {
                    332:     this.newDocumentBuilder = IE5DocumentBuilderFactory_newDocumentBuilder;
                    333:   }
                    334: 
1.3       dom-ts-4  335:   if(factory == null && navigator.appName.indexOf("Microsoft") != -1) {
                    336:     factory = new IE5DocumentBuilderFactory();
1.1       dom-ts-4  337:   }
                    338: 

Webmaster