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

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 assertSize(descr, expected, actual) {
                     14:     var actualSize;
                     15:     actualSize = actual.length;
                     16:     assertEquals(descr, expected, actualSize);
                     17:   }
                     18:  
                     19:   function assertEqualsCollection(descr, expected, actual) {
                     20:     //
                     21:     //  if they aren't the same size, they aren't equal
                     22:     assertEquals(descr, expected.length, actual.length);
                     23:     //
                     24:     //  if there length is the same, then every entry in the expected list
                     25:     //     must appear once and only once in the actual list
                     26:     var expectedLen = expected.length;
                     27:     var expectedValue;
                     28:     var actualLen = actual.length;
                     29:     var i;
                     30:     var j;
                     31:     var matches;
                     32:     for(i = 0; i < expectedLen; i++) {
                     33:         matches = 0;
                     34:         expectedValue = expected[i];
                     35:         for(j = 0; j < actualLen; j++) {
                     36:             if(expectedValue == actual[j]) {
                     37:                 matches++;
                     38:             }
                     39:         }
                     40:         if(matches == 0) {
                     41:             assert(descr + ": No match found for " + expectedValue,false);
                     42:         }
                     43:         if(matches > 1) {
                     44:             assert(descr + ": Multiple matches found for " + expectedValue, false);
                     45:         }
                     46:     }
                     47:   }
                     48: 
                     49: 
                     50:   function assertEqualsList(descr, expected, actual) {
                     51:     //
                     52:     //  if they aren't the same size, they aren't equal
                     53:     assertEquals(descr, expected.length, actual.length);
                     54:     //
                     55:     var actualLen = actual.length;
                     56:     var i;
                     57:     for(i = 0; i < actualLen; i++) {
                     58:         if(expected[i] != actual[i]) {
                     59:             assertEquals(descr, expected[i], actual[i]);
                     60:         }
                     61:     }
                     62:   }
                     63: 
                     64:   function assertInstanceOf(descr, type, obj) {
                     65:     if(type == "Attr") {
                     66:         assertEquals(descr,2,obj.nodeType);
                     67:         var specd = obj.specified;
                     68:     }
                     69:   }
                     70: 
                     71:   function assertSame(descr, expected, actual) {
                     72:     if(expected != actual) {
                     73:         assertEquals(descr, expected.nodeType, actual.nodeType);
                     74:         assertEquals(descr, expected.nodeValue, actual.nodeValue);
                     75:     }
                     76:   }
                     77: 
                     78:   var factory = null;
                     79: 
                     80: 
                     81:   function MozApplyParserAttributes(parser, attrNames, attrValues) {
                     82:   }
                     83:  
                     84:   var mozStaffDocs = new Array(); 
                     85: 
                     86:   var docsStarted = 0;
                     87:   var docsLoaded = 0;
                     88: 
                     89:   function documentLoaded(e) {
                     90:       docsLoaded++;
                     91:   }
                     92: 
                     93: 
                     94:   //
                     95:   //   get a document ready for the next test
                     96:   //
                     97:   function MozAdvanceLoad(sourceURL) {
                     98:     var doc = document.implementation.createDocument("","temp",null);
                     99:     doc.load(sourceURL);
                    100:     for(var i = 0; i <= mozStaffDocs.length; i++) {
                    101:         if(i == mozStaffDocs.length || mozStaffDocs[i] == null) {
                    102:             mozStaffDocs[i] = doc;
                    103:             break;
                    104:         }
                    105:     }
                    106:   }
                    107: 
                    108:   function MozDocumentBuilder_load(sourceURL, willBeModified) {
                    109:     if(sourceURL == 'staff.xml' && this.attrNames == null && this.attrValues == null) {
                    110:         var doc = null;
                    111:         for(var i = 0; i < mozStaffDocs.length; i++) {
                    112:             doc = mozStaffDocs[i];
                    113:             if(doc != null && doc.documentElement.nodeName == "staff") {
                    114:                 if(willBeModified) {
                    115:                     mozStaffDocs[i] = null;
                    116:                     MozAdvanceLoad(sourceURL);
                    117:                 }
                    118:                 return doc;
                    119:             }
                    120:         }
                    121:     }
                    122:     doc = document.implementation.createDocument("","temp",null);
                    123:     //
                    124:     //  never seems to fire
                    125:     //
                    126:     doc.addEventListener("load",documentLoaded,false);
                    127:     //
                    128:     //   need to add listener and wait
                    129:     docsStarted++;
                    130:     doc.load(sourceURL);
                    131:     for(var i = 0; i < 5; i++) {
                    132:         if(docsLoaded >= docsStarted) break;
                    133:         alert("Load attempt " + docsStarted.toString() + ": Press OK continue.");
                    134:         if(doc.documentElement.nodeName != "temp") break;
                    135:     }
                    136: 
                    137:     if(sourceURL == "staff.xml") {
                    138:         if(willBeModified) {
                    139:             MozAdvanceLoad(sourceURL);
                    140:         }
                    141:         else {
                    142:             mozStaffDocs[mozStaffDocs.length] = doc;
                    143:         }
                    144:     }
                    145:     return doc;
                    146:   }
                    147: 
                    148:    function MozDocumentBuilder_getDOMImplementation() {
                    149:         return document.implementation;
                    150:    }
                    151: 
                    152:   function MozDocumentBuilder_isDOMExceptionCode(ex, code) {
                    153:     return true;
                    154:   }
                    155: 
                    156:   function MozDocumentBuilder_getImplementationAttribute(attr) {
1.4       dom-ts-4  157:     if(attr == "expandEntityReferences") {
                    158:         return true;
                    159:     }
1.3       dom-ts-4  160:     return false;
                    161:   }
                    162: 
                    163:   function MozDocumentBuilder(attrNames, attrValues) {
                    164:     this.attrNames = attrNames;
                    165:     this.attrValues = attrValues;
                    166:     this.load = MozDocumentBuilder_load;
                    167:     this.isDOMExceptionCode = MozDocumentBuilder_isDOMExceptionCode;
                    168:     this.getDOMImplementation = MozDocumentBuilder_getDOMImplementation;
                    169:     this.getImplementationAttribute = MozDocumentBuilder_getImplementationAttribute;
1.4       dom-ts-4  170:     //
                    171:     //   check if expandEntityReferences is false
                    172:     //     and throw an excpetion since that behavior is not supported
                    173:     //
                    174:     if(attrNames != null) {
                    175:         for(var i = 0; i < attrNames.length; i++) {
                    176:             if(attrNames[i] == "expandEntityReferences" && attrValues[i] == false) {
                    177:                 throw "Mozilla doesn't support entity preservation";
                    178:             }
                    179:         }
                    180:     }
1.3       dom-ts-4  181:   }
                    182: 
                    183:   var mozDefaultBuilder = null;
                    184: 
                    185:   function MozDocumentBuilderFactory_newDocumentBuilder(attrNames, attrValues) {
                    186:     if(attrNames == null && attrValues == null) {
                    187:         return mozDefaultBuilder;
                    188:     }
                    189:     return new MozDocumentBuilder(attrNames, attrValues);
                    190:   }
                    191: 
                    192:   function MozDocumentBuilderFactory() {
                    193:     this.newDocumentBuilder = MozDocumentBuilderFactory_newDocumentBuilder;
                    194:   }
                    195: 
                    196:   if(navigator.appName.indexOf("Netscape") != -1) {
                    197:     mozDefaultBuilder = new MozDocumentBuilder(null,null);
                    198:     factory = new MozDocumentBuilderFactory();
                    199:     //
                    200:     //  preload a couple of staff.xml documents
                    201:     //
                    202:     MozAdvanceLoad("staff.xml");
                    203:     MozAdvanceLoad("staff.xml");
                    204:   }
1.5       dom-ts-4  205: 
                    206: 
                    207:   //
                    208:   //   Adobe SVG Viewer support 
                    209:   //
                    210:   //
                    211:   //
1.3       dom-ts-4  212:     
1.5       dom-ts-4  213:   function ASVApplyParserAttributes(parser, attrNames, attrValues) {
                    214:   }
                    215:  
                    216:   //
                    217:   //   actually, array of <EMBED> elements
                    218:   //
                    219:   var asvStaffDocs = new Array();
                    220: 
                    221:   //
                    222:   //   get a document ready for the next test
                    223:   //
                    224:   function ASVStartLoad(sourceURL) {
                    225:     //
                    226:     //   must create embed element in containing HTML
                    227:     //
                    228:     var embed = document.createElement("embed");
                    229:     embed.src = sourceURL + ".svg";
                    230:     embed.height= 100;
                    231:     embed.width = 100;
                    232:     embed.type = "image/svg+xml";
                    233:     var htmlelem = document.documentElement;
                    234:     var child = htmlelem.firstChild;
                    235:     while(child != null) {
                    236:                if(child.nodeName == "BODY") {
                    237:                        child.appendChild(embed);
                    238:                        break;
                    239:                }
                    240:                child = child.nextSibling;
                    241:        }
                    242:        return embed;
                    243:   }
                    244: 
                    245:   function ASVDocumentBuilder_load(sourceURL, willBeModified) {
                    246:     if(sourceURL == 'staff.xml' && this.attrNames == null && this.attrValues == null) {
                    247:         var embed = null;
                    248:         for(var i = 0; i < asvStaffDocs.length; i++) {
                    249:             embed = asvStaffDocs[i];
                    250:             if(embed != null && embed.readyState == "complete") {
                    251:                 if(willBeModified) {
                    252:                     asvStaffDocs[i] = ASVStartLoad(sourceURL);
                    253:                 }
                    254:                 return embed.getSVGDocument();
                    255:             }
                    256:         }
                    257:     }
                    258:     var embed = ASVStartLoad(sourceURL);
                    259:     for(var i = 0; i < 5; i++) {
                    260:         alert("Document loading: Press OK continue.");
                    261:         if(embed.readyState == "complete") break;
                    262:     }
                    263: 
                    264:        doc = embed.getSVGDocument();
                    265: 
                    266:     if(sourceURL == "staff.xml") {
                    267:         if(willBeModified) {
                    268:             for(var i = 0; i <= asvStaffDocs.length; i++) {
                    269:                 if(i == asvStaffDocs.length || asvStaffDocs[i] == null) {
                    270:                     asvStaffDocs[i] = ASVStartLoad(sourceURL);
                    271:                     break;
                    272:                 }
                    273:             }
                    274:         }
                    275:         else {
                    276:             asvStaffDocs[asvStaffDocs.length] = embed;
                    277:         }
                    278:     }
                    279:     return doc;
                    280:   }
                    281: 
                    282:    function ASVDocumentBuilder_getDOMImplementation() {
                    283:         for(var i = 0; i < asvStaffDocs.length; i++) {
                    284:             if(asvStaffDocs[i] != null && asvStaffDocs[i].readyState == "complete") {
                    285:                 return asvStaffDocs[i].getSVGDocument().implementation;
                    286:             }
                    287:         }
                    288:         var embed = ASVStaffLoad("staff.xml");
                    289:         asvStaffDocs[asvStaffDocs.length] = embed;
                    290:         return embed.getSVGDocument().implementation;
                    291:    }
                    292: 
                    293:   function ASVDocumentBuilder_isDOMExceptionCode(ex, code) {
                    294:     return true;
                    295:   }
                    296: 
                    297:   function ASVDocumentBuilder_getImplementationAttribute(attr) {
                    298:     if(attr == "expandEntityReferences") {
                    299:         return true;
                    300:     }
                    301:     return false;
                    302:   }
                    303: 
                    304:   function ASVDocumentBuilder(attrNames, attrValues) {
                    305:     this.attrNames = attrNames;
                    306:     this.attrValues = attrValues;
                    307:     this.load = ASVDocumentBuilder_load;
                    308:     this.isDOMExceptionCode = ASVDocumentBuilder_isDOMExceptionCode;
                    309:     this.getDOMImplementation = ASVDocumentBuilder_getDOMImplementation;
                    310:     this.getImplementationAttribute = ASVDocumentBuilder_getImplementationAttribute;
                    311:     if(attrNames != null) {
                    312:         for(var i = 0; i < attrNames.length; i++) {
                    313:             if(attrNames[i] == "expandEntityReferences" && attrValues[i] == false) {
                    314:                 throw "Adobe SVG Viewer can not preserve entities";
                    315:             }
                    316:             if(attrNames[i] == "ignoringElementContentWhitespace" && attrValues[i] == true) {
                    317:                 throw "Adobe SVG Viewer can not preserve ignorable whitespace";
                    318:             }
                    319:         }
                    320:     }
                    321:   }
                    322: 
                    323:   var asvDefaultBuilder = null;
                    324: 
                    325:   function ASVDocumentBuilderFactory_newDocumentBuilder(attrNames, attrValues) {
                    326:     if(attrNames == null && attrValues == null) {
                    327:         return asvDefaultBuilder;
                    328:     }
                    329:     return new ASVDocumentBuilder(attrNames, attrValues);
                    330:   }
                    331: 
                    332:   function ASVDocumentBuilderFactory() {
                    333:     this.newDocumentBuilder = ASVDocumentBuilderFactory_newDocumentBuilder;
                    334:   }
                    335: 
                    336: 
                    337:   //
                    338:   //
                    339:   //    Uncomment the following 4 lines to test Adobe SVG Viewer
                    340:   //
                    341:   //
                    342:   //
                    343: 
1.6     ! dom-ts-4  344:   if(navigator.appName.indexOf("Microsoft") != -1) {
        !           345:     asvDefaultBuilder = new ASVDocumentBuilder(null,null);
        !           346:     factory = new ASVDocumentBuilderFactory();
        !           347:   }
1.5       dom-ts-4  348: 
1.3       dom-ts-4  349: 
                    350: 
1.1       dom-ts-4  351:   function IE5ApplyParserAttributes(parser, attrNames, attrValues) {
                    352:        if(attrNames != null) {
                    353:            var i;
                    354:                for(i = 0; i < attrNames.length; i++) {
                    355:                        if(attrNames[i] == "expandEntityReferences") {
                    356:                                if(attrValues[i] == true) {
                    357:                                        throw "MSXML does not support expanding entity references";
                    358:                                }
                    359:                        }
                    360:                        if(attrNames[i] == "validate") {
                    361:                                parser.validateOnParse = attrValues[i];
                    362:                        }
                    363:                        if(attrNames[i] == "ignoreElementContentWhitespace") {
                    364:                                parser.preserveWhiteSpace = !attrValues[i];
                    365:                        }
                    366:                }
                    367:         }
                    368:   }
                    369: 
                    370:   function IE5DocumentBuilder_load(sourceURL, willBeModified) {
1.2       dom-ts-4  371:        var actualURL = sourceURL;
1.1       dom-ts-4  372:        if(!this.parser.load(actualURL)) {
                    373:                throw this.parser.parseError.reason;
                    374:        }
                    375:     //
                    376:     //   if the first child of the document is a PI representing
                    377:     //      the XML Declaration, remove it from the tree.
                    378:     //
                    379:     //   According to the DOM FAQ, this behavior is not wrong,
                    380:     //      but the tests are written assumming that it is not there.
                    381:     //
                    382:     var xmlDecl = this.parser.firstChild;
                    383:     if(xmlDecl.nodeType == 7 && xmlDecl.target.toLowerCase() == "xml") {
                    384:         this.parser.removeChild(xmlDecl);
                    385:     }
                    386:        return this.parser;
                    387:   }
                    388: 
                    389:    function IE5DocumentBuilder_getDOMImplementation() {
                    390:         return this.parser.domImplementation;
                    391:    }
                    392: 
1.5       dom-ts-4  393:   //
                    394:   //   This function checks the exception raised in the test
                    395:   //   If this function returns true, then the exception is 
                    396:   //      consistent with the exception code parameter
                    397:   //
                    398:   //   This code attempts to determine the reason for the exception
                    399:   //      to reduce the chance that an unrelated exception causes
                    400:   //      the test to pass.
1.1       dom-ts-4  401:   function IE5DocumentBuilder_isDOMExceptionCode(ex, code) {
1.2       dom-ts-4  402:        var retval;
                    403:     switch(code) {
                    404:         //
                    405:         //  INDEX_SIZE_ERR
                    406:         case 1:
                    407:         retval = (ex.number == -2147024809);
                    408:         break;
                    409: 
                    410:         //
                    411:         //  HIERARCHY_REQUEST_ERR
                    412:         case 3:
                    413:         retval = (ex.number == -2147467259);
                    414:         break;
                    415: 
                    416: 
                    417:         //
                    418:         //  INVALID_CHARACTER_ERR
                    419:         case 5:
                    420:         retval = (ex.description.search(".*may not contain.*") >= 0);
                    421:         break;
                    422: 
                    423:         //
                    424:         //   NO_MODIFICATION_ALLOWED_ERR
                    425:         case 7:
1.1       dom-ts-4  426:                retval = (ex.description.search(".*read.*only.*") >= 0);
1.2       dom-ts-4  427:         break;
                    428: 
                    429:         //
                    430:         //   NOT_FOUND_ERR
                    431:         //
                    432:         case 8:
                    433:         retval = (ex.number == -2147024809 || ex.number == -2147467259);
                    434:         break;
                    435: 
                    436:         //
                    437:         //   INUSE_ATTRIBUTE_ERR
                    438:         case 10:
                    439:         retval = (ex.description.search(".*must be removed.*") >= 0);
                    440:         break;
1.1       dom-ts-4  441:        }
                    442:        return retval;
                    443:   }
                    444: 
                    445:   function IE5DocumentBuilder_getImplementationAttribute(attr) {
                    446:     if(attr == "ignoringElementContentWhitespace") {
                    447:         return !this.parser.preserveWhiteSpace;
                    448:     }
                    449:     return false;
                    450:   }
                    451: 
                    452:   function IE5DocumentBuilder(attrNames, attrValues) {
                    453:     this.attrNames = attrNames;
                    454:     this.attrValues = attrValues;
                    455:     this.load = IE5DocumentBuilder_load;
                    456:     this.isDOMExceptionCode = IE5DocumentBuilder_isDOMExceptionCode;
                    457:     this.getDOMImplementation = IE5DocumentBuilder_getDOMImplementation;
                    458:     this.getImplementationAttribute = IE5DocumentBuilder_getImplementationAttribute;
                    459:     this.parser = new ActiveXObject("MSXML2.DOMDocument.3.0");
                    460:     this.parser.async = false;
                    461:     this.parser.preserveWhiteSpace = true;
                    462:     IE5ApplyParserAttributes(this.parser,this.attrNames, this.attrValues);    
                    463:   }
                    464: 
                    465:   var IE5DefaultBuilder = new IE5DocumentBuilder(null,null);
                    466: 
                    467:   function IE5DocumentBuilderFactory_newDocumentBuilder(attrNames, attrValues) {
                    468:     if(attrNames == null && attrValues == null) {
                    469:         return IE5DefaultBuilder;
                    470:     }
                    471:     return new IE5DocumentBuilder(attrNames, attrValues);
                    472:   }
                    473: 
                    474:   function IE5DocumentBuilderFactory() {
                    475:     this.newDocumentBuilder = IE5DocumentBuilderFactory_newDocumentBuilder;
                    476:   }
                    477: 
1.3       dom-ts-4  478:   if(factory == null && navigator.appName.indexOf("Microsoft") != -1) {
                    479:     factory = new IE5DocumentBuilderFactory();
1.1       dom-ts-4  480:   }
                    481: 

Webmaster