aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/urlpattern.rs
blob: c811d3a9a7017128ee7725c90660f9af87b6a329 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use dom_struct::dom_struct;
use js::rust::HandleObject;
use script_bindings::codegen::GenericUnionTypes::USVStringOrURLPatternInit;
use script_bindings::error::{Error, Fallible};
use script_bindings::reflector::Reflector;
use script_bindings::root::DomRoot;
use script_bindings::script_runtime::CanGc;
use script_bindings::str::USVString;

use crate::dom::bindings::codegen::Bindings::URLPatternBinding;
use crate::dom::bindings::codegen::Bindings::URLPatternBinding::URLPatternMethods;
use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
use crate::dom::globalscope::GlobalScope;

/// <https://urlpattern.spec.whatwg.org/#urlpattern>
#[dom_struct]
pub(crate) struct URLPattern {
    reflector: Reflector,

    /// <https://urlpattern.spec.whatwg.org/#urlpattern-associated-url-pattern>
    #[no_trace]
    associated_url_pattern: urlpattern::UrlPattern,
}

impl URLPattern {
    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
    fn new_inherited(associated_url_pattern: urlpattern::UrlPattern) -> URLPattern {
        URLPattern {
            reflector: Reflector::new(),
            associated_url_pattern,
        }
    }

    /// <https://urlpattern.spec.whatwg.org/#urlpattern-initialize>
    pub(crate) fn initialize(
        global: &GlobalScope,
        proto: Option<HandleObject>,
        input: USVStringOrURLPatternInit,
        base_url: Option<USVString>,
        options: &URLPatternBinding::URLPatternOptions,
        can_gc: CanGc,
    ) -> Fallible<DomRoot<URLPattern>> {
        // The section below converts from servos types to the types used in the urlpattern crate
        let base_url = base_url.map(|usv_string| usv_string.0);
        let input = bindings_to_third_party::map_urlpattern_input(input, base_url.clone());
        let options = urlpattern::UrlPatternOptions {
            ignore_case: options.ignoreCase,
        };

        // Parse and initialize the URL pattern.
        let pattern_init =
            urlpattern::quirks::process_construct_pattern_input(input, base_url.as_deref())
                .map_err(|error| Error::Type(format!("{error}")))?;

        let pattern = urlpattern::UrlPattern::parse(pattern_init, options)
            .map_err(|error| Error::Type(format!("{error}")))?;

        let url_pattern = reflect_dom_object_with_proto(
            Box::new(URLPattern::new_inherited(pattern)),
            global,
            proto,
            can_gc,
        );
        Ok(url_pattern)
    }
}

impl URLPatternMethods<crate::DomTypeHolder> for URLPattern {
    // <https://urlpattern.spec.whatwg.org/#dom-urlpattern-urlpattern>
    fn Constructor(
        global: &GlobalScope,
        proto: Option<HandleObject>,
        can_gc: CanGc,
        input: USVStringOrURLPatternInit,
        base_url: USVString,
        options: &URLPatternBinding::URLPatternOptions,
    ) -> Fallible<DomRoot<URLPattern>> {
        URLPattern::initialize(global, proto, input, Some(base_url), options, can_gc)
    }

    /// <https://urlpattern.spec.whatwg.org/#dom-urlpattern-urlpattern-input-options>
    fn Constructor_(
        global: &GlobalScope,
        proto: Option<HandleObject>,
        can_gc: CanGc,
        input: USVStringOrURLPatternInit,
        options: &URLPatternBinding::URLPatternOptions,
    ) -> Fallible<DomRoot<URLPattern>> {
        // Step 1. Run initialize given this, input, null, and options.
        URLPattern::initialize(global, proto, input, None, options, can_gc)
    }

    /// <https://urlpattern.spec.whatwg.org/#dom-urlpattern-protocol>
    fn Protocol(&self) -> USVString {
        // Step 1. Return this’s associated URL pattern’s protocol component’s pattern string.
        USVString(self.associated_url_pattern.protocol().to_owned())
    }

    /// <https://urlpattern.spec.whatwg.org/#dom-urlpattern-username>
    fn Username(&self) -> USVString {
        // Step 1. Return this’s associated URL pattern’s username component’s pattern string.
        USVString(self.associated_url_pattern.username().to_owned())
    }

    /// <https://urlpattern.spec.whatwg.org/#dom-urlpattern-password>
    fn Password(&self) -> USVString {
        // Step 1. Return this’s associated URL pattern’s password component’s pattern string.
        USVString(self.associated_url_pattern.password().to_owned())
    }

    /// <https://urlpattern.spec.whatwg.org/#dom-urlpattern-hostname>
    fn Hostname(&self) -> USVString {
        // Step 1. Return this’s associated URL pattern’s hostname component’s pattern string.
        USVString(self.associated_url_pattern.hostname().to_owned())
    }

    /// <https://urlpattern.spec.whatwg.org/#dom-urlpattern-port>
    fn Port(&self) -> USVString {
        // Step 1. Return this’s associated URL pattern’s port component’s pattern string.
        USVString(self.associated_url_pattern.port().to_owned())
    }

    /// <https://urlpattern.spec.whatwg.org/#dom-urlpattern-pathname>
    fn Pathname(&self) -> USVString {
        // Step 1. Return this’s associated URL pattern’s pathname component’s pattern string.
        USVString(self.associated_url_pattern.pathname().to_owned())
    }

    /// <https://urlpattern.spec.whatwg.org/#dom-urlpattern-search>
    fn Search(&self) -> USVString {
        // Step 1. Return this’s associated URL pattern’s search component’s pattern string.
        USVString(self.associated_url_pattern.search().to_owned())
    }

    /// <https://urlpattern.spec.whatwg.org/#dom-urlpattern-hash>
    fn Hash(&self) -> USVString {
        // Step 1. Return this’s associated URL pattern’s hash component’s pattern string.
        USVString(self.associated_url_pattern.hash().to_owned())
    }

    /// <https://urlpattern.spec.whatwg.org/#dom-urlpattern-hasregexpgroups>
    fn HasRegExpGroups(&self) -> bool {
        // Step 1. If this’s associated URL pattern’s has regexp groups, then return true.
        // Step 2. Return false.
        self.associated_url_pattern.has_regexp_groups()
    }
}

mod bindings_to_third_party {
    use crate::dom::urlpattern::USVStringOrURLPatternInit;

    pub(super) fn map_urlpattern_input(
        input: USVStringOrURLPatternInit,
        base_url: Option<String>,
    ) -> urlpattern::quirks::StringOrInit {
        match input {
            USVStringOrURLPatternInit::USVString(usv_string) => {
                urlpattern::quirks::StringOrInit::String(usv_string.0)
            },
            USVStringOrURLPatternInit::URLPatternInit(pattern_init) => {
                let pattern_init = urlpattern::quirks::UrlPatternInit {
                    protocol: pattern_init
                        .protocol
                        .as_ref()
                        .map(|usv_string| usv_string.to_string()),
                    username: pattern_init
                        .username
                        .as_ref()
                        .map(|usv_string| usv_string.to_string()),
                    password: pattern_init
                        .password
                        .as_ref()
                        .map(|usv_string| usv_string.to_string()),
                    hostname: pattern_init
                        .hostname
                        .as_ref()
                        .map(|usv_string| usv_string.to_string()),
                    port: pattern_init
                        .port
                        .as_ref()
                        .map(|usv_string| usv_string.to_string()),
                    pathname: pattern_init
                        .pathname
                        .as_ref()
                        .map(|usv_string| usv_string.to_string()),
                    search: pattern_init
                        .search
                        .as_ref()
                        .map(|usv_string| usv_string.to_string()),
                    hash: pattern_init
                        .hash
                        .as_ref()
                        .map(|usv_string| usv_string.to_string()),
                    base_url,
                };
                urlpattern::quirks::StringOrInit::Init(pattern_init)
            },
        }
    }
}