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