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