1 /**
  2  * The MIT License (MIT)
  3  *
  4  * Copyright (c) 2016 DeNA Co., Ltd.
  5  *
  6  * Permission is hereby granted, free of charge, to any person obtaining a copy
  7  * of this software and associated documentation files (the "Software"), to deal
  8  * in the Software without restriction, including without limitation the rights
  9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10  * copies of the Software, and to permit persons to whom the Software is
 11  * furnished to do so, subject to the following conditions:
 12  *
 13  * The above copyright notice and this permission notice shall be included in
 14  * all copies or substantial portions of the Software.
 15  *
 16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 22  * SOFTWARE.
 23  */
 24 
 25 /// <reference path="base.js"/>
 26 
 27 /**
 28  * A class that parses a URL and creates properties compatible with the ones of
 29  * the Location interface.
 30  * @param {string} source
 31  * @constructor
 32  */
 33 createjs.Location = function(source) {
 34   var match = source.match(createjs.Location.PATTERN_);
 35 
 36   /**
 37    * The protocol scheme of the source URL.
 38    * @const {string}
 39    */
 40   this.protocol = match[1];
 41 
 42   /**
 43    * The user name of the source URL and its password.
 44    * @const {string}
 45    */
 46   this.user = match[2];
 47 
 48   /**
 49    * The host name of the source URL and its port number.
 50    * @const {string}
 51    */
 52   this.hostname = match[3];
 53 
 54   /**
 55    * A file path of the source URL.
 56    * @const {string}
 57    */
 58   this.pathname = match[4];
 59 
 60   /**
 61    * The search name (a.k.a. query) of the source URL.
 62    * @const {string}
 63    */
 64   this.search = match[5];
 65 
 66   /**
 67    * The fragment identifier (a.k.a. hash) of the source URL.
 68    * @const {string}
 69    */
 70   this.hash = match[6];
 71 };
 72 
 73 /**
 74  * A regular expression matching to a URI. This expression is used for dividing
 75  * a URI into properties compatible with the Location interface.
 76  * @const {RegExp}
 77  * @private
 78  */
 79 createjs.Location.PATTERN_ = new RegExp(
 80     '^' +
 81     '(?:' +
 82       '([^:/?#.]+:)' +   // $1 = protocol
 83     ')?' +
 84     '(?://' +
 85       '([^/?#]*@)?' +    // $2 = username + password
 86       '([^/?#@]+)?' +    // $3 = hostname
 87     ')?' +
 88     '([^?#]+)?' +        // $4 = pathname
 89     '(?:\\?([^#]*))?' +  // $5 = search
 90     '(?:#(.*))?' +       // $6 = hash
 91     '$');
 92 
 93 /**
 94  * Returns whether this location is a relative one.
 95  * @return {boolean}
 96  * @const
 97  */
 98 createjs.Location.prototype.isRelative = function() {
 99   return !!this.pathname && this.pathname.charCodeAt(0) != 0x2f;
100 };
101 
102 /**
103  * Returns whether this location is a cross-domain one.
104  * @return {boolean}
105  * @const
106  */
107 createjs.Location.prototype.isCrossDomain = function() {
108   var targetOrigin = this.protocol + '//' + this.hostname;
109   var origin = createjs.getOrigin();
110   return targetOrigin != origin;
111 };
112 
113 /**
114  * Returns whether this location is a local one.
115  * @return {boolean}
116  * @const
117  */
118 createjs.Location.prototype.isLocal = function() {
119   return !this.hostname || this.protocol == 'file:';
120 };
121 
122 /**
123  * Returns the extension.
124  * @return {string}
125  * @const
126  */
127 createjs.Location.prototype.getExtension = function() {
128   if (!this.pathname) {
129     return '';
130   }
131   var index = this.pathname.lastIndexOf('.');
132   if (index < 0) {
133     return '';
134   }
135   return this.pathname.substring(index + 1).toLowerCase();
136 };
137