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 initialNumber: number; 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 initialNumber: number; 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; 82const SyntaxErrorCodeId = 10200002; 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; 95 const hexAdecimal = 16; 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; 106 const hexAdecimal = 16; 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(/%2B/g, '+'); 115} 116 117class URLParams { 118 urlClass: NativeURLParams; 119 constructor(input: object | string | Iterable<[]> | null | undefined) { 120 let out: string[] = parameterProcess(input); 121 this.urlClass = new UrlInterface.URLParams1(); 122 this.urlClass.array = out; 123 this.urlClass.initialNumber = out.length; 124 } 125 126 append(params1: string, params2: string): void { 127 if (arguments.length === 0 || typeof params1 !== 'string') { 128 throw new BusinessError(`Parameter error.The type of ${params1} must be string`); 129 } 130 if (arguments.length === 1 || typeof params2 !== 'string') { 131 throw new BusinessError(`Parameter error.The type of ${params2} must be string`); 132 } 133 params1 = customEncodeURIComponent(params1); 134 params2 = customEncodeURIComponent(params2); 135 this.urlClass.append(params1, params2); 136 } 137 138 set(setname: string, setvalues: string): void { 139 if (arguments.length === 0 || typeof setname !== 'string') { 140 throw new BusinessError(`Parameter error.The type of ${setname} must be string`); 141 } 142 if (arguments.length === 1 || typeof setvalues !== 'string') { 143 throw new BusinessError(`Parameter error.The type of ${setvalues} must be string`); 144 } 145 setname = customEncodeURIComponent(setname); 146 setvalues = customEncodeURIComponent(setvalues); 147 this.urlClass.set(setname, setvalues); 148 } 149 150 sort(): void { 151 this.urlClass.sort(); 152 } 153 154 has(hasname: string): boolean { 155 if (typeof hasname !== 'string') { 156 throw new BusinessError(`Parameter error.The type of ${hasname} must be string`); 157 } 158 return this.urlClass.has(hasname); 159 } 160 161 toString(): string { 162 let outPut: string = customEncodeForToString(this.urlClass.array[0]) + '=' + customEncodeForToString(this.urlClass.array[1]); 163 let arrayLen: number = this.urlClass.array.length; 164 if (arrayLen % 2 === 0) { 165 let pos: number = 2; 166 for (; pos < arrayLen; pos += 2) { 167 if (pos < this.urlClass.initialNumber) { 168 outPut += '&' + customEncodeForToString(this.urlClass.array[pos]) + '=' + customEncodeForToString(this.urlClass.array[pos + 1]); 169 } else { 170 outPut += '&' + this.urlClass.array[pos] + '=' + this.urlClass.array[pos + 1]; 171 } 172 } 173 } 174 return outPut; 175 } 176 177 keys(): Object { 178 return this.urlClass.keys(); 179 } 180 181 values(): Object { 182 return this.urlClass.values(); 183 } 184 185 getAll(getname: string): string[] { 186 if ((arguments.length !== 1) || (typeof getname !== 'string')) { 187 throw new BusinessError(`Parameter error.The type of ${getname} must be string`); 188 } 189 return this.urlClass.getAll(getname); 190 } 191 192 get(getname: string): string { 193 if (arguments.length === 0 || typeof getname !== 'string') { 194 throw new BusinessError(`Parameter error.The type of ${getname} must be string`); 195 } 196 return this.urlClass.get(getname); 197 } 198 199 entries(): Object { 200 return this.urlClass.entries(); 201 } 202 203 delete(deletename: string): void { 204 if (arguments.length === 0 || typeof deletename !== 'string') { 205 throw new BusinessError(`Parameter error.The type of ${deletename} must be string`); 206 } 207 this.urlClass.delete(deletename); 208 } 209 210 forEach(objfun: Function, thisArg?: Object) { 211 if (typeof objfun !== 'function') { 212 throw new BusinessError(`Parameter error.The type of ${objfun} must be function`); 213 } 214 let array = this.urlClass.array; 215 if (array.length === 0) { 216 return; 217 } 218 let size = array.length - 1; 219 for (let i = 0; i < size; i += 2) { // 2:Searching for the number and number of keys and values 2 220 let key = array[i]; 221 let value = array[i + 1]; 222 objfun.call(thisArg, value, key, this); 223 } 224 } 225 226 [Symbol.iterator](): Object { 227 return this.urlClass.entries(); 228 } 229 230 updateParams(input: string): void { 231 let out = []; 232 out = parameterProcess(input); 233 this.urlClass.array = out; 234 } 235} 236 237class URLSearchParams { 238 urlClass: NativeURLSearchParams; 239 constructor(input: object | string | Iterable<[]> | null | undefined) { 240 let out: string[] = parameterProcessing(input); 241 this.urlClass = new UrlInterface.URLSearchParams1(); 242 this.urlClass.array = out; 243 this.urlClass.initialNumber = out.length; 244 } 245 append(params1: string, params2: string): void { 246 if (arguments.length === 0 || typeof params1 !== 'string') { 247 throw new BusinessError(`Parameter error.The type of ${params1} must be string`); 248 } 249 if (arguments.length === 1 || typeof params2 !== 'string') { 250 throw new BusinessError(`Parameter error.The type of ${params2} must be string`); 251 } 252 params1 = customEncodeURIComponent(params1); 253 params2 = customEncodeURIComponent(params2); 254 this.urlClass.append(params1, params2); 255 } 256 257 set(setName: string, setValues: string): void { 258 if (arguments.length === 0 || typeof setName !== 'string') { 259 throw new BusinessError(`Parameter error.The type of ${setName} must be string`); 260 } 261 if (arguments.length === 1 || typeof setValues !== 'string') { 262 throw new BusinessError(`Parameter error.The type of ${setValues} must be string`); 263 } 264 setName = customEncodeURIComponent(setName); 265 setValues = customEncodeURIComponent(setValues); 266 this.urlClass.set(setName, setValues); 267 } 268 269 sort(): void { 270 this.urlClass.sort(); 271 } 272 273 has(hasname: string): boolean { 274 if (typeof hasname !== 'string') { 275 throw new BusinessError(`Parameter error.The type of ${hasname} must be string`); 276 } 277 return this.urlClass.has(hasname); 278 } 279 280 toString(): string { 281 let outPut: string = customEncodeForToString(this.urlClass.array[0]) + '=' + customEncodeForToString(this.urlClass.array[1]); 282 let arrayLen: number = this.urlClass.array.length; 283 if (arrayLen % 2 === 0) { 284 let pos: number = 2; 285 for (; pos < arrayLen; pos += 2) { 286 if (pos < this.urlClass.initialNumber) { 287 outPut += '&' + customEncodeForToString(this.urlClass.array[pos]) + '=' + customEncodeForToString(this.urlClass.array[pos + 1]); 288 } else { 289 outPut += '&' + this.urlClass.array[pos] + '=' + this.urlClass.array[pos + 1]; 290 } 291 } 292 } 293 return outPut; 294 } 295 296 keys(): Object { 297 return this.urlClass.keys(); 298 } 299 300 values(): Object { 301 return this.urlClass.values(); 302 } 303 304 getAll(getAllname: string): string[] { 305 return this.urlClass.getAll(getAllname); 306 } 307 308 get(getname: string): string { 309 return this.urlClass.get(getname); 310 } 311 312 entries(): Object { 313 314 return this.urlClass.entries(); 315 } 316 317 delete(deletename: string): void { 318 this.urlClass.delete(deletename); 319 } 320 321 forEach(objfun: Function, thisArg?: Object): void { 322 let array = this.urlClass.array; 323 if (array.length === 0) { 324 return; 325 } 326 327 let size = array.length - 1; 328 for (let i = 0; i < size; i += 2) { // 2:Searching for the number and number of keys and values 2 329 let key = array[i]; 330 let value = array[i + 1]; 331 objfun.call(thisArg, value, key, this); 332 } 333 } 334 335 [Symbol.iterator](): Object { 336 return this.urlClass.entries(); 337 } 338 339 updateParams(input: string) { 340 let out = []; 341 out = parameterProcessing(input); 342 this.urlClass.array = out; 343 } 344} 345 346function toHleString(arg: string | symbol | number) { 347 return arg.toString(); 348} 349 350function parameterProcess(input: object | string | Iterable<[]>) { 351 if (input === null || typeof input === 'undefined') { 352 seachParamsArr = []; 353 return seachParamsArr; 354 } else if (typeof input === 'object' || typeof input === 'function') { 355 return sysObjectParams(input); 356 } else { 357 return initToStringSeachParams(input); 358 } 359} 360 361function parameterProcessing(input: object | string | Iterable<[]>) { 362 if (input === null || typeof input === 'undefined') { 363 seachParamsArr = []; 364 return seachParamsArr; 365 } else if (typeof input === 'object' || typeof input === 'function') { 366 return initObjectSeachParams(input); 367 } else { 368 return initToStringSeachParams(input); 369 } 370} 371 372function sysObjectParams(input: object | Iterable<[]>) { 373 if (typeof input[Symbol.iterator] === 'function') { 374 return iteratorMethodThrow(input as Iterable<[string]>); 375 } 376 return recordMethod(input); 377} 378 379function initObjectSeachParams(input: object | Iterable<[]>) { 380 if (typeof input[Symbol.iterator] === 'function') { 381 return iteratorMethod(input as Iterable<[string]>); 382 } 383 return recordMethod(input); 384} 385 386function recordMethod(input: object) { 387 const keys = Reflect.ownKeys(input); 388 seachParamsArr = []; 389 for (let i = 0; i <= keys.length; i++) { 390 const key = keys[i]; 391 const desc = Reflect.getOwnPropertyDescriptor(input, key); 392 if (desc !== undefined && desc.enumerable) { 393 const typedKey = toHleString(key); 394 const typedValue = toHleString(input[key]); 395 seachParamsArr.push(typedKey, typedValue); 396 } 397 } 398 return seachParamsArr; 399} 400 401function iteratorMethodThrow(input: Iterable<[string]>) { 402 let pairs = []; 403 seachParamsArr = []; 404 for (const pair of input) { 405 if ((typeof pair !== 'object' && typeof pair !== 'function') || pair === null || typeof pair[Symbol.iterator] !== 'function') { 406 throw new BusinessError(`Parameter error.The type of ${input} must be string[][]`); 407 } 408 const convertedPair = []; 409 for (let element of pair) { 410 convertedPair.push(element); 411 } 412 pairs.push(convertedPair); 413 } 414 415 for (const pair of pairs) { 416 if (pair.length !== 2) { // 2:Searching for the number and number of keys and values 2 417 throw new BusinessError(`Parameter error.The type of ${input} must be string[][]`); 418 } 419 seachParamsArr.push(pair[0], pair[1]); 420 } 421 return seachParamsArr; 422} 423 424function iteratorMethod(input: Iterable<[string]>) { 425 let pairs = []; 426 seachParamsArr = []; 427 for (const pair of input) { 428 const convertedPair = []; 429 for (let element of pair) { 430 convertedPair.push(element); 431 } 432 pairs.push(convertedPair); 433 } 434 435 for (const pair of pairs) { 436 if (pair.length !== 2) { // 2:Searching for the number and number of keys and values 2 437 console.error('key-value-is-worong'); 438 } 439 seachParamsArr.push(pair[0], pair[1]); 440 } 441 return seachParamsArr; 442} 443 444function initToStringSeachParams(input: string): Array<string> { 445 if (typeof input !== 'string') { 446 throw new BusinessError(`Parameter error.The type of ${input} must be string`); 447 } 448 if (input[0] === '?') { 449 input = input.slice(1); 450 } 451 let strVal: string = decodeURI(input); 452 seachParamsArr = UrlInterface.stringParmas(strVal); 453 return seachParamsArr; 454} 455 456class URL { 457 href_: string = ''; 458 search_: string = ''; 459 origin_: string = ''; 460 username_: string = ''; 461 password_: string = ''; 462 hostname_: string = ''; 463 host_: string = ''; 464 hash_: string = ''; 465 protocol_: string = ''; 466 pathname_: string = ''; 467 port_: string = ''; 468 searchParamsClass_ !: URLSearchParams; 469 URLParamsClass_ !: URLParams; 470 c_info !: NativeUrl; 471 public constructor() 472 public constructor(inputUrl: string, inputBase?: string | URL) 473 public constructor(inputUrl?: string, inputBase?: string | URL) { 474 if (arguments.length === 0) { 475 } 476 let nativeUrl !: NativeUrl; 477 if (arguments.length === 1) { 478 if (typeof inputUrl === 'string' && inputUrl.length > 0) { 479 nativeUrl = new UrlInterface.Url(inputUrl); 480 } else { 481 console.error('Input parameter error'); 482 } 483 } 484 485 if (arguments.length === 2) { // 2:The number of parameters is 2 486 if (typeof inputUrl === 'string') { 487 if (typeof inputBase === 'string') { 488 if (inputBase.length > 0) { 489 nativeUrl = new UrlInterface.Url(inputUrl, inputBase); 490 } else { 491 console.error('Input parameter error'); 492 return; 493 } 494 } else if (typeof inputBase === 'object') { 495 let nativeBase: NativeUrl = inputBase.getInfo(); 496 nativeUrl = new UrlInterface.Url(inputUrl, nativeBase); 497 } 498 } 499 } 500 if (arguments.length === 1 || arguments.length === 2) { // 2:The number of parameters is 2 501 this.c_info = nativeUrl; 502 if (nativeUrl.onOrOff) { 503 this.search_ = encodeURI(nativeUrl.search); 504 this.username_ = encodeURI(nativeUrl.username); 505 this.password_ = encodeURI(nativeUrl.password); 506 if (nativeUrl.GetIsIpv6) { 507 this.hostname_ = nativeUrl.hostname; 508 this.host_ = nativeUrl.host; 509 } else { 510 this.hostname_ = encodeURI(nativeUrl.hostname); 511 this.host_ = encodeURI(nativeUrl.host); 512 } 513 this.hash_ = encodeURI(nativeUrl.hash); 514 this.protocol_ = encodeURI(nativeUrl.protocol); 515 this.pathname_ = encodeURI(nativeUrl.pathname); 516 this.port_ = nativeUrl.port; 517 this.origin_ = nativeUrl.protocol + '//' + nativeUrl.host; 518 this.searchParamsClass_ = new URLSearchParams(this.search_); 519 this.URLParamsClass_ = new URLParams(this.search_) 520 this.setHref(); 521 } else { 522 console.error('constructor failed'); 523 } 524 } 525 } 526 527 static parseURL(inputUrl: string, inputBase?: string | NativeUrl | URL): URL { 528 if (typeof inputUrl !== 'string') { 529 throw new BusinessError(`Parameter error.The type of ${inputUrl} must be string`); 530 } 531 let nativeUrl !: NativeUrl; 532 if (arguments.length === 1) { 533 nativeUrl = new UrlInterface.Url(inputUrl); 534 } 535 if (arguments.length === 2) { // 2:The number of parameters is 2 536 if (typeof inputBase === 'string') { 537 if (inputBase.length > 0) { 538 nativeUrl = new UrlInterface.Url(inputUrl, inputBase); 539 } else { 540 throw new BusinessError(`Parameter error.The type of ${inputBase} must be string`); 541 } 542 } else if (typeof inputBase === 'object') { 543 let nativeBase: NativeUrl = inputBase.getInfo(); 544 nativeUrl = new UrlInterface.Url(inputUrl, nativeBase); 545 } else { 546 throw new BusinessError(`Parameter error.The type of ${inputBase} must be string or URL`); 547 } 548 } 549 let urlHelper = new URL(); 550 urlHelper.c_info = nativeUrl; 551 if (nativeUrl.onOrOff) { 552 urlHelper.search_ = encodeURI(nativeUrl.search); 553 urlHelper.username_ = encodeURI(nativeUrl.username); 554 urlHelper.password_ = encodeURI(nativeUrl.password); 555 if (nativeUrl.GetIsIpv6) { 556 urlHelper.hostname_ = nativeUrl.hostname; 557 urlHelper.host_ = nativeUrl.host; 558 } else { 559 urlHelper.hostname_ = encodeURI(nativeUrl.hostname); 560 urlHelper.host_ = encodeURI(nativeUrl.host); 561 } 562 urlHelper.hash_ = encodeURI(nativeUrl.hash); 563 urlHelper.protocol_ = encodeURI(nativeUrl.protocol); 564 urlHelper.pathname_ = encodeURI(nativeUrl.pathname); 565 urlHelper.port_ = nativeUrl.port; 566 urlHelper.origin_ = nativeUrl.protocol + '//' + nativeUrl.host; 567 urlHelper.searchParamsClass_ = new URLSearchParams(urlHelper.search_); 568 urlHelper.URLParamsClass_ = new URLParams(urlHelper.search_); 569 urlHelper.setHref(); 570 } else { 571 let err : BusinessError = new BusinessError('Syntax Error. Invalid Url string'); 572 err.code = SyntaxErrorCodeId; 573 throw err; 574 } 575 return urlHelper; 576 } 577 578 getInfo(): NativeUrl { 579 return this.c_info; 580 } 581 toString(): string { 582 return this.href_; 583 } 584 585 get protocol(): string { 586 return this.protocol_; 587 } 588 set protocol(scheme) { 589 if (scheme.length === 0) { 590 return; 591 } 592 if (this.protocol_ === 'file:' && (this.host_ === '' || this.host_ === null)) { 593 return; 594 } 595 this.c_info.protocol = scheme; 596 this.protocol_ = this.c_info.protocol; 597 this.setHref(); 598 } 599 get origin(): string { 600 let kOpaqueOrigin: string = 'null'; 601 switch (this.protocol_) { 602 case 'ftp:': 603 case 'gopher:': 604 case 'http:': 605 case 'https:': 606 case 'ws:': 607 case 'wss:': 608 return this.origin_; 609 } 610 return kOpaqueOrigin; 611 } 612 get username(): string { 613 return this.username_; 614 } 615 set username(input) { 616 if (this.host_ === null || this.host_ === '' || this.protocol_ === 'file:') { 617 return; 618 } 619 const usname_ = encodeURI(input); 620 this.c_info.username = usname_; 621 this.username_ = this.c_info.username; 622 this.setHref(); 623 } 624 get password(): string { 625 return this.password_; 626 } 627 set password(input) { 628 if (this.host_ === null || this.host_ === '' || this.protocol_ === 'file:') { 629 return; 630 } 631 const passwd_ = encodeURI(input); 632 this.c_info.password = passwd_; 633 this.password_ = this.c_info.password; 634 this.setHref(); 635 } 636 get hash(): string { 637 return this.hash_; 638 } 639 set hash(fragment) { 640 const fragment_ = encodeURI(fragment); 641 this.c_info.hash = fragment_; 642 this.hash_ = this.c_info.hash; 643 this.setHref(); 644 } 645 get search(): string { 646 return this.search_; 647 } 648 set search(query) { 649 const query_ = encodeURI(query); 650 this.c_info.search = query_; 651 this.search_ = this.c_info.search; 652 this.searchParamsClass_.updateParams(this.search_); 653 this.URLParamsClass_.updateParams(this.search_); 654 this.setHref(); 655 } 656 get hostname(): string { 657 return this.hostname_; 658 } 659 set hostname(hostname) { 660 this.c_info.hostname = hostname; 661 if (this.c_info.GetIsIpv6) { 662 this.hostname_ = this.c_info.hostname; 663 } else { 664 this.hostname_ = encodeURI(this.c_info.hostname); 665 } 666 this.setHref(); 667 } 668 get host(): string { 669 return this.host_; 670 } 671 set host(host_) { 672 this.c_info.host = host_; 673 if (this.c_info.GetIsIpv6) { 674 this.host_ = this.c_info.host; 675 this.hostname_ = this.c_info.hostname; 676 this.port_ = this.c_info.port; 677 } else { 678 this.host_ = encodeURI(this.c_info.host); 679 this.hostname_ = encodeURI(this.c_info.hostname); 680 this.port_ = this.c_info.port; 681 } 682 this.setHref(); 683 } 684 get port(): string { 685 return this.port_; 686 } 687 set port(port) { 688 if (this.host_ === '' || this.protocol_ === 'file:' || port === '') { 689 return; 690 } 691 this.c_info.port = port; 692 this.port_ = this.c_info.port; 693 this.setHref(); 694 } 695 get href(): string { 696 return this.href_; 697 } 698 set href(href_) { 699 this.c_info.href(href_); 700 if (this.c_info.onOrOff) { 701 this.search_ = encodeURI(this.c_info.search); 702 this.username_ = encodeURI(this.c_info.username); 703 this.password_ = encodeURI(this.c_info.password); 704 if (this.c_info.GetIsIpv6) { 705 this.hostname_ = this.c_info.hostname; 706 this.host_ = this.c_info.host; 707 } else { 708 this.hostname_ = encodeURI(this.c_info.hostname); 709 this.host_ = encodeURI(this.c_info.host); 710 } 711 this.hash_ = encodeURI(this.c_info.hash); 712 this.protocol_ = encodeURI(this.c_info.protocol); 713 this.pathname_ = encodeURI(this.c_info.pathname); 714 this.port_ = this.c_info.port; 715 this.origin_ = this.protocol_ + '//' + this.host_; 716 this.searchParamsClass_.updateParams(this.search_); 717 this.URLParamsClass_.updateParams(this.search_); 718 this.setHref(); 719 } 720 } 721 722 get pathname(): string { 723 return this.pathname_; 724 } 725 set pathname(path) { 726 const path_ = encodeURI(path); 727 this.c_info.pathname = path_; 728 this.pathname_ = this.c_info.pathname; 729 this.setHref(); 730 } 731 732 get searchParams(): URLSearchParams { 733 return this.searchParamsClass_; 734 } 735 736 get params(): URLParams { 737 return this.URLParamsClass_; 738 } 739 740 toJSON(): string { 741 return this.href_; 742 } 743 setHref(): void { 744 let temp: string = this.protocol_; 745 if (this.hostname_ !== '') { 746 temp += '//'; 747 if (this.password_ !== '' || this.username_ !== '') { 748 if (this.username_ !== '') { 749 temp += this.username_; 750 } 751 if (this.password_ !== '') { 752 temp += ':'; 753 temp += this.password_; 754 } 755 temp += '@'; 756 } 757 temp += this.hostname_; 758 if (this.port_ !== '') { 759 temp += ':'; 760 temp += this.port_; 761 } 762 } else if (this.protocol_ === 'file:') { 763 temp += '//'; 764 } 765 temp += this.pathname_; 766 if (this.search_) { 767 temp += this.search_; 768 } 769 if (this.hash_) { 770 temp += this.hash_; 771 } 772 this.href_ = temp; 773 } 774} 775 776export default { 777 URLSearchParams: URLSearchParams, 778 URL: URL, 779 URLParams: URLParams, 780}