@@ -647,6 +647,35 @@ function getObjectFromParams(array) {
647
647
return obj ;
648
648
}
649
649
650
+ // Mainly to mitigate func-name-matching ESLint rule
651
+ function defineIDLClass ( proto , classStr , obj ) {
652
+ // https://heycam.github.io/webidl/#dfn-class-string
653
+ Object . defineProperty ( proto , Symbol . toStringTag , {
654
+ writable : false ,
655
+ enumerable : false ,
656
+ configurable : true ,
657
+ value : classStr
658
+ } ) ;
659
+
660
+ // https://heycam.github.io/webidl/#es-operations
661
+ for ( const key of Object . keys ( obj ) ) {
662
+ Object . defineProperty ( proto , key , {
663
+ writable : true ,
664
+ enumerable : true ,
665
+ configurable : true ,
666
+ value : obj [ key ]
667
+ } ) ;
668
+ }
669
+ for ( const key of Object . getOwnPropertySymbols ( obj ) ) {
670
+ Object . defineProperty ( proto , key , {
671
+ writable : true ,
672
+ enumerable : false ,
673
+ configurable : true ,
674
+ value : obj [ key ]
675
+ } ) ;
676
+ }
677
+ }
678
+
650
679
class URLSearchParams {
651
680
constructor ( init = '' ) {
652
681
if ( init instanceof URLSearchParams ) {
@@ -662,11 +691,39 @@ class URLSearchParams {
662
691
this [ context ] = null ;
663
692
}
664
693
665
- get [ Symbol . toStringTag ] ( ) {
666
- return this instanceof URLSearchParams ?
667
- 'URLSearchParams' : 'URLSearchParamsPrototype' ;
694
+ [ util . inspect . custom ] ( recurseTimes , ctx ) {
695
+ if ( ! this || ! ( this instanceof URLSearchParams ) ) {
696
+ throw new TypeError ( 'Value of `this` is not a URLSearchParams' ) ;
697
+ }
698
+
699
+ const separator = ', ' ;
700
+ const innerOpts = Object . assign ( { } , ctx ) ;
701
+ if ( recurseTimes !== null ) {
702
+ innerOpts . depth = recurseTimes - 1 ;
703
+ }
704
+ const innerInspect = ( v ) => util . inspect ( v , innerOpts ) ;
705
+
706
+ const list = this [ searchParams ] ;
707
+ const output = [ ] ;
708
+ for ( var i = 0 ; i < list . length ; i += 2 )
709
+ output . push ( `${ innerInspect ( list [ i ] ) } => ${ innerInspect ( list [ i + 1 ] ) } ` ) ;
710
+
711
+ const colorRe = / \u001b \[ \d \d ? m / g;
712
+ const length = output . reduce (
713
+ ( prev , cur ) => prev + cur . replace ( colorRe , '' ) . length + separator . length ,
714
+ - separator . length
715
+ ) ;
716
+ if ( length > ctx . breakLength ) {
717
+ return `${ this . constructor . name } {\n ${ output . join ( ',\n ' ) } }` ;
718
+ } else if ( output . length ) {
719
+ return `${ this . constructor . name } { ${ output . join ( separator ) } }` ;
720
+ } else {
721
+ return `${ this . constructor . name } {}` ;
722
+ }
668
723
}
724
+ }
669
725
726
+ defineIDLClass ( URLSearchParams . prototype , 'URLSearchParams' , {
670
727
append ( name , value ) {
671
728
if ( ! this || ! ( this instanceof URLSearchParams ) ) {
672
729
throw new TypeError ( 'Value of `this` is not a URLSearchParams' ) ;
@@ -679,7 +736,7 @@ class URLSearchParams {
679
736
value = String ( value ) ;
680
737
this [ searchParams ] . push ( name , value ) ;
681
738
update ( this [ context ] , this ) ;
682
- }
739
+ } ,
683
740
684
741
delete ( name ) {
685
742
if ( ! this || ! ( this instanceof URLSearchParams ) ) {
@@ -700,47 +757,7 @@ class URLSearchParams {
700
757
}
701
758
}
702
759
update ( this [ context ] , this ) ;
703
- }
704
-
705
- set ( name , value ) {
706
- if ( ! this || ! ( this instanceof URLSearchParams ) ) {
707
- throw new TypeError ( 'Value of `this` is not a URLSearchParams' ) ;
708
- }
709
- if ( arguments . length < 2 ) {
710
- throw new TypeError ( '"name" and "value" arguments must be specified' ) ;
711
- }
712
-
713
- const list = this [ searchParams ] ;
714
- name = String ( name ) ;
715
- value = String ( value ) ;
716
-
717
- // If there are any name-value pairs whose name is `name`, in `list`, set
718
- // the value of the first such name-value pair to `value` and remove the
719
- // others.
720
- var found = false ;
721
- for ( var i = 0 ; i < list . length ; ) {
722
- const cur = list [ i ] ;
723
- if ( cur === name ) {
724
- if ( ! found ) {
725
- list [ i + 1 ] = value ;
726
- found = true ;
727
- i += 2 ;
728
- } else {
729
- list . splice ( i , 2 ) ;
730
- }
731
- } else {
732
- i += 2 ;
733
- }
734
- }
735
-
736
- // Otherwise, append a new name-value pair whose name is `name` and value
737
- // is `value`, to `list`.
738
- if ( ! found ) {
739
- list . push ( name , value ) ;
740
- }
741
-
742
- update ( this [ context ] , this ) ;
743
- }
760
+ } ,
744
761
745
762
get ( name ) {
746
763
if ( ! this || ! ( this instanceof URLSearchParams ) ) {
@@ -758,7 +775,7 @@ class URLSearchParams {
758
775
}
759
776
}
760
777
return null ;
761
- }
778
+ } ,
762
779
763
780
getAll ( name ) {
764
781
if ( ! this || ! ( this instanceof URLSearchParams ) ) {
@@ -777,7 +794,7 @@ class URLSearchParams {
777
794
}
778
795
}
779
796
return values ;
780
- }
797
+ } ,
781
798
782
799
has ( name ) {
783
800
if ( ! this || ! ( this instanceof URLSearchParams ) ) {
@@ -795,7 +812,47 @@ class URLSearchParams {
795
812
}
796
813
}
797
814
return false ;
798
- }
815
+ } ,
816
+
817
+ set ( name , value ) {
818
+ if ( ! this || ! ( this instanceof URLSearchParams ) ) {
819
+ throw new TypeError ( 'Value of `this` is not a URLSearchParams' ) ;
820
+ }
821
+ if ( arguments . length < 2 ) {
822
+ throw new TypeError ( '"name" and "value" arguments must be specified' ) ;
823
+ }
824
+
825
+ const list = this [ searchParams ] ;
826
+ name = String ( name ) ;
827
+ value = String ( value ) ;
828
+
829
+ // If there are any name-value pairs whose name is `name`, in `list`, set
830
+ // the value of the first such name-value pair to `value` and remove the
831
+ // others.
832
+ var found = false ;
833
+ for ( var i = 0 ; i < list . length ; ) {
834
+ const cur = list [ i ] ;
835
+ if ( cur === name ) {
836
+ if ( ! found ) {
837
+ list [ i + 1 ] = value ;
838
+ found = true ;
839
+ i += 2 ;
840
+ } else {
841
+ list . splice ( i , 2 ) ;
842
+ }
843
+ } else {
844
+ i += 2 ;
845
+ }
846
+ }
847
+
848
+ // Otherwise, append a new name-value pair whose name is `name` and value
849
+ // is `value`, to `list`.
850
+ if ( ! found ) {
851
+ list . push ( name , value ) ;
852
+ }
853
+
854
+ update ( this [ context ] , this ) ;
855
+ } ,
799
856
800
857
// https://heycam.github.io/webidl/#es-iterators
801
858
// Define entries here rather than [Symbol.iterator] as the function name
@@ -806,7 +863,7 @@ class URLSearchParams {
806
863
}
807
864
808
865
return createSearchParamsIterator ( this , 'key+value' ) ;
809
- }
866
+ } ,
810
867
811
868
forEach ( callback , thisArg = undefined ) {
812
869
if ( ! this || ! ( this instanceof URLSearchParams ) ) {
@@ -827,7 +884,7 @@ class URLSearchParams {
827
884
list = this [ searchParams ] ;
828
885
i += 2 ;
829
886
}
830
- }
887
+ } ,
831
888
832
889
// https://heycam.github.io/webidl/#es-iterable
833
890
keys ( ) {
@@ -836,16 +893,17 @@ class URLSearchParams {
836
893
}
837
894
838
895
return createSearchParamsIterator ( this , 'key' ) ;
839
- }
896
+ } ,
840
897
841
898
values ( ) {
842
899
if ( ! this || ! ( this instanceof URLSearchParams ) ) {
843
900
throw new TypeError ( 'Value of `this` is not a URLSearchParams' ) ;
844
901
}
845
902
846
903
return createSearchParamsIterator ( this , 'value' ) ;
847
- }
904
+ } ,
848
905
906
+ // https://heycam.github.io/webidl/#es-stringifier
849
907
// https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior
850
908
toString ( ) {
851
909
if ( ! this || ! ( this instanceof URLSearchParams ) ) {
@@ -854,37 +912,14 @@ class URLSearchParams {
854
912
855
913
return querystring . stringify ( getObjectFromParams ( this [ searchParams ] ) ) ;
856
914
}
857
- }
858
- // https://heycam.github.io/webidl/#es-iterable-entries
859
- URLSearchParams . prototype [ Symbol . iterator ] = URLSearchParams . prototype . entries ;
860
-
861
- URLSearchParams . prototype [ util . inspect . custom ] =
862
- function inspect ( recurseTimes , ctx ) {
863
- const separator = ', ' ;
864
- const innerOpts = Object . assign ( { } , ctx ) ;
865
- if ( recurseTimes !== null ) {
866
- innerOpts . depth = recurseTimes - 1 ;
867
- }
868
- const innerInspect = ( v ) => util . inspect ( v , innerOpts ) ;
869
-
870
- const list = this [ searchParams ] ;
871
- const output = [ ] ;
872
- for ( var i = 0 ; i < list . length ; i += 2 )
873
- output . push ( `${ innerInspect ( list [ i ] ) } => ${ innerInspect ( list [ i + 1 ] ) } ` ) ;
915
+ } ) ;
874
916
875
- const colorRe = / \u001b \[ \d \d ? m / g;
876
- const length = output . reduce (
877
- ( prev , cur ) => prev + cur . replace ( colorRe , '' ) . length + separator . length ,
878
- - separator . length
879
- ) ;
880
- if ( length > ctx . breakLength ) {
881
- return `${ this . constructor . name } {\n ${ output . join ( ',\n ' ) } }` ;
882
- } else if ( output . length ) {
883
- return `${ this . constructor . name } { ${ output . join ( separator ) } }` ;
884
- } else {
885
- return `${ this . constructor . name } {}` ;
886
- }
887
- } ;
917
+ // https://heycam.github.io/webidl/#es-iterable-entries
918
+ Object . defineProperty ( URLSearchParams . prototype , Symbol . iterator , {
919
+ writable : true ,
920
+ configurable : true ,
921
+ value : URLSearchParams . prototype . entries
922
+ } ) ;
888
923
889
924
// https://heycam.github.io/webidl/#dfn-default-iterator-object
890
925
function createSearchParamsIterator ( target , kind ) {
@@ -898,7 +933,9 @@ function createSearchParamsIterator(target, kind) {
898
933
}
899
934
900
935
// https://heycam.github.io/webidl/#dfn-iterator-prototype-object
901
- const URLSearchParamsIteratorPrototype = Object . setPrototypeOf ( {
936
+ const URLSearchParamsIteratorPrototype = Object . create ( IteratorPrototype ) ;
937
+
938
+ defineIDLClass ( URLSearchParamsIteratorPrototype , 'URLSearchParamsIterator' , {
902
939
next ( ) {
903
940
if ( ! this ||
904
941
Object . getPrototypeOf ( this ) !== URLSearchParamsIteratorPrototype ) {
@@ -937,7 +974,7 @@ const URLSearchParamsIteratorPrototype = Object.setPrototypeOf({
937
974
done : false
938
975
} ;
939
976
} ,
940
- [ util . inspect . custom ] : function inspect ( recurseTimes , ctx ) {
977
+ [ util . inspect . custom ] ( recurseTimes , ctx ) {
941
978
const innerOpts = Object . assign ( { } , ctx ) ;
942
979
if ( recurseTimes !== null ) {
943
980
innerOpts . depth = recurseTimes - 1 ;
@@ -968,15 +1005,6 @@ const URLSearchParamsIteratorPrototype = Object.setPrototypeOf({
968
1005
}
969
1006
return `${ this [ Symbol . toStringTag ] } {${ outputStr } }` ;
970
1007
}
971
- } , IteratorPrototype ) ;
972
-
973
- // Unlike interface and its prototype object, both default iterator object and
974
- // iterator prototype object of an interface have the same class string.
975
- Object . defineProperty ( URLSearchParamsIteratorPrototype , Symbol . toStringTag , {
976
- value : 'URLSearchParamsIterator' ,
977
- writable : false ,
978
- enumerable : false ,
979
- configurable : true
980
1008
} ) ;
981
1009
982
1010
function originFor ( url , base ) {
0 commit comments