1 | "use strict";
|
2 | var __extends = (this && this.__extends) || function (d, b) {
|
3 | for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
4 | function __() { this.constructor = d; }
|
5 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
6 | };
|
7 | var collection_1 = require('../src/facade/collection');
|
8 | var exceptions_1 = require('../src/facade/exceptions');
|
9 | var lang_1 = require('../src/facade/lang');
|
10 | function convertUrlParamsToArray(urlParams) {
|
11 | var paramsArray = [];
|
12 | if (lang_1.isBlank(urlParams)) {
|
13 | return [];
|
14 | }
|
15 | collection_1.StringMapWrapper.forEach(urlParams, function (value /** TODO #9100 */, key /** TODO #9100 */) {
|
16 | paramsArray.push((value === true) ? key : key + '=' + value);
|
17 | });
|
18 | return paramsArray;
|
19 | }
|
20 | exports.convertUrlParamsToArray = convertUrlParamsToArray;
|
21 |
|
22 | function serializeParams(urlParams, joiner) {
|
23 | if (joiner === void 0) { joiner = '&'; }
|
24 | return convertUrlParamsToArray(urlParams).join(joiner);
|
25 | }
|
26 | exports.serializeParams = serializeParams;
|
27 |
|
28 |
|
29 |
|
30 | var Url = (function () {
|
31 | function Url(path, child, auxiliary, params) {
|
32 | if (child === void 0) { child = null; }
|
33 | if (auxiliary === void 0) { auxiliary = []; }
|
34 | if (params === void 0) { params = {}; }
|
35 | this.path = path;
|
36 | this.child = child;
|
37 | this.auxiliary = auxiliary;
|
38 | this.params = params;
|
39 | }
|
40 | Url.prototype.toString = function () {
|
41 | return this.path + this._matrixParamsToString() + this._auxToString() + this._childString();
|
42 | };
|
43 | Url.prototype.segmentToString = function () { return this.path + this._matrixParamsToString(); };
|
44 |
|
45 | Url.prototype._auxToString = function () {
|
46 | return this.auxiliary.length > 0 ?
|
47 | ('(' + this.auxiliary.map(function (sibling) { return sibling.toString(); }).join('//') + ')') :
|
48 | '';
|
49 | };
|
50 | Url.prototype._matrixParamsToString = function () {
|
51 | var paramString = serializeParams(this.params, ';');
|
52 | if (paramString.length > 0) {
|
53 | return ';' + paramString;
|
54 | }
|
55 | return '';
|
56 | };
|
57 |
|
58 | Url.prototype._childString = function () { return lang_1.isPresent(this.child) ? ('/' + this.child.toString()) : ''; };
|
59 | return Url;
|
60 | }());
|
61 | exports.Url = Url;
|
62 | var RootUrl = (function (_super) {
|
63 | __extends(RootUrl, _super);
|
64 | function RootUrl(path, child, auxiliary, params) {
|
65 | if (child === void 0) { child = null; }
|
66 | if (auxiliary === void 0) { auxiliary = []; }
|
67 | if (params === void 0) { params = null; }
|
68 | _super.call(this, path, child, auxiliary, params);
|
69 | }
|
70 | RootUrl.prototype.toString = function () {
|
71 | return this.path + this._auxToString() + this._childString() + this._queryParamsToString();
|
72 | };
|
73 | RootUrl.prototype.segmentToString = function () { return this.path + this._queryParamsToString(); };
|
74 | RootUrl.prototype._queryParamsToString = function () {
|
75 | if (lang_1.isBlank(this.params)) {
|
76 | return '';
|
77 | }
|
78 | return '?' + serializeParams(this.params);
|
79 | };
|
80 | return RootUrl;
|
81 | }(Url));
|
82 | exports.RootUrl = RootUrl;
|
83 | function pathSegmentsToUrl(pathSegments) {
|
84 | var url = new Url(pathSegments[pathSegments.length - 1]);
|
85 | for (var i = pathSegments.length - 2; i >= 0; i -= 1) {
|
86 | url = new Url(pathSegments[i], url);
|
87 | }
|
88 | return url;
|
89 | }
|
90 | exports.pathSegmentsToUrl = pathSegmentsToUrl;
|
91 | var SEGMENT_RE = lang_1.RegExpWrapper.create('^[^\\/\\(\\)\\?;=&#]+');
|
92 | function matchUrlSegment(str) {
|
93 | var match = lang_1.RegExpWrapper.firstMatch(SEGMENT_RE, str);
|
94 | return lang_1.isPresent(match) ? match[0] : '';
|
95 | }
|
96 | var QUERY_PARAM_VALUE_RE = lang_1.RegExpWrapper.create('^[^\\(\\)\\?;&#]+');
|
97 | function matchUrlQueryParamValue(str) {
|
98 | var match = lang_1.RegExpWrapper.firstMatch(QUERY_PARAM_VALUE_RE, str);
|
99 | return lang_1.isPresent(match) ? match[0] : '';
|
100 | }
|
101 | var UrlParser = (function () {
|
102 | function UrlParser() {
|
103 | }
|
104 | UrlParser.prototype.peekStartsWith = function (str) { return this._remaining.startsWith(str); };
|
105 | UrlParser.prototype.capture = function (str) {
|
106 | if (!this._remaining.startsWith(str)) {
|
107 | throw new exceptions_1.BaseException("Expected \"" + str + "\".");
|
108 | }
|
109 | this._remaining = this._remaining.substring(str.length);
|
110 | };
|
111 | UrlParser.prototype.parse = function (url) {
|
112 | this._remaining = url;
|
113 | if (url == '' || url == '/') {
|
114 | return new Url('');
|
115 | }
|
116 | return this.parseRoot();
|
117 | };
|
118 |
|
119 | UrlParser.prototype.parseRoot = function () {
|
120 | if (this.peekStartsWith('/')) {
|
121 | this.capture('/');
|
122 | }
|
123 | var path = matchUrlSegment(this._remaining);
|
124 | this.capture(path);
|
125 | var aux = [];
|
126 | if (this.peekStartsWith('(')) {
|
127 | aux = this.parseAuxiliaryRoutes();
|
128 | }
|
129 | if (this.peekStartsWith(';')) {
|
130 |
|
131 | this.parseMatrixParams();
|
132 | }
|
133 | var child = null;
|
134 | if (this.peekStartsWith('/') && !this.peekStartsWith('//')) {
|
135 | this.capture('/');
|
136 | child = this.parseSegment();
|
137 | }
|
138 | var queryParams = null;
|
139 | if (this.peekStartsWith('?')) {
|
140 | queryParams = this.parseQueryParams();
|
141 | }
|
142 | return new RootUrl(path, child, aux, queryParams);
|
143 | };
|
144 |
|
145 | UrlParser.prototype.parseSegment = function () {
|
146 | if (this._remaining.length == 0) {
|
147 | return null;
|
148 | }
|
149 | if (this.peekStartsWith('/')) {
|
150 | this.capture('/');
|
151 | }
|
152 | var path = matchUrlSegment(this._remaining);
|
153 | this.capture(path);
|
154 | var matrixParams = null;
|
155 | if (this.peekStartsWith(';')) {
|
156 | matrixParams = this.parseMatrixParams();
|
157 | }
|
158 | var aux = [];
|
159 | if (this.peekStartsWith('(')) {
|
160 | aux = this.parseAuxiliaryRoutes();
|
161 | }
|
162 | var child = null;
|
163 | if (this.peekStartsWith('/') && !this.peekStartsWith('//')) {
|
164 | this.capture('/');
|
165 | child = this.parseSegment();
|
166 | }
|
167 | return new Url(path, child, aux, matrixParams);
|
168 | };
|
169 | UrlParser.prototype.parseQueryParams = function () {
|
170 | var params = {};
|
171 | this.capture('?');
|
172 | this.parseQueryParam(params);
|
173 | while (this._remaining.length > 0 && this.peekStartsWith('&')) {
|
174 | this.capture('&');
|
175 | this.parseQueryParam(params);
|
176 | }
|
177 | return params;
|
178 | };
|
179 | UrlParser.prototype.parseMatrixParams = function () {
|
180 | var params = {};
|
181 | while (this._remaining.length > 0 && this.peekStartsWith(';')) {
|
182 | this.capture(';');
|
183 | this.parseParam(params);
|
184 | }
|
185 | return params;
|
186 | };
|
187 | UrlParser.prototype.parseParam = function (params) {
|
188 | var key = matchUrlSegment(this._remaining);
|
189 | if (lang_1.isBlank(key)) {
|
190 | return;
|
191 | }
|
192 | this.capture(key);
|
193 | var value = true;
|
194 | if (this.peekStartsWith('=')) {
|
195 | this.capture('=');
|
196 | var valueMatch = matchUrlSegment(this._remaining);
|
197 | if (lang_1.isPresent(valueMatch)) {
|
198 | value = valueMatch;
|
199 | this.capture(value);
|
200 | }
|
201 | }
|
202 | params[key] = value;
|
203 | };
|
204 | UrlParser.prototype.parseQueryParam = function (params) {
|
205 | var key = matchUrlSegment(this._remaining);
|
206 | if (lang_1.isBlank(key)) {
|
207 | return;
|
208 | }
|
209 | this.capture(key);
|
210 | var value = true;
|
211 | if (this.peekStartsWith('=')) {
|
212 | this.capture('=');
|
213 | var valueMatch = matchUrlQueryParamValue(this._remaining);
|
214 | if (lang_1.isPresent(valueMatch)) {
|
215 | value = valueMatch;
|
216 | this.capture(value);
|
217 | }
|
218 | }
|
219 | params[key] = value;
|
220 | };
|
221 | UrlParser.prototype.parseAuxiliaryRoutes = function () {
|
222 | var routes = [];
|
223 | this.capture('(');
|
224 | while (!this.peekStartsWith(')') && this._remaining.length > 0) {
|
225 | routes.push(this.parseSegment());
|
226 | if (this.peekStartsWith('//')) {
|
227 | this.capture('//');
|
228 | }
|
229 | }
|
230 | this.capture(')');
|
231 | return routes;
|
232 | };
|
233 | return UrlParser;
|
234 | }());
|
235 | exports.UrlParser = UrlParser;
|
236 | exports.parser = new UrlParser();
|
237 |
|
\ | No newline at end of file |