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

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: 
1.7       dom-ts-4   78:   function assertURIEquals(assertID, scheme, path, host, file, query, fragment, isAbsolute, actual) {
                     79:     //
                     80:     //  URI must be non-null
                     81:     assertNotNull(assertID, actual);
                     82: 
                     83:     var uri = actual;
                     84: 
                     85:     var lastPound = actual.lastIndexOf("#");
                     86:     var actualFragment = "";
                     87:     if(lastPound != -1) {
                     88:         //
                     89:         //   substring before pound
                     90:         //
                     91:         uri = actual.substring(0,lastPound);
                     92:         actualFragment = actual.substring(lastPound+1);
                     93:     }
                     94:     if(fragment != null) assertEquals(assertID,fragment, actualFragment);
                     95: 
                     96:     var lastQuestion = uri.lastIndexOf("?");
                     97:     var actualQuery = "";
                     98:     if(lastQuestion != -1) {
                     99:         //
                    100:         //   substring before pound
                    101:         //
                    102:         uri = actual.substring(0,lastQuestion);
                    103:         actualQuery = actual.substring(lastQuestion+1);
                    104:     }
                    105:     if(query != null) assertEquals(assertID, query, actualQuery);
                    106: 
                    107:     var firstColon = uri.indexOf(":");
                    108:     var firstSlash = uri.indexOf("/");
                    109:     var actualPath = uri;
                    110:     var actualScheme = "";
                    111:     if(firstColon != -1 && firstColon < firstSlash) {
                    112:         actualScheme = uri.substring(0,firstColon);
                    113:         actualPath = uri.substring(firstColon + 1);
                    114:     }
                    115: 
                    116:     if(scheme != null) {
                    117:         assertEquals(assertID, scheme, actualScheme);
                    118:     }
                    119: 
                    120:     if(path != null) {
                    121:         assertEquals(assertID, path, actualPath);
                    122:     }
                    123: 
                    124:     if(host != null) {
                    125:         var actualHost = "";
                    126:         if(actualPath.startsWith("//")) {
1.8     ! dom-ts-4  127:             var termSlash = actualPath.substring(2).indexOf("/") + 2;
1.7       dom-ts-4  128:             actualHost = actualPath.substring(0,termSlash);
                    129:         }
                    130:         assertEquals(assertID, host, actualHost);
                    131:     }
                    132: 
                    133:     if(file != null) {
                    134:         var actualFile = actualPath;
                    135:         var finalSlash = actualPath.lastIndexOf("/");
                    136:         if(finalSlash != -1) {
                    137:             actualFile = actualPath.substring(finalSlash+1);
                    138:         }
                    139:         assertEquals(assertID, file, actualFile);
                    140:     }
                    141: 
                    142:     if(isAbsolute != null) {
                    143:         assertEquals(assertID, isAbsolute.booleanValue(), actualPath.startsWith("/"));
                    144:     }
                    145:   }
                    146: 
                    147: 
1.3       dom-ts-4  148:   var factory = null;
                    149: 
                    150: 
                    151:   function MozApplyParserAttributes(parser, attrNames, attrValues) {
                    152:   }
                    153:  
                    154:   var mozStaffDocs = new Array(); 
                    155: 
                    156:   var docsStarted = 0;
                    157:   var docsLoaded = 0;
                    158: 
                    159:   function documentLoaded(e) {
                    160:       docsLoaded++;
                    161:   }
                    162: 
                    163: 
                    164:   //
                    165:   //   get a document ready for the next test
                    166:   //
                    167:   function MozAdvanceLoad(sourceURL) {
                    168:     var doc = document.implementation.createDocument("","temp",null);
                    169:     doc.load(sourceURL);
                    170:     for(var i = 0; i <= mozStaffDocs.length; i++) {
                    171:         if(i == mozStaffDocs.length || mozStaffDocs[i] == null) {
                    172:             mozStaffDocs[i] = doc;
                    173:             break;
                    174:         }
                    175:     }
                    176:   }
                    177: 
                    178:   function MozDocumentBuilder_load(sourceURL, willBeModified) {
                    179:     if(sourceURL == 'staff.xml' && this.attrNames == null && this.attrValues == null) {
                    180:         var doc = null;
                    181:         for(var i = 0; i < mozStaffDocs.length; i++) {
                    182:             doc = mozStaffDocs[i];
                    183:             if(doc != null && doc.documentElement.nodeName == "staff") {
                    184:                 if(willBeModified) {
                    185:                     mozStaffDocs[i] = null;
                    186:                     MozAdvanceLoad(sourceURL);
                    187:                 }
                    188:                 return doc;
                    189:             }
                    190:         }
                    191:     }
                    192:     doc = document.implementation.createDocument("","temp",null);
                    193:     //
                    194:     //  never seems to fire
                    195:     //
                    196:     doc.addEventListener("load",documentLoaded,false);
                    197:     //
                    198:     //   need to add listener and wait
                    199:     docsStarted++;
                    200:     doc.load(sourceURL);
                    201:     for(var i = 0; i < 5; i++) {
                    202:         if(docsLoaded >= docsStarted) break;
                    203:         alert("Load attempt " + docsStarted.toString() + ": Press OK continue.");
                    204:         if(doc.documentElement.nodeName != "temp") break;
                    205:     }
                    206: 
                    207:     if(sourceURL == "staff.xml") {
                    208:         if(willBeModified) {
                    209:             MozAdvanceLoad(sourceURL);
                    210:         }
                    211:         else {
                    212:             mozStaffDocs[mozStaffDocs.length] = doc;
                    213:         }
                    214:     }
                    215:     return doc;
                    216:   }
                    217: 
                    218:    function MozDocumentBuilder_getDOMImplementation() {
                    219:         return document.implementation;
                    220:    }
                    221: 
                    222:   function MozDocumentBuilder_isDOMExceptionCode(ex, code) {
                    223:     return true;
                    224:   }
                    225: 
                    226:   function MozDocumentBuilder_getImplementationAttribute(attr) {
1.4       dom-ts-4  227:     if(attr == "expandEntityReferences") {
                    228:         return true;
                    229:     }
1.3       dom-ts-4  230:     return false;
                    231:   }
                    232: 
                    233:   function MozDocumentBuilder(attrNames, attrValues) {
                    234:     this.attrNames = attrNames;
                    235:     this.attrValues = attrValues;
                    236:     this.load = MozDocumentBuilder_load;
                    237:     this.isDOMExceptionCode = MozDocumentBuilder_isDOMExceptionCode;
                    238:     this.getDOMImplementation = MozDocumentBuilder_getDOMImplementation;
                    239:     this.getImplementationAttribute = MozDocumentBuilder_getImplementationAttribute;
1.4       dom-ts-4  240:     //
                    241:     //   check if expandEntityReferences is false
                    242:     //     and throw an excpetion since that behavior is not supported
                    243:     //
                    244:     if(attrNames != null) {
                    245:         for(var i = 0; i < attrNames.length; i++) {
                    246:             if(attrNames[i] == "expandEntityReferences" && attrValues[i] == false) {
                    247:                 throw "Mozilla doesn't support entity preservation";
                    248:             }
                    249:         }
                    250:     }
1.3       dom-ts-4  251:   }
                    252: 
                    253:   var mozDefaultBuilder = null;
                    254: 
                    255:   function MozDocumentBuilderFactory_newDocumentBuilder(attrNames, attrValues) {
                    256:     if(attrNames == null && attrValues == null) {
                    257:         return mozDefaultBuilder;
                    258:     }
                    259:     return new MozDocumentBuilder(attrNames, attrValues);
                    260:   }
                    261: 
                    262:   function MozDocumentBuilderFactory() {
                    263:     this.newDocumentBuilder = MozDocumentBuilderFactory_newDocumentBuilder;
                    264:   }
                    265: 
                    266:   if(navigator.appName.indexOf("Netscape") != -1) {
                    267:     mozDefaultBuilder = new MozDocumentBuilder(null,null);
                    268:     factory = new MozDocumentBuilderFactory();
                    269:     //
                    270:     //  preload a couple of staff.xml documents
                    271:     //
                    272:     MozAdvanceLoad("staff.xml");
                    273:     MozAdvanceLoad("staff.xml");
                    274:   }
1.5       dom-ts-4  275: 
                    276: 
                    277:   //
                    278:   //   Adobe SVG Viewer support 
                    279:   //
                    280:   //
                    281:   //
1.3       dom-ts-4  282:     
1.5       dom-ts-4  283:   function ASVApplyParserAttributes(parser, attrNames, attrValues) {
                    284:   }
                    285:  
                    286:   //
                    287:   //   actually, array of <EMBED> elements
                    288:   //
                    289:   var asvStaffDocs = new Array();
                    290: 
                    291:   //
                    292:   //   get a document ready for the next test
                    293:   //
                    294:   function ASVStartLoad(sourceURL) {
                    295:     //
                    296:     //   must create embed element in containing HTML
                    297:     //
                    298:     var embed = document.createElement("embed");
                    299:     embed.src = sourceURL + ".svg";
                    300:     embed.height= 100;
                    301:     embed.width = 100;
                    302:     embed.type = "image/svg+xml";
                    303:     var htmlelem = document.documentElement;
                    304:     var child = htmlelem.firstChild;
                    305:     while(child != null) {
                    306:                if(child.nodeName == "BODY") {
                    307:                        child.appendChild(embed);
                    308:                        break;
                    309:                }
                    310:                child = child.nextSibling;
                    311:        }
                    312:        return embed;
                    313:   }
                    314: 
                    315:   function ASVDocumentBuilder_load(sourceURL, willBeModified) {
                    316:     if(sourceURL == 'staff.xml' && this.attrNames == null && this.attrValues == null) {
                    317:         var embed = null;
                    318:         for(var i = 0; i < asvStaffDocs.length; i++) {
                    319:             embed = asvStaffDocs[i];
                    320:             if(embed != null && embed.readyState == "complete") {
                    321:                 if(willBeModified) {
                    322:                     asvStaffDocs[i] = ASVStartLoad(sourceURL);
                    323:                 }
                    324:                 return embed.getSVGDocument();
                    325:             }
                    326:         }
                    327:     }
                    328:     var embed = ASVStartLoad(sourceURL);
                    329:     for(var i = 0; i < 5; i++) {
                    330:         alert("Document loading: Press OK continue.");
                    331:         if(embed.readyState == "complete") break;
                    332:     }
                    333: 
                    334:        doc = embed.getSVGDocument();
                    335: 
                    336:     if(sourceURL == "staff.xml") {
                    337:         if(willBeModified) {
                    338:             for(var i = 0; i <= asvStaffDocs.length; i++) {
                    339:                 if(i == asvStaffDocs.length || asvStaffDocs[i] == null) {
                    340:                     asvStaffDocs[i] = ASVStartLoad(sourceURL);
                    341:                     break;
                    342:                 }
                    343:             }
                    344:         }
                    345:         else {
                    346:             asvStaffDocs[asvStaffDocs.length] = embed;
                    347:         }
                    348:     }
                    349:     return doc;
                    350:   }
                    351: 
                    352:    function ASVDocumentBuilder_getDOMImplementation() {
                    353:         for(var i = 0; i < asvStaffDocs.length; i++) {
                    354:             if(asvStaffDocs[i] != null && asvStaffDocs[i].readyState == "complete") {
                    355:                 return asvStaffDocs[i].getSVGDocument().implementation;
                    356:             }
                    357:         }
                    358:         var embed = ASVStaffLoad("staff.xml");
                    359:         asvStaffDocs[asvStaffDocs.length] = embed;
                    360:         return embed.getSVGDocument().implementation;
                    361:    }
                    362: 
                    363:   function ASVDocumentBuilder_isDOMExceptionCode(ex, code) {
                    364:     return true;
                    365:   }
                    366: 
                    367:   function ASVDocumentBuilder_getImplementationAttribute(attr) {
                    368:     if(attr == "expandEntityReferences") {
                    369:         return true;
                    370:     }
                    371:     return false;
                    372:   }
                    373: 
                    374:   function ASVDocumentBuilder(attrNames, attrValues) {
                    375:     this.attrNames = attrNames;
                    376:     this.attrValues = attrValues;
                    377:     this.load = ASVDocumentBuilder_load;
                    378:     this.isDOMExceptionCode = ASVDocumentBuilder_isDOMExceptionCode;
                    379:     this.getDOMImplementation = ASVDocumentBuilder_getDOMImplementation;
                    380:     this.getImplementationAttribute = ASVDocumentBuilder_getImplementationAttribute;
                    381:     if(attrNames != null) {
                    382:         for(var i = 0; i < attrNames.length; i++) {
                    383:             if(attrNames[i] == "expandEntityReferences" && attrValues[i] == false) {
                    384:                 throw "Adobe SVG Viewer can not preserve entities";
                    385:             }
                    386:             if(attrNames[i] == "ignoringElementContentWhitespace" && attrValues[i] == true) {
                    387:                 throw "Adobe SVG Viewer can not preserve ignorable whitespace";
                    388:             }
                    389:         }
                    390:     }
                    391:   }
                    392: 
                    393:   var asvDefaultBuilder = null;
                    394: 
                    395:   function ASVDocumentBuilderFactory_newDocumentBuilder(attrNames, attrValues) {
                    396:     if(attrNames == null && attrValues == null) {
                    397:         return asvDefaultBuilder;
                    398:     }
                    399:     return new ASVDocumentBuilder(attrNames, attrValues);
                    400:   }
                    401: 
                    402:   function ASVDocumentBuilderFactory() {
                    403:     this.newDocumentBuilder = ASVDocumentBuilderFactory_newDocumentBuilder;
                    404:   }
                    405: 
                    406: 
                    407:   //
                    408:   //
                    409:   //    Uncomment the following 4 lines to test Adobe SVG Viewer
                    410:   //
                    411:   //
                    412:   //
                    413: 
1.8     ! dom-ts-4  414: //  if(navigator.appName.indexOf("Microsoft") != -1) {
        !           415: //    asvDefaultBuilder = new ASVDocumentBuilder(null,null);
        !           416: //    factory = new ASVDocumentBuilderFactory();
        !           417: //  }
1.5       dom-ts-4  418: 
1.3       dom-ts-4  419: 
                    420: 
1.1       dom-ts-4  421:   function IE5ApplyParserAttributes(parser, attrNames, attrValues) {
                    422:        if(attrNames != null) {
                    423:            var i;
                    424:                for(i = 0; i < attrNames.length; i++) {
                    425:                        if(attrNames[i] == "expandEntityReferences") {
                    426:                                if(attrValues[i] == true) {
                    427:                                        throw "MSXML does not support expanding entity references";
                    428:                                }
                    429:                        }
                    430:                        if(attrNames[i] == "validate") {
                    431:                                parser.validateOnParse = attrValues[i];
                    432:                        }
                    433:                        if(attrNames[i] == "ignoreElementContentWhitespace") {
                    434:                                parser.preserveWhiteSpace = !attrValues[i];
                    435:                        }
                    436:                }
                    437:         }
                    438:   }
                    439: 
                    440:   function IE5DocumentBuilder_load(sourceURL, willBeModified) {
1.2       dom-ts-4  441:        var actualURL = sourceURL;
1.1       dom-ts-4  442:        if(!this.parser.load(actualURL)) {
                    443:                throw this.parser.parseError.reason;
                    444:        }
                    445:     //
                    446:     //   if the first child of the document is a PI representing
                    447:     //      the XML Declaration, remove it from the tree.
                    448:     //
                    449:     //   According to the DOM FAQ, this behavior is not wrong,
                    450:     //      but the tests are written assumming that it is not there.
                    451:     //
                    452:     var xmlDecl = this.parser.firstChild;
                    453:     if(xmlDecl.nodeType == 7 && xmlDecl.target.toLowerCase() == "xml") {
                    454:         this.parser.removeChild(xmlDecl);
                    455:     }
                    456:        return this.parser;
                    457:   }
                    458: 
                    459:    function IE5DocumentBuilder_getDOMImplementation() {
                    460:         return this.parser.domImplementation;
                    461:    }
                    462: 
1.5       dom-ts-4  463:   //
                    464:   //   This function checks the exception raised in the test
                    465:   //   If this function returns true, then the exception is 
                    466:   //      consistent with the exception code parameter
                    467:   //
                    468:   //   This code attempts to determine the reason for the exception
                    469:   //      to reduce the chance that an unrelated exception causes
                    470:   //      the test to pass.
1.1       dom-ts-4  471:   function IE5DocumentBuilder_isDOMExceptionCode(ex, code) {
1.2       dom-ts-4  472:        var retval;
                    473:     switch(code) {
                    474:         //
                    475:         //  INDEX_SIZE_ERR
                    476:         case 1:
                    477:         retval = (ex.number == -2147024809);
                    478:         break;
                    479: 
                    480:         //
                    481:         //  HIERARCHY_REQUEST_ERR
                    482:         case 3:
                    483:         retval = (ex.number == -2147467259);
                    484:         break;
                    485: 
                    486: 
                    487:         //
                    488:         //  INVALID_CHARACTER_ERR
                    489:         case 5:
                    490:         retval = (ex.description.search(".*may not contain.*") >= 0);
                    491:         break;
                    492: 
                    493:         //
                    494:         //   NO_MODIFICATION_ALLOWED_ERR
                    495:         case 7:
1.1       dom-ts-4  496:                retval = (ex.description.search(".*read.*only.*") >= 0);
1.2       dom-ts-4  497:         break;
                    498: 
                    499:         //
                    500:         //   NOT_FOUND_ERR
                    501:         //
                    502:         case 8:
                    503:         retval = (ex.number == -2147024809 || ex.number == -2147467259);
                    504:         break;
                    505: 
                    506:         //
                    507:         //   INUSE_ATTRIBUTE_ERR
                    508:         case 10:
                    509:         retval = (ex.description.search(".*must be removed.*") >= 0);
                    510:         break;
1.1       dom-ts-4  511:        }
                    512:        return retval;
                    513:   }
                    514: 
                    515:   function IE5DocumentBuilder_getImplementationAttribute(attr) {
                    516:     if(attr == "ignoringElementContentWhitespace") {
                    517:         return !this.parser.preserveWhiteSpace;
                    518:     }
                    519:     return false;
                    520:   }
                    521: 
                    522:   function IE5DocumentBuilder(attrNames, attrValues) {
                    523:     this.attrNames = attrNames;
                    524:     this.attrValues = attrValues;
                    525:     this.load = IE5DocumentBuilder_load;
                    526:     this.isDOMExceptionCode = IE5DocumentBuilder_isDOMExceptionCode;
                    527:     this.getDOMImplementation = IE5DocumentBuilder_getDOMImplementation;
                    528:     this.getImplementationAttribute = IE5DocumentBuilder_getImplementationAttribute;
                    529:     this.parser = new ActiveXObject("MSXML2.DOMDocument.3.0");
                    530:     this.parser.async = false;
                    531:     this.parser.preserveWhiteSpace = true;
                    532:     IE5ApplyParserAttributes(this.parser,this.attrNames, this.attrValues);    
                    533:   }
                    534: 
                    535:   var IE5DefaultBuilder = new IE5DocumentBuilder(null,null);
                    536: 
                    537:   function IE5DocumentBuilderFactory_newDocumentBuilder(attrNames, attrValues) {
                    538:     if(attrNames == null && attrValues == null) {
                    539:         return IE5DefaultBuilder;
                    540:     }
                    541:     return new IE5DocumentBuilder(attrNames, attrValues);
                    542:   }
                    543: 
                    544:   function IE5DocumentBuilderFactory() {
                    545:     this.newDocumentBuilder = IE5DocumentBuilderFactory_newDocumentBuilder;
                    546:   }
                    547: 
1.3       dom-ts-4  548:   if(factory == null && navigator.appName.indexOf("Microsoft") != -1) {
                    549:     factory = new IE5DocumentBuilderFactory();
1.1       dom-ts-4  550:   }
                    551: 

Webmaster