| | 501 | |
| | 502 | dojo.Url = function(/*dojo.Url||String...*/){ |
| | 503 | // summary: |
| | 504 | // Constructor to create an object representing a URL. |
| | 505 | // description: |
| | 506 | // Each argument is evaluated in order relative to the next until |
| | 507 | // a canonical uri is produced. To get an absolute Uri relative to |
| | 508 | // the current document use: |
| | 509 | // new dojo.Url(document.baseURI, url) |
| | 510 | |
| | 511 | // TODO: support for IPv6, see RFC 2732 |
| | 512 | |
| | 513 | // resolve uri components relative to each other |
| | 514 | var n = null; |
| | 515 | var _a = arguments; |
| | 516 | var uri = _a[0]; |
| | 517 | for(var i = 1; i<_a.length; i++){ |
| | 518 | if(!_a[i]){ continue; } |
| | 519 | |
| | 520 | // Safari doesn't support this.constructor so we have to be explicit |
| | 521 | var relobj = new dojo.Url(_a[i]+""); |
| | 522 | var uriobj = new dojo.Url(uri+""); |
| | 523 | |
| | 524 | if( |
| | 525 | (relobj.path=="") && |
| | 526 | (!relobj.scheme) && |
| | 527 | (!relobj.authority) && |
| | 528 | (!relobj.query) |
| | 529 | ){ |
| | 530 | if(relobj.fragment != null){ |
| | 531 | uriobj.fragment = relobj.fragment; |
| | 532 | } |
| | 533 | relobj = uriobj; |
| | 534 | }else if(relobj.scheme == null){ |
| | 535 | relobj.scheme = uriobj.scheme; |
| | 536 | |
| | 537 | if(relobj.authority == null){ |
| | 538 | relobj.authority = uriobj.authority; |
| | 539 | |
| | 540 | if(relobj.path.charAt(0) != "/"){ |
| | 541 | var path = uriobj.path.substring(0, |
| | 542 | uriobj.path.lastIndexOf("/") + 1) + relobj.path; |
| | 543 | |
| | 544 | var segs = path.split("/"); |
| | 545 | for(var j = 0; j < segs.length; j++){ |
| | 546 | if(segs[j] == "."){ |
| | 547 | if (j == segs.length - 1) { segs[j] = ""; } |
| | 548 | else { segs.splice(j, 1); j--; } |
| | 549 | }else if(j > 0 && !(j == 1 && segs[0] == "") && |
| | 550 | segs[j] == ".." && segs[j-1] != ".."){ |
| | 551 | |
| | 552 | if(j == (segs.length - 1)){ |
| | 553 | segs.splice(j, 1); segs[j - 1] = ""; |
| | 554 | }else{ |
| | 555 | segs.splice(j - 1, 2); j -= 2; |
| | 556 | } |
| | 557 | } |
| | 558 | } |
| | 559 | relobj.path = segs.join("/"); |
| | 560 | } |
| | 561 | } |
| | 562 | } |
| | 563 | |
| | 564 | uri = ""; |
| | 565 | if(relobj.scheme != null){ |
| | 566 | uri += relobj.scheme + ":"; |
| | 567 | } |
| | 568 | if(relobj.authority != null){ |
| | 569 | uri += "//" + relobj.authority; |
| | 570 | } |
| | 571 | uri += relobj.path; |
| | 572 | if(relobj.query != null){ |
| | 573 | uri += "?" + relobj.query; |
| | 574 | } |
| | 575 | if(relobj.fragment != null){ |
| | 576 | uri += "#" + relobj.fragment; |
| | 577 | } |
| | 578 | } |
| | 579 | |
| | 580 | this.uri = uri.toString(); |
| | 581 | |
| | 582 | // break the uri into its main components |
| | 583 | var regexp = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$"; |
| | 584 | var r = this.uri.match(new RegExp(regexp)); |
| | 585 | |
| | 586 | this.scheme = r[2] || (r[1] ? "" : null); |
| | 587 | this.authority = r[4] || (r[3] ? "" : null); |
| | 588 | this.path = r[5]; // can never be undefined |
| | 589 | this.query = r[7] || (r[6] ? "" : null); |
| | 590 | this.fragment = r[9] || (r[8] ? "" : null); |
| | 591 | |
| | 592 | if(this.authority != null){ |
| | 593 | // server based naming authority |
| | 594 | regexp = "^((([^:]+:)?([^@]+))@)?([^:]*)(:([0-9]+))?$"; |
| | 595 | r = this.authority.match(new RegExp(regexp)); |
| | 596 | |
| | 597 | this.user = r[3] || null; |
| | 598 | this.password = r[4] || null; |
| | 599 | this.host = r[5]; |
| | 600 | this.port = r[7] || null; |
| | 601 | } |
| | 602 | |
| | 603 | this.toString = function(){ return this.uri; } |
| | 604 | } |
| | 605 | |
| | 606 | dojo.moduleUrl = function(/*String*/module, /*dojo.Url||String*/url){ |
| | 607 | // summary: |
| | 608 | // returns a Url object relative to a module |
| | 609 | // description: |
| | 610 | // Examples: |
| | 611 | // dojo.moduleUrl("dojo.widget","templates/template.html"); |
| | 612 | // dojo.moduleUrl("acme","images/small.png") |
| | 613 | |
| | 614 | var loc = dojo._getModuleSymbols(module).join('/'); |
| | 615 | if(!loc){ return null; } |
| | 616 | if(loc.lastIndexOf("/") != loc.length-1){ |
| | 617 | loc += "/"; |
| | 618 | } |
| | 619 | |
| | 620 | //If the path is an absolute path (starts with a / or is on another |
| | 621 | //domain/xdomain) then don't add the _baseUrl. |
| | 622 | var colonIndex = loc.indexOf(":"); |
| | 623 | if(loc.charAt(0) != "/" && (colonIndex == -1 || colonIndex > loc.indexOf("/"))){ |
| | 624 | loc = dojo._baseUrl + loc; |
| | 625 | } |
| | 626 | |
| | 627 | return new dojo.Url(loc, url); |
| | 628 | } |
| | 629 | |