• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16interface NativeURLSearchParams {
17  new(input?: object | string | Iterable<[]> | null | undefined): NativeURLSearchParams;
18  append(params1: string, params2: string): void;
19  set(setname: string, setvalues: string): void;
20  sort(): void;
21  has(hasname: string): boolean;
22  toString(): string;
23  keys(): Array<string>;
24  values(): Array<string>;
25  getAll(getAllname: string): string[];
26  get(getname: string): string;
27  entries(): Array<Array<string>>;
28  delete(deletename: string): void;
29  updateParams(): void;
30  array: string[];
31}
32
33interface NativeURLParams {
34  new(input?: object | string | Iterable<[]> | null | undefined): NativeURLParams;
35  append(params1: string, params2: string): void;
36  set(setname: string, setvalues: string): void;
37  sort(): void;
38  has(hasname: string): boolean;
39  toString(): string;
40  keys(): Array<string>;
41  values(): Array<string>;
42  getAll(getAllname: string): string[];
43  get(getname: string): string;
44  entries(): Array<Array<string>>;
45  delete(deletename: string): void;
46  updateParams(): void;
47  array: string[];
48}
49
50interface NativeUrl {
51  new(input: string, base?: string | NativeUrl): NativeUrl;
52  new(): NativeUrl;
53  protocol: string;
54  username: string;
55  password: string;
56  hash: string;
57  search: string;
58  hostname: string;
59  host: string;
60  port: string;
61  encodeHost: string;
62  encodeSearch: string;
63  href(input: string): void;
64  pathname: string;
65  onOrOff: boolean;
66  GetIsIpv6: boolean;
67  parseURL(input: string, base?: string | NativeUrl | URL): NativeUrl;
68}
69interface UrlInterface {
70  URLSearchParams1: NativeURLSearchParams;
71  Url: NativeUrl;
72  URLParams1: NativeURLParams;
73  stringParmas(input: string): string[];
74  fixUSVstring(input: string): string;
75}
76
77declare function requireInternal(s: string): UrlInterface;
78const UrlInterface = requireInternal('url');
79
80
81let seachParamsArr: Array<string> = [];
82const typeErrorCodeId = 401; // 401:ErrorCodeId
83const syntaxErrorCodeId = 10200002; // 10200002:syntaxErrorCodeId
84
85class BusinessError extends Error {
86  code: number;
87  constructor(msg: string) {
88    super(msg);
89    this.name = 'BusinessError';
90    this.code = typeErrorCodeId;
91  }
92}
93
94function decodeSafelyOut(input: string): string {
95  let decodedString: string = '';
96  let decodedTemp: string = '';
97  let index: number = 0;
98  while (index < input.length) {
99    if (input[index] === '%' && /[0-9A-Fa-f]{2}/.test(input.slice(index + 1, index + 3))) {
100      const encodedChar = input.slice(index, index + 3);
101    try {
102      decodedString += decodeURIComponent(decodedTemp + encodedChar);
103      decodedTemp = '';
104    } catch (e) {
105      decodedTemp += encodedChar;
106    }
107      index += 3;
108      continue;
109    }
110    if (decodedTemp === '') {
111      decodedString += input[index];
112    } else {
113      decodedString += decodedTemp;
114      decodedString += input[index];
115      decodedTemp = '';
116    }
117    index++;
118  }
119  return decodedTemp === '' ? decodedString : decodedString += decodedTemp;
120}
121
122function customEncodeForToString(str: string): string {
123  const hexStrLen = 2; // 2:String length of hexadecimal encoded values
124  const hexAdecimal = 16; // 16:Hexadecimal number system
125  const regex = /[!'()~]/g;
126  try {
127    str = encodeURIComponent(str);
128  } catch (error) {
129    str = encodeURIComponent(UrlInterface.fixUSVstring(str));
130  }
131  return str.replace(regex, function (c) {
132    let hex = c.charCodeAt(0).toString(hexAdecimal);
133    return '%' + (hex.length < hexStrLen ? '0' : '') + hex.toUpperCase();
134  })
135    .replace(/%20/g, '+');
136}
137
138function removeKeyValuePairs(str: string, key: string): string {
139  const regex = new RegExp(`\\b${key}=[^&]*&?`, 'g');
140  let result = str.replace(regex, '');
141  if (result.endsWith('&')) {
142    result = result.slice(0, -1);
143  }
144  return result;
145}
146
147function containIllegalCode(str: string): Boolean {
148  const unpairedSurrogateRe =
149    /(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
150  const regex = new RegExp(unpairedSurrogateRe);
151  return regex.test(str);
152}
153
154function fixIllegalString(str: string):string {
155  if(containIllegalCode(str)) {
156    return UrlInterface.fixUSVstring(str);
157  } else {
158    return str;
159  }
160}
161
162class URLParams {
163  urlClass: NativeURLParams;
164  parentUrl: URL | null = null;
165  constructor(input: object | string | Iterable<[]> | null | undefined) {
166    let out: string[] = parameterProcess(input);
167    this.urlClass = new UrlInterface.URLParams1();
168    this.urlClass.array = out;
169  }
170
171  append(params1: string, params2: string): void {
172    if (arguments.length === 0 || typeof params1 !== 'string') {
173      throw new BusinessError(`Parameter error. The type of ${params1} must be string`);
174    }
175    if (arguments.length === 1 || typeof params2 !== 'string') {
176      throw new BusinessError(`Parameter error. The type of ${params2} must be string`);
177    }
178    this.urlClass.append(params1, params2);
179    if (this.parentUrl !== null) {
180      this.parentUrl.c_info.search = this.toString();
181      this.parentUrl.search_ = this.parentUrl.c_info.search;
182      this.parentUrl.setHref();
183    }
184  }
185
186  set(setName: string, setValues: string): void {
187    if (arguments.length === 0 || typeof setName !== 'string') {
188      throw new BusinessError(`Parameter error. The type of ${setName} must be string`);
189    }
190    if (arguments.length === 1 || typeof setValues !== 'string') {
191      throw new BusinessError(`Parameter error. The type of ${setValues} must be string`);
192    }
193    this.urlClass.set(setName, setValues);
194    if (this.parentUrl !== null) {
195      this.parentUrl.c_info.search = this.toString();
196      this.parentUrl.search_ = this.parentUrl.c_info.search;
197      this.parentUrl.setHref();
198    }
199  }
200
201  sort(): void {
202    this.urlClass.sort();
203    if (this.parentUrl !== null) {
204      this.parentUrl.c_info.search = this.toString();
205      this.parentUrl.search_ = this.parentUrl.c_info.search;
206      this.parentUrl.setHref();
207    }
208  }
209
210  has(hasname: string): boolean {
211    if (typeof hasname !== 'string') {
212      throw new BusinessError(`Parameter error. The type of ${hasname} must be string`);
213    }
214    return this.urlClass.has(hasname);
215  }
216
217  toString(): string {
218    const resultArray: string[] = [];
219    let arrayLen: number = this.urlClass.array.length;
220    let array: string[] = this.urlClass.array;
221    let key: string = '';
222    let value: string = '';
223    for (let pos: number = 0; pos < arrayLen; pos += 2) { // 2:Even subscripts exist as key values
224      key = customEncodeForToString(array[pos]).replace(/=/g, '%3D');
225      value = customEncodeForToString(array[pos + 1]).replace(/=/g, '%3D');
226      resultArray.push(`${pos > 0 ? '&' : ''}${key}=${value}`);
227    }
228    return resultArray.join('');
229  }
230
231  keys(): Object {
232    return this.urlClass.keys();
233  }
234
235  values(): Object {
236    return this.urlClass.values();
237  }
238
239  getAll(getAllname: string): string[] {
240    if ((arguments.length !== 1) || (typeof getAllname !== 'string')) {
241      throw new BusinessError(`Parameter error. The type of ${getAllname} must be string`);
242    }
243    return this.urlClass.getAll(getAllname);
244  }
245
246  get(getname: string): string {
247    if (arguments.length === 0 || typeof getname !== 'string') {
248      throw new BusinessError(`Parameter error. The type of ${getname} must be string`);
249    }
250    return this.urlClass.get(getname);
251  }
252
253  entries(): Object {
254    return this.urlClass.entries();
255  }
256
257  delete(deleteName: string): void {
258    if (arguments.length === 0 || typeof deleteName !== 'string') {
259      throw new BusinessError(`Parameter error. The type of ${deleteName} must be string`);
260    }
261    this.urlClass.delete(deleteName);
262    if (this.parentUrl !== null) {
263      let searchStr: string = removeKeyValuePairs(this.parentUrl.c_info.search, deleteName);
264      this.parentUrl.c_info.search = searchStr;
265      this.parentUrl.search_ = searchStr;
266      this.parentUrl.setHref();
267    }
268  }
269
270  forEach(objfun: Function, thisArg?: Object): void {
271    if (typeof objfun !== 'function') {
272      throw new BusinessError(`Parameter error. The type of ${objfun} must be function`);
273    }
274    let array = this.urlClass.array;
275    if (array.length === 0) {
276      return;
277    }
278    if (typeof thisArg === 'undefined' || thisArg === null) {
279      thisArg = Object;
280    }
281    let size = array.length - 1;
282    for (let i = 0; i < size; i += 2) { // 2:Searching for the number and number of keys and values 2
283      let key = array[i];
284      let value = array[i + 1];
285      objfun.call(thisArg, value, key, this);
286    }
287  }
288
289  [Symbol.iterator](): Object {
290    return this.urlClass.entries();
291  }
292
293  updateParams(input: string): void {
294    let out = [];
295    out = parameterProcess(input);
296    this.urlClass.array = out;
297  }
298}
299
300class URLSearchParams {
301  urlClass: NativeURLSearchParams;
302  parentUrl: URL | null = null;
303  constructor(input: object | string | Iterable<[]> | null | undefined) {
304    let out: string[] = parameterProcessing(input);
305    this.urlClass = new UrlInterface.URLSearchParams1();
306    this.urlClass.array = out;
307  }
308  append(params1: string, params2: string): void {
309    if (arguments.length === 0 || typeof params1 !== 'string') {
310      throw new BusinessError(`Parameter error. The type of ${params1} must be string`);
311    }
312    if (arguments.length === 1 || typeof params2 !== 'string') {
313      throw new BusinessError(`Parameter error. The type of ${params2} must be string`);
314    }
315    this.urlClass.append(params1, params2);
316    if (this.parentUrl !== null) {
317      this.parentUrl.c_info.search = this.toString();
318      this.parentUrl.search_ = this.parentUrl.c_info.search;
319      this.parentUrl.setHref();
320    }
321  }
322
323  set(setName: string, setValues: string): void {
324    if (arguments.length === 0 || typeof setName !== 'string') {
325      throw new BusinessError(`Parameter error. The type of ${setName} must be string`);
326    }
327    if (arguments.length === 1 || typeof setValues !== 'string') {
328      throw new BusinessError(`Parameter error. The type of ${setValues} must be string`);
329    }
330    this.urlClass.set(setName, setValues);
331    if (this.parentUrl !== null) {
332      this.parentUrl.c_info.search = this.toString();
333      this.parentUrl.search_ = this.parentUrl.c_info.search;
334      this.parentUrl.setHref();
335    }
336  }
337
338  sort(): void {
339    this.urlClass.sort();
340    if (this.parentUrl !== null) {
341      this.parentUrl.c_info.search = this.toString();
342      this.parentUrl.search_ = this.parentUrl.c_info.search;
343      this.parentUrl.setHref();
344    }
345  }
346
347  has(hasname: string): boolean {
348    if (typeof hasname !== 'string') {
349      throw new BusinessError(`Parameter error. The type of ${hasname} must be string`);
350    }
351    return this.urlClass.has(hasname);
352  }
353
354  toString(): string {
355    const resultArray: string[] = [];
356    let arrayLen: number = this.urlClass.array.length;
357    let array: string[] = this.urlClass.array;
358    let key: string = '';
359    let value: string = '';
360    for (let pos: number = 0; pos < arrayLen; pos += 2) { // 2:Even subscripts exist as key values
361      key = customEncodeForToString(array[pos]).replace(/=/g, '%3D');
362      value = customEncodeForToString(array[pos + 1]).replace(/=/g, '%3D');
363      resultArray.push(`${pos > 0 ? '&' : ''}${key}=${value}`);
364    }
365    return resultArray.join('');
366  }
367
368  keys(): Object {
369    return this.urlClass.keys();
370  }
371
372  values(): Object {
373    return this.urlClass.values();
374  }
375
376  getAll(getAllname: string): string[] {
377    return this.urlClass.getAll(getAllname);
378  }
379
380  get(getname: string): string {
381    return this.urlClass.get(getname);
382  }
383
384  entries(): Object {
385    return this.urlClass.entries();
386  }
387
388  delete(deleteName: string): void {
389    this.urlClass.delete(deleteName);
390    if (this.parentUrl !== null) {
391      let searchStr: string = removeKeyValuePairs(this.parentUrl.c_info.search, deleteName);
392      this.parentUrl.c_info.search = searchStr;
393      this.parentUrl.search_ = searchStr;
394      this.parentUrl.setHref();
395    }
396  }
397
398  forEach(objfun: Function, thisArg?: Object): void {
399    let array = this.urlClass.array;
400    if (array.length === 0) {
401      return;
402    }
403    if (typeof thisArg === 'undefined' || thisArg === null) {
404      thisArg = Object;
405    }
406    let size = array.length - 1;
407    for (let i = 0; i < size; i += 2) { // 2:Searching for the number and number of keys and values 2
408      let key = array[i];
409      let value = array[i + 1];
410      objfun.call(thisArg, value, key, this);
411    }
412  }
413
414  [Symbol.iterator](): Object {
415    return this.urlClass.entries();
416  }
417
418  updateParams(input: string): void {
419    let out = [];
420    out = parameterProcessing(input);
421    this.urlClass.array = out;
422  }
423}
424
425function toHleString(arg: string | symbol | number): string {
426  return arg.toString();
427}
428
429function parameterProcess(input: object | string | Iterable<[]>): Array<string> {
430  if (input === null || typeof input === 'undefined' || input === '') {
431    seachParamsArr = [];
432    return seachParamsArr;
433  } else if (typeof input === 'object' || typeof input === 'function') {
434    if (input instanceof URLParams) {
435        return input.urlClass.array;
436    }
437    return sysObjectParams(input);
438  } else {
439    return initToStringSeachParams(input);
440  }
441}
442
443function parameterProcessing(input: object | string | Iterable<[]>): Array<string> {
444  if (input === null || typeof input === 'undefined' || input === '') {
445    seachParamsArr = [];
446    return seachParamsArr;
447  } else if (typeof input === 'object' || typeof input === 'function') {
448    if (input instanceof URLSearchParams) {
449        return input.urlClass.array;
450    }
451    return initObjectSeachParams(input);
452  } else {
453    return initToStringSeachParams(input);
454  }
455}
456
457function sysObjectParams(input: object | Iterable<[]>): Array<string> {
458  if (typeof input[Symbol.iterator] === 'function') {
459    return iteratorMethodThrow(input as Iterable<[string]>);
460  }
461  return recordMethod(input);
462}
463
464function initObjectSeachParams(input: object | Iterable<[]>): Array<string> {
465  if (typeof input[Symbol.iterator] === 'function') {
466    return iteratorMethod(input as Iterable<[string]>);
467  }
468  return recordMethod(input);
469}
470
471function recordMethod(input: object) : Array<string> {
472  const keys = Reflect.ownKeys(input);
473  seachParamsArr = [];
474  for (let i = 0; i <= keys.length; i++) {
475    const key = keys[i];
476    const desc = Reflect.getOwnPropertyDescriptor(input, key);
477    if (desc !== undefined && desc.enumerable) {
478      const typedKey = toHleString(key);
479      const typedValue = toHleString(input[key]);
480      seachParamsArr.push(typedKey, typedValue);
481    }
482  }
483  return seachParamsArr;
484}
485
486function iteratorMethodThrow(input: Iterable<[string]>): Array<string> {
487  let pairs = [];
488  seachParamsArr = [];
489  for (const pair of input) {
490    if ((typeof pair !== 'object' && typeof pair !== 'function') || pair === null || typeof pair[Symbol.iterator] !== 'function') {
491      throw new BusinessError(`Parameter error. The type of ${input} must be string[][]`);
492    }
493    const convertedPair = [];
494    for (let element of pair) {
495      convertedPair.push(element);
496    }
497    pairs.push(convertedPair);
498  }
499
500  for (const pair of pairs) {
501    if (pair.length !== 2) { // 2:Searching for the number and number of keys and values 2
502      throw new BusinessError(`Parameter error. The type of ${input} must be string[][]`);
503    }
504    seachParamsArr.push(pair[0], pair[1]);
505  }
506  return seachParamsArr;
507}
508
509function iteratorMethod(input: Iterable<[string]>): Array<string> {
510  let pairs = [];
511  seachParamsArr = [];
512  for (const pair of input) {
513    const convertedPair = [];
514    for (let element of pair) {
515      convertedPair.push(element);
516    }
517    pairs.push(convertedPair);
518  }
519
520  for (const pair of pairs) {
521    if (pair.length !== 2) { // 2:Searching for the number and number of keys and values 2
522      console.error('key-value-is-worong');
523    }
524    seachParamsArr.push(pair[0], pair[1]);
525  }
526  return seachParamsArr;
527}
528
529function decodeStringParmas(input: string): string {
530  let strVal = '';
531  try {
532    strVal = decodeURIComponent(input);
533  } catch (e) {
534    strVal = decodeSafelyOut(input);
535  }
536  return strVal;
537}
538
539function initToStringSeachParams(input: string): Array<string> {
540  if (typeof input !== 'string') {
541    throw new BusinessError(`Parameter error. The type of ${input} must be string`);
542  }
543  if (input[0] === '?') {
544    input = input.slice(1);
545  }
546  let strVal = input.replace(/\+/g, ' ');
547  seachParamsArr = UrlInterface.stringParmas(strVal);
548  return seachParamsArr;
549}
550
551class URL {
552  href_: string = '';
553  search_: string = '';
554  origin_: string = '';
555  username_: string = '';
556  password_: string = '';
557  hostname_: string = '';
558  host_: string = '';
559  hash_: string = '';
560  protocol_: string = '';
561  pathname_: string = '';
562  port_: string = '';
563  searchParamsClass_ !: URLSearchParams;
564  URLParamsClass_ !: URLParams;
565  c_info !: NativeUrl;
566  public constructor();
567  public constructor(inputUrl: string, inputBase?: string | URL);
568  public constructor(inputUrl?: string, inputBase?: string | URL) {
569    if (arguments.length === 0) {
570    }
571    let nativeUrl !: NativeUrl;
572    if (arguments.length === 1 || (arguments.length === 2 && (typeof inputBase === 'undefined' || inputBase === null))) {
573      if (typeof inputUrl === 'string' && inputUrl.length > 0) {
574        nativeUrl = new UrlInterface.Url(fixIllegalString(inputUrl));
575      } else {
576        throw new BusinessError(`Parameter error. The type of ${inputUrl} must be string`);
577      }
578    } else if (arguments.length === 2) { // 2:The number of parameters is 2
579      if (typeof inputUrl === 'string') {
580        if (typeof inputBase === 'string') {
581          if (inputBase.length > 0) {
582            nativeUrl = new UrlInterface.Url(fixIllegalString(inputUrl), fixIllegalString(inputBase));
583          } else {
584            throw new BusinessError(`Parameter error. The type of ${inputUrl} must be string`);
585            return;
586          }
587        } else if (typeof inputBase === 'object') {
588          let nativeBase: NativeUrl = inputBase.getInfo();
589          nativeUrl = new UrlInterface.Url(fixIllegalString(inputUrl), nativeBase);
590        }
591      }
592    }
593    if (arguments.length === 1 || arguments.length === 2) { // 2:The number of parameters is 2
594      this.c_info = nativeUrl;
595      if (nativeUrl.onOrOff) {
596        URL.setParamsFromNativeUrl(nativeUrl, this);
597      } else {
598        console.error('constructor failed');
599      }
600    }
601  }
602
603  static setParamsFromNativeUrl(nativeUrl: NativeUrl, urlHelper:URL): void {
604    urlHelper.search_ = nativeUrl.search;
605    urlHelper.username_ = nativeUrl.username;
606    urlHelper.password_ = nativeUrl.password;
607    urlHelper.hostname_ = nativeUrl.hostname;
608    urlHelper.host_ = nativeUrl.encodeHost;
609    urlHelper.hash_ = nativeUrl.hash;
610    urlHelper.protocol_ = nativeUrl.protocol;
611    urlHelper.pathname_ = nativeUrl.pathname;
612    urlHelper.port_ = nativeUrl.port;
613    urlHelper.origin_ = nativeUrl.protocol + '//' + nativeUrl.host;
614    urlHelper.searchParamsClass_ = new URLSearchParams(nativeUrl.encodeSearch);
615    urlHelper.URLParamsClass_ = new URLParams(nativeUrl.encodeSearch);
616    urlHelper.URLParamsClass_.parentUrl = urlHelper;
617    urlHelper.searchParamsClass_.parentUrl = urlHelper;
618    urlHelper.setHref();
619  }
620
621  static parseURL(inputUrl: string, inputBase?: string | NativeUrl | URL): URL {
622    if (typeof inputUrl !== 'string') {
623      throw new BusinessError(`Parameter error. The type of ${inputUrl} must be string`);
624    }
625    let nativeUrl !: NativeUrl;
626    if (arguments.length === 1 || (arguments.length === 2 && (typeof inputBase === 'undefined' || inputBase === null))) {
627      nativeUrl = new UrlInterface.Url(fixIllegalString(inputUrl));
628    } else if (arguments.length === 2) { // 2:The number of parameters is 2
629      if (typeof inputBase === 'string') {
630        if (inputBase.length > 0) {
631          nativeUrl = new UrlInterface.Url(fixIllegalString(inputUrl), fixIllegalString(inputBase));
632        } else {
633          throw new BusinessError(`Parameter error. The type of ${inputBase} must be string`);
634        }
635      } else if (typeof inputBase === 'object') {
636        let nativeBase: NativeUrl = inputBase.getInfo();
637        nativeUrl = new UrlInterface.Url(fixIllegalString(inputUrl), nativeBase);
638      } else {
639        throw new BusinessError(`Parameter error. The type of ${inputBase} must be string or URL`);
640      }
641    }
642    let urlHelper = new URL();
643    urlHelper.c_info = nativeUrl;
644    if (nativeUrl.onOrOff) {
645      URL.setParamsFromNativeUrl(nativeUrl, urlHelper);
646    } else {
647      let err : BusinessError = new BusinessError('Syntax Error. Invalid Url string');
648      err.code = syntaxErrorCodeId;
649      throw err;
650    }
651    return urlHelper;
652  }
653
654  getInfo(): NativeUrl {
655    return this.c_info;
656  }
657  toString(): string {
658    return this.href_;
659  }
660
661  get protocol(): string {
662    return this.protocol_;
663  }
664  set protocol(scheme) {
665    if (scheme.length === 0) {
666      return;
667    }
668    if (this.protocol_ === 'file:' && (this.host_ === '' || this.host_ === null)) {
669      return;
670    }
671    this.c_info.protocol = scheme;
672    this.protocol_ = this.c_info.protocol;
673    this.setHref();
674  }
675  get origin(): string {
676    let kOpaqueOrigin: string = 'null';
677    switch (this.protocol_) {
678      case 'ftp:':
679      case 'gopher:':
680      case 'http:':
681      case 'https:':
682      case 'ws:':
683      case 'wss:':
684        return this.origin_;
685    }
686    return kOpaqueOrigin;
687  }
688  get username(): string {
689    return this.username_;
690  }
691  set username(input) {
692    if (this.host_ === null || this.host_ === '' || this.protocol_ === 'file:') {
693      return;
694    }
695    this.c_info.username = fixIllegalString(input);
696    this.username_ = this.c_info.username;
697    this.setHref();
698  }
699  get password(): string {
700    return this.password_;
701  }
702  set password(input) {
703    if (this.host_ === null || this.host_ === '' || this.protocol_ === 'file:') {
704      return;
705    }
706    this.c_info.password = fixIllegalString(input);
707    this.password_ = this.c_info.password;
708    this.setHref();
709  }
710  get hash(): string {
711    return this.hash_;
712  }
713  set hash(fragment) {
714    this.c_info.hash = fixIllegalString(fragment);
715    this.hash_ = this.c_info.hash;
716    this.setHref();
717  }
718  get search(): string {
719    return this.search_;
720  }
721  set search(query) {
722    this.c_info.encodeSearch = fixIllegalString(query);
723    this.search_ = this.c_info.search;
724    this.searchParamsClass_.updateParams(this.search_);
725    this.URLParamsClass_.updateParams(this.search_);
726    this.setHref();
727  }
728  get hostname(): string {
729    return this.hostname_;
730  }
731  set hostname(hostname) {
732    this.c_info.hostname = hostname;
733    this.hostname_ = this.c_info.hostname;
734    this.setHref();
735  }
736  get host(): string {
737    return this.host_;
738  }
739  set host(host_) {
740    this.c_info.host = host_;
741    this.host_ = this.c_info.encodeHost;
742    this.hostname_ = this.c_info.hostname;
743    this.port_ = this.c_info.port;
744    this.setHref();
745  }
746  get port(): string {
747    return this.port_;
748  }
749  set port(port) {
750    if (this.host_ === '' || this.protocol_ === 'file:' || port === '') {
751      return;
752    }
753    this.c_info.port = port;
754    this.port_ = this.c_info.port;
755    this.setHref();
756  }
757  get href(): string {
758    return this.href_;
759  }
760  set href(href_) {
761    this.c_info.href(href_);
762    if (this.c_info.onOrOff) {
763      this.search_ = this.c_info.search;
764      this.username_ = this.c_info.username;
765      this.password_ = this.c_info.password;
766      this.hostname_ = this.c_info.hostname;
767      this.host_ = this.c_info.encodeHost;
768      this.hash_ = this.c_info.hash;
769      this.protocol_ = this.c_info.protocol;
770      this.pathname_ = this.c_info.pathname;
771      this.port_ = this.c_info.port;
772      this.origin_ = this.protocol_ + '//' + this.host_;
773      this.searchParamsClass_.updateParams(this.search_);
774      this.URLParamsClass_.updateParams(this.search_);
775      this.setHref();
776    }
777  }
778
779  get pathname(): string {
780    return this.pathname_;
781  }
782  set pathname(path) {
783    this.c_info.pathname = fixIllegalString(path);
784    this.pathname_ = this.c_info.pathname;
785    this.setHref();
786  }
787
788  get searchParams(): URLSearchParams {
789    return this.searchParamsClass_;
790  }
791
792  get params(): URLParams {
793    return this.URLParamsClass_;
794  }
795
796  toJSON(): string {
797    return this.href_;
798  }
799  setHref(): void {
800    let temp: string = this.protocol_;
801    if (this.hostname_ !== '') {
802      temp += '//';
803      if (this.password_ !== '' || this.username_ !== '') {
804        if (this.username_ !== '') {
805          temp += this.username_;
806        }
807        if (this.password_ !== '') {
808          temp += ':';
809          temp += this.password_;
810        }
811        temp += '@';
812      }
813      temp += this.hostname_;
814      if (this.port_ !== '') {
815        temp += ':';
816        temp += this.port_;
817      }
818    } else if (this.protocol_ === 'file:') {
819      temp += '//';
820    }
821    temp += this.pathname_;
822    if (this.search_) {
823      temp += this.search_;
824    }
825    if (this.hash_) {
826      temp += this.hash_;
827    }
828    this.href_ = temp;
829  }
830}
831
832export default {
833  URLSearchParams: URLSearchParams,
834  URL: URL,
835  URLParams: URLParams,
836};