├── .gitignore
├── IPv6.js
├── LICENSE
├── README.md
├── SecondLevelDomains.js
├── SwiftMoyaCodeGenerator.js
├── URI.js
├── URI.min.js
├── moya.mustache
├── mustache.js
└── punycode.js
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 |
--------------------------------------------------------------------------------
/IPv6.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * URI.js - Mutating URLs
3 | * IPv6 Support
4 | *
5 | * Version: 1.16.0
6 | *
7 | * Author: Rodney Rehm
8 | * Web: http://medialize.github.io/URI.js/
9 | *
10 | * Licensed under
11 | * MIT License http://www.opensource.org/licenses/mit-license
12 | * GPL v3 http://opensource.org/licenses/GPL-3.0
13 | *
14 | */
15 |
16 | (function (root, factory) {
17 | 'use strict';
18 | // https://github.com/umdjs/umd/blob/master/returnExports.js
19 | if (typeof exports === 'object') {
20 | // Node
21 | module.exports = factory();
22 | } else if (typeof define === 'function' && define.amd) {
23 | // AMD. Register as an anonymous module.
24 | define(factory);
25 | } else {
26 | // Browser globals (root is window)
27 | root.IPv6 = factory(root);
28 | }
29 | }(this, function (root) {
30 | 'use strict';
31 |
32 | /*
33 | var _in = "fe80:0000:0000:0000:0204:61ff:fe9d:f156";
34 | var _out = IPv6.best(_in);
35 | var _expected = "fe80::204:61ff:fe9d:f156";
36 |
37 | console.log(_in, _out, _expected, _out === _expected);
38 | */
39 |
40 | // save current IPv6 variable, if any
41 | var _IPv6 = root && root.IPv6;
42 |
43 | function bestPresentation(address) {
44 | // based on:
45 | // Javascript to test an IPv6 address for proper format, and to
46 | // present the "best text representation" according to IETF Draft RFC at
47 | // http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-04
48 | // 8 Feb 2010 Rich Brown, Dartware, LLC
49 | // Please feel free to use this code as long as you provide a link to
50 | // http://www.intermapper.com
51 | // http://intermapper.com/support/tools/IPV6-Validator.aspx
52 | // http://download.dartware.com/thirdparty/ipv6validator.js
53 |
54 | var _address = address.toLowerCase();
55 | var segments = _address.split(':');
56 | var length = segments.length;
57 | var total = 8;
58 |
59 | // trim colons (:: or ::a:b:c… or …a:b:c::)
60 | if (segments[0] === '' && segments[1] === '' && segments[2] === '') {
61 | // must have been ::
62 | // remove first two items
63 | segments.shift();
64 | segments.shift();
65 | } else if (segments[0] === '' && segments[1] === '') {
66 | // must have been ::xxxx
67 | // remove the first item
68 | segments.shift();
69 | } else if (segments[length - 1] === '' && segments[length - 2] === '') {
70 | // must have been xxxx::
71 | segments.pop();
72 | }
73 |
74 | length = segments.length;
75 |
76 | // adjust total segments for IPv4 trailer
77 | if (segments[length - 1].indexOf('.') !== -1) {
78 | // found a "." which means IPv4
79 | total = 7;
80 | }
81 |
82 | // fill empty segments them with "0000"
83 | var pos;
84 | for (pos = 0; pos < length; pos++) {
85 | if (segments[pos] === '') {
86 | break;
87 | }
88 | }
89 |
90 | if (pos < total) {
91 | segments.splice(pos, 1, '0000');
92 | while (segments.length < total) {
93 | segments.splice(pos, 0, '0000');
94 | }
95 |
96 | length = segments.length;
97 | }
98 |
99 | // strip leading zeros
100 | var _segments;
101 | for (var i = 0; i < total; i++) {
102 | _segments = segments[i].split('');
103 | for (var j = 0; j < 3 ; j++) {
104 | if (_segments[0] === '0' && _segments.length > 1) {
105 | _segments.splice(0,1);
106 | } else {
107 | break;
108 | }
109 | }
110 |
111 | segments[i] = _segments.join('');
112 | }
113 |
114 | // find longest sequence of zeroes and coalesce them into one segment
115 | var best = -1;
116 | var _best = 0;
117 | var _current = 0;
118 | var current = -1;
119 | var inzeroes = false;
120 | // i; already declared
121 |
122 | for (i = 0; i < total; i++) {
123 | if (inzeroes) {
124 | if (segments[i] === '0') {
125 | _current += 1;
126 | } else {
127 | inzeroes = false;
128 | if (_current > _best) {
129 | best = current;
130 | _best = _current;
131 | }
132 | }
133 | } else {
134 | if (segments[i] === '0') {
135 | inzeroes = true;
136 | current = i;
137 | _current = 1;
138 | }
139 | }
140 | }
141 |
142 | if (_current > _best) {
143 | best = current;
144 | _best = _current;
145 | }
146 |
147 | if (_best > 1) {
148 | segments.splice(best, _best, '');
149 | }
150 |
151 | length = segments.length;
152 |
153 | // assemble remaining segments
154 | var result = '';
155 | if (segments[0] === '') {
156 | result = ':';
157 | }
158 |
159 | for (i = 0; i < length; i++) {
160 | result += segments[i];
161 | if (i === length - 1) {
162 | break;
163 | }
164 |
165 | result += ':';
166 | }
167 |
168 | if (segments[length - 1] === '') {
169 | result += ':';
170 | }
171 |
172 | return result;
173 | }
174 |
175 | function noConflict() {
176 | /*jshint validthis: true */
177 | if (root.IPv6 === this) {
178 | root.IPv6 = _IPv6;
179 | }
180 |
181 | return this;
182 | }
183 |
184 | return {
185 | best: bestPresentation,
186 | noConflict: noConflict
187 | };
188 | }));
189 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Łukasz Kuczborski
4 | Copyright (c) 2017 Narlei Moreira
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 all
14 | 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 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Swift Moya Code Generator (Paw Extension)
2 |
3 | A [Paw Extension](http://luckymarmot.com/paw/extensions/) that generates [Moya](https://github.com/Moya/Moya) code for you!
4 |
5 | ## Install via Paw
6 | * Download directly from [Paw Extensions](https://paw.cloud/extensions/SwiftMoyaCodeGenerator)
7 |
8 | ## Install Directly
9 | * Donwload the project and rename the folder to `com.nam.PawExtensions.SwiftMoyaCodeGenerator`
10 | * Open Paw -> Preferences -> Extensions -> Open Extensions Directory
11 | * Copy the `com.nam.PawExtensions.SwiftMoyaCodeGenerator` folder to extensions directory
12 |
13 |
14 | ## Features
15 | * Parsing query params
16 | * Parsing key-value object provided as JSON Body
17 | * Type matching for params (`String` or `Int` based on param data provided in PAW, e.g. id = 2 will generate `let id: Int`)
18 |
19 | ### License
20 |
21 | This Paw Extension is released under the [MIT License](LICENSE). Feel free to fork, and modify!
22 |
23 | ### Bug report
24 | Create a github [issue](https://github.com/narlei/SwiftMoyaCodeGenerator/issues) or email to contato@narlei.com
25 |
26 | ### Credits
27 |
28 | * [Mustache.js](https://github.com/janl/mustache.js/), also released under the MIT License
29 | * [URI.js](http://medialize.github.io/URI.js/), also released under the MIT License
30 |
31 | Pay me a coffee:
32 |
33 | [](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=NMQM9R9GLZQXC&lc=BR&item_name=Narlei%20Moreira&item_number=development%2fdesign¤cy_code=BRL&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted)
34 |
--------------------------------------------------------------------------------
/SecondLevelDomains.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * URI.js - Mutating URLs
3 | * Second Level Domain (SLD) Support
4 | *
5 | * Version: 1.16.0
6 | *
7 | * Author: Rodney Rehm
8 | * Web: http://medialize.github.io/URI.js/
9 | *
10 | * Licensed under
11 | * MIT License http://www.opensource.org/licenses/mit-license
12 | * GPL v3 http://opensource.org/licenses/GPL-3.0
13 | *
14 | */
15 |
16 | (function (root, factory) {
17 | 'use strict';
18 | // https://github.com/umdjs/umd/blob/master/returnExports.js
19 | if (typeof exports === 'object') {
20 | // Node
21 | module.exports = factory();
22 | } else if (typeof define === 'function' && define.amd) {
23 | // AMD. Register as an anonymous module.
24 | define(factory);
25 | } else {
26 | // Browser globals (root is window)
27 | root.SecondLevelDomains = factory(root);
28 | }
29 | }(this, function (root) {
30 | 'use strict';
31 |
32 | // save current SecondLevelDomains variable, if any
33 | var _SecondLevelDomains = root && root.SecondLevelDomains;
34 |
35 | var SLD = {
36 | // list of known Second Level Domains
37 | // converted list of SLDs from https://github.com/gavingmiller/second-level-domains
38 | // ----
39 | // publicsuffix.org is more current and actually used by a couple of browsers internally.
40 | // downside is it also contains domains like "dyndns.org" - which is fine for the security
41 | // issues browser have to deal with (SOP for cookies, etc) - but is way overboard for URI.js
42 | // ----
43 | list: {
44 | 'ac':' com gov mil net org ',
45 | 'ae':' ac co gov mil name net org pro sch ',
46 | 'af':' com edu gov net org ',
47 | 'al':' com edu gov mil net org ',
48 | 'ao':' co ed gv it og pb ',
49 | 'ar':' com edu gob gov int mil net org tur ',
50 | 'at':' ac co gv or ',
51 | 'au':' asn com csiro edu gov id net org ',
52 | 'ba':' co com edu gov mil net org rs unbi unmo unsa untz unze ',
53 | 'bb':' biz co com edu gov info net org store tv ',
54 | 'bh':' biz cc com edu gov info net org ',
55 | 'bn':' com edu gov net org ',
56 | 'bo':' com edu gob gov int mil net org tv ',
57 | 'br':' adm adv agr am arq art ato b bio blog bmd cim cng cnt com coop ecn edu eng esp etc eti far flog fm fnd fot fst g12 ggf gov imb ind inf jor jus lel mat med mil mus net nom not ntr odo org ppg pro psc psi qsl rec slg srv tmp trd tur tv vet vlog wiki zlg ',
58 | 'bs':' com edu gov net org ',
59 | 'bz':' du et om ov rg ',
60 | 'ca':' ab bc mb nb nf nl ns nt nu on pe qc sk yk ',
61 | 'ck':' biz co edu gen gov info net org ',
62 | 'cn':' ac ah bj com cq edu fj gd gov gs gx gz ha hb he hi hl hn jl js jx ln mil net nm nx org qh sc sd sh sn sx tj tw xj xz yn zj ',
63 | 'co':' com edu gov mil net nom org ',
64 | 'cr':' ac c co ed fi go or sa ',
65 | 'cy':' ac biz com ekloges gov ltd name net org parliament press pro tm ',
66 | 'do':' art com edu gob gov mil net org sld web ',
67 | 'dz':' art asso com edu gov net org pol ',
68 | 'ec':' com edu fin gov info med mil net org pro ',
69 | 'eg':' com edu eun gov mil name net org sci ',
70 | 'er':' com edu gov ind mil net org rochest w ',
71 | 'es':' com edu gob nom org ',
72 | 'et':' biz com edu gov info name net org ',
73 | 'fj':' ac biz com info mil name net org pro ',
74 | 'fk':' ac co gov net nom org ',
75 | 'fr':' asso com f gouv nom prd presse tm ',
76 | 'gg':' co net org ',
77 | 'gh':' com edu gov mil org ',
78 | 'gn':' ac com gov net org ',
79 | 'gr':' com edu gov mil net org ',
80 | 'gt':' com edu gob ind mil net org ',
81 | 'gu':' com edu gov net org ',
82 | 'hk':' com edu gov idv net org ',
83 | 'hu':' 2000 agrar bolt casino city co erotica erotika film forum games hotel info ingatlan jogasz konyvelo lakas media news org priv reklam sex shop sport suli szex tm tozsde utazas video ',
84 | 'id':' ac co go mil net or sch web ',
85 | 'il':' ac co gov idf k12 muni net org ',
86 | 'in':' ac co edu ernet firm gen gov i ind mil net nic org res ',
87 | 'iq':' com edu gov i mil net org ',
88 | 'ir':' ac co dnssec gov i id net org sch ',
89 | 'it':' edu gov ',
90 | 'je':' co net org ',
91 | 'jo':' com edu gov mil name net org sch ',
92 | 'jp':' ac ad co ed go gr lg ne or ',
93 | 'ke':' ac co go info me mobi ne or sc ',
94 | 'kh':' com edu gov mil net org per ',
95 | 'ki':' biz com de edu gov info mob net org tel ',
96 | 'km':' asso com coop edu gouv k medecin mil nom notaires pharmaciens presse tm veterinaire ',
97 | 'kn':' edu gov net org ',
98 | 'kr':' ac busan chungbuk chungnam co daegu daejeon es gangwon go gwangju gyeongbuk gyeonggi gyeongnam hs incheon jeju jeonbuk jeonnam k kg mil ms ne or pe re sc seoul ulsan ',
99 | 'kw':' com edu gov net org ',
100 | 'ky':' com edu gov net org ',
101 | 'kz':' com edu gov mil net org ',
102 | 'lb':' com edu gov net org ',
103 | 'lk':' assn com edu gov grp hotel int ltd net ngo org sch soc web ',
104 | 'lr':' com edu gov net org ',
105 | 'lv':' asn com conf edu gov id mil net org ',
106 | 'ly':' com edu gov id med net org plc sch ',
107 | 'ma':' ac co gov m net org press ',
108 | 'mc':' asso tm ',
109 | 'me':' ac co edu gov its net org priv ',
110 | 'mg':' com edu gov mil nom org prd tm ',
111 | 'mk':' com edu gov inf name net org pro ',
112 | 'ml':' com edu gov net org presse ',
113 | 'mn':' edu gov org ',
114 | 'mo':' com edu gov net org ',
115 | 'mt':' com edu gov net org ',
116 | 'mv':' aero biz com coop edu gov info int mil museum name net org pro ',
117 | 'mw':' ac co com coop edu gov int museum net org ',
118 | 'mx':' com edu gob net org ',
119 | 'my':' com edu gov mil name net org sch ',
120 | 'nf':' arts com firm info net other per rec store web ',
121 | 'ng':' biz com edu gov mil mobi name net org sch ',
122 | 'ni':' ac co com edu gob mil net nom org ',
123 | 'np':' com edu gov mil net org ',
124 | 'nr':' biz com edu gov info net org ',
125 | 'om':' ac biz co com edu gov med mil museum net org pro sch ',
126 | 'pe':' com edu gob mil net nom org sld ',
127 | 'ph':' com edu gov i mil net ngo org ',
128 | 'pk':' biz com edu fam gob gok gon gop gos gov net org web ',
129 | 'pl':' art bialystok biz com edu gda gdansk gorzow gov info katowice krakow lodz lublin mil net ngo olsztyn org poznan pwr radom slupsk szczecin torun warszawa waw wroc wroclaw zgora ',
130 | 'pr':' ac biz com edu est gov info isla name net org pro prof ',
131 | 'ps':' com edu gov net org plo sec ',
132 | 'pw':' belau co ed go ne or ',
133 | 'ro':' arts com firm info nom nt org rec store tm www ',
134 | 'rs':' ac co edu gov in org ',
135 | 'sb':' com edu gov net org ',
136 | 'sc':' com edu gov net org ',
137 | 'sh':' co com edu gov net nom org ',
138 | 'sl':' com edu gov net org ',
139 | 'st':' co com consulado edu embaixada gov mil net org principe saotome store ',
140 | 'sv':' com edu gob org red ',
141 | 'sz':' ac co org ',
142 | 'tr':' av bbs bel biz com dr edu gen gov info k12 name net org pol tel tsk tv web ',
143 | 'tt':' aero biz cat co com coop edu gov info int jobs mil mobi museum name net org pro tel travel ',
144 | 'tw':' club com ebiz edu game gov idv mil net org ',
145 | 'mu':' ac co com gov net or org ',
146 | 'mz':' ac co edu gov org ',
147 | 'na':' co com ',
148 | 'nz':' ac co cri geek gen govt health iwi maori mil net org parliament school ',
149 | 'pa':' abo ac com edu gob ing med net nom org sld ',
150 | 'pt':' com edu gov int net nome org publ ',
151 | 'py':' com edu gov mil net org ',
152 | 'qa':' com edu gov mil net org ',
153 | 're':' asso com nom ',
154 | 'ru':' ac adygeya altai amur arkhangelsk astrakhan bashkiria belgorod bir bryansk buryatia cbg chel chelyabinsk chita chukotka chuvashia com dagestan e-burg edu gov grozny int irkutsk ivanovo izhevsk jar joshkar-ola kalmykia kaluga kamchatka karelia kazan kchr kemerovo khabarovsk khakassia khv kirov koenig komi kostroma kranoyarsk kuban kurgan kursk lipetsk magadan mari mari-el marine mil mordovia mosreg msk murmansk nalchik net nnov nov novosibirsk nsk omsk orenburg org oryol penza perm pp pskov ptz rnd ryazan sakhalin samara saratov simbirsk smolensk spb stavropol stv surgut tambov tatarstan tom tomsk tsaritsyn tsk tula tuva tver tyumen udm udmurtia ulan-ude vladikavkaz vladimir vladivostok volgograd vologda voronezh vrn vyatka yakutia yamal yekaterinburg yuzhno-sakhalinsk ',
155 | 'rw':' ac co com edu gouv gov int mil net ',
156 | 'sa':' com edu gov med net org pub sch ',
157 | 'sd':' com edu gov info med net org tv ',
158 | 'se':' a ac b bd c d e f g h i k l m n o org p parti pp press r s t tm u w x y z ',
159 | 'sg':' com edu gov idn net org per ',
160 | 'sn':' art com edu gouv org perso univ ',
161 | 'sy':' com edu gov mil net news org ',
162 | 'th':' ac co go in mi net or ',
163 | 'tj':' ac biz co com edu go gov info int mil name net nic org test web ',
164 | 'tn':' agrinet com defense edunet ens fin gov ind info intl mincom nat net org perso rnrt rns rnu tourism ',
165 | 'tz':' ac co go ne or ',
166 | 'ua':' biz cherkassy chernigov chernovtsy ck cn co com crimea cv dn dnepropetrovsk donetsk dp edu gov if in ivano-frankivsk kh kharkov kherson khmelnitskiy kiev kirovograd km kr ks kv lg lugansk lutsk lviv me mk net nikolaev od odessa org pl poltava pp rovno rv sebastopol sumy te ternopil uzhgorod vinnica vn zaporizhzhe zhitomir zp zt ',
167 | 'ug':' ac co go ne or org sc ',
168 | 'uk':' ac bl british-library co cym gov govt icnet jet lea ltd me mil mod national-library-scotland nel net nhs nic nls org orgn parliament plc police sch scot soc ',
169 | 'us':' dni fed isa kids nsn ',
170 | 'uy':' com edu gub mil net org ',
171 | 've':' co com edu gob info mil net org web ',
172 | 'vi':' co com k12 net org ',
173 | 'vn':' ac biz com edu gov health info int name net org pro ',
174 | 'ye':' co com gov ltd me net org plc ',
175 | 'yu':' ac co edu gov org ',
176 | 'za':' ac agric alt bourse city co cybernet db edu gov grondar iaccess imt inca landesign law mil net ngo nis nom olivetti org pix school tm web ',
177 | 'zm':' ac co com edu gov net org sch '
178 | },
179 | // gorhill 2013-10-25: Using indexOf() instead Regexp(). Significant boost
180 | // in both performance and memory footprint. No initialization required.
181 | // http://jsperf.com/uri-js-sld-regex-vs-binary-search/4
182 | // Following methods use lastIndexOf() rather than array.split() in order
183 | // to avoid any memory allocations.
184 | has: function(domain) {
185 | var tldOffset = domain.lastIndexOf('.');
186 | if (tldOffset <= 0 || tldOffset >= (domain.length-1)) {
187 | return false;
188 | }
189 | var sldOffset = domain.lastIndexOf('.', tldOffset-1);
190 | if (sldOffset <= 0 || sldOffset >= (tldOffset-1)) {
191 | return false;
192 | }
193 | var sldList = SLD.list[domain.slice(tldOffset+1)];
194 | if (!sldList) {
195 | return false;
196 | }
197 | return sldList.indexOf(' ' + domain.slice(sldOffset+1, tldOffset) + ' ') >= 0;
198 | },
199 | is: function(domain) {
200 | var tldOffset = domain.lastIndexOf('.');
201 | if (tldOffset <= 0 || tldOffset >= (domain.length-1)) {
202 | return false;
203 | }
204 | var sldOffset = domain.lastIndexOf('.', tldOffset-1);
205 | if (sldOffset >= 0) {
206 | return false;
207 | }
208 | var sldList = SLD.list[domain.slice(tldOffset+1)];
209 | if (!sldList) {
210 | return false;
211 | }
212 | return sldList.indexOf(' ' + domain.slice(0, tldOffset) + ' ') >= 0;
213 | },
214 | get: function(domain) {
215 | var tldOffset = domain.lastIndexOf('.');
216 | if (tldOffset <= 0 || tldOffset >= (domain.length-1)) {
217 | return null;
218 | }
219 | var sldOffset = domain.lastIndexOf('.', tldOffset-1);
220 | if (sldOffset <= 0 || sldOffset >= (tldOffset-1)) {
221 | return null;
222 | }
223 | var sldList = SLD.list[domain.slice(tldOffset+1)];
224 | if (!sldList) {
225 | return null;
226 | }
227 | if (sldList.indexOf(' ' + domain.slice(sldOffset+1, tldOffset) + ' ') < 0) {
228 | return null;
229 | }
230 | return domain.slice(sldOffset+1);
231 | },
232 | noConflict: function(){
233 | if (root.SecondLevelDomains === this) {
234 | root.SecondLevelDomains = _SecondLevelDomains;
235 | }
236 | return this;
237 | }
238 | };
239 |
240 | return SLD;
241 | }));
242 |
--------------------------------------------------------------------------------
/SwiftMoyaCodeGenerator.js:
--------------------------------------------------------------------------------
1 | (function() {
2 | var uri = require('/URI');
3 | var mustache = require('./mustache');
4 |
5 | var SwiftMoyaCodeGenerator = function() {
6 |
7 | this.generate = function(context) {
8 |
9 | var template = readFile("moya.mustache");
10 | var request = context.getCurrentRequest();
11 | var apiName = "";
12 | var url = uri.parse(request.url);
13 | var requestMethod = request.method.toLowerCase();
14 | var headers = "";
15 |
16 | var requestParameter = "";
17 | var requestName = request.name;
18 | var queryParamsType = "";
19 | var pathExtension = url.path;
20 |
21 | if (request.parent) {
22 | apiName = request.parent.name;
23 | }
24 |
25 | if (request.name.indexOf('{') > 0) {
26 | requestParameter = request.name.match("{([^']+)}")[1];
27 | requestName = requestName.replace(request.name.match("{([^']+)}")[0], "");
28 | queryParamsType = requestParameter + ": Int";
29 | }
30 |
31 | requestName = requestName.replace("/", "");
32 |
33 | var pathFragments = pathExtension.split('/');
34 | for (var i = 0; i < pathFragments.length; i++) {
35 | if (pathFragments[i] == requestName) {
36 | pathExtension = pathExtension.replace(pathFragments[i + 1], "\\(" + requestParameter + ")");
37 | break;
38 | }
39 | }
40 |
41 | for (header_name in request.headers) {
42 | var header_value = request.headers[header_name];
43 | if (header_name.toLowerCase() !== 'authorization') {
44 | if (headers) {
45 | headers = headers + ", \n ";
46 | }
47 | headers = headers + '\"' + header_name + '\" : \"' + header_value + '\"';
48 | }
49 | }
50 |
51 |
52 | var view = {
53 | "request": request,
54 | "baseURL": url.protocol + "://" + url.hostname,
55 | "pathExtension": pathExtension,
56 | "requestName": requestName,
57 | "requestParameter": "let " + requestParameter,
58 | "apiName": apiName,
59 | "requestMethod": requestMethod,
60 | "headers": headers,
61 | };
62 |
63 |
64 |
65 | var query = url.query;
66 | if (query) {
67 | var fragments = query.split('&');
68 | var keyvalue = fragments[0].split('=');
69 |
70 | if (queryParamsType) {
71 | queryParamsType = queryParamsType + ", " + keyvalue[0] + ": " + typeForObject(keyvalue[1]);
72 | }else{
73 | queryParamsType = keyvalue[0] + ": " + typeForObject(keyvalue[1]);
74 | }
75 | var queryParamsTemplate = "_";
76 | var queryParams = "let " + keyvalue[0];
77 | var queryDictString = "\"" + keyvalue[0] + "\": " + keyvalue[0];
78 |
79 |
80 | for (var i = 1; i < fragments.length; i++) {
81 | keyvalue = fragments[i].split('=');
82 | queryParamsType += ", " + keyvalue[0] + ": " + typeForObject(keyvalue[1]);
83 | queryParamsTemplate += ", _";
84 | queryParams += ", let " + keyvalue[0];
85 | queryDictString += ", \"" + keyvalue[0] + "\": " + keyvalue[0];
86 | }
87 |
88 | view["queryParamsTemplate"] = queryParamsTemplate;
89 | view["queryParams"] = queryParams;
90 | view["queryDictString"] = queryDictString;
91 | }
92 |
93 | view["queryParamsType"] = queryParamsType;
94 |
95 | var jsonBody = request.jsonBody;
96 | if (jsonBody && Object.keys(jsonBody).length > 0) {
97 | var firstKey = Object.keys(jsonBody)[0];
98 | var jsonBodyParamsType = firstKey + ": " + typeForObject(jsonBody[firstKey]);
99 | var jsonBodyParamsTemplate = "_";
100 | var jsonBodyParams = "let " + firstKey;
101 | var jsonBodyDictString = "\"" + firstKey + "\": " + firstKey;
102 | for (var i = 1; i < Object.keys(jsonBody).length; i++) {
103 | var key = Object.keys(jsonBody)[i];
104 | if (jsonBody.hasOwnProperty(key)) {
105 | jsonBodyParamsType += ", " + key + ": " + typeForObject(jsonBody[key]);
106 | jsonBodyParamsTemplate += ", _";
107 | jsonBodyParams += ", let " + key;
108 | jsonBodyDictString += ", \"" + key + "\": " + key;
109 | }
110 | }
111 |
112 | view["jsonBodyParamsType"] = jsonBodyParamsType;
113 | view["jsonBodyParamsTemplate"] = jsonBodyParamsTemplate;
114 | view["jsonBodyParams"] = jsonBodyParams;
115 | view["jsonBodyDictString"] = jsonBodyDictString;
116 | }
117 |
118 | return mustache.render(template, view);
119 | }
120 | }
121 |
122 | function isNumber(obj) {
123 | return !isNaN(parseFloat(obj));
124 | }
125 |
126 | function typeForObject(obj) {
127 | return (isNumber(obj) ? "Int" : "String");
128 | }
129 |
130 | SwiftMoyaCodeGenerator.identifier = "com.nam.PawExtensions.SwiftMoyaCodeGenerator";
131 | SwiftMoyaCodeGenerator.title = "Swift (Moya)";
132 | SwiftMoyaCodeGenerator.fileExtension = "swift";
133 | SwiftMoyaCodeGenerator.languageHighlighter = "swift";
134 |
135 | registerCodeGenerator(SwiftMoyaCodeGenerator);
136 | }).call(this);
--------------------------------------------------------------------------------
/URI.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * URI.js - Mutating URLs
3 | *
4 | * Version: 1.16.0
5 | *
6 | * Author: Rodney Rehm
7 | * Web: http://medialize.github.io/URI.js/
8 | *
9 | * Licensed under
10 | * MIT License http://www.opensource.org/licenses/mit-license
11 | * GPL v3 http://opensource.org/licenses/GPL-3.0
12 | *
13 | */
14 | (function (root, factory) {
15 | 'use strict';
16 | // https://github.com/umdjs/umd/blob/master/returnExports.js
17 | if (typeof exports === 'object') {
18 | // Node
19 | module.exports = factory(require('./punycode'), require('./IPv6'), require('./SecondLevelDomains'));
20 | } else if (typeof define === 'function' && define.amd) {
21 | // AMD. Register as an anonymous module.
22 | define(['./punycode', './IPv6', './SecondLevelDomains'], factory);
23 | } else {
24 | // Browser globals (root is window)
25 | root.URI = factory(root.punycode, root.IPv6, root.SecondLevelDomains, root);
26 | }
27 | }(this, function (punycode, IPv6, SLD, root) {
28 | 'use strict';
29 | /*global location, escape, unescape */
30 | // FIXME: v2.0.0 renamce non-camelCase properties to uppercase
31 | /*jshint camelcase: false */
32 |
33 | // save current URI variable, if any
34 | var _URI = root && root.URI;
35 |
36 | function URI(url, base) {
37 | var _urlSupplied = arguments.length >= 1;
38 | var _baseSupplied = arguments.length >= 2;
39 |
40 | // Allow instantiation without the 'new' keyword
41 | if (!(this instanceof URI)) {
42 | if (_urlSupplied) {
43 | if (_baseSupplied) {
44 | return new URI(url, base);
45 | }
46 |
47 | return new URI(url);
48 | }
49 |
50 | return new URI();
51 | }
52 |
53 | if (url === undefined) {
54 | if (_urlSupplied) {
55 | throw new TypeError('undefined is not a valid argument for URI');
56 | }
57 |
58 | if (typeof location !== 'undefined') {
59 | url = location.href + '';
60 | } else {
61 | url = '';
62 | }
63 | }
64 |
65 | this.href(url);
66 |
67 | // resolve to base according to http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#constructor
68 | if (base !== undefined) {
69 | return this.absoluteTo(base);
70 | }
71 |
72 | return this;
73 | }
74 |
75 | URI.version = '1.16.0';
76 |
77 | var p = URI.prototype;
78 | var hasOwn = Object.prototype.hasOwnProperty;
79 |
80 | function escapeRegEx(string) {
81 | // https://github.com/medialize/URI.js/commit/85ac21783c11f8ccab06106dba9735a31a86924d#commitcomment-821963
82 | return string.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
83 | }
84 |
85 | function getType(value) {
86 | // IE8 doesn't return [Object Undefined] but [Object Object] for undefined value
87 | if (value === undefined) {
88 | return 'Undefined';
89 | }
90 |
91 | return String(Object.prototype.toString.call(value)).slice(8, -1);
92 | }
93 |
94 | function isArray(obj) {
95 | return getType(obj) === 'Array';
96 | }
97 |
98 | function filterArrayValues(data, value) {
99 | var lookup = {};
100 | var i, length;
101 |
102 | if (getType(value) === 'RegExp') {
103 | lookup = null;
104 | } else if (isArray(value)) {
105 | for (i = 0, length = value.length; i < length; i++) {
106 | lookup[value[i]] = true;
107 | }
108 | } else {
109 | lookup[value] = true;
110 | }
111 |
112 | for (i = 0, length = data.length; i < length; i++) {
113 | /*jshint laxbreak: true */
114 | var _match = lookup && lookup[data[i]] !== undefined
115 | || !lookup && value.test(data[i]);
116 | /*jshint laxbreak: false */
117 | if (_match) {
118 | data.splice(i, 1);
119 | length--;
120 | i--;
121 | }
122 | }
123 |
124 | return data;
125 | }
126 |
127 | function arrayContains(list, value) {
128 | var i, length;
129 |
130 | // value may be string, number, array, regexp
131 | if (isArray(value)) {
132 | // Note: this can be optimized to O(n) (instead of current O(m * n))
133 | for (i = 0, length = value.length; i < length; i++) {
134 | if (!arrayContains(list, value[i])) {
135 | return false;
136 | }
137 | }
138 |
139 | return true;
140 | }
141 |
142 | var _type = getType(value);
143 | for (i = 0, length = list.length; i < length; i++) {
144 | if (_type === 'RegExp') {
145 | if (typeof list[i] === 'string' && list[i].match(value)) {
146 | return true;
147 | }
148 | } else if (list[i] === value) {
149 | return true;
150 | }
151 | }
152 |
153 | return false;
154 | }
155 |
156 | function arraysEqual(one, two) {
157 | if (!isArray(one) || !isArray(two)) {
158 | return false;
159 | }
160 |
161 | // arrays can't be equal if they have different amount of content
162 | if (one.length !== two.length) {
163 | return false;
164 | }
165 |
166 | one.sort();
167 | two.sort();
168 |
169 | for (var i = 0, l = one.length; i < l; i++) {
170 | if (one[i] !== two[i]) {
171 | return false;
172 | }
173 | }
174 |
175 | return true;
176 | }
177 |
178 | URI._parts = function() {
179 | return {
180 | protocol: null,
181 | username: null,
182 | password: null,
183 | hostname: null,
184 | urn: null,
185 | port: null,
186 | path: null,
187 | query: null,
188 | fragment: null,
189 | // state
190 | duplicateQueryParameters: URI.duplicateQueryParameters,
191 | escapeQuerySpace: URI.escapeQuerySpace
192 | };
193 | };
194 | // state: allow duplicate query parameters (a=1&a=1)
195 | URI.duplicateQueryParameters = false;
196 | // state: replaces + with %20 (space in query strings)
197 | URI.escapeQuerySpace = true;
198 | // static properties
199 | URI.protocol_expression = /^[a-z][a-z0-9.+-]*$/i;
200 | URI.idn_expression = /[^a-z0-9\.-]/i;
201 | URI.punycode_expression = /(xn--)/i;
202 | // well, 333.444.555.666 matches, but it sure ain't no IPv4 - do we care?
203 | URI.ip4_expression = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
204 | // credits to Rich Brown
205 | // source: http://forums.intermapper.com/viewtopic.php?p=1096#1096
206 | // specification: http://www.ietf.org/rfc/rfc4291.txt
207 | URI.ip6_expression = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;
208 | // expression used is "gruber revised" (@gruber v2) determined to be the
209 | // best solution in a regex-golf we did a couple of ages ago at
210 | // * http://mathiasbynens.be/demo/url-regex
211 | // * http://rodneyrehm.de/t/url-regex.html
212 | URI.find_uri_expression = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/ig;
213 | URI.findUri = {
214 | // valid "scheme://" or "www."
215 | start: /\b(?:([a-z][a-z0-9.+-]*:\/\/)|www\.)/gi,
216 | // everything up to the next whitespace
217 | end: /[\s\r\n]|$/,
218 | // trim trailing punctuation captured by end RegExp
219 | trim: /[`!()\[\]{};:'".,<>?«»“”„‘’]+$/
220 | };
221 | // http://www.iana.org/assignments/uri-schemes.html
222 | // http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Well-known_ports
223 | URI.defaultPorts = {
224 | http: '80',
225 | https: '443',
226 | ftp: '21',
227 | gopher: '70',
228 | ws: '80',
229 | wss: '443'
230 | };
231 | // allowed hostname characters according to RFC 3986
232 | // ALPHA DIGIT "-" "." "_" "~" "!" "$" "&" "'" "(" ")" "*" "+" "," ";" "=" %encoded
233 | // I've never seen a (non-IDN) hostname other than: ALPHA DIGIT . -
234 | URI.invalid_hostname_characters = /[^a-zA-Z0-9\.-]/;
235 | // map DOM Elements to their URI attribute
236 | URI.domAttributes = {
237 | 'a': 'href',
238 | 'blockquote': 'cite',
239 | 'link': 'href',
240 | 'base': 'href',
241 | 'script': 'src',
242 | 'form': 'action',
243 | 'img': 'src',
244 | 'area': 'href',
245 | 'iframe': 'src',
246 | 'embed': 'src',
247 | 'source': 'src',
248 | 'track': 'src',
249 | 'input': 'src', // but only if type="image"
250 | 'audio': 'src',
251 | 'video': 'src'
252 | };
253 | URI.getDomAttribute = function(node) {
254 | if (!node || !node.nodeName) {
255 | return undefined;
256 | }
257 |
258 | var nodeName = node.nodeName.toLowerCase();
259 | // should only expose src for type="image"
260 | if (nodeName === 'input' && node.type !== 'image') {
261 | return undefined;
262 | }
263 |
264 | return URI.domAttributes[nodeName];
265 | };
266 |
267 | function escapeForDumbFirefox36(value) {
268 | // https://github.com/medialize/URI.js/issues/91
269 | return escape(value);
270 | }
271 |
272 | // encoding / decoding according to RFC3986
273 | function strictEncodeURIComponent(string) {
274 | // see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent
275 | return encodeURIComponent(string)
276 | .replace(/[!'()*]/g, escapeForDumbFirefox36)
277 | .replace(/\*/g, '%2A');
278 | }
279 | URI.encode = strictEncodeURIComponent;
280 | URI.decode = decodeURIComponent;
281 | URI.iso8859 = function() {
282 | URI.encode = escape;
283 | URI.decode = unescape;
284 | };
285 | URI.unicode = function() {
286 | URI.encode = strictEncodeURIComponent;
287 | URI.decode = decodeURIComponent;
288 | };
289 | URI.characters = {
290 | pathname: {
291 | encode: {
292 | // RFC3986 2.1: For consistency, URI producers and normalizers should
293 | // use uppercase hexadecimal digits for all percent-encodings.
294 | expression: /%(24|26|2B|2C|3B|3D|3A|40)/ig,
295 | map: {
296 | // -._~!'()*
297 | '%24': '$',
298 | '%26': '&',
299 | '%2B': '+',
300 | '%2C': ',',
301 | '%3B': ';',
302 | '%3D': '=',
303 | '%3A': ':',
304 | '%40': '@'
305 | }
306 | },
307 | decode: {
308 | expression: /[\/\?#]/g,
309 | map: {
310 | '/': '%2F',
311 | '?': '%3F',
312 | '#': '%23'
313 | }
314 | }
315 | },
316 | reserved: {
317 | encode: {
318 | // RFC3986 2.1: For consistency, URI producers and normalizers should
319 | // use uppercase hexadecimal digits for all percent-encodings.
320 | expression: /%(21|23|24|26|27|28|29|2A|2B|2C|2F|3A|3B|3D|3F|40|5B|5D)/ig,
321 | map: {
322 | // gen-delims
323 | '%3A': ':',
324 | '%2F': '/',
325 | '%3F': '?',
326 | '%23': '#',
327 | '%5B': '[',
328 | '%5D': ']',
329 | '%40': '@',
330 | // sub-delims
331 | '%21': '!',
332 | '%24': '$',
333 | '%26': '&',
334 | '%27': '\'',
335 | '%28': '(',
336 | '%29': ')',
337 | '%2A': '*',
338 | '%2B': '+',
339 | '%2C': ',',
340 | '%3B': ';',
341 | '%3D': '='
342 | }
343 | }
344 | },
345 | urnpath: {
346 | // The characters under `encode` are the characters called out by RFC 2141 as being acceptable
347 | // for usage in a URN. RFC2141 also calls out "-", ".", and "_" as acceptable characters, but
348 | // these aren't encoded by encodeURIComponent, so we don't have to call them out here. Also
349 | // note that the colon character is not featured in the encoding map; this is because URI.js
350 | // gives the colons in URNs semantic meaning as the delimiters of path segements, and so it
351 | // should not appear unencoded in a segment itself.
352 | // See also the note above about RFC3986 and capitalalized hex digits.
353 | encode: {
354 | expression: /%(21|24|27|28|29|2A|2B|2C|3B|3D|40)/ig,
355 | map: {
356 | '%21': '!',
357 | '%24': '$',
358 | '%27': '\'',
359 | '%28': '(',
360 | '%29': ')',
361 | '%2A': '*',
362 | '%2B': '+',
363 | '%2C': ',',
364 | '%3B': ';',
365 | '%3D': '=',
366 | '%40': '@'
367 | }
368 | },
369 | // These characters are the characters called out by RFC2141 as "reserved" characters that
370 | // should never appear in a URN, plus the colon character (see note above).
371 | decode: {
372 | expression: /[\/\?#:]/g,
373 | map: {
374 | '/': '%2F',
375 | '?': '%3F',
376 | '#': '%23',
377 | ':': '%3A'
378 | }
379 | }
380 | }
381 | };
382 | URI.encodeQuery = function(string, escapeQuerySpace) {
383 | var escaped = URI.encode(string + '');
384 | if (escapeQuerySpace === undefined) {
385 | escapeQuerySpace = URI.escapeQuerySpace;
386 | }
387 |
388 | return escapeQuerySpace ? escaped.replace(/%20/g, '+') : escaped;
389 | };
390 | URI.decodeQuery = function(string, escapeQuerySpace) {
391 | string += '';
392 | if (escapeQuerySpace === undefined) {
393 | escapeQuerySpace = URI.escapeQuerySpace;
394 | }
395 |
396 | try {
397 | return URI.decode(escapeQuerySpace ? string.replace(/\+/g, '%20') : string);
398 | } catch(e) {
399 | // we're not going to mess with weird encodings,
400 | // give up and return the undecoded original string
401 | // see https://github.com/medialize/URI.js/issues/87
402 | // see https://github.com/medialize/URI.js/issues/92
403 | return string;
404 | }
405 | };
406 | // generate encode/decode path functions
407 | var _parts = {'encode':'encode', 'decode':'decode'};
408 | var _part;
409 | var generateAccessor = function(_group, _part) {
410 | return function(string) {
411 | try {
412 | return URI[_part](string + '').replace(URI.characters[_group][_part].expression, function(c) {
413 | return URI.characters[_group][_part].map[c];
414 | });
415 | } catch (e) {
416 | // we're not going to mess with weird encodings,
417 | // give up and return the undecoded original string
418 | // see https://github.com/medialize/URI.js/issues/87
419 | // see https://github.com/medialize/URI.js/issues/92
420 | return string;
421 | }
422 | };
423 | };
424 |
425 | for (_part in _parts) {
426 | URI[_part + 'PathSegment'] = generateAccessor('pathname', _parts[_part]);
427 | URI[_part + 'UrnPathSegment'] = generateAccessor('urnpath', _parts[_part]);
428 | }
429 |
430 | var generateSegmentedPathFunction = function(_sep, _codingFuncName, _innerCodingFuncName) {
431 | return function(string) {
432 | // Why pass in names of functions, rather than the function objects themselves? The
433 | // definitions of some functions (but in particular, URI.decode) will occasionally change due
434 | // to URI.js having ISO8859 and Unicode modes. Passing in the name and getting it will ensure
435 | // that the functions we use here are "fresh".
436 | var actualCodingFunc;
437 | if (!_innerCodingFuncName) {
438 | actualCodingFunc = URI[_codingFuncName];
439 | } else {
440 | actualCodingFunc = function(string) {
441 | return URI[_codingFuncName](URI[_innerCodingFuncName](string));
442 | };
443 | }
444 |
445 | var segments = (string + '').split(_sep);
446 |
447 | for (var i = 0, length = segments.length; i < length; i++) {
448 | segments[i] = actualCodingFunc(segments[i]);
449 | }
450 |
451 | return segments.join(_sep);
452 | };
453 | };
454 |
455 | // This takes place outside the above loop because we don't want, e.g., encodeUrnPath functions.
456 | URI.decodePath = generateSegmentedPathFunction('/', 'decodePathSegment');
457 | URI.decodeUrnPath = generateSegmentedPathFunction(':', 'decodeUrnPathSegment');
458 | URI.recodePath = generateSegmentedPathFunction('/', 'encodePathSegment', 'decode');
459 | URI.recodeUrnPath = generateSegmentedPathFunction(':', 'encodeUrnPathSegment', 'decode');
460 |
461 | URI.encodeReserved = generateAccessor('reserved', 'encode');
462 |
463 | URI.parse = function(string, parts) {
464 | var pos;
465 | if (!parts) {
466 | parts = {};
467 | }
468 | // [protocol"://"[username[":"password]"@"]hostname[":"port]"/"?][path]["?"querystring]["#"fragment]
469 |
470 | // extract fragment
471 | pos = string.indexOf('#');
472 | if (pos > -1) {
473 | // escaping?
474 | parts.fragment = string.substring(pos + 1) || null;
475 | string = string.substring(0, pos);
476 | }
477 |
478 | // extract query
479 | pos = string.indexOf('?');
480 | if (pos > -1) {
481 | // escaping?
482 | parts.query = string.substring(pos + 1) || null;
483 | string = string.substring(0, pos);
484 | }
485 |
486 | // extract protocol
487 | if (string.substring(0, 2) === '//') {
488 | // relative-scheme
489 | parts.protocol = null;
490 | string = string.substring(2);
491 | // extract "user:pass@host:port"
492 | string = URI.parseAuthority(string, parts);
493 | } else {
494 | pos = string.indexOf(':');
495 | if (pos > -1) {
496 | parts.protocol = string.substring(0, pos) || null;
497 | if (parts.protocol && !parts.protocol.match(URI.protocol_expression)) {
498 | // : may be within the path
499 | parts.protocol = undefined;
500 | } else if (string.substring(pos + 1, pos + 3) === '//') {
501 | string = string.substring(pos + 3);
502 |
503 | // extract "user:pass@host:port"
504 | string = URI.parseAuthority(string, parts);
505 | } else {
506 | string = string.substring(pos + 1);
507 | parts.urn = true;
508 | }
509 | }
510 | }
511 |
512 | // what's left must be the path
513 | parts.path = string;
514 |
515 | // and we're done
516 | return parts;
517 | };
518 | URI.parseHost = function(string, parts) {
519 | // Copy chrome, IE, opera backslash-handling behavior.
520 | // Back slashes before the query string get converted to forward slashes
521 | // See: https://github.com/joyent/node/blob/386fd24f49b0e9d1a8a076592a404168faeecc34/lib/url.js#L115-L124
522 | // See: https://code.google.com/p/chromium/issues/detail?id=25916
523 | // https://github.com/medialize/URI.js/pull/233
524 | string = string.replace(/\\/g, '/');
525 |
526 | // extract host:port
527 | var pos = string.indexOf('/');
528 | var bracketPos;
529 | var t;
530 |
531 | if (pos === -1) {
532 | pos = string.length;
533 | }
534 |
535 | if (string.charAt(0) === '[') {
536 | // IPv6 host - http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-04#section-6
537 | // I claim most client software breaks on IPv6 anyways. To simplify things, URI only accepts
538 | // IPv6+port in the format [2001:db8::1]:80 (for the time being)
539 | bracketPos = string.indexOf(']');
540 | parts.hostname = string.substring(1, bracketPos) || null;
541 | parts.port = string.substring(bracketPos + 2, pos) || null;
542 | if (parts.port === '/') {
543 | parts.port = null;
544 | }
545 | } else {
546 | var firstColon = string.indexOf(':');
547 | var firstSlash = string.indexOf('/');
548 | var nextColon = string.indexOf(':', firstColon + 1);
549 | if (nextColon !== -1 && (firstSlash === -1 || nextColon < firstSlash)) {
550 | // IPv6 host contains multiple colons - but no port
551 | // this notation is actually not allowed by RFC 3986, but we're a liberal parser
552 | parts.hostname = string.substring(0, pos) || null;
553 | parts.port = null;
554 | } else {
555 | t = string.substring(0, pos).split(':');
556 | parts.hostname = t[0] || null;
557 | parts.port = t[1] || null;
558 | }
559 | }
560 |
561 | if (parts.hostname && string.substring(pos).charAt(0) !== '/') {
562 | pos++;
563 | string = '/' + string;
564 | }
565 |
566 | return string.substring(pos) || '/';
567 | };
568 | URI.parseAuthority = function(string, parts) {
569 | string = URI.parseUserinfo(string, parts);
570 | return URI.parseHost(string, parts);
571 | };
572 | URI.parseUserinfo = function(string, parts) {
573 | // extract username:password
574 | var firstSlash = string.indexOf('/');
575 | var pos = string.lastIndexOf('@', firstSlash > -1 ? firstSlash : string.length - 1);
576 | var t;
577 |
578 | // authority@ must come before /path
579 | if (pos > -1 && (firstSlash === -1 || pos < firstSlash)) {
580 | t = string.substring(0, pos).split(':');
581 | parts.username = t[0] ? URI.decode(t[0]) : null;
582 | t.shift();
583 | parts.password = t[0] ? URI.decode(t.join(':')) : null;
584 | string = string.substring(pos + 1);
585 | } else {
586 | parts.username = null;
587 | parts.password = null;
588 | }
589 |
590 | return string;
591 | };
592 | URI.parseQuery = function(string, escapeQuerySpace) {
593 | if (!string) {
594 | return {};
595 | }
596 |
597 | // throw out the funky business - "?"[name"="value"&"]+
598 | string = string.replace(/&+/g, '&').replace(/^\?*&*|&+$/g, '');
599 |
600 | if (!string) {
601 | return {};
602 | }
603 |
604 | var items = {};
605 | var splits = string.split('&');
606 | var length = splits.length;
607 | var v, name, value;
608 |
609 | for (var i = 0; i < length; i++) {
610 | v = splits[i].split('=');
611 | name = URI.decodeQuery(v.shift(), escapeQuerySpace);
612 | // no "=" is null according to http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#collect-url-parameters
613 | value = v.length ? URI.decodeQuery(v.join('='), escapeQuerySpace) : null;
614 |
615 | if (hasOwn.call(items, name)) {
616 | if (typeof items[name] === 'string' || items[name] === null) {
617 | items[name] = [items[name]];
618 | }
619 |
620 | items[name].push(value);
621 | } else {
622 | items[name] = value;
623 | }
624 | }
625 |
626 | return items;
627 | };
628 |
629 | URI.build = function(parts) {
630 | var t = '';
631 |
632 | if (parts.protocol) {
633 | t += parts.protocol + ':';
634 | }
635 |
636 | if (!parts.urn && (t || parts.hostname)) {
637 | t += '//';
638 | }
639 |
640 | t += (URI.buildAuthority(parts) || '');
641 |
642 | if (typeof parts.path === 'string') {
643 | if (parts.path.charAt(0) !== '/' && typeof parts.hostname === 'string') {
644 | t += '/';
645 | }
646 |
647 | t += parts.path;
648 | }
649 |
650 | if (typeof parts.query === 'string' && parts.query) {
651 | t += '?' + parts.query;
652 | }
653 |
654 | if (typeof parts.fragment === 'string' && parts.fragment) {
655 | t += '#' + parts.fragment;
656 | }
657 | return t;
658 | };
659 | URI.buildHost = function(parts) {
660 | var t = '';
661 |
662 | if (!parts.hostname) {
663 | return '';
664 | } else if (URI.ip6_expression.test(parts.hostname)) {
665 | t += '[' + parts.hostname + ']';
666 | } else {
667 | t += parts.hostname;
668 | }
669 |
670 | if (parts.port) {
671 | t += ':' + parts.port;
672 | }
673 |
674 | return t;
675 | };
676 | URI.buildAuthority = function(parts) {
677 | return URI.buildUserinfo(parts) + URI.buildHost(parts);
678 | };
679 | URI.buildUserinfo = function(parts) {
680 | var t = '';
681 |
682 | if (parts.username) {
683 | t += URI.encode(parts.username);
684 |
685 | if (parts.password) {
686 | t += ':' + URI.encode(parts.password);
687 | }
688 |
689 | t += '@';
690 | }
691 |
692 | return t;
693 | };
694 | URI.buildQuery = function(data, duplicateQueryParameters, escapeQuerySpace) {
695 | // according to http://tools.ietf.org/html/rfc3986 or http://labs.apache.org/webarch/uri/rfc/rfc3986.html
696 | // being »-._~!$&'()*+,;=:@/?« %HEX and alnum are allowed
697 | // the RFC explicitly states ?/foo being a valid use case, no mention of parameter syntax!
698 | // URI.js treats the query string as being application/x-www-form-urlencoded
699 | // see http://www.w3.org/TR/REC-html40/interact/forms.html#form-content-type
700 |
701 | var t = '';
702 | var unique, key, i, length;
703 | for (key in data) {
704 | if (hasOwn.call(data, key) && key) {
705 | if (isArray(data[key])) {
706 | unique = {};
707 | for (i = 0, length = data[key].length; i < length; i++) {
708 | if (data[key][i] !== undefined && unique[data[key][i] + ''] === undefined) {
709 | t += '&' + URI.buildQueryParameter(key, data[key][i], escapeQuerySpace);
710 | if (duplicateQueryParameters !== true) {
711 | unique[data[key][i] + ''] = true;
712 | }
713 | }
714 | }
715 | } else if (data[key] !== undefined) {
716 | t += '&' + URI.buildQueryParameter(key, data[key], escapeQuerySpace);
717 | }
718 | }
719 | }
720 |
721 | return t.substring(1);
722 | };
723 | URI.buildQueryParameter = function(name, value, escapeQuerySpace) {
724 | // http://www.w3.org/TR/REC-html40/interact/forms.html#form-content-type -- application/x-www-form-urlencoded
725 | // don't append "=" for null values, according to http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#url-parameter-serialization
726 | return URI.encodeQuery(name, escapeQuerySpace) + (value !== null ? '=' + URI.encodeQuery(value, escapeQuerySpace) : '');
727 | };
728 |
729 | URI.addQuery = function(data, name, value) {
730 | if (typeof name === 'object') {
731 | for (var key in name) {
732 | if (hasOwn.call(name, key)) {
733 | URI.addQuery(data, key, name[key]);
734 | }
735 | }
736 | } else if (typeof name === 'string') {
737 | if (data[name] === undefined) {
738 | data[name] = value;
739 | return;
740 | } else if (typeof data[name] === 'string') {
741 | data[name] = [data[name]];
742 | }
743 |
744 | if (!isArray(value)) {
745 | value = [value];
746 | }
747 |
748 | data[name] = (data[name] || []).concat(value);
749 | } else {
750 | throw new TypeError('URI.addQuery() accepts an object, string as the name parameter');
751 | }
752 | };
753 | URI.removeQuery = function(data, name, value) {
754 | var i, length, key;
755 |
756 | if (isArray(name)) {
757 | for (i = 0, length = name.length; i < length; i++) {
758 | data[name[i]] = undefined;
759 | }
760 | } else if (getType(name) === 'RegExp') {
761 | for (key in data) {
762 | if (name.test(key)) {
763 | data[key] = undefined;
764 | }
765 | }
766 | } else if (typeof name === 'object') {
767 | for (key in name) {
768 | if (hasOwn.call(name, key)) {
769 | URI.removeQuery(data, key, name[key]);
770 | }
771 | }
772 | } else if (typeof name === 'string') {
773 | if (value !== undefined) {
774 | if (getType(value) === 'RegExp') {
775 | if (!isArray(data[name]) && value.test(data[name])) {
776 | data[name] = undefined;
777 | } else {
778 | data[name] = filterArrayValues(data[name], value);
779 | }
780 | } else if (data[name] === value) {
781 | data[name] = undefined;
782 | } else if (isArray(data[name])) {
783 | data[name] = filterArrayValues(data[name], value);
784 | }
785 | } else {
786 | data[name] = undefined;
787 | }
788 | } else {
789 | throw new TypeError('URI.removeQuery() accepts an object, string, RegExp as the first parameter');
790 | }
791 | };
792 | URI.hasQuery = function(data, name, value, withinArray) {
793 | if (typeof name === 'object') {
794 | for (var key in name) {
795 | if (hasOwn.call(name, key)) {
796 | if (!URI.hasQuery(data, key, name[key])) {
797 | return false;
798 | }
799 | }
800 | }
801 |
802 | return true;
803 | } else if (typeof name !== 'string') {
804 | throw new TypeError('URI.hasQuery() accepts an object, string as the name parameter');
805 | }
806 |
807 | switch (getType(value)) {
808 | case 'Undefined':
809 | // true if exists (but may be empty)
810 | return name in data; // data[name] !== undefined;
811 |
812 | case 'Boolean':
813 | // true if exists and non-empty
814 | var _booly = Boolean(isArray(data[name]) ? data[name].length : data[name]);
815 | return value === _booly;
816 |
817 | case 'Function':
818 | // allow complex comparison
819 | return !!value(data[name], name, data);
820 |
821 | case 'Array':
822 | if (!isArray(data[name])) {
823 | return false;
824 | }
825 |
826 | var op = withinArray ? arrayContains : arraysEqual;
827 | return op(data[name], value);
828 |
829 | case 'RegExp':
830 | if (!isArray(data[name])) {
831 | return Boolean(data[name] && data[name].match(value));
832 | }
833 |
834 | if (!withinArray) {
835 | return false;
836 | }
837 |
838 | return arrayContains(data[name], value);
839 |
840 | case 'Number':
841 | value = String(value);
842 | /* falls through */
843 | case 'String':
844 | if (!isArray(data[name])) {
845 | return data[name] === value;
846 | }
847 |
848 | if (!withinArray) {
849 | return false;
850 | }
851 |
852 | return arrayContains(data[name], value);
853 |
854 | default:
855 | throw new TypeError('URI.hasQuery() accepts undefined, boolean, string, number, RegExp, Function as the value parameter');
856 | }
857 | };
858 |
859 |
860 | URI.commonPath = function(one, two) {
861 | var length = Math.min(one.length, two.length);
862 | var pos;
863 |
864 | // find first non-matching character
865 | for (pos = 0; pos < length; pos++) {
866 | if (one.charAt(pos) !== two.charAt(pos)) {
867 | pos--;
868 | break;
869 | }
870 | }
871 |
872 | if (pos < 1) {
873 | return one.charAt(0) === two.charAt(0) && one.charAt(0) === '/' ? '/' : '';
874 | }
875 |
876 | // revert to last /
877 | if (one.charAt(pos) !== '/' || two.charAt(pos) !== '/') {
878 | pos = one.substring(0, pos).lastIndexOf('/');
879 | }
880 |
881 | return one.substring(0, pos + 1);
882 | };
883 |
884 | URI.withinString = function(string, callback, options) {
885 | options || (options = {});
886 | var _start = options.start || URI.findUri.start;
887 | var _end = options.end || URI.findUri.end;
888 | var _trim = options.trim || URI.findUri.trim;
889 | var _attributeOpen = /[a-z0-9-]=["']?$/i;
890 |
891 | _start.lastIndex = 0;
892 | while (true) {
893 | var match = _start.exec(string);
894 | if (!match) {
895 | break;
896 | }
897 |
898 | var start = match.index;
899 | if (options.ignoreHtml) {
900 | // attribut(e=["']?$)
901 | var attributeOpen = string.slice(Math.max(start - 3, 0), start);
902 | if (attributeOpen && _attributeOpen.test(attributeOpen)) {
903 | continue;
904 | }
905 | }
906 |
907 | var end = start + string.slice(start).search(_end);
908 | var slice = string.slice(start, end).replace(_trim, '');
909 | if (options.ignore && options.ignore.test(slice)) {
910 | continue;
911 | }
912 |
913 | end = start + slice.length;
914 | var result = callback(slice, start, end, string);
915 | string = string.slice(0, start) + result + string.slice(end);
916 | _start.lastIndex = start + result.length;
917 | }
918 |
919 | _start.lastIndex = 0;
920 | return string;
921 | };
922 |
923 | URI.ensureValidHostname = function(v) {
924 | // Theoretically URIs allow percent-encoding in Hostnames (according to RFC 3986)
925 | // they are not part of DNS and therefore ignored by URI.js
926 |
927 | if (v.match(URI.invalid_hostname_characters)) {
928 | // test punycode
929 | if (!punycode) {
930 | throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-] and Punycode.js is not available');
931 | }
932 |
933 | if (punycode.toASCII(v).match(URI.invalid_hostname_characters)) {
934 | throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-]');
935 | }
936 | }
937 | };
938 |
939 | // noConflict
940 | URI.noConflict = function(removeAll) {
941 | if (removeAll) {
942 | var unconflicted = {
943 | URI: this.noConflict()
944 | };
945 |
946 | if (root.URITemplate && typeof root.URITemplate.noConflict === 'function') {
947 | unconflicted.URITemplate = root.URITemplate.noConflict();
948 | }
949 |
950 | if (root.IPv6 && typeof root.IPv6.noConflict === 'function') {
951 | unconflicted.IPv6 = root.IPv6.noConflict();
952 | }
953 |
954 | if (root.SecondLevelDomains && typeof root.SecondLevelDomains.noConflict === 'function') {
955 | unconflicted.SecondLevelDomains = root.SecondLevelDomains.noConflict();
956 | }
957 |
958 | return unconflicted;
959 | } else if (root.URI === this) {
960 | root.URI = _URI;
961 | }
962 |
963 | return this;
964 | };
965 |
966 | p.build = function(deferBuild) {
967 | if (deferBuild === true) {
968 | this._deferred_build = true;
969 | } else if (deferBuild === undefined || this._deferred_build) {
970 | this._string = URI.build(this._parts);
971 | this._deferred_build = false;
972 | }
973 |
974 | return this;
975 | };
976 |
977 | p.clone = function() {
978 | return new URI(this);
979 | };
980 |
981 | p.valueOf = p.toString = function() {
982 | return this.build(false)._string;
983 | };
984 |
985 |
986 | function generateSimpleAccessor(_part){
987 | return function(v, build) {
988 | if (v === undefined) {
989 | return this._parts[_part] || '';
990 | } else {
991 | this._parts[_part] = v || null;
992 | this.build(!build);
993 | return this;
994 | }
995 | };
996 | }
997 |
998 | function generatePrefixAccessor(_part, _key){
999 | return function(v, build) {
1000 | if (v === undefined) {
1001 | return this._parts[_part] || '';
1002 | } else {
1003 | if (v !== null) {
1004 | v = v + '';
1005 | if (v.charAt(0) === _key) {
1006 | v = v.substring(1);
1007 | }
1008 | }
1009 |
1010 | this._parts[_part] = v;
1011 | this.build(!build);
1012 | return this;
1013 | }
1014 | };
1015 | }
1016 |
1017 | p.protocol = generateSimpleAccessor('protocol');
1018 | p.username = generateSimpleAccessor('username');
1019 | p.password = generateSimpleAccessor('password');
1020 | p.hostname = generateSimpleAccessor('hostname');
1021 | p.port = generateSimpleAccessor('port');
1022 | p.query = generatePrefixAccessor('query', '?');
1023 | p.fragment = generatePrefixAccessor('fragment', '#');
1024 |
1025 | p.search = function(v, build) {
1026 | var t = this.query(v, build);
1027 | return typeof t === 'string' && t.length ? ('?' + t) : t;
1028 | };
1029 | p.hash = function(v, build) {
1030 | var t = this.fragment(v, build);
1031 | return typeof t === 'string' && t.length ? ('#' + t) : t;
1032 | };
1033 |
1034 | p.pathname = function(v, build) {
1035 | if (v === undefined || v === true) {
1036 | var res = this._parts.path || (this._parts.hostname ? '/' : '');
1037 | return v ? (this._parts.urn ? URI.decodeUrnPath : URI.decodePath)(res) : res;
1038 | } else {
1039 | if (this._parts.urn) {
1040 | this._parts.path = v ? URI.recodeUrnPath(v) : '';
1041 | } else {
1042 | this._parts.path = v ? URI.recodePath(v) : '/';
1043 | }
1044 | this.build(!build);
1045 | return this;
1046 | }
1047 | };
1048 | p.path = p.pathname;
1049 | p.href = function(href, build) {
1050 | var key;
1051 |
1052 | if (href === undefined) {
1053 | return this.toString();
1054 | }
1055 |
1056 | this._string = '';
1057 | this._parts = URI._parts();
1058 |
1059 | var _URI = href instanceof URI;
1060 | var _object = typeof href === 'object' && (href.hostname || href.path || href.pathname);
1061 | if (href.nodeName) {
1062 | var attribute = URI.getDomAttribute(href);
1063 | href = href[attribute] || '';
1064 | _object = false;
1065 | }
1066 |
1067 | // window.location is reported to be an object, but it's not the sort
1068 | // of object we're looking for:
1069 | // * location.protocol ends with a colon
1070 | // * location.query != object.search
1071 | // * location.hash != object.fragment
1072 | // simply serializing the unknown object should do the trick
1073 | // (for location, not for everything...)
1074 | if (!_URI && _object && href.pathname !== undefined) {
1075 | href = href.toString();
1076 | }
1077 |
1078 | if (typeof href === 'string' || href instanceof String) {
1079 | this._parts = URI.parse(String(href), this._parts);
1080 | } else if (_URI || _object) {
1081 | var src = _URI ? href._parts : href;
1082 | for (key in src) {
1083 | if (hasOwn.call(this._parts, key)) {
1084 | this._parts[key] = src[key];
1085 | }
1086 | }
1087 | } else {
1088 | throw new TypeError('invalid input');
1089 | }
1090 |
1091 | this.build(!build);
1092 | return this;
1093 | };
1094 |
1095 | // identification accessors
1096 | p.is = function(what) {
1097 | var ip = false;
1098 | var ip4 = false;
1099 | var ip6 = false;
1100 | var name = false;
1101 | var sld = false;
1102 | var idn = false;
1103 | var punycode = false;
1104 | var relative = !this._parts.urn;
1105 |
1106 | if (this._parts.hostname) {
1107 | relative = false;
1108 | ip4 = URI.ip4_expression.test(this._parts.hostname);
1109 | ip6 = URI.ip6_expression.test(this._parts.hostname);
1110 | ip = ip4 || ip6;
1111 | name = !ip;
1112 | sld = name && SLD && SLD.has(this._parts.hostname);
1113 | idn = name && URI.idn_expression.test(this._parts.hostname);
1114 | punycode = name && URI.punycode_expression.test(this._parts.hostname);
1115 | }
1116 |
1117 | switch (what.toLowerCase()) {
1118 | case 'relative':
1119 | return relative;
1120 |
1121 | case 'absolute':
1122 | return !relative;
1123 |
1124 | // hostname identification
1125 | case 'domain':
1126 | case 'name':
1127 | return name;
1128 |
1129 | case 'sld':
1130 | return sld;
1131 |
1132 | case 'ip':
1133 | return ip;
1134 |
1135 | case 'ip4':
1136 | case 'ipv4':
1137 | case 'inet4':
1138 | return ip4;
1139 |
1140 | case 'ip6':
1141 | case 'ipv6':
1142 | case 'inet6':
1143 | return ip6;
1144 |
1145 | case 'idn':
1146 | return idn;
1147 |
1148 | case 'url':
1149 | return !this._parts.urn;
1150 |
1151 | case 'urn':
1152 | return !!this._parts.urn;
1153 |
1154 | case 'punycode':
1155 | return punycode;
1156 | }
1157 |
1158 | return null;
1159 | };
1160 |
1161 | // component specific input validation
1162 | var _protocol = p.protocol;
1163 | var _port = p.port;
1164 | var _hostname = p.hostname;
1165 |
1166 | p.protocol = function(v, build) {
1167 | if (v !== undefined) {
1168 | if (v) {
1169 | // accept trailing ://
1170 | v = v.replace(/:(\/\/)?$/, '');
1171 |
1172 | if (!v.match(URI.protocol_expression)) {
1173 | throw new TypeError('Protocol "' + v + '" contains characters other than [A-Z0-9.+-] or doesn\'t start with [A-Z]');
1174 | }
1175 | }
1176 | }
1177 | return _protocol.call(this, v, build);
1178 | };
1179 | p.scheme = p.protocol;
1180 | p.port = function(v, build) {
1181 | if (this._parts.urn) {
1182 | return v === undefined ? '' : this;
1183 | }
1184 |
1185 | if (v !== undefined) {
1186 | if (v === 0) {
1187 | v = null;
1188 | }
1189 |
1190 | if (v) {
1191 | v += '';
1192 | if (v.charAt(0) === ':') {
1193 | v = v.substring(1);
1194 | }
1195 |
1196 | if (v.match(/[^0-9]/)) {
1197 | throw new TypeError('Port "' + v + '" contains characters other than [0-9]');
1198 | }
1199 | }
1200 | }
1201 | return _port.call(this, v, build);
1202 | };
1203 | p.hostname = function(v, build) {
1204 | if (this._parts.urn) {
1205 | return v === undefined ? '' : this;
1206 | }
1207 |
1208 | if (v !== undefined) {
1209 | var x = {};
1210 | var res = URI.parseHost(v, x);
1211 | if (res !== '/') {
1212 | throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-]');
1213 | }
1214 |
1215 | v = x.hostname;
1216 | }
1217 | return _hostname.call(this, v, build);
1218 | };
1219 |
1220 | // compound accessors
1221 | p.host = function(v, build) {
1222 | if (this._parts.urn) {
1223 | return v === undefined ? '' : this;
1224 | }
1225 |
1226 | if (v === undefined) {
1227 | return this._parts.hostname ? URI.buildHost(this._parts) : '';
1228 | } else {
1229 | var res = URI.parseHost(v, this._parts);
1230 | if (res !== '/') {
1231 | throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-]');
1232 | }
1233 |
1234 | this.build(!build);
1235 | return this;
1236 | }
1237 | };
1238 | p.authority = function(v, build) {
1239 | if (this._parts.urn) {
1240 | return v === undefined ? '' : this;
1241 | }
1242 |
1243 | if (v === undefined) {
1244 | return this._parts.hostname ? URI.buildAuthority(this._parts) : '';
1245 | } else {
1246 | var res = URI.parseAuthority(v, this._parts);
1247 | if (res !== '/') {
1248 | throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-]');
1249 | }
1250 |
1251 | this.build(!build);
1252 | return this;
1253 | }
1254 | };
1255 | p.userinfo = function(v, build) {
1256 | if (this._parts.urn) {
1257 | return v === undefined ? '' : this;
1258 | }
1259 |
1260 | if (v === undefined) {
1261 | if (!this._parts.username) {
1262 | return '';
1263 | }
1264 |
1265 | var t = URI.buildUserinfo(this._parts);
1266 | return t.substring(0, t.length -1);
1267 | } else {
1268 | if (v[v.length-1] !== '@') {
1269 | v += '@';
1270 | }
1271 |
1272 | URI.parseUserinfo(v, this._parts);
1273 | this.build(!build);
1274 | return this;
1275 | }
1276 | };
1277 | p.resource = function(v, build) {
1278 | var parts;
1279 |
1280 | if (v === undefined) {
1281 | return this.path() + this.search() + this.hash();
1282 | }
1283 |
1284 | parts = URI.parse(v);
1285 | this._parts.path = parts.path;
1286 | this._parts.query = parts.query;
1287 | this._parts.fragment = parts.fragment;
1288 | this.build(!build);
1289 | return this;
1290 | };
1291 |
1292 | // fraction accessors
1293 | p.subdomain = function(v, build) {
1294 | if (this._parts.urn) {
1295 | return v === undefined ? '' : this;
1296 | }
1297 |
1298 | // convenience, return "www" from "www.example.org"
1299 | if (v === undefined) {
1300 | if (!this._parts.hostname || this.is('IP')) {
1301 | return '';
1302 | }
1303 |
1304 | // grab domain and add another segment
1305 | var end = this._parts.hostname.length - this.domain().length - 1;
1306 | return this._parts.hostname.substring(0, end) || '';
1307 | } else {
1308 | var e = this._parts.hostname.length - this.domain().length;
1309 | var sub = this._parts.hostname.substring(0, e);
1310 | var replace = new RegExp('^' + escapeRegEx(sub));
1311 |
1312 | if (v && v.charAt(v.length - 1) !== '.') {
1313 | v += '.';
1314 | }
1315 |
1316 | if (v) {
1317 | URI.ensureValidHostname(v);
1318 | }
1319 |
1320 | this._parts.hostname = this._parts.hostname.replace(replace, v);
1321 | this.build(!build);
1322 | return this;
1323 | }
1324 | };
1325 | p.domain = function(v, build) {
1326 | if (this._parts.urn) {
1327 | return v === undefined ? '' : this;
1328 | }
1329 |
1330 | if (typeof v === 'boolean') {
1331 | build = v;
1332 | v = undefined;
1333 | }
1334 |
1335 | // convenience, return "example.org" from "www.example.org"
1336 | if (v === undefined) {
1337 | if (!this._parts.hostname || this.is('IP')) {
1338 | return '';
1339 | }
1340 |
1341 | // if hostname consists of 1 or 2 segments, it must be the domain
1342 | var t = this._parts.hostname.match(/\./g);
1343 | if (t && t.length < 2) {
1344 | return this._parts.hostname;
1345 | }
1346 |
1347 | // grab tld and add another segment
1348 | var end = this._parts.hostname.length - this.tld(build).length - 1;
1349 | end = this._parts.hostname.lastIndexOf('.', end -1) + 1;
1350 | return this._parts.hostname.substring(end) || '';
1351 | } else {
1352 | if (!v) {
1353 | throw new TypeError('cannot set domain empty');
1354 | }
1355 |
1356 | URI.ensureValidHostname(v);
1357 |
1358 | if (!this._parts.hostname || this.is('IP')) {
1359 | this._parts.hostname = v;
1360 | } else {
1361 | var replace = new RegExp(escapeRegEx(this.domain()) + '$');
1362 | this._parts.hostname = this._parts.hostname.replace(replace, v);
1363 | }
1364 |
1365 | this.build(!build);
1366 | return this;
1367 | }
1368 | };
1369 | p.tld = function(v, build) {
1370 | if (this._parts.urn) {
1371 | return v === undefined ? '' : this;
1372 | }
1373 |
1374 | if (typeof v === 'boolean') {
1375 | build = v;
1376 | v = undefined;
1377 | }
1378 |
1379 | // return "org" from "www.example.org"
1380 | if (v === undefined) {
1381 | if (!this._parts.hostname || this.is('IP')) {
1382 | return '';
1383 | }
1384 |
1385 | var pos = this._parts.hostname.lastIndexOf('.');
1386 | var tld = this._parts.hostname.substring(pos + 1);
1387 |
1388 | if (build !== true && SLD && SLD.list[tld.toLowerCase()]) {
1389 | return SLD.get(this._parts.hostname) || tld;
1390 | }
1391 |
1392 | return tld;
1393 | } else {
1394 | var replace;
1395 |
1396 | if (!v) {
1397 | throw new TypeError('cannot set TLD empty');
1398 | } else if (v.match(/[^a-zA-Z0-9-]/)) {
1399 | if (SLD && SLD.is(v)) {
1400 | replace = new RegExp(escapeRegEx(this.tld()) + '$');
1401 | this._parts.hostname = this._parts.hostname.replace(replace, v);
1402 | } else {
1403 | throw new TypeError('TLD "' + v + '" contains characters other than [A-Z0-9]');
1404 | }
1405 | } else if (!this._parts.hostname || this.is('IP')) {
1406 | throw new ReferenceError('cannot set TLD on non-domain host');
1407 | } else {
1408 | replace = new RegExp(escapeRegEx(this.tld()) + '$');
1409 | this._parts.hostname = this._parts.hostname.replace(replace, v);
1410 | }
1411 |
1412 | this.build(!build);
1413 | return this;
1414 | }
1415 | };
1416 | p.directory = function(v, build) {
1417 | if (this._parts.urn) {
1418 | return v === undefined ? '' : this;
1419 | }
1420 |
1421 | if (v === undefined || v === true) {
1422 | if (!this._parts.path && !this._parts.hostname) {
1423 | return '';
1424 | }
1425 |
1426 | if (this._parts.path === '/') {
1427 | return '/';
1428 | }
1429 |
1430 | var end = this._parts.path.length - this.filename().length - 1;
1431 | var res = this._parts.path.substring(0, end) || (this._parts.hostname ? '/' : '');
1432 |
1433 | return v ? URI.decodePath(res) : res;
1434 |
1435 | } else {
1436 | var e = this._parts.path.length - this.filename().length;
1437 | var directory = this._parts.path.substring(0, e);
1438 | var replace = new RegExp('^' + escapeRegEx(directory));
1439 |
1440 | // fully qualifier directories begin with a slash
1441 | if (!this.is('relative')) {
1442 | if (!v) {
1443 | v = '/';
1444 | }
1445 |
1446 | if (v.charAt(0) !== '/') {
1447 | v = '/' + v;
1448 | }
1449 | }
1450 |
1451 | // directories always end with a slash
1452 | if (v && v.charAt(v.length - 1) !== '/') {
1453 | v += '/';
1454 | }
1455 |
1456 | v = URI.recodePath(v);
1457 | this._parts.path = this._parts.path.replace(replace, v);
1458 | this.build(!build);
1459 | return this;
1460 | }
1461 | };
1462 | p.filename = function(v, build) {
1463 | if (this._parts.urn) {
1464 | return v === undefined ? '' : this;
1465 | }
1466 |
1467 | if (v === undefined || v === true) {
1468 | if (!this._parts.path || this._parts.path === '/') {
1469 | return '';
1470 | }
1471 |
1472 | var pos = this._parts.path.lastIndexOf('/');
1473 | var res = this._parts.path.substring(pos+1);
1474 |
1475 | return v ? URI.decodePathSegment(res) : res;
1476 | } else {
1477 | var mutatedDirectory = false;
1478 |
1479 | if (v.charAt(0) === '/') {
1480 | v = v.substring(1);
1481 | }
1482 |
1483 | if (v.match(/\.?\//)) {
1484 | mutatedDirectory = true;
1485 | }
1486 |
1487 | var replace = new RegExp(escapeRegEx(this.filename()) + '$');
1488 | v = URI.recodePath(v);
1489 | this._parts.path = this._parts.path.replace(replace, v);
1490 |
1491 | if (mutatedDirectory) {
1492 | this.normalizePath(build);
1493 | } else {
1494 | this.build(!build);
1495 | }
1496 |
1497 | return this;
1498 | }
1499 | };
1500 | p.suffix = function(v, build) {
1501 | if (this._parts.urn) {
1502 | return v === undefined ? '' : this;
1503 | }
1504 |
1505 | if (v === undefined || v === true) {
1506 | if (!this._parts.path || this._parts.path === '/') {
1507 | return '';
1508 | }
1509 |
1510 | var filename = this.filename();
1511 | var pos = filename.lastIndexOf('.');
1512 | var s, res;
1513 |
1514 | if (pos === -1) {
1515 | return '';
1516 | }
1517 |
1518 | // suffix may only contain alnum characters (yup, I made this up.)
1519 | s = filename.substring(pos+1);
1520 | res = (/^[a-z0-9%]+$/i).test(s) ? s : '';
1521 | return v ? URI.decodePathSegment(res) : res;
1522 | } else {
1523 | if (v.charAt(0) === '.') {
1524 | v = v.substring(1);
1525 | }
1526 |
1527 | var suffix = this.suffix();
1528 | var replace;
1529 |
1530 | if (!suffix) {
1531 | if (!v) {
1532 | return this;
1533 | }
1534 |
1535 | this._parts.path += '.' + URI.recodePath(v);
1536 | } else if (!v) {
1537 | replace = new RegExp(escapeRegEx('.' + suffix) + '$');
1538 | } else {
1539 | replace = new RegExp(escapeRegEx(suffix) + '$');
1540 | }
1541 |
1542 | if (replace) {
1543 | v = URI.recodePath(v);
1544 | this._parts.path = this._parts.path.replace(replace, v);
1545 | }
1546 |
1547 | this.build(!build);
1548 | return this;
1549 | }
1550 | };
1551 | p.segment = function(segment, v, build) {
1552 | var separator = this._parts.urn ? ':' : '/';
1553 | var path = this.path();
1554 | var absolute = path.substring(0, 1) === '/';
1555 | var segments = path.split(separator);
1556 |
1557 | if (segment !== undefined && typeof segment !== 'number') {
1558 | build = v;
1559 | v = segment;
1560 | segment = undefined;
1561 | }
1562 |
1563 | if (segment !== undefined && typeof segment !== 'number') {
1564 | throw new Error('Bad segment "' + segment + '", must be 0-based integer');
1565 | }
1566 |
1567 | if (absolute) {
1568 | segments.shift();
1569 | }
1570 |
1571 | if (segment < 0) {
1572 | // allow negative indexes to address from the end
1573 | segment = Math.max(segments.length + segment, 0);
1574 | }
1575 |
1576 | if (v === undefined) {
1577 | /*jshint laxbreak: true */
1578 | return segment === undefined
1579 | ? segments
1580 | : segments[segment];
1581 | /*jshint laxbreak: false */
1582 | } else if (segment === null || segments[segment] === undefined) {
1583 | if (isArray(v)) {
1584 | segments = [];
1585 | // collapse empty elements within array
1586 | for (var i=0, l=v.length; i < l; i++) {
1587 | if (!v[i].length && (!segments.length || !segments[segments.length -1].length)) {
1588 | continue;
1589 | }
1590 |
1591 | if (segments.length && !segments[segments.length -1].length) {
1592 | segments.pop();
1593 | }
1594 |
1595 | segments.push(v[i]);
1596 | }
1597 | } else if (v || typeof v === 'string') {
1598 | if (segments[segments.length -1] === '') {
1599 | // empty trailing elements have to be overwritten
1600 | // to prevent results such as /foo//bar
1601 | segments[segments.length -1] = v;
1602 | } else {
1603 | segments.push(v);
1604 | }
1605 | }
1606 | } else {
1607 | if (v) {
1608 | segments[segment] = v;
1609 | } else {
1610 | segments.splice(segment, 1);
1611 | }
1612 | }
1613 |
1614 | if (absolute) {
1615 | segments.unshift('');
1616 | }
1617 |
1618 | return this.path(segments.join(separator), build);
1619 | };
1620 | p.segmentCoded = function(segment, v, build) {
1621 | var segments, i, l;
1622 |
1623 | if (typeof segment !== 'number') {
1624 | build = v;
1625 | v = segment;
1626 | segment = undefined;
1627 | }
1628 |
1629 | if (v === undefined) {
1630 | segments = this.segment(segment, v, build);
1631 | if (!isArray(segments)) {
1632 | segments = segments !== undefined ? URI.decode(segments) : undefined;
1633 | } else {
1634 | for (i = 0, l = segments.length; i < l; i++) {
1635 | segments[i] = URI.decode(segments[i]);
1636 | }
1637 | }
1638 |
1639 | return segments;
1640 | }
1641 |
1642 | if (!isArray(v)) {
1643 | v = (typeof v === 'string' || v instanceof String) ? URI.encode(v) : v;
1644 | } else {
1645 | for (i = 0, l = v.length; i < l; i++) {
1646 | v[i] = URI.encode(v[i]);
1647 | }
1648 | }
1649 |
1650 | return this.segment(segment, v, build);
1651 | };
1652 |
1653 | // mutating query string
1654 | var q = p.query;
1655 | p.query = function(v, build) {
1656 | if (v === true) {
1657 | return URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
1658 | } else if (typeof v === 'function') {
1659 | var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
1660 | var result = v.call(this, data);
1661 | this._parts.query = URI.buildQuery(result || data, this._parts.duplicateQueryParameters, this._parts.escapeQuerySpace);
1662 | this.build(!build);
1663 | return this;
1664 | } else if (v !== undefined && typeof v !== 'string') {
1665 | this._parts.query = URI.buildQuery(v, this._parts.duplicateQueryParameters, this._parts.escapeQuerySpace);
1666 | this.build(!build);
1667 | return this;
1668 | } else {
1669 | return q.call(this, v, build);
1670 | }
1671 | };
1672 | p.setQuery = function(name, value, build) {
1673 | var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
1674 |
1675 | if (typeof name === 'string' || name instanceof String) {
1676 | data[name] = value !== undefined ? value : null;
1677 | } else if (typeof name === 'object') {
1678 | for (var key in name) {
1679 | if (hasOwn.call(name, key)) {
1680 | data[key] = name[key];
1681 | }
1682 | }
1683 | } else {
1684 | throw new TypeError('URI.addQuery() accepts an object, string as the name parameter');
1685 | }
1686 |
1687 | this._parts.query = URI.buildQuery(data, this._parts.duplicateQueryParameters, this._parts.escapeQuerySpace);
1688 | if (typeof name !== 'string') {
1689 | build = value;
1690 | }
1691 |
1692 | this.build(!build);
1693 | return this;
1694 | };
1695 | p.addQuery = function(name, value, build) {
1696 | var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
1697 | URI.addQuery(data, name, value === undefined ? null : value);
1698 | this._parts.query = URI.buildQuery(data, this._parts.duplicateQueryParameters, this._parts.escapeQuerySpace);
1699 | if (typeof name !== 'string') {
1700 | build = value;
1701 | }
1702 |
1703 | this.build(!build);
1704 | return this;
1705 | };
1706 | p.removeQuery = function(name, value, build) {
1707 | var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
1708 | URI.removeQuery(data, name, value);
1709 | this._parts.query = URI.buildQuery(data, this._parts.duplicateQueryParameters, this._parts.escapeQuerySpace);
1710 | if (typeof name !== 'string') {
1711 | build = value;
1712 | }
1713 |
1714 | this.build(!build);
1715 | return this;
1716 | };
1717 | p.hasQuery = function(name, value, withinArray) {
1718 | var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
1719 | return URI.hasQuery(data, name, value, withinArray);
1720 | };
1721 | p.setSearch = p.setQuery;
1722 | p.addSearch = p.addQuery;
1723 | p.removeSearch = p.removeQuery;
1724 | p.hasSearch = p.hasQuery;
1725 |
1726 | // sanitizing URLs
1727 | p.normalize = function() {
1728 | if (this._parts.urn) {
1729 | return this
1730 | .normalizeProtocol(false)
1731 | .normalizePath(false)
1732 | .normalizeQuery(false)
1733 | .normalizeFragment(false)
1734 | .build();
1735 | }
1736 |
1737 | return this
1738 | .normalizeProtocol(false)
1739 | .normalizeHostname(false)
1740 | .normalizePort(false)
1741 | .normalizePath(false)
1742 | .normalizeQuery(false)
1743 | .normalizeFragment(false)
1744 | .build();
1745 | };
1746 | p.normalizeProtocol = function(build) {
1747 | if (typeof this._parts.protocol === 'string') {
1748 | this._parts.protocol = this._parts.protocol.toLowerCase();
1749 | this.build(!build);
1750 | }
1751 |
1752 | return this;
1753 | };
1754 | p.normalizeHostname = function(build) {
1755 | if (this._parts.hostname) {
1756 | if (this.is('IDN') && punycode) {
1757 | this._parts.hostname = punycode.toASCII(this._parts.hostname);
1758 | } else if (this.is('IPv6') && IPv6) {
1759 | this._parts.hostname = IPv6.best(this._parts.hostname);
1760 | }
1761 |
1762 | this._parts.hostname = this._parts.hostname.toLowerCase();
1763 | this.build(!build);
1764 | }
1765 |
1766 | return this;
1767 | };
1768 | p.normalizePort = function(build) {
1769 | // remove port of it's the protocol's default
1770 | if (typeof this._parts.protocol === 'string' && this._parts.port === URI.defaultPorts[this._parts.protocol]) {
1771 | this._parts.port = null;
1772 | this.build(!build);
1773 | }
1774 |
1775 | return this;
1776 | };
1777 | p.normalizePath = function(build) {
1778 | var _path = this._parts.path;
1779 | if (!_path) {
1780 | return this;
1781 | }
1782 |
1783 | if (this._parts.urn) {
1784 | this._parts.path = URI.recodeUrnPath(this._parts.path);
1785 | this.build(!build);
1786 | return this;
1787 | }
1788 |
1789 | if (this._parts.path === '/') {
1790 | return this;
1791 | }
1792 |
1793 | var _was_relative;
1794 | var _leadingParents = '';
1795 | var _parent, _pos;
1796 |
1797 | // handle relative paths
1798 | if (_path.charAt(0) !== '/') {
1799 | _was_relative = true;
1800 | _path = '/' + _path;
1801 | }
1802 |
1803 | // handle relative files (as opposed to directories)
1804 | if (_path.slice(-3) === '/..' || _path.slice(-2) === '/.') {
1805 | _path += '/';
1806 | }
1807 |
1808 | // resolve simples
1809 | _path = _path
1810 | .replace(/(\/(\.\/)+)|(\/\.$)/g, '/')
1811 | .replace(/\/{2,}/g, '/');
1812 |
1813 | // remember leading parents
1814 | if (_was_relative) {
1815 | _leadingParents = _path.substring(1).match(/^(\.\.\/)+/) || '';
1816 | if (_leadingParents) {
1817 | _leadingParents = _leadingParents[0];
1818 | }
1819 | }
1820 |
1821 | // resolve parents
1822 | while (true) {
1823 | _parent = _path.indexOf('/..');
1824 | if (_parent === -1) {
1825 | // no more ../ to resolve
1826 | break;
1827 | } else if (_parent === 0) {
1828 | // top level cannot be relative, skip it
1829 | _path = _path.substring(3);
1830 | continue;
1831 | }
1832 |
1833 | _pos = _path.substring(0, _parent).lastIndexOf('/');
1834 | if (_pos === -1) {
1835 | _pos = _parent;
1836 | }
1837 | _path = _path.substring(0, _pos) + _path.substring(_parent + 3);
1838 | }
1839 |
1840 | // revert to relative
1841 | if (_was_relative && this.is('relative')) {
1842 | _path = _leadingParents + _path.substring(1);
1843 | }
1844 |
1845 | _path = URI.recodePath(_path);
1846 | this._parts.path = _path;
1847 | this.build(!build);
1848 | return this;
1849 | };
1850 | p.normalizePathname = p.normalizePath;
1851 | p.normalizeQuery = function(build) {
1852 | if (typeof this._parts.query === 'string') {
1853 | if (!this._parts.query.length) {
1854 | this._parts.query = null;
1855 | } else {
1856 | this.query(URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace));
1857 | }
1858 |
1859 | this.build(!build);
1860 | }
1861 |
1862 | return this;
1863 | };
1864 | p.normalizeFragment = function(build) {
1865 | if (!this._parts.fragment) {
1866 | this._parts.fragment = null;
1867 | this.build(!build);
1868 | }
1869 |
1870 | return this;
1871 | };
1872 | p.normalizeSearch = p.normalizeQuery;
1873 | p.normalizeHash = p.normalizeFragment;
1874 |
1875 | p.iso8859 = function() {
1876 | // expect unicode input, iso8859 output
1877 | var e = URI.encode;
1878 | var d = URI.decode;
1879 |
1880 | URI.encode = escape;
1881 | URI.decode = decodeURIComponent;
1882 | try {
1883 | this.normalize();
1884 | } finally {
1885 | URI.encode = e;
1886 | URI.decode = d;
1887 | }
1888 | return this;
1889 | };
1890 |
1891 | p.unicode = function() {
1892 | // expect iso8859 input, unicode output
1893 | var e = URI.encode;
1894 | var d = URI.decode;
1895 |
1896 | URI.encode = strictEncodeURIComponent;
1897 | URI.decode = unescape;
1898 | try {
1899 | this.normalize();
1900 | } finally {
1901 | URI.encode = e;
1902 | URI.decode = d;
1903 | }
1904 | return this;
1905 | };
1906 |
1907 | p.readable = function() {
1908 | var uri = this.clone();
1909 | // removing username, password, because they shouldn't be displayed according to RFC 3986
1910 | uri.username('').password('').normalize();
1911 | var t = '';
1912 | if (uri._parts.protocol) {
1913 | t += uri._parts.protocol + '://';
1914 | }
1915 |
1916 | if (uri._parts.hostname) {
1917 | if (uri.is('punycode') && punycode) {
1918 | t += punycode.toUnicode(uri._parts.hostname);
1919 | if (uri._parts.port) {
1920 | t += ':' + uri._parts.port;
1921 | }
1922 | } else {
1923 | t += uri.host();
1924 | }
1925 | }
1926 |
1927 | if (uri._parts.hostname && uri._parts.path && uri._parts.path.charAt(0) !== '/') {
1928 | t += '/';
1929 | }
1930 |
1931 | t += uri.path(true);
1932 | if (uri._parts.query) {
1933 | var q = '';
1934 | for (var i = 0, qp = uri._parts.query.split('&'), l = qp.length; i < l; i++) {
1935 | var kv = (qp[i] || '').split('=');
1936 | q += '&' + URI.decodeQuery(kv[0], this._parts.escapeQuerySpace)
1937 | .replace(/&/g, '%26');
1938 |
1939 | if (kv[1] !== undefined) {
1940 | q += '=' + URI.decodeQuery(kv[1], this._parts.escapeQuerySpace)
1941 | .replace(/&/g, '%26');
1942 | }
1943 | }
1944 | t += '?' + q.substring(1);
1945 | }
1946 |
1947 | t += URI.decodeQuery(uri.hash(), true);
1948 | return t;
1949 | };
1950 |
1951 | // resolving relative and absolute URLs
1952 | p.absoluteTo = function(base) {
1953 | var resolved = this.clone();
1954 | var properties = ['protocol', 'username', 'password', 'hostname', 'port'];
1955 | var basedir, i, p;
1956 |
1957 | if (this._parts.urn) {
1958 | throw new Error('URNs do not have any generally defined hierarchical components');
1959 | }
1960 |
1961 | if (!(base instanceof URI)) {
1962 | base = new URI(base);
1963 | }
1964 |
1965 | if (!resolved._parts.protocol) {
1966 | resolved._parts.protocol = base._parts.protocol;
1967 | }
1968 |
1969 | if (this._parts.hostname) {
1970 | return resolved;
1971 | }
1972 |
1973 | for (i = 0; (p = properties[i]); i++) {
1974 | resolved._parts[p] = base._parts[p];
1975 | }
1976 |
1977 | if (!resolved._parts.path) {
1978 | resolved._parts.path = base._parts.path;
1979 | if (!resolved._parts.query) {
1980 | resolved._parts.query = base._parts.query;
1981 | }
1982 | } else if (resolved._parts.path.substring(-2) === '..') {
1983 | resolved._parts.path += '/';
1984 | }
1985 |
1986 | if (resolved.path().charAt(0) !== '/') {
1987 | basedir = base.directory();
1988 | basedir = basedir ? basedir : base.path().indexOf('/') === 0 ? '/' : '';
1989 | resolved._parts.path = (basedir ? (basedir + '/') : '') + resolved._parts.path;
1990 | resolved.normalizePath();
1991 | }
1992 |
1993 | resolved.build();
1994 | return resolved;
1995 | };
1996 | p.relativeTo = function(base) {
1997 | var relative = this.clone().normalize();
1998 | var relativeParts, baseParts, common, relativePath, basePath;
1999 |
2000 | if (relative._parts.urn) {
2001 | throw new Error('URNs do not have any generally defined hierarchical components');
2002 | }
2003 |
2004 | base = new URI(base).normalize();
2005 | relativeParts = relative._parts;
2006 | baseParts = base._parts;
2007 | relativePath = relative.path();
2008 | basePath = base.path();
2009 |
2010 | if (relativePath.charAt(0) !== '/') {
2011 | throw new Error('URI is already relative');
2012 | }
2013 |
2014 | if (basePath.charAt(0) !== '/') {
2015 | throw new Error('Cannot calculate a URI relative to another relative URI');
2016 | }
2017 |
2018 | if (relativeParts.protocol === baseParts.protocol) {
2019 | relativeParts.protocol = null;
2020 | }
2021 |
2022 | if (relativeParts.username !== baseParts.username || relativeParts.password !== baseParts.password) {
2023 | return relative.build();
2024 | }
2025 |
2026 | if (relativeParts.protocol !== null || relativeParts.username !== null || relativeParts.password !== null) {
2027 | return relative.build();
2028 | }
2029 |
2030 | if (relativeParts.hostname === baseParts.hostname && relativeParts.port === baseParts.port) {
2031 | relativeParts.hostname = null;
2032 | relativeParts.port = null;
2033 | } else {
2034 | return relative.build();
2035 | }
2036 |
2037 | if (relativePath === basePath) {
2038 | relativeParts.path = '';
2039 | return relative.build();
2040 | }
2041 |
2042 | // determine common sub path
2043 | common = URI.commonPath(relativePath, basePath);
2044 |
2045 | // If the paths have nothing in common, return a relative URL with the absolute path.
2046 | if (!common) {
2047 | return relative.build();
2048 | }
2049 |
2050 | var parents = baseParts.path
2051 | .substring(common.length)
2052 | .replace(/[^\/]*$/, '')
2053 | .replace(/.*?\//g, '../');
2054 |
2055 | relativeParts.path = (parents + relativeParts.path.substring(common.length)) || './';
2056 |
2057 | return relative.build();
2058 | };
2059 |
2060 | // comparing URIs
2061 | p.equals = function(uri) {
2062 | var one = this.clone();
2063 | var two = new URI(uri);
2064 | var one_map = {};
2065 | var two_map = {};
2066 | var checked = {};
2067 | var one_query, two_query, key;
2068 |
2069 | one.normalize();
2070 | two.normalize();
2071 |
2072 | // exact match
2073 | if (one.toString() === two.toString()) {
2074 | return true;
2075 | }
2076 |
2077 | // extract query string
2078 | one_query = one.query();
2079 | two_query = two.query();
2080 | one.query('');
2081 | two.query('');
2082 |
2083 | // definitely not equal if not even non-query parts match
2084 | if (one.toString() !== two.toString()) {
2085 | return false;
2086 | }
2087 |
2088 | // query parameters have the same length, even if they're permuted
2089 | if (one_query.length !== two_query.length) {
2090 | return false;
2091 | }
2092 |
2093 | one_map = URI.parseQuery(one_query, this._parts.escapeQuerySpace);
2094 | two_map = URI.parseQuery(two_query, this._parts.escapeQuerySpace);
2095 |
2096 | for (key in one_map) {
2097 | if (hasOwn.call(one_map, key)) {
2098 | if (!isArray(one_map[key])) {
2099 | if (one_map[key] !== two_map[key]) {
2100 | return false;
2101 | }
2102 | } else if (!arraysEqual(one_map[key], two_map[key])) {
2103 | return false;
2104 | }
2105 |
2106 | checked[key] = true;
2107 | }
2108 | }
2109 |
2110 | for (key in two_map) {
2111 | if (hasOwn.call(two_map, key)) {
2112 | if (!checked[key]) {
2113 | // two contains a parameter not present in one
2114 | return false;
2115 | }
2116 | }
2117 | }
2118 |
2119 | return true;
2120 | };
2121 |
2122 | // state
2123 | p.duplicateQueryParameters = function(v) {
2124 | this._parts.duplicateQueryParameters = !!v;
2125 | return this;
2126 | };
2127 |
2128 | p.escapeQuerySpace = function(v) {
2129 | this._parts.escapeQuerySpace = !!v;
2130 | return this;
2131 | };
2132 |
2133 | return URI;
2134 | }));
2135 |
--------------------------------------------------------------------------------
/URI.min.js:
--------------------------------------------------------------------------------
1 | /*! URI.js v1.16.0 http://medialize.github.io/URI.js/ */
2 | /* build contains: IPv6.js, punycode.js, SecondLevelDomains.js, URI.js, URITemplate.js */
3 | (function(e,n){"object"===typeof exports?module.exports=n():"function"===typeof define&&define.amd?define(n):e.IPv6=n(e)})(this,function(e){var n=e&&e.IPv6;return{best:function(g){g=g.toLowerCase().split(":");var l=g.length,b=8;""===g[0]&&""===g[1]&&""===g[2]?(g.shift(),g.shift()):""===g[0]&&""===g[1]?g.shift():""===g[l-1]&&""===g[l-2]&&g.pop();l=g.length;-1!==g[l-1].indexOf(".")&&(b=7);var h;for(h=0;he;e++)if("0"===l[0]&&1e&&(l=k,e=n)):"0"===g[h]&&(u=!0,k=h,n=1);n>e&&(l=k,e=n);1=a&&k>>10&1023|55296),b=56320|b&1023);return f+=x(b)}).join("")}function A(b,
6 | f){return b+22+75*(26>b)-((0!=f)<<5)}function w(b,f,k){var g=0;b=k?q(b/700):b>>1;for(b+=q(b/f);455m&&(m=0);for(z=0;z=k&&n("invalid-input");e=b.charCodeAt(m++);e=10>e-48?e-22:26>e-65?e-65:26>e-97?e-97:36;(36<=e||e>q((2147483647-a)/g))&&n("overflow");a+=e*g;l=
7 | y<=d?1:y>=d+26?26:y-d;if(eq(2147483647/e)&&n("overflow");g*=e}g=f.length+1;d=w(a-z,g,0==z);q(a/g)>2147483647-c&&n("overflow");c+=q(a/g);a%=g;f.splice(a++,0,c)}return h(f)}function u(f){var g,k,e,a,c,d,m,z,y,l=[],u,h,p;f=b(f);u=f.length;g=128;k=0;c=72;for(d=0;dy&&l.push(x(y));for((e=a=l.length)&&l.push("-");e=g&&yq((2147483647-k)/h)&&n("overflow");k+=(m-g)*h;g=m;for(d=0;d=c+26?26:m-c;if(z= 0x80 (not a basic code point)",
9 | "invalid-input":"Invalid input"},q=Math.floor,x=String.fromCharCode,C;t={version:"1.2.3",ucs2:{decode:b,encode:h},decode:k,encode:u,toASCII:function(b){return l(b,function(b){return p.test(b)?"xn--"+u(b):b})},toUnicode:function(b){return l(b,function(b){return r.test(b)?k(b.slice(4).toLowerCase()):b})}};if("function"==typeof define&&"object"==typeof define.amd&&define.amd)define(function(){return t});else if(D&&!D.nodeType)if(E)E.exports=t;else for(C in t)t.hasOwnProperty(C)&&(D[C]=t[C]);else e.punycode=
10 | t})(this);
11 | (function(e,n){"object"===typeof exports?module.exports=n():"function"===typeof define&&define.amd?define(n):e.SecondLevelDomains=n(e)})(this,function(e){var n=e&&e.SecondLevelDomains,g={list:{ac:" com gov mil net org ",ae:" ac co gov mil name net org pro sch ",af:" com edu gov net org ",al:" com edu gov mil net org ",ao:" co ed gv it og pb ",ar:" com edu gob gov int mil net org tur ",at:" ac co gv or ",au:" asn com csiro edu gov id net org ",ba:" co com edu gov mil net org rs unbi unmo unsa untz unze ",bb:" biz co com edu gov info net org store tv ",
12 | bh:" biz cc com edu gov info net org ",bn:" com edu gov net org ",bo:" com edu gob gov int mil net org tv ",br:" adm adv agr am arq art ato b bio blog bmd cim cng cnt com coop ecn edu eng esp etc eti far flog fm fnd fot fst g12 ggf gov imb ind inf jor jus lel mat med mil mus net nom not ntr odo org ppg pro psc psi qsl rec slg srv tmp trd tur tv vet vlog wiki zlg ",bs:" com edu gov net org ",bz:" du et om ov rg ",ca:" ab bc mb nb nf nl ns nt nu on pe qc sk yk ",ck:" biz co edu gen gov info net org ",
13 | cn:" ac ah bj com cq edu fj gd gov gs gx gz ha hb he hi hl hn jl js jx ln mil net nm nx org qh sc sd sh sn sx tj tw xj xz yn zj ",co:" com edu gov mil net nom org ",cr:" ac c co ed fi go or sa ",cy:" ac biz com ekloges gov ltd name net org parliament press pro tm ","do":" art com edu gob gov mil net org sld web ",dz:" art asso com edu gov net org pol ",ec:" com edu fin gov info med mil net org pro ",eg:" com edu eun gov mil name net org sci ",er:" com edu gov ind mil net org rochest w ",es:" com edu gob nom org ",
14 | et:" biz com edu gov info name net org ",fj:" ac biz com info mil name net org pro ",fk:" ac co gov net nom org ",fr:" asso com f gouv nom prd presse tm ",gg:" co net org ",gh:" com edu gov mil org ",gn:" ac com gov net org ",gr:" com edu gov mil net org ",gt:" com edu gob ind mil net org ",gu:" com edu gov net org ",hk:" com edu gov idv net org ",hu:" 2000 agrar bolt casino city co erotica erotika film forum games hotel info ingatlan jogasz konyvelo lakas media news org priv reklam sex shop sport suli szex tm tozsde utazas video ",
15 | id:" ac co go mil net or sch web ",il:" ac co gov idf k12 muni net org ","in":" ac co edu ernet firm gen gov i ind mil net nic org res ",iq:" com edu gov i mil net org ",ir:" ac co dnssec gov i id net org sch ",it:" edu gov ",je:" co net org ",jo:" com edu gov mil name net org sch ",jp:" ac ad co ed go gr lg ne or ",ke:" ac co go info me mobi ne or sc ",kh:" com edu gov mil net org per ",ki:" biz com de edu gov info mob net org tel ",km:" asso com coop edu gouv k medecin mil nom notaires pharmaciens presse tm veterinaire ",
16 | kn:" edu gov net org ",kr:" ac busan chungbuk chungnam co daegu daejeon es gangwon go gwangju gyeongbuk gyeonggi gyeongnam hs incheon jeju jeonbuk jeonnam k kg mil ms ne or pe re sc seoul ulsan ",kw:" com edu gov net org ",ky:" com edu gov net org ",kz:" com edu gov mil net org ",lb:" com edu gov net org ",lk:" assn com edu gov grp hotel int ltd net ngo org sch soc web ",lr:" com edu gov net org ",lv:" asn com conf edu gov id mil net org ",ly:" com edu gov id med net org plc sch ",ma:" ac co gov m net org press ",
17 | mc:" asso tm ",me:" ac co edu gov its net org priv ",mg:" com edu gov mil nom org prd tm ",mk:" com edu gov inf name net org pro ",ml:" com edu gov net org presse ",mn:" edu gov org ",mo:" com edu gov net org ",mt:" com edu gov net org ",mv:" aero biz com coop edu gov info int mil museum name net org pro ",mw:" ac co com coop edu gov int museum net org ",mx:" com edu gob net org ",my:" com edu gov mil name net org sch ",nf:" arts com firm info net other per rec store web ",ng:" biz com edu gov mil mobi name net org sch ",
18 | ni:" ac co com edu gob mil net nom org ",np:" com edu gov mil net org ",nr:" biz com edu gov info net org ",om:" ac biz co com edu gov med mil museum net org pro sch ",pe:" com edu gob mil net nom org sld ",ph:" com edu gov i mil net ngo org ",pk:" biz com edu fam gob gok gon gop gos gov net org web ",pl:" art bialystok biz com edu gda gdansk gorzow gov info katowice krakow lodz lublin mil net ngo olsztyn org poznan pwr radom slupsk szczecin torun warszawa waw wroc wroclaw zgora ",pr:" ac biz com edu est gov info isla name net org pro prof ",
19 | ps:" com edu gov net org plo sec ",pw:" belau co ed go ne or ",ro:" arts com firm info nom nt org rec store tm www ",rs:" ac co edu gov in org ",sb:" com edu gov net org ",sc:" com edu gov net org ",sh:" co com edu gov net nom org ",sl:" com edu gov net org ",st:" co com consulado edu embaixada gov mil net org principe saotome store ",sv:" com edu gob org red ",sz:" ac co org ",tr:" av bbs bel biz com dr edu gen gov info k12 name net org pol tel tsk tv web ",tt:" aero biz cat co com coop edu gov info int jobs mil mobi museum name net org pro tel travel ",
20 | tw:" club com ebiz edu game gov idv mil net org ",mu:" ac co com gov net or org ",mz:" ac co edu gov org ",na:" co com ",nz:" ac co cri geek gen govt health iwi maori mil net org parliament school ",pa:" abo ac com edu gob ing med net nom org sld ",pt:" com edu gov int net nome org publ ",py:" com edu gov mil net org ",qa:" com edu gov mil net org ",re:" asso com nom ",ru:" ac adygeya altai amur arkhangelsk astrakhan bashkiria belgorod bir bryansk buryatia cbg chel chelyabinsk chita chukotka chuvashia com dagestan e-burg edu gov grozny int irkutsk ivanovo izhevsk jar joshkar-ola kalmykia kaluga kamchatka karelia kazan kchr kemerovo khabarovsk khakassia khv kirov koenig komi kostroma kranoyarsk kuban kurgan kursk lipetsk magadan mari mari-el marine mil mordovia mosreg msk murmansk nalchik net nnov nov novosibirsk nsk omsk orenburg org oryol penza perm pp pskov ptz rnd ryazan sakhalin samara saratov simbirsk smolensk spb stavropol stv surgut tambov tatarstan tom tomsk tsaritsyn tsk tula tuva tver tyumen udm udmurtia ulan-ude vladikavkaz vladimir vladivostok volgograd vologda voronezh vrn vyatka yakutia yamal yekaterinburg yuzhno-sakhalinsk ",
21 | rw:" ac co com edu gouv gov int mil net ",sa:" com edu gov med net org pub sch ",sd:" com edu gov info med net org tv ",se:" a ac b bd c d e f g h i k l m n o org p parti pp press r s t tm u w x y z ",sg:" com edu gov idn net org per ",sn:" art com edu gouv org perso univ ",sy:" com edu gov mil net news org ",th:" ac co go in mi net or ",tj:" ac biz co com edu go gov info int mil name net nic org test web ",tn:" agrinet com defense edunet ens fin gov ind info intl mincom nat net org perso rnrt rns rnu tourism ",
22 | tz:" ac co go ne or ",ua:" biz cherkassy chernigov chernovtsy ck cn co com crimea cv dn dnepropetrovsk donetsk dp edu gov if in ivano-frankivsk kh kharkov kherson khmelnitskiy kiev kirovograd km kr ks kv lg lugansk lutsk lviv me mk net nikolaev od odessa org pl poltava pp rovno rv sebastopol sumy te ternopil uzhgorod vinnica vn zaporizhzhe zhitomir zp zt ",ug:" ac co go ne or org sc ",uk:" ac bl british-library co cym gov govt icnet jet lea ltd me mil mod national-library-scotland nel net nhs nic nls org orgn parliament plc police sch scot soc ",
23 | us:" dni fed isa kids nsn ",uy:" com edu gub mil net org ",ve:" co com edu gob info mil net org web ",vi:" co com k12 net org ",vn:" ac biz com edu gov health info int name net org pro ",ye:" co com gov ltd me net org plc ",yu:" ac co edu gov org ",za:" ac agric alt bourse city co cybernet db edu gov grondar iaccess imt inca landesign law mil net ngo nis nom olivetti org pix school tm web ",zm:" ac co com edu gov net org sch "},has:function(e){var b=e.lastIndexOf(".");if(0>=b||b>=e.length-1)return!1;
24 | var h=e.lastIndexOf(".",b-1);if(0>=h||h>=b-1)return!1;var n=g.list[e.slice(b+1)];return n?0<=n.indexOf(" "+e.slice(h+1,b)+" "):!1},is:function(e){var b=e.lastIndexOf(".");if(0>=b||b>=e.length-1||0<=e.lastIndexOf(".",b-1))return!1;var h=g.list[e.slice(b+1)];return h?0<=h.indexOf(" "+e.slice(0,b)+" "):!1},get:function(e){var b=e.lastIndexOf(".");if(0>=b||b>=e.length-1)return null;var h=e.lastIndexOf(".",b-1);if(0>=h||h>=b-1)return null;var n=g.list[e.slice(b+1)];return!n||0>n.indexOf(" "+e.slice(h+
25 | 1,b)+" ")?null:e.slice(h+1)},noConflict:function(){e.SecondLevelDomains===this&&(e.SecondLevelDomains=n);return this}};return g});
26 | (function(e,n){"object"===typeof exports?module.exports=n(require("./punycode"),require("./IPv6"),require("./SecondLevelDomains")):"function"===typeof define&&define.amd?define(["./punycode","./IPv6","./SecondLevelDomains"],n):e.URI=n(e.punycode,e.IPv6,e.SecondLevelDomains,e)})(this,function(e,n,g,l){function b(a,c){var d=1<=arguments.length,m=2<=arguments.length;if(!(this instanceof b))return d?m?new b(a,c):new b(a):new b;if(void 0===a){if(d)throw new TypeError("undefined is not a valid argument for URI");
27 | a="undefined"!==typeof location?location.href+"":""}this.href(a);return void 0!==c?this.absoluteTo(c):this}function h(a){return a.replace(/([.*+?^=!:${}()|[\]\/\\])/g,"\\$1")}function A(a){return void 0===a?"Undefined":String(Object.prototype.toString.call(a)).slice(8,-1)}function w(a){return"Array"===A(a)}function k(a,c){var d={},b,f;if("RegExp"===A(c))d=null;else if(w(c))for(b=0,f=c.length;b]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?\u00ab\u00bb\u201c\u201d\u2018\u2019]))/ig;b.findUri={start:/\b(?:([a-z][a-z0-9.+-]*:\/\/)|www\.)/gi,end:/[\s\r\n]|$/,trim:/[`!()\[\]{};:'".,<>?\u00ab\u00bb\u201c\u201d\u201e\u2018\u2019]+$/};b.defaultPorts={http:"80",https:"443",ftp:"21",gopher:"70",ws:"80",wss:"443"};b.invalid_hostname_characters=
32 | /[^a-zA-Z0-9\.-]/;b.domAttributes={a:"href",blockquote:"cite",link:"href",base:"href",script:"src",form:"action",img:"src",area:"href",iframe:"src",embed:"src",source:"src",track:"src",input:"src",audio:"src",video:"src"};b.getDomAttribute=function(a){if(a&&a.nodeName){var c=a.nodeName.toLowerCase();return"input"===c&&"image"!==a.type?void 0:b.domAttributes[c]}};b.encode=B;b.decode=decodeURIComponent;b.iso8859=function(){b.encode=escape;b.decode=unescape};b.unicode=function(){b.encode=B;b.decode=
33 | decodeURIComponent};b.characters={pathname:{encode:{expression:/%(24|26|2B|2C|3B|3D|3A|40)/ig,map:{"%24":"$","%26":"&","%2B":"+","%2C":",","%3B":";","%3D":"=","%3A":":","%40":"@"}},decode:{expression:/[\/\?#]/g,map:{"/":"%2F","?":"%3F","#":"%23"}}},reserved:{encode:{expression:/%(21|23|24|26|27|28|29|2A|2B|2C|2F|3A|3B|3D|3F|40|5B|5D)/ig,map:{"%3A":":","%2F":"/","%3F":"?","%23":"#","%5B":"[","%5D":"]","%40":"@","%21":"!","%24":"$","%26":"&","%27":"'","%28":"(","%29":")","%2A":"*","%2B":"+","%2C":",",
34 | "%3B":";","%3D":"="}}},urnpath:{encode:{expression:/%(21|24|27|28|29|2A|2B|2C|3B|3D|40)/ig,map:{"%21":"!","%24":"$","%27":"'","%28":"(","%29":")","%2A":"*","%2B":"+","%2C":",","%3B":";","%3D":"=","%40":"@"}},decode:{expression:/[\/\?#:]/g,map:{"/":"%2F","?":"%3F","#":"%23",":":"%3A"}}}};b.encodeQuery=function(a,c){var d=b.encode(a+"");void 0===c&&(c=b.escapeQuerySpace);return c?d.replace(/%20/g,"+"):d};b.decodeQuery=function(a,c){a+="";void 0===c&&(c=b.escapeQuerySpace);try{return b.decode(c?a.replace(/\+/g,
35 | "%20"):a)}catch(d){return a}};var q={encode:"encode",decode:"decode"},x,C=function(a,c){return function(d){try{return b[c](d+"").replace(b.characters[a][c].expression,function(d){return b.characters[a][c].map[d]})}catch(m){return d}}};for(x in q)b[x+"PathSegment"]=C("pathname",q[x]),b[x+"UrnPathSegment"]=C("urnpath",q[x]);q=function(a,c,d){return function(m){var f;f=d?function(a){return b[c](b[d](a))}:b[c];m=(m+"").split(a);for(var e=0,k=m.length;eb)return a.charAt(0)===c.charAt(0)&&"/"===a.charAt(0)?"/":"";if("/"!==a.charAt(b)||"/"!==c.charAt(b))b=a.substring(0,b).lastIndexOf("/");return a.substring(0,b+1)};b.withinString=function(a,c,d){d||(d={});var m=d.start||b.findUri.start,f=d.end||b.findUri.end,e=d.trim||b.findUri.trim,k=/[a-z0-9-]=["']?$/i;for(m.lastIndex=0;;){var g=m.exec(a);if(!g)break;g=g.index;if(d.ignoreHtml){var u=
46 | a.slice(Math.max(g-3,0),g);if(u&&k.test(u))continue}var u=g+a.slice(g).search(f),h=a.slice(g,u).replace(e,"");d.ignore&&d.ignore.test(h)||(u=g+h.length,h=c(h,g,u,a),a=a.slice(0,g)+h+a.slice(u),m.lastIndex=g+h.length)}m.lastIndex=0;return a};b.ensureValidHostname=function(a){if(a.match(b.invalid_hostname_characters)){if(!e)throw new TypeError('Hostname "'+a+'" contains characters other than [A-Z0-9.-] and Punycode.js is not available');if(e.toASCII(a).match(b.invalid_hostname_characters))throw new TypeError('Hostname "'+
47 | a+'" contains characters other than [A-Z0-9.-]');}};b.noConflict=function(a){if(a)return a={URI:this.noConflict()},l.URITemplate&&"function"===typeof l.URITemplate.noConflict&&(a.URITemplate=l.URITemplate.noConflict()),l.IPv6&&"function"===typeof l.IPv6.noConflict&&(a.IPv6=l.IPv6.noConflict()),l.SecondLevelDomains&&"function"===typeof l.SecondLevelDomains.noConflict&&(a.SecondLevelDomains=l.SecondLevelDomains.noConflict()),a;l.URI===this&&(l.URI=p);return this};f.build=function(a){if(!0===a)this._deferred_build=
48 | !0;else if(void 0===a||this._deferred_build)this._string=b.build(this._parts),this._deferred_build=!1;return this};f.clone=function(){return new b(this)};f.valueOf=f.toString=function(){return this.build(!1)._string};f.protocol=t("protocol");f.username=t("username");f.password=t("password");f.hostname=t("hostname");f.port=t("port");f.query=r("query","?");f.fragment=r("fragment","#");f.search=function(a,c){var b=this.query(a,c);return"string"===typeof b&&b.length?"?"+b:b};f.hash=function(a,c){var b=
49 | this.fragment(a,c);return"string"===typeof b&&b.length?"#"+b:b};f.pathname=function(a,c){if(void 0===a||!0===a){var d=this._parts.path||(this._parts.hostname?"/":"");return a?(this._parts.urn?b.decodeUrnPath:b.decodePath)(d):d}this._parts.path=this._parts.urn?a?b.recodeUrnPath(a):"":a?b.recodePath(a):"/";this.build(!c);return this};f.path=f.pathname;f.href=function(a,c){var d;if(void 0===a)return this.toString();this._string="";this._parts=b._parts();var f=a instanceof b,e="object"===typeof a&&(a.hostname||
50 | a.path||a.pathname);a.nodeName&&(e=b.getDomAttribute(a),a=a[e]||"",e=!1);!f&&e&&void 0!==a.pathname&&(a=a.toString());if("string"===typeof a||a instanceof String)this._parts=b.parse(String(a),this._parts);else if(f||e)for(d in f=f?a._parts:a,f)v.call(this._parts,d)&&(this._parts[d]=f[d]);else throw new TypeError("invalid input");this.build(!c);return this};f.is=function(a){var c=!1,d=!1,f=!1,e=!1,k=!1,u=!1,h=!1,l=!this._parts.urn;this._parts.hostname&&(l=!1,d=b.ip4_expression.test(this._parts.hostname),
51 | f=b.ip6_expression.test(this._parts.hostname),c=d||f,k=(e=!c)&&g&&g.has(this._parts.hostname),u=e&&b.idn_expression.test(this._parts.hostname),h=e&&b.punycode_expression.test(this._parts.hostname));switch(a.toLowerCase()){case "relative":return l;case "absolute":return!l;case "domain":case "name":return e;case "sld":return k;case "ip":return c;case "ip4":case "ipv4":case "inet4":return d;case "ip6":case "ipv6":case "inet6":return f;case "idn":return u;case "url":return!this._parts.urn;case "urn":return!!this._parts.urn;
52 | case "punycode":return h}return null};var F=f.protocol,G=f.port,H=f.hostname;f.protocol=function(a,c){if(void 0!==a&&a&&(a=a.replace(/:(\/\/)?$/,""),!a.match(b.protocol_expression)))throw new TypeError('Protocol "'+a+"\" contains characters other than [A-Z0-9.+-] or doesn't start with [A-Z]");return F.call(this,a,c)};f.scheme=f.protocol;f.port=function(a,c){if(this._parts.urn)return void 0===a?"":this;if(void 0!==a&&(0===a&&(a=null),a&&(a+="",":"===a.charAt(0)&&(a=a.substring(1)),a.match(/[^0-9]/))))throw new TypeError('Port "'+
53 | a+'" contains characters other than [0-9]');return G.call(this,a,c)};f.hostname=function(a,c){if(this._parts.urn)return void 0===a?"":this;if(void 0!==a){var d={};if("/"!==b.parseHost(a,d))throw new TypeError('Hostname "'+a+'" contains characters other than [A-Z0-9.-]');a=d.hostname}return H.call(this,a,c)};f.host=function(a,c){if(this._parts.urn)return void 0===a?"":this;if(void 0===a)return this._parts.hostname?b.buildHost(this._parts):"";if("/"!==b.parseHost(a,this._parts))throw new TypeError('Hostname "'+
54 | a+'" contains characters other than [A-Z0-9.-]');this.build(!c);return this};f.authority=function(a,c){if(this._parts.urn)return void 0===a?"":this;if(void 0===a)return this._parts.hostname?b.buildAuthority(this._parts):"";if("/"!==b.parseAuthority(a,this._parts))throw new TypeError('Hostname "'+a+'" contains characters other than [A-Z0-9.-]');this.build(!c);return this};f.userinfo=function(a,c){if(this._parts.urn)return void 0===a?"":this;if(void 0===a){if(!this._parts.username)return"";var d=b.buildUserinfo(this._parts);
55 | return d.substring(0,d.length-1)}"@"!==a[a.length-1]&&(a+="@");b.parseUserinfo(a,this._parts);this.build(!c);return this};f.resource=function(a,c){var d;if(void 0===a)return this.path()+this.search()+this.hash();d=b.parse(a);this._parts.path=d.path;this._parts.query=d.query;this._parts.fragment=d.fragment;this.build(!c);return this};f.subdomain=function(a,c){if(this._parts.urn)return void 0===a?"":this;if(void 0===a){if(!this._parts.hostname||this.is("IP"))return"";var d=this._parts.hostname.length-
56 | this.domain().length-1;return this._parts.hostname.substring(0,d)||""}d=this._parts.hostname.length-this.domain().length;d=this._parts.hostname.substring(0,d);d=new RegExp("^"+h(d));a&&"."!==a.charAt(a.length-1)&&(a+=".");a&&b.ensureValidHostname(a);this._parts.hostname=this._parts.hostname.replace(d,a);this.build(!c);return this};f.domain=function(a,c){if(this._parts.urn)return void 0===a?"":this;"boolean"===typeof a&&(c=a,a=void 0);if(void 0===a){if(!this._parts.hostname||this.is("IP"))return"";
57 | var d=this._parts.hostname.match(/\./g);if(d&&2>d.length)return this._parts.hostname;d=this._parts.hostname.length-this.tld(c).length-1;d=this._parts.hostname.lastIndexOf(".",d-1)+1;return this._parts.hostname.substring(d)||""}if(!a)throw new TypeError("cannot set domain empty");b.ensureValidHostname(a);!this._parts.hostname||this.is("IP")?this._parts.hostname=a:(d=new RegExp(h(this.domain())+"$"),this._parts.hostname=this._parts.hostname.replace(d,a));this.build(!c);return this};f.tld=function(a,
58 | c){if(this._parts.urn)return void 0===a?"":this;"boolean"===typeof a&&(c=a,a=void 0);if(void 0===a){if(!this._parts.hostname||this.is("IP"))return"";var b=this._parts.hostname.lastIndexOf("."),b=this._parts.hostname.substring(b+1);return!0!==c&&g&&g.list[b.toLowerCase()]?g.get(this._parts.hostname)||b:b}if(a)if(a.match(/[^a-zA-Z0-9-]/))if(g&&g.is(a))b=new RegExp(h(this.tld())+"$"),this._parts.hostname=this._parts.hostname.replace(b,a);else throw new TypeError('TLD "'+a+'" contains characters other than [A-Z0-9]');
59 | else{if(!this._parts.hostname||this.is("IP"))throw new ReferenceError("cannot set TLD on non-domain host");b=new RegExp(h(this.tld())+"$");this._parts.hostname=this._parts.hostname.replace(b,a)}else throw new TypeError("cannot set TLD empty");this.build(!c);return this};f.directory=function(a,c){if(this._parts.urn)return void 0===a?"":this;if(void 0===a||!0===a){if(!this._parts.path&&!this._parts.hostname)return"";if("/"===this._parts.path)return"/";var d=this._parts.path.length-this.filename().length-
60 | 1,d=this._parts.path.substring(0,d)||(this._parts.hostname?"/":"");return a?b.decodePath(d):d}d=this._parts.path.length-this.filename().length;d=this._parts.path.substring(0,d);d=new RegExp("^"+h(d));this.is("relative")||(a||(a="/"),"/"!==a.charAt(0)&&(a="/"+a));a&&"/"!==a.charAt(a.length-1)&&(a+="/");a=b.recodePath(a);this._parts.path=this._parts.path.replace(d,a);this.build(!c);return this};f.filename=function(a,c){if(this._parts.urn)return void 0===a?"":this;if(void 0===a||!0===a){if(!this._parts.path||
61 | "/"===this._parts.path)return"";var d=this._parts.path.lastIndexOf("/"),d=this._parts.path.substring(d+1);return a?b.decodePathSegment(d):d}d=!1;"/"===a.charAt(0)&&(a=a.substring(1));a.match(/\.?\//)&&(d=!0);var f=new RegExp(h(this.filename())+"$");a=b.recodePath(a);this._parts.path=this._parts.path.replace(f,a);d?this.normalizePath(c):this.build(!c);return this};f.suffix=function(a,c){if(this._parts.urn)return void 0===a?"":this;if(void 0===a||!0===a){if(!this._parts.path||"/"===this._parts.path)return"";
62 | var d=this.filename(),f=d.lastIndexOf(".");if(-1===f)return"";d=d.substring(f+1);d=/^[a-z0-9%]+$/i.test(d)?d:"";return a?b.decodePathSegment(d):d}"."===a.charAt(0)&&(a=a.substring(1));if(d=this.suffix())f=a?new RegExp(h(d)+"$"):new RegExp(h("."+d)+"$");else{if(!a)return this;this._parts.path+="."+b.recodePath(a)}f&&(a=b.recodePath(a),this._parts.path=this._parts.path.replace(f,a));this.build(!c);return this};f.segment=function(a,c,b){var f=this._parts.urn?":":"/",e=this.path(),k="/"===e.substring(0,
63 | 1),e=e.split(f);void 0!==a&&"number"!==typeof a&&(b=c,c=a,a=void 0);if(void 0!==a&&"number"!==typeof a)throw Error('Bad segment "'+a+'", must be 0-based integer');k&&e.shift();0>a&&(a=Math.max(e.length+a,0));if(void 0===c)return void 0===a?e:e[a];if(null===a||void 0===e[a])if(w(c)){e=[];a=0;for(var g=c.length;a': '>',
64 | '"': '"',
65 | "'": ''',
66 | '/': '/',
67 | '`': '`',
68 | '=': '='
69 | };
70 |
71 | function escapeHtml (string) {
72 | return String(string).replace(/[&<>"'`=\/]/g, function fromEntityMap (s) {
73 | return entityMap[s];
74 | });
75 | }
76 |
77 | var whiteRe = /\s*/;
78 | var spaceRe = /\s+/;
79 | var equalsRe = /\s*=/;
80 | var curlyRe = /\s*\}/;
81 | var tagRe = /#|\^|\/|>|\{|&|=|!/;
82 |
83 | /**
84 | * Breaks up the given `template` string into a tree of tokens. If the `tags`
85 | * argument is given here it must be an array with two string values: the
86 | * opening and closing tags used in the template (e.g. [ "<%", "%>" ]). Of
87 | * course, the default is to use mustaches (i.e. mustache.tags).
88 | *
89 | * A token is an array with at least 4 elements. The first element is the
90 | * mustache symbol that was used inside the tag, e.g. "#" or "&". If the tag
91 | * did not contain a symbol (i.e. {{myValue}}) this element is "name". For
92 | * all text that appears outside a symbol this element is "text".
93 | *
94 | * The second element of a token is its "value". For mustache tags this is
95 | * whatever else was inside the tag besides the opening symbol. For text tokens
96 | * this is the text itself.
97 | *
98 | * The third and fourth elements of the token are the start and end indices,
99 | * respectively, of the token in the original template.
100 | *
101 | * Tokens that are the root node of a subtree contain two more elements: 1) an
102 | * array of tokens in the subtree and 2) the index in the original template at
103 | * which the closing tag for that section begins.
104 | */
105 | function parseTemplate (template, tags) {
106 | if (!template)
107 | return [];
108 |
109 | var sections = []; // Stack to hold section tokens
110 | var tokens = []; // Buffer to hold the tokens
111 | var spaces = []; // Indices of whitespace tokens on the current line
112 | var hasTag = false; // Is there a {{tag}} on the current line?
113 | var nonSpace = false; // Is there a non-space char on the current line?
114 |
115 | // Strips all whitespace tokens array for the current line
116 | // if there was a {{#tag}} on it and otherwise only space.
117 | function stripSpace () {
118 | if (hasTag && !nonSpace) {
119 | while (spaces.length)
120 | delete tokens[spaces.pop()];
121 | } else {
122 | spaces = [];
123 | }
124 |
125 | hasTag = false;
126 | nonSpace = false;
127 | }
128 |
129 | var openingTagRe, closingTagRe, closingCurlyRe;
130 | function compileTags (tagsToCompile) {
131 | if (typeof tagsToCompile === 'string')
132 | tagsToCompile = tagsToCompile.split(spaceRe, 2);
133 |
134 | if (!isArray(tagsToCompile) || tagsToCompile.length !== 2)
135 | throw new Error('Invalid tags: ' + tagsToCompile);
136 |
137 | openingTagRe = new RegExp(escapeRegExp(tagsToCompile[0]) + '\\s*');
138 | closingTagRe = new RegExp('\\s*' + escapeRegExp(tagsToCompile[1]));
139 | closingCurlyRe = new RegExp('\\s*' + escapeRegExp('}' + tagsToCompile[1]));
140 | }
141 |
142 | compileTags(tags || mustache.tags);
143 |
144 | var scanner = new Scanner(template);
145 |
146 | var start, type, value, chr, token, openSection;
147 | while (!scanner.eos()) {
148 | start = scanner.pos;
149 |
150 | // Match any text between tags.
151 | value = scanner.scanUntil(openingTagRe);
152 |
153 | if (value) {
154 | for (var i = 0, valueLength = value.length; i < valueLength; ++i) {
155 | chr = value.charAt(i);
156 |
157 | if (isWhitespace(chr)) {
158 | spaces.push(tokens.length);
159 | } else {
160 | nonSpace = true;
161 | }
162 |
163 | tokens.push([ 'text', chr, start, start + 1 ]);
164 | start += 1;
165 |
166 | // Check for whitespace on the current line.
167 | if (chr === '\n')
168 | stripSpace();
169 | }
170 | }
171 |
172 | // Match the opening tag.
173 | if (!scanner.scan(openingTagRe))
174 | break;
175 |
176 | hasTag = true;
177 |
178 | // Get the tag type.
179 | type = scanner.scan(tagRe) || 'name';
180 | scanner.scan(whiteRe);
181 |
182 | // Get the tag value.
183 | if (type === '=') {
184 | value = scanner.scanUntil(equalsRe);
185 | scanner.scan(equalsRe);
186 | scanner.scanUntil(closingTagRe);
187 | } else if (type === '{') {
188 | value = scanner.scanUntil(closingCurlyRe);
189 | scanner.scan(curlyRe);
190 | scanner.scanUntil(closingTagRe);
191 | type = '&';
192 | } else {
193 | value = scanner.scanUntil(closingTagRe);
194 | }
195 |
196 | // Match the closing tag.
197 | if (!scanner.scan(closingTagRe))
198 | throw new Error('Unclosed tag at ' + scanner.pos);
199 |
200 | token = [ type, value, start, scanner.pos ];
201 | tokens.push(token);
202 |
203 | if (type === '#' || type === '^') {
204 | sections.push(token);
205 | } else if (type === '/') {
206 | // Check section nesting.
207 | openSection = sections.pop();
208 |
209 | if (!openSection)
210 | throw new Error('Unopened section "' + value + '" at ' + start);
211 |
212 | if (openSection[1] !== value)
213 | throw new Error('Unclosed section "' + openSection[1] + '" at ' + start);
214 | } else if (type === 'name' || type === '{' || type === '&') {
215 | nonSpace = true;
216 | } else if (type === '=') {
217 | // Set the tags for the next time around.
218 | compileTags(value);
219 | }
220 | }
221 |
222 | // Make sure there are no open sections when we're done.
223 | openSection = sections.pop();
224 |
225 | if (openSection)
226 | throw new Error('Unclosed section "' + openSection[1] + '" at ' + scanner.pos);
227 |
228 | return nestTokens(squashTokens(tokens));
229 | }
230 |
231 | /**
232 | * Combines the values of consecutive text tokens in the given `tokens` array
233 | * to a single token.
234 | */
235 | function squashTokens (tokens) {
236 | var squashedTokens = [];
237 |
238 | var token, lastToken;
239 | for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
240 | token = tokens[i];
241 |
242 | if (token) {
243 | if (token[0] === 'text' && lastToken && lastToken[0] === 'text') {
244 | lastToken[1] += token[1];
245 | lastToken[3] = token[3];
246 | } else {
247 | squashedTokens.push(token);
248 | lastToken = token;
249 | }
250 | }
251 | }
252 |
253 | return squashedTokens;
254 | }
255 |
256 | /**
257 | * Forms the given array of `tokens` into a nested tree structure where
258 | * tokens that represent a section have two additional items: 1) an array of
259 | * all tokens that appear in that section and 2) the index in the original
260 | * template that represents the end of that section.
261 | */
262 | function nestTokens (tokens) {
263 | var nestedTokens = [];
264 | var collector = nestedTokens;
265 | var sections = [];
266 |
267 | var token, section;
268 | for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
269 | token = tokens[i];
270 |
271 | switch (token[0]) {
272 | case '#':
273 | case '^':
274 | collector.push(token);
275 | sections.push(token);
276 | collector = token[4] = [];
277 | break;
278 | case '/':
279 | section = sections.pop();
280 | section[5] = token[2];
281 | collector = sections.length > 0 ? sections[sections.length - 1][4] : nestedTokens;
282 | break;
283 | default:
284 | collector.push(token);
285 | }
286 | }
287 |
288 | return nestedTokens;
289 | }
290 |
291 | /**
292 | * A simple string scanner that is used by the template parser to find
293 | * tokens in template strings.
294 | */
295 | function Scanner (string) {
296 | this.string = string;
297 | this.tail = string;
298 | this.pos = 0;
299 | }
300 |
301 | /**
302 | * Returns `true` if the tail is empty (end of string).
303 | */
304 | Scanner.prototype.eos = function eos () {
305 | return this.tail === '';
306 | };
307 |
308 | /**
309 | * Tries to match the given regular expression at the current position.
310 | * Returns the matched text if it can match, the empty string otherwise.
311 | */
312 | Scanner.prototype.scan = function scan (re) {
313 | var match = this.tail.match(re);
314 |
315 | if (!match || match.index !== 0)
316 | return '';
317 |
318 | var string = match[0];
319 |
320 | this.tail = this.tail.substring(string.length);
321 | this.pos += string.length;
322 |
323 | return string;
324 | };
325 |
326 | /**
327 | * Skips all text until the given regular expression can be matched. Returns
328 | * the skipped string, which is the entire tail if no match can be made.
329 | */
330 | Scanner.prototype.scanUntil = function scanUntil (re) {
331 | var index = this.tail.search(re), match;
332 |
333 | switch (index) {
334 | case -1:
335 | match = this.tail;
336 | this.tail = '';
337 | break;
338 | case 0:
339 | match = '';
340 | break;
341 | default:
342 | match = this.tail.substring(0, index);
343 | this.tail = this.tail.substring(index);
344 | }
345 |
346 | this.pos += match.length;
347 |
348 | return match;
349 | };
350 |
351 | /**
352 | * Represents a rendering context by wrapping a view object and
353 | * maintaining a reference to the parent context.
354 | */
355 | function Context (view, parentContext) {
356 | this.view = view;
357 | this.cache = { '.': this.view };
358 | this.parent = parentContext;
359 | }
360 |
361 | /**
362 | * Creates a new context using the given view with this context
363 | * as the parent.
364 | */
365 | Context.prototype.push = function push (view) {
366 | return new Context(view, this);
367 | };
368 |
369 | /**
370 | * Returns the value of the given name in this context, traversing
371 | * up the context hierarchy if the value is absent in this context's view.
372 | */
373 | Context.prototype.lookup = function lookup (name) {
374 | var cache = this.cache;
375 |
376 | var value;
377 | if (cache.hasOwnProperty(name)) {
378 | value = cache[name];
379 | } else {
380 | var context = this, names, index, lookupHit = false;
381 |
382 | while (context) {
383 | if (name.indexOf('.') > 0) {
384 | value = context.view;
385 | names = name.split('.');
386 | index = 0;
387 |
388 | /**
389 | * Using the dot notion path in `name`, we descend through the
390 | * nested objects.
391 | *
392 | * To be certain that the lookup has been successful, we have to
393 | * check if the last object in the path actually has the property
394 | * we are looking for. We store the result in `lookupHit`.
395 | *
396 | * This is specially necessary for when the value has been set to
397 | * `undefined` and we want to avoid looking up parent contexts.
398 | **/
399 | while (value != null && index < names.length) {
400 | if (index === names.length - 1)
401 | lookupHit = hasProperty(value, names[index]);
402 |
403 | value = value[names[index++]];
404 | }
405 | } else {
406 | value = context.view[name];
407 | lookupHit = hasProperty(context.view, name);
408 | }
409 |
410 | if (lookupHit)
411 | break;
412 |
413 | context = context.parent;
414 | }
415 |
416 | cache[name] = value;
417 | }
418 |
419 | if (isFunction(value))
420 | value = value.call(this.view);
421 |
422 | return value;
423 | };
424 |
425 | /**
426 | * A Writer knows how to take a stream of tokens and render them to a
427 | * string, given a context. It also maintains a cache of templates to
428 | * avoid the need to parse the same template twice.
429 | */
430 | function Writer () {
431 | this.cache = {};
432 | }
433 |
434 | /**
435 | * Clears all cached templates in this writer.
436 | */
437 | Writer.prototype.clearCache = function clearCache () {
438 | this.cache = {};
439 | };
440 |
441 | /**
442 | * Parses and caches the given `template` and returns the array of tokens
443 | * that is generated from the parse.
444 | */
445 | Writer.prototype.parse = function parse (template, tags) {
446 | var cache = this.cache;
447 | var tokens = cache[template];
448 |
449 | if (tokens == null)
450 | tokens = cache[template] = parseTemplate(template, tags);
451 |
452 | return tokens;
453 | };
454 |
455 | /**
456 | * High-level method that is used to render the given `template` with
457 | * the given `view`.
458 | *
459 | * The optional `partials` argument may be an object that contains the
460 | * names and templates of partials that are used in the template. It may
461 | * also be a function that is used to load partial templates on the fly
462 | * that takes a single argument: the name of the partial.
463 | */
464 | Writer.prototype.render = function render (template, view, partials) {
465 | var tokens = this.parse(template);
466 | var context = (view instanceof Context) ? view : new Context(view);
467 | return this.renderTokens(tokens, context, partials, template);
468 | };
469 |
470 | /**
471 | * Low-level method that renders the given array of `tokens` using
472 | * the given `context` and `partials`.
473 | *
474 | * Note: The `originalTemplate` is only ever used to extract the portion
475 | * of the original template that was contained in a higher-order section.
476 | * If the template doesn't use higher-order sections, this argument may
477 | * be omitted.
478 | */
479 | Writer.prototype.renderTokens = function renderTokens (tokens, context, partials, originalTemplate) {
480 | var buffer = '';
481 |
482 | var token, symbol, value;
483 | for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
484 | value = undefined;
485 | token = tokens[i];
486 | symbol = token[0];
487 |
488 | if (symbol === '#') value = this.renderSection(token, context, partials, originalTemplate);
489 | else if (symbol === '^') value = this.renderInverted(token, context, partials, originalTemplate);
490 | else if (symbol === '>') value = this.renderPartial(token, context, partials, originalTemplate);
491 | else if (symbol === '&') value = this.unescapedValue(token, context);
492 | else if (symbol === 'name') value = this.escapedValue(token, context);
493 | else if (symbol === 'text') value = this.rawValue(token);
494 |
495 | if (value !== undefined)
496 | buffer += value;
497 | }
498 |
499 | return buffer;
500 | };
501 |
502 | Writer.prototype.renderSection = function renderSection (token, context, partials, originalTemplate) {
503 | var self = this;
504 | var buffer = '';
505 | var value = context.lookup(token[1]);
506 |
507 | // This function is used to render an arbitrary template
508 | // in the current context by higher-order sections.
509 | function subRender (template) {
510 | return self.render(template, context, partials);
511 | }
512 |
513 | if (!value) return;
514 |
515 | if (isArray(value)) {
516 | for (var j = 0, valueLength = value.length; j < valueLength; ++j) {
517 | buffer += this.renderTokens(token[4], context.push(value[j]), partials, originalTemplate);
518 | }
519 | } else if (typeof value === 'object' || typeof value === 'string' || typeof value === 'number') {
520 | buffer += this.renderTokens(token[4], context.push(value), partials, originalTemplate);
521 | } else if (isFunction(value)) {
522 | if (typeof originalTemplate !== 'string')
523 | throw new Error('Cannot use higher-order sections without the original template');
524 |
525 | // Extract the portion of the original template that the section contains.
526 | value = value.call(context.view, originalTemplate.slice(token[3], token[5]), subRender);
527 |
528 | if (value != null)
529 | buffer += value;
530 | } else {
531 | buffer += this.renderTokens(token[4], context, partials, originalTemplate);
532 | }
533 | return buffer;
534 | };
535 |
536 | Writer.prototype.renderInverted = function renderInverted (token, context, partials, originalTemplate) {
537 | var value = context.lookup(token[1]);
538 |
539 | // Use JavaScript's definition of falsy. Include empty arrays.
540 | // See https://github.com/janl/mustache.js/issues/186
541 | if (!value || (isArray(value) && value.length === 0))
542 | return this.renderTokens(token[4], context, partials, originalTemplate);
543 | };
544 |
545 | Writer.prototype.renderPartial = function renderPartial (token, context, partials) {
546 | if (!partials) return;
547 |
548 | var value = isFunction(partials) ? partials(token[1]) : partials[token[1]];
549 | if (value != null)
550 | return this.renderTokens(this.parse(value), context, partials, value);
551 | };
552 |
553 | Writer.prototype.unescapedValue = function unescapedValue (token, context) {
554 | var value = context.lookup(token[1]);
555 | if (value != null)
556 | return value;
557 | };
558 |
559 | Writer.prototype.escapedValue = function escapedValue (token, context) {
560 | var value = context.lookup(token[1]);
561 | if (value != null)
562 | return mustache.escape(value);
563 | };
564 |
565 | Writer.prototype.rawValue = function rawValue (token) {
566 | return token[1];
567 | };
568 |
569 | mustache.name = 'mustache.js';
570 | mustache.version = '2.2.1';
571 | mustache.tags = [ '{{', '}}' ];
572 |
573 | // All high-level mustache.* functions use this writer.
574 | var defaultWriter = new Writer();
575 |
576 | /**
577 | * Clears all cached templates in the default writer.
578 | */
579 | mustache.clearCache = function clearCache () {
580 | return defaultWriter.clearCache();
581 | };
582 |
583 | /**
584 | * Parses and caches the given template in the default writer and returns the
585 | * array of tokens it contains. Doing this ahead of time avoids the need to
586 | * parse templates on the fly as they are rendered.
587 | */
588 | mustache.parse = function parse (template, tags) {
589 | return defaultWriter.parse(template, tags);
590 | };
591 |
592 | /**
593 | * Renders the `template` with the given `view` and `partials` using the
594 | * default writer.
595 | */
596 | mustache.render = function render (template, view, partials) {
597 | if (typeof template !== 'string') {
598 | throw new TypeError('Invalid template! Template should be a "string" ' +
599 | 'but "' + typeStr(template) + '" was given as the first ' +
600 | 'argument for mustache#render(template, view, partials)');
601 | }
602 |
603 | return defaultWriter.render(template, view, partials);
604 | };
605 |
606 | // This is here for backwards compatibility with 0.4.x.,
607 | /*eslint-disable */ // eslint wants camel cased function name
608 | mustache.to_html = function to_html (template, view, partials, send) {
609 | /*eslint-enable*/
610 |
611 | var result = mustache.render(template, view, partials);
612 |
613 | if (isFunction(send)) {
614 | send(result);
615 | } else {
616 | return result;
617 | }
618 | };
619 |
620 | // Export the escaping function so that the user may override it.
621 | // See https://github.com/janl/mustache.js/issues/244
622 | mustache.escape = escapeHtml;
623 |
624 | // Export these mainly for testing, but also for advanced usage.
625 | mustache.Scanner = Scanner;
626 | mustache.Context = Context;
627 | mustache.Writer = Writer;
628 |
629 | }));
630 |
--------------------------------------------------------------------------------
/punycode.js:
--------------------------------------------------------------------------------
1 | /*! http://mths.be/punycode v1.2.3 by @mathias */
2 | ;(function(root) {
3 |
4 | /** Detect free variables */
5 | var freeExports = typeof exports == 'object' && exports;
6 | var freeModule = typeof module == 'object' && module &&
7 | module.exports == freeExports && module;
8 | var freeGlobal = typeof global == 'object' && global;
9 | if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {
10 | root = freeGlobal;
11 | }
12 |
13 | /**
14 | * The `punycode` object.
15 | * @name punycode
16 | * @type Object
17 | */
18 | var punycode,
19 |
20 | /** Highest positive signed 32-bit float value */
21 | maxInt = 2147483647, // aka. 0x7FFFFFFF or 2^31-1
22 |
23 | /** Bootstring parameters */
24 | base = 36,
25 | tMin = 1,
26 | tMax = 26,
27 | skew = 38,
28 | damp = 700,
29 | initialBias = 72,
30 | initialN = 128, // 0x80
31 | delimiter = '-', // '\x2D'
32 |
33 | /** Regular expressions */
34 | regexPunycode = /^xn--/,
35 | regexNonASCII = /[^ -~]/, // unprintable ASCII chars + non-ASCII chars
36 | regexSeparators = /\x2E|\u3002|\uFF0E|\uFF61/g, // RFC 3490 separators
37 |
38 | /** Error messages */
39 | errors = {
40 | 'overflow': 'Overflow: input needs wider integers to process',
41 | 'not-basic': 'Illegal input >= 0x80 (not a basic code point)',
42 | 'invalid-input': 'Invalid input'
43 | },
44 |
45 | /** Convenience shortcuts */
46 | baseMinusTMin = base - tMin,
47 | floor = Math.floor,
48 | stringFromCharCode = String.fromCharCode,
49 |
50 | /** Temporary variable */
51 | key;
52 |
53 | /*--------------------------------------------------------------------------*/
54 |
55 | /**
56 | * A generic error utility function.
57 | * @private
58 | * @param {String} type The error type.
59 | * @returns {Error} Throws a `RangeError` with the applicable error message.
60 | */
61 | function error(type) {
62 | throw RangeError(errors[type]);
63 | }
64 |
65 | /**
66 | * A generic `Array#map` utility function.
67 | * @private
68 | * @param {Array} array The array to iterate over.
69 | * @param {Function} callback The function that gets called for every array
70 | * item.
71 | * @returns {Array} A new array of values returned by the callback function.
72 | */
73 | function map(array, fn) {
74 | var length = array.length;
75 | while (length--) {
76 | array[length] = fn(array[length]);
77 | }
78 | return array;
79 | }
80 |
81 | /**
82 | * A simple `Array#map`-like wrapper to work with domain name strings.
83 | * @private
84 | * @param {String} domain The domain name.
85 | * @param {Function} callback The function that gets called for every
86 | * character.
87 | * @returns {Array} A new string of characters returned by the callback
88 | * function.
89 | */
90 | function mapDomain(string, fn) {
91 | return map(string.split(regexSeparators), fn).join('.');
92 | }
93 |
94 | /**
95 | * Creates an array containing the numeric code points of each Unicode
96 | * character in the string. While JavaScript uses UCS-2 internally,
97 | * this function will convert a pair of surrogate halves (each of which
98 | * UCS-2 exposes as separate characters) into a single code point,
99 | * matching UTF-16.
100 | * @see `punycode.ucs2.encode`
101 | * @see
102 | * @memberOf punycode.ucs2
103 | * @name decode
104 | * @param {String} string The Unicode input string (UCS-2).
105 | * @returns {Array} The new array of code points.
106 | */
107 | function ucs2decode(string) {
108 | var output = [],
109 | counter = 0,
110 | length = string.length,
111 | value,
112 | extra;
113 | while (counter < length) {
114 | value = string.charCodeAt(counter++);
115 | if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
116 | // high surrogate, and there is a next character
117 | extra = string.charCodeAt(counter++);
118 | if ((extra & 0xFC00) == 0xDC00) { // low surrogate
119 | output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
120 | } else {
121 | // unmatched surrogate; only append this code unit, in case the next
122 | // code unit is the high surrogate of a surrogate pair
123 | output.push(value);
124 | counter--;
125 | }
126 | } else {
127 | output.push(value);
128 | }
129 | }
130 | return output;
131 | }
132 |
133 | /**
134 | * Creates a string based on an array of numeric code points.
135 | * @see `punycode.ucs2.decode`
136 | * @memberOf punycode.ucs2
137 | * @name encode
138 | * @param {Array} codePoints The array of numeric code points.
139 | * @returns {String} The new Unicode string (UCS-2).
140 | */
141 | function ucs2encode(array) {
142 | return map(array, function(value) {
143 | var output = '';
144 | if (value > 0xFFFF) {
145 | value -= 0x10000;
146 | output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800);
147 | value = 0xDC00 | value & 0x3FF;
148 | }
149 | output += stringFromCharCode(value);
150 | return output;
151 | }).join('');
152 | }
153 |
154 | /**
155 | * Converts a basic code point into a digit/integer.
156 | * @see `digitToBasic()`
157 | * @private
158 | * @param {Number} codePoint The basic numeric code point value.
159 | * @returns {Number} The numeric value of a basic code point (for use in
160 | * representing integers) in the range `0` to `base - 1`, or `base` if
161 | * the code point does not represent a value.
162 | */
163 | function basicToDigit(codePoint) {
164 | if (codePoint - 48 < 10) {
165 | return codePoint - 22;
166 | }
167 | if (codePoint - 65 < 26) {
168 | return codePoint - 65;
169 | }
170 | if (codePoint - 97 < 26) {
171 | return codePoint - 97;
172 | }
173 | return base;
174 | }
175 |
176 | /**
177 | * Converts a digit/integer into a basic code point.
178 | * @see `basicToDigit()`
179 | * @private
180 | * @param {Number} digit The numeric value of a basic code point.
181 | * @returns {Number} The basic code point whose value (when used for
182 | * representing integers) is `digit`, which needs to be in the range
183 | * `0` to `base - 1`. If `flag` is non-zero, the uppercase form is
184 | * used; else, the lowercase form is used. The behavior is undefined
185 | * if `flag` is non-zero and `digit` has no uppercase form.
186 | */
187 | function digitToBasic(digit, flag) {
188 | // 0..25 map to ASCII a..z or A..Z
189 | // 26..35 map to ASCII 0..9
190 | return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5);
191 | }
192 |
193 | /**
194 | * Bias adaptation function as per section 3.4 of RFC 3492.
195 | * http://tools.ietf.org/html/rfc3492#section-3.4
196 | * @private
197 | */
198 | function adapt(delta, numPoints, firstTime) {
199 | var k = 0;
200 | delta = firstTime ? floor(delta / damp) : delta >> 1;
201 | delta += floor(delta / numPoints);
202 | for (/* no initialization */; delta > baseMinusTMin * tMax >> 1; k += base) {
203 | delta = floor(delta / baseMinusTMin);
204 | }
205 | return floor(k + (baseMinusTMin + 1) * delta / (delta + skew));
206 | }
207 |
208 | /**
209 | * Converts a Punycode string of ASCII-only symbols to a string of Unicode
210 | * symbols.
211 | * @memberOf punycode
212 | * @param {String} input The Punycode string of ASCII-only symbols.
213 | * @returns {String} The resulting string of Unicode symbols.
214 | */
215 | function decode(input) {
216 | // Don't use UCS-2
217 | var output = [],
218 | inputLength = input.length,
219 | out,
220 | i = 0,
221 | n = initialN,
222 | bias = initialBias,
223 | basic,
224 | j,
225 | index,
226 | oldi,
227 | w,
228 | k,
229 | digit,
230 | t,
231 | length,
232 | /** Cached calculation results */
233 | baseMinusT;
234 |
235 | // Handle the basic code points: let `basic` be the number of input code
236 | // points before the last delimiter, or `0` if there is none, then copy
237 | // the first basic code points to the output.
238 |
239 | basic = input.lastIndexOf(delimiter);
240 | if (basic < 0) {
241 | basic = 0;
242 | }
243 |
244 | for (j = 0; j < basic; ++j) {
245 | // if it's not a basic code point
246 | if (input.charCodeAt(j) >= 0x80) {
247 | error('not-basic');
248 | }
249 | output.push(input.charCodeAt(j));
250 | }
251 |
252 | // Main decoding loop: start just after the last delimiter if any basic code
253 | // points were copied; start at the beginning otherwise.
254 |
255 | for (index = basic > 0 ? basic + 1 : 0; index < inputLength; /* no final expression */) {
256 |
257 | // `index` is the index of the next character to be consumed.
258 | // Decode a generalized variable-length integer into `delta`,
259 | // which gets added to `i`. The overflow checking is easier
260 | // if we increase `i` as we go, then subtract off its starting
261 | // value at the end to obtain `delta`.
262 | for (oldi = i, w = 1, k = base; /* no condition */; k += base) {
263 |
264 | if (index >= inputLength) {
265 | error('invalid-input');
266 | }
267 |
268 | digit = basicToDigit(input.charCodeAt(index++));
269 |
270 | if (digit >= base || digit > floor((maxInt - i) / w)) {
271 | error('overflow');
272 | }
273 |
274 | i += digit * w;
275 | t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);
276 |
277 | if (digit < t) {
278 | break;
279 | }
280 |
281 | baseMinusT = base - t;
282 | if (w > floor(maxInt / baseMinusT)) {
283 | error('overflow');
284 | }
285 |
286 | w *= baseMinusT;
287 |
288 | }
289 |
290 | out = output.length + 1;
291 | bias = adapt(i - oldi, out, oldi == 0);
292 |
293 | // `i` was supposed to wrap around from `out` to `0`,
294 | // incrementing `n` each time, so we'll fix that now:
295 | if (floor(i / out) > maxInt - n) {
296 | error('overflow');
297 | }
298 |
299 | n += floor(i / out);
300 | i %= out;
301 |
302 | // Insert `n` at position `i` of the output
303 | output.splice(i++, 0, n);
304 |
305 | }
306 |
307 | return ucs2encode(output);
308 | }
309 |
310 | /**
311 | * Converts a string of Unicode symbols to a Punycode string of ASCII-only
312 | * symbols.
313 | * @memberOf punycode
314 | * @param {String} input The string of Unicode symbols.
315 | * @returns {String} The resulting Punycode string of ASCII-only symbols.
316 | */
317 | function encode(input) {
318 | var n,
319 | delta,
320 | handledCPCount,
321 | basicLength,
322 | bias,
323 | j,
324 | m,
325 | q,
326 | k,
327 | t,
328 | currentValue,
329 | output = [],
330 | /** `inputLength` will hold the number of code points in `input`. */
331 | inputLength,
332 | /** Cached calculation results */
333 | handledCPCountPlusOne,
334 | baseMinusT,
335 | qMinusT;
336 |
337 | // Convert the input in UCS-2 to Unicode
338 | input = ucs2decode(input);
339 |
340 | // Cache the length
341 | inputLength = input.length;
342 |
343 | // Initialize the state
344 | n = initialN;
345 | delta = 0;
346 | bias = initialBias;
347 |
348 | // Handle the basic code points
349 | for (j = 0; j < inputLength; ++j) {
350 | currentValue = input[j];
351 | if (currentValue < 0x80) {
352 | output.push(stringFromCharCode(currentValue));
353 | }
354 | }
355 |
356 | handledCPCount = basicLength = output.length;
357 |
358 | // `handledCPCount` is the number of code points that have been handled;
359 | // `basicLength` is the number of basic code points.
360 |
361 | // Finish the basic string - if it is not empty - with a delimiter
362 | if (basicLength) {
363 | output.push(delimiter);
364 | }
365 |
366 | // Main encoding loop:
367 | while (handledCPCount < inputLength) {
368 |
369 | // All non-basic code points < n have been handled already. Find the next
370 | // larger one:
371 | for (m = maxInt, j = 0; j < inputLength; ++j) {
372 | currentValue = input[j];
373 | if (currentValue >= n && currentValue < m) {
374 | m = currentValue;
375 | }
376 | }
377 |
378 | // Increase `delta` enough to advance the decoder's state to ,
379 | // but guard against overflow
380 | handledCPCountPlusOne = handledCPCount + 1;
381 | if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) {
382 | error('overflow');
383 | }
384 |
385 | delta += (m - n) * handledCPCountPlusOne;
386 | n = m;
387 |
388 | for (j = 0; j < inputLength; ++j) {
389 | currentValue = input[j];
390 |
391 | if (currentValue < n && ++delta > maxInt) {
392 | error('overflow');
393 | }
394 |
395 | if (currentValue == n) {
396 | // Represent delta as a generalized variable-length integer
397 | for (q = delta, k = base; /* no condition */; k += base) {
398 | t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);
399 | if (q < t) {
400 | break;
401 | }
402 | qMinusT = q - t;
403 | baseMinusT = base - t;
404 | output.push(
405 | stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT, 0))
406 | );
407 | q = floor(qMinusT / baseMinusT);
408 | }
409 |
410 | output.push(stringFromCharCode(digitToBasic(q, 0)));
411 | bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength);
412 | delta = 0;
413 | ++handledCPCount;
414 | }
415 | }
416 |
417 | ++delta;
418 | ++n;
419 |
420 | }
421 | return output.join('');
422 | }
423 |
424 | /**
425 | * Converts a Punycode string representing a domain name to Unicode. Only the
426 | * Punycoded parts of the domain name will be converted, i.e. it doesn't
427 | * matter if you call it on a string that has already been converted to
428 | * Unicode.
429 | * @memberOf punycode
430 | * @param {String} domain The Punycode domain name to convert to Unicode.
431 | * @returns {String} The Unicode representation of the given Punycode
432 | * string.
433 | */
434 | function toUnicode(domain) {
435 | return mapDomain(domain, function(string) {
436 | return regexPunycode.test(string)
437 | ? decode(string.slice(4).toLowerCase())
438 | : string;
439 | });
440 | }
441 |
442 | /**
443 | * Converts a Unicode string representing a domain name to Punycode. Only the
444 | * non-ASCII parts of the domain name will be converted, i.e. it doesn't
445 | * matter if you call it with a domain that's already in ASCII.
446 | * @memberOf punycode
447 | * @param {String} domain The domain name to convert, as a Unicode string.
448 | * @returns {String} The Punycode representation of the given domain name.
449 | */
450 | function toASCII(domain) {
451 | return mapDomain(domain, function(string) {
452 | return regexNonASCII.test(string)
453 | ? 'xn--' + encode(string)
454 | : string;
455 | });
456 | }
457 |
458 | /*--------------------------------------------------------------------------*/
459 |
460 | /** Define the public API */
461 | punycode = {
462 | /**
463 | * A string representing the current Punycode.js version number.
464 | * @memberOf punycode
465 | * @type String
466 | */
467 | 'version': '1.2.3',
468 | /**
469 | * An object of methods to convert from JavaScript's internal character
470 | * representation (UCS-2) to Unicode code points, and back.
471 | * @see
472 | * @memberOf punycode
473 | * @type Object
474 | */
475 | 'ucs2': {
476 | 'decode': ucs2decode,
477 | 'encode': ucs2encode
478 | },
479 | 'decode': decode,
480 | 'encode': encode,
481 | 'toASCII': toASCII,
482 | 'toUnicode': toUnicode
483 | };
484 |
485 | /** Expose `punycode` */
486 | // Some AMD build optimizers, like r.js, check for specific condition patterns
487 | // like the following:
488 | if (
489 | typeof define == 'function' &&
490 | typeof define.amd == 'object' &&
491 | define.amd
492 | ) {
493 | define(function() {
494 | return punycode;
495 | });
496 | } else if (freeExports && !freeExports.nodeType) {
497 | if (freeModule) { // in Node.js or RingoJS v0.8.0+
498 | freeModule.exports = punycode;
499 | } else { // in Narwhal or RingoJS v0.7.0-
500 | for (key in punycode) {
501 | punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]);
502 | }
503 | }
504 | } else { // in Rhino or a web browser
505 | root.punycode = punycode;
506 | }
507 |
508 | }(this));
509 |
--------------------------------------------------------------------------------