@@ -18,8 +18,9 @@ mod tokenizer;
18
18
pub use error:: Error ;
19
19
use url:: Url ;
20
20
21
- use crate :: canonicalize_and_process:: is_special_scheme;
22
21
use crate :: canonicalize_and_process:: special_scheme_default_port;
22
+ use crate :: canonicalize_and_process:: ProcessType ;
23
+ use crate :: canonicalize_and_process:: { is_special_scheme, process_base_url} ;
23
24
use crate :: component:: Component ;
24
25
use crate :: regexp:: RegExp ;
25
26
@@ -55,7 +56,7 @@ impl UrlPatternInit {
55
56
#[ allow( clippy:: too_many_arguments) ]
56
57
fn process (
57
58
& self ,
58
- kind : canonicalize_and_process :: ProcessType ,
59
+ kind : ProcessType ,
59
60
protocol : Option < String > ,
60
61
username : Option < String > ,
61
62
password : Option < String > ,
@@ -78,18 +79,84 @@ impl UrlPatternInit {
78
79
} ;
79
80
80
81
let base_url = if let Some ( parsed_base_url) = & self . base_url {
81
- // TODO: check if these are correct
82
- result. protocol = Some ( parsed_base_url. scheme ( ) . to_string ( ) ) ;
83
- result. username = Some ( parsed_base_url. username ( ) . to_string ( ) ) ;
84
- result. password =
85
- Some ( parsed_base_url. password ( ) . unwrap_or_default ( ) . to_string ( ) ) ;
86
- result. hostname =
87
- Some ( parsed_base_url. host_str ( ) . unwrap_or_default ( ) . to_string ( ) ) ;
88
- result. port = Some ( url:: quirks:: port ( parsed_base_url) . to_string ( ) ) ;
89
- result. pathname =
90
- Some ( url:: quirks:: pathname ( parsed_base_url) . to_string ( ) ) ;
91
- result. search = Some ( parsed_base_url. query ( ) . unwrap_or ( "" ) . to_string ( ) ) ;
92
- result. hash = Some ( parsed_base_url. fragment ( ) . unwrap_or ( "" ) . to_string ( ) ) ;
82
+ if self . protocol . is_none ( ) {
83
+ result. protocol =
84
+ Some ( process_base_url ( parsed_base_url. scheme ( ) , & kind) ) ;
85
+ }
86
+
87
+ if kind != ProcessType :: Pattern
88
+ && ( self . protocol . is_none ( )
89
+ && self . hostname . is_none ( )
90
+ && self . port . is_none ( )
91
+ && self . username . is_none ( ) )
92
+ {
93
+ result. username =
94
+ Some ( process_base_url ( parsed_base_url. username ( ) , & kind) ) ;
95
+ }
96
+
97
+ if kind != ProcessType :: Pattern
98
+ && ( self . protocol . is_none ( )
99
+ && self . hostname . is_none ( )
100
+ && self . port . is_none ( )
101
+ && self . username . is_none ( )
102
+ && self . password . is_none ( ) )
103
+ {
104
+ result. password = Some ( process_base_url (
105
+ parsed_base_url. password ( ) . unwrap_or_default ( ) ,
106
+ & kind,
107
+ ) ) ;
108
+ }
109
+
110
+ if self . protocol . is_none ( ) && self . hostname . is_none ( ) {
111
+ result. hostname = Some ( process_base_url (
112
+ parsed_base_url. host_str ( ) . unwrap_or_default ( ) ,
113
+ & kind,
114
+ ) ) ;
115
+ }
116
+
117
+ if self . protocol . is_none ( )
118
+ && self . hostname . is_none ( )
119
+ && self . port . is_none ( )
120
+ {
121
+ result. port =
122
+ Some ( process_base_url ( url:: quirks:: port ( parsed_base_url) , & kind) ) ;
123
+ }
124
+
125
+ if self . protocol . is_none ( )
126
+ && self . hostname . is_none ( )
127
+ && self . port . is_none ( )
128
+ && self . pathname . is_none ( )
129
+ {
130
+ result. pathname = Some ( process_base_url (
131
+ url:: quirks:: pathname ( parsed_base_url) ,
132
+ & kind,
133
+ ) ) ;
134
+ }
135
+
136
+ if self . protocol . is_none ( )
137
+ && self . hostname . is_none ( )
138
+ && self . port . is_none ( )
139
+ && self . pathname . is_none ( )
140
+ && self . search . is_none ( )
141
+ {
142
+ result. search = Some ( process_base_url (
143
+ parsed_base_url. query ( ) . unwrap_or_default ( ) ,
144
+ & kind,
145
+ ) ) ;
146
+ }
147
+
148
+ if self . protocol . is_none ( )
149
+ && self . hostname . is_none ( )
150
+ && self . port . is_none ( )
151
+ && self . pathname . is_none ( )
152
+ && self . search . is_none ( )
153
+ && self . hash . is_none ( )
154
+ {
155
+ result. hash = Some ( process_base_url (
156
+ parsed_base_url. fragment ( ) . unwrap_or_default ( ) ,
157
+ & kind,
158
+ ) ) ;
159
+ }
93
160
94
161
Some ( parsed_base_url)
95
162
} else {
@@ -235,7 +302,7 @@ impl<R: RegExp> UrlPattern<R> {
235
302
report_regex_errors : bool ,
236
303
) -> Result < Self , Error > {
237
304
let mut processed_init = init. process (
238
- canonicalize_and_process :: ProcessType :: Pattern ,
305
+ ProcessType :: Pattern ,
239
306
None ,
240
307
None ,
241
308
None ,
@@ -413,7 +480,7 @@ impl<R: RegExp> UrlPattern<R> {
413
480
& self ,
414
481
input : UrlPatternMatchInput ,
415
482
) -> Result < Option < UrlPatternResult > , Error > {
416
- let input = match crate :: quirks:: parse_match_input ( input) {
483
+ let input = match quirks:: parse_match_input ( input) {
417
484
Some ( input) => input,
418
485
None => return Ok ( None ) ,
419
486
} ;
@@ -591,9 +658,9 @@ mod tests {
591
658
592
659
fn test_case ( case : TestCase ) {
593
660
let input = case. pattern . first ( ) . cloned ( ) ;
594
- let mut base_url = case. pattern . get ( 1 ) . map ( |input| match input {
595
- StringOrInit :: String ( str) => str. clone ( ) ,
596
- StringOrInit :: Init ( _) => unreachable ! ( ) ,
661
+ let mut base_url = case. pattern . get ( 1 ) . and_then ( |input| match input {
662
+ StringOrInit :: String ( str) => Some ( str. clone ( ) ) ,
663
+ StringOrInit :: Init ( _) => None ,
597
664
} ) ;
598
665
599
666
println ! ( "\n =====" ) ;
@@ -664,7 +731,48 @@ mod tests {
664
731
} ) = & input
665
732
{
666
733
expected = Some ( $field. to_owned( ) )
667
- } else if let Some ( base_url) = & base_url {
734
+ } else if {
735
+ if let StringOrInit :: Init ( init) = & input {
736
+ match stringify!( $field) {
737
+ "protocol" => false ,
738
+ "hostname" => init. protocol. is_some( ) ,
739
+ "port" => init. protocol. is_some( ) || init. hostname. is_some( ) ,
740
+ "username" => false ,
741
+ "password" => false ,
742
+ "pathname" => {
743
+ init. protocol. is_some( )
744
+ || init. hostname. is_some( )
745
+ || init. port. is_some( )
746
+ }
747
+ "search" => {
748
+ init. protocol. is_some( )
749
+ || init. hostname. is_some( )
750
+ || init. port. is_some( )
751
+ || init. pathname. is_some( )
752
+ }
753
+ "hash" => {
754
+ init. protocol. is_some( )
755
+ || init. hostname. is_some( )
756
+ || init. port. is_some( )
757
+ || init. pathname. is_some( )
758
+ || init. search. is_some( )
759
+ }
760
+ _ => unreachable!( ) ,
761
+ }
762
+ } else {
763
+ false
764
+ }
765
+ } {
766
+ expected = Some ( "*" . to_owned( ) )
767
+ } else if let Some ( base_url) =
768
+ base_url. as_ref( ) . and_then( |base_url| {
769
+ if !matches!( stringify!( $field) , "username" | "password" ) {
770
+ Some ( base_url)
771
+ } else {
772
+ None
773
+ }
774
+ } )
775
+ {
668
776
let base_url = Url :: parse( base_url) . unwrap( ) ;
669
777
let field = url:: quirks:: $field( & base_url) ;
670
778
let field: String = match stringify!( $field) {
@@ -684,8 +792,8 @@ mod tests {
684
792
let pattern = & pattern. $field. pattern_string;
685
793
686
794
assert_eq!(
687
- pattern,
688
795
& expected,
796
+ pattern,
689
797
"pattern for {} does not match" ,
690
798
stringify!( $field)
691
799
) ;
0 commit comments