Skip to main content

script/
links.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Defines shared hyperlink behaviour for `<link>`, `<a>`, `<area>` and `<form>` elements.
6
7use html5ever::local_name;
8use js::context::JSContext;
9use malloc_size_of::malloc_size_of_is_0;
10use net_traits::request::Referrer;
11use servo_constellation_traits::{LoadData, LoadOrigin, NavigationHistoryBehavior};
12use style::str::HTML_SPACE_CHARACTERS;
13
14use crate::dom::bindings::inheritance::Castable;
15use crate::dom::bindings::refcounted::Trusted;
16use crate::dom::bindings::str::DOMString;
17use crate::dom::element::referrer_policy_for_element;
18use crate::dom::html::htmlanchorelement::HTMLAnchorElement;
19use crate::dom::html::htmlareaelement::HTMLAreaElement;
20use crate::dom::html::htmlformelement::HTMLFormElement;
21use crate::dom::html::htmllinkelement::HTMLLinkElement;
22use crate::dom::node::NodeTraits;
23use crate::dom::types::Element;
24use crate::navigation::navigate;
25
26bitflags::bitflags! {
27    /// Describes the different relations that can be specified on elements using the `rel`
28    /// attribute.
29    ///
30    /// Refer to <https://html.spec.whatwg.org/multipage/#linkTypes> for more information.
31    #[derive(Clone, Copy, Debug, PartialEq)]
32    pub(crate) struct LinkRelations: u32 {
33        /// <https://html.spec.whatwg.org/multipage/#rel-alternate>
34        const ALTERNATE = 1;
35
36        /// <https://html.spec.whatwg.org/multipage/#link-type-author>
37        const AUTHOR = 1 << 1;
38
39        /// <https://html.spec.whatwg.org/multipage/#link-type-bookmark>
40        const BOOKMARK = 1 << 2;
41
42        /// <https://html.spec.whatwg.org/multipage/#link-type-canonical>
43        const CANONICAL = 1 << 3;
44
45        /// <https://html.spec.whatwg.org/multipage/#link-type-dns-prefetch>
46        const DNS_PREFETCH = 1 << 4;
47
48        /// <https://html.spec.whatwg.org/multipage/#link-type-expect>
49        const EXPECT = 1 << 5;
50
51        /// <https://html.spec.whatwg.org/multipage/#link-type-external>
52        const EXTERNAL = 1 << 6;
53
54        /// <https://html.spec.whatwg.org/multipage/#link-type-help>
55        const HELP = 1 << 7;
56
57        /// <https://html.spec.whatwg.org/multipage/#rel-icon>
58        const ICON = 1 << 8;
59
60        /// <https://html.spec.whatwg.org/multipage/#link-type-license>
61        const LICENSE = 1 << 9;
62
63        /// <https://html.spec.whatwg.org/multipage/#link-type-next>
64        const NEXT = 1 << 10;
65
66        /// <https://html.spec.whatwg.org/multipage/#link-type-manifest>
67        const MANIFEST = 1 << 11;
68
69        /// <https://html.spec.whatwg.org/multipage/#link-type-modulepreload>
70        const MODULE_PRELOAD = 1 << 12;
71
72        /// <https://html.spec.whatwg.org/multipage/#link-type-nofollow>
73        const NO_FOLLOW = 1 << 13;
74
75        /// <https://html.spec.whatwg.org/multipage/#link-type-noopener>
76        const NO_OPENER = 1 << 14;
77
78        /// <https://html.spec.whatwg.org/multipage/#link-type-noreferrer>
79        const NO_REFERRER = 1 << 15;
80
81        /// <https://html.spec.whatwg.org/multipage/#link-type-opener>
82        const OPENER = 1 << 16;
83
84        /// <https://html.spec.whatwg.org/multipage/#link-type-pingback>
85        const PING_BACK = 1 << 17;
86
87        /// <https://html.spec.whatwg.org/multipage/#link-type-preconnect>
88        const PRECONNECT = 1 << 18;
89
90        /// <https://html.spec.whatwg.org/multipage/#link-type-prefetch>
91        const PREFETCH = 1 << 19;
92
93        /// <https://html.spec.whatwg.org/multipage/#link-type-preload>
94        const PRELOAD = 1 << 20;
95
96        /// <https://html.spec.whatwg.org/multipage/#link-type-prev>
97        const PREV = 1 << 21;
98
99        /// <https://html.spec.whatwg.org/multipage/#link-type-privacy-policy>
100        const PRIVACY_POLICY = 1 << 22;
101
102        /// <https://html.spec.whatwg.org/multipage/#link-type-search>
103        const SEARCH = 1 << 23;
104
105        /// <https://html.spec.whatwg.org/multipage/#link-type-stylesheet>
106        const STYLESHEET = 1 << 24;
107
108        /// <https://html.spec.whatwg.org/multipage/#link-type-tag>
109        const TAG = 1 << 25;
110
111        /// <https://html.spec.whatwg.org/multipage/#link-type-terms-of-service>
112        const TERMS_OF_SERVICE = 1 << 26;
113    }
114}
115
116impl LinkRelations {
117    /// The set of allowed relations for [`<link>`] elements
118    ///
119    /// [`<link>`]: https://html.spec.whatwg.org/multipage/#htmllinkelement
120    pub(crate) const ALLOWED_LINK_RELATIONS: Self = Self::ALTERNATE
121        .union(Self::CANONICAL)
122        .union(Self::AUTHOR)
123        .union(Self::DNS_PREFETCH)
124        .union(Self::EXPECT)
125        .union(Self::HELP)
126        .union(Self::ICON)
127        .union(Self::MANIFEST)
128        .union(Self::MODULE_PRELOAD)
129        .union(Self::LICENSE)
130        .union(Self::NEXT)
131        .union(Self::PING_BACK)
132        .union(Self::PRECONNECT)
133        .union(Self::PREFETCH)
134        .union(Self::PRELOAD)
135        .union(Self::PREV)
136        .union(Self::PRIVACY_POLICY)
137        .union(Self::SEARCH)
138        .union(Self::STYLESHEET)
139        .union(Self::TERMS_OF_SERVICE);
140
141    /// The set of allowed relations for [`<a>`] and [`<area>`] elements
142    ///
143    /// [`<a>`]: https://html.spec.whatwg.org/multipage/#the-a-element
144    /// [`<area>`]: https://html.spec.whatwg.org/multipage/#the-area-element
145    pub(crate) const ALLOWED_ANCHOR_OR_AREA_RELATIONS: Self = Self::ALTERNATE
146        .union(Self::AUTHOR)
147        .union(Self::BOOKMARK)
148        .union(Self::EXTERNAL)
149        .union(Self::HELP)
150        .union(Self::LICENSE)
151        .union(Self::NEXT)
152        .union(Self::NO_FOLLOW)
153        .union(Self::NO_OPENER)
154        .union(Self::NO_REFERRER)
155        .union(Self::OPENER)
156        .union(Self::PREV)
157        .union(Self::PRIVACY_POLICY)
158        .union(Self::SEARCH)
159        .union(Self::TAG)
160        .union(Self::TERMS_OF_SERVICE);
161
162    /// The set of allowed relations for [`<form>`] elements
163    ///
164    /// [`<form>`]: https://html.spec.whatwg.org/multipage/#the-form-element
165    pub(crate) const ALLOWED_FORM_RELATIONS: Self = Self::EXTERNAL
166        .union(Self::HELP)
167        .union(Self::LICENSE)
168        .union(Self::NEXT)
169        .union(Self::NO_FOLLOW)
170        .union(Self::NO_OPENER)
171        .union(Self::NO_REFERRER)
172        .union(Self::OPENER)
173        .union(Self::PREV)
174        .union(Self::SEARCH);
175
176    /// Compute the set of relations for an element given its `"rel"` attribute
177    ///
178    /// This function should only be used with [`<link>`], [`<a>`], [`<area>`] and [`<form>`] elements.
179    ///
180    /// [`<link>`]: https://html.spec.whatwg.org/multipage/#htmllinkelement
181    /// [`<a>`]: https://html.spec.whatwg.org/multipage/#the-a-element
182    /// [`<area>`]: https://html.spec.whatwg.org/multipage/#the-area-element
183    /// [`<form>`]: https://html.spec.whatwg.org/multipage/#the-form-element
184    pub(crate) fn for_element(element: &Element) -> Self {
185        let rel = element.get_attribute_string_value(&local_name!("rel"));
186
187        let mut relations = rel
188            .map(|attribute| {
189                attribute
190                    .split(HTML_SPACE_CHARACTERS)
191                    .map(Self::from_single_keyword)
192                    .collect()
193            })
194            .unwrap_or(Self::empty());
195
196        // For historical reasons, "rev=made" is treated as if the "author" relation was specified
197        let has_legacy_author_relation = element
198            .get_attribute_string_value(&local_name!("rev"))
199            .is_some_and(|rev| rev == "made");
200        if has_legacy_author_relation {
201            relations |= Self::AUTHOR;
202        }
203
204        let allowed_relations = if element.is::<HTMLLinkElement>() {
205            Self::ALLOWED_LINK_RELATIONS
206        } else if element.is::<HTMLAnchorElement>() || element.is::<HTMLAreaElement>() {
207            Self::ALLOWED_ANCHOR_OR_AREA_RELATIONS
208        } else if element.is::<HTMLFormElement>() {
209            Self::ALLOWED_FORM_RELATIONS
210        } else {
211            Self::empty()
212        };
213
214        relations & allowed_relations
215    }
216
217    /// Parse one single link relation keyword
218    ///
219    /// If the keyword is invalid then `Self::empty()` is returned.
220    fn from_single_keyword(keyword: &str) -> Self {
221        if keyword.eq_ignore_ascii_case("alternate") {
222            Self::ALTERNATE
223        } else if keyword.eq_ignore_ascii_case("canonical") {
224            Self::CANONICAL
225        } else if keyword.eq_ignore_ascii_case("author") {
226            Self::AUTHOR
227        } else if keyword.eq_ignore_ascii_case("bookmark") {
228            Self::BOOKMARK
229        } else if keyword.eq_ignore_ascii_case("dns-prefetch") {
230            Self::DNS_PREFETCH
231        } else if keyword.eq_ignore_ascii_case("expect") {
232            Self::EXPECT
233        } else if keyword.eq_ignore_ascii_case("external") {
234            Self::EXTERNAL
235        } else if keyword.eq_ignore_ascii_case("help") {
236            Self::HELP
237        } else if keyword.eq_ignore_ascii_case("icon") ||
238            keyword.eq_ignore_ascii_case("shortcut icon") ||
239            keyword.eq_ignore_ascii_case("apple-touch-icon")
240        {
241            // TODO: "apple-touch-icon" is not in the spec. Where did it come from? Do we need it?
242            //       There is also "apple-touch-icon-precomposed" listed in
243            //       https://github.com/servo/servo/blob/e43e4778421be8ea30db9d5c553780c042161522/components/script/dom/htmllinkelement.rs#L452-L467
244            Self::ICON
245        } else if keyword.eq_ignore_ascii_case("manifest") {
246            Self::MANIFEST
247        } else if keyword.eq_ignore_ascii_case("modulepreload") {
248            Self::MODULE_PRELOAD
249        } else if keyword.eq_ignore_ascii_case("license") ||
250            keyword.eq_ignore_ascii_case("copyright")
251        {
252            Self::LICENSE
253        } else if keyword.eq_ignore_ascii_case("next") {
254            Self::NEXT
255        } else if keyword.eq_ignore_ascii_case("nofollow") {
256            Self::NO_FOLLOW
257        } else if keyword.eq_ignore_ascii_case("noopener") {
258            Self::NO_OPENER
259        } else if keyword.eq_ignore_ascii_case("noreferrer") {
260            Self::NO_REFERRER
261        } else if keyword.eq_ignore_ascii_case("opener") {
262            Self::OPENER
263        } else if keyword.eq_ignore_ascii_case("pingback") {
264            Self::PING_BACK
265        } else if keyword.eq_ignore_ascii_case("preconnect") {
266            Self::PRECONNECT
267        } else if keyword.eq_ignore_ascii_case("prefetch") {
268            Self::PREFETCH
269        } else if keyword.eq_ignore_ascii_case("preload") {
270            Self::PRELOAD
271        } else if keyword.eq_ignore_ascii_case("prev") || keyword.eq_ignore_ascii_case("previous") {
272            Self::PREV
273        } else if keyword.eq_ignore_ascii_case("privacy-policy") {
274            Self::PRIVACY_POLICY
275        } else if keyword.eq_ignore_ascii_case("search") {
276            Self::SEARCH
277        } else if keyword.eq_ignore_ascii_case("stylesheet") {
278            Self::STYLESHEET
279        } else if keyword.eq_ignore_ascii_case("tag") {
280            Self::TAG
281        } else if keyword.eq_ignore_ascii_case("terms-of-service") {
282            Self::TERMS_OF_SERVICE
283        } else {
284            Self::empty()
285        }
286    }
287
288    /// <https://html.spec.whatwg.org/multipage/#get-an-element%27s-noopener>
289    pub(crate) fn get_element_noopener(&self, target_attribute_value: Option<&DOMString>) -> bool {
290        // Step 1. If element's link types include the noopener or noreferrer keyword, then return true.
291        if self.contains(Self::NO_OPENER) || self.contains(Self::NO_REFERRER) {
292            return true;
293        }
294
295        // Step 2. If element's link types do not include the opener keyword and
296        //         target is an ASCII case-insensitive match for "_blank", then return true.
297        let target_is_blank =
298            target_attribute_value.is_some_and(|target| target.to_ascii_lowercase() == "_blank");
299        if !self.contains(Self::OPENER) && target_is_blank {
300            return true;
301        }
302
303        // Step 3. Return false.
304        false
305    }
306}
307
308malloc_size_of_is_0!(LinkRelations);
309
310/// <https://html.spec.whatwg.org/multipage/#valid-navigable-target-name>
311fn valid_navigable_target_name(target: &DOMString) -> bool {
312    // > A valid navigable target name is any string with at least one character that does not contain both
313    // > an ASCII tab or newline and a U+003C (<), and it does not start with a U+005F (_).
314    // > (Names starting with a U+005F (_) are reserved for special keywords.)
315    if target.is_empty() {
316        return false;
317    }
318    if target.contains_tab_or_newline() && target.contains("\u{003C}") {
319        return false;
320    }
321    if target.starts_with('\u{005F}') {
322        return false;
323    }
324    true
325}
326
327/// <https://html.spec.whatwg.org/multipage/#valid-navigable-target-name-or-keyword>
328pub(crate) fn valid_navigable_target_name_or_keyword(target: &DOMString) -> bool {
329    // > A valid navigable target name or keyword is any string that is either a valid navigable target name
330    // > or that is an ASCII case-insensitive match for one of: _blank, _self, _parent, or _top.
331    if valid_navigable_target_name(target) {
332        return true;
333    }
334    let target = target.to_ascii_lowercase();
335    target == "_blank" || target == "_self" || target == "_parent" || target == "_top"
336}
337
338/// <https://html.spec.whatwg.org/multipage/#get-an-element%27s-target>
339pub(crate) fn get_element_target(
340    subject: &Element,
341    target: Option<DOMString>,
342) -> Option<DOMString> {
343    assert!(
344        subject.is::<HTMLAreaElement>() ||
345            subject.is::<HTMLAnchorElement>() ||
346            subject.is::<HTMLFormElement>()
347    );
348
349    // Step 1. If target is null, then:
350    let target = target.or_else(|| {
351        // Step 1.1. If element has a target attribute, then set target to that attribute's value.
352        //
353        // Note that for a target attribute to be valid, it must be a valid navigable target name
354        // or keyword
355        let element_target = subject.get_string_attribute(&local_name!("target"));
356        if valid_navigable_target_name_or_keyword(&element_target) {
357            Some(element_target)
358        } else {
359            // Step 1.2. Otherwise, if element's node document contains a base element with a target attribute,
360            // set target to the value of the target attribute of the first such base element.
361            subject
362                .owner_document()
363                .target_base_element()
364                .and_then(|base_element| {
365                    let element = base_element.upcast::<Element>();
366                    if element.has_attribute(&local_name!("target")) {
367                        Some(element.get_string_attribute(&local_name!("target")))
368                    } else {
369                        None
370                    }
371                })
372        }
373    });
374    // Step 2. If target is not null, and contains an ASCII tab or newline and a U+003C (<), then set target to "_blank".
375    if let Some(ref target) = target &&
376        target.contains_tab_or_newline() &&
377        target.contains("\u{003C}")
378    {
379        return Some("_blank".into());
380    }
381    // Step 3. Return target.
382    target
383}
384
385/// <https://html.spec.whatwg.org/multipage/#following-hyperlinks-2>
386pub(crate) fn follow_hyperlink(
387    cx: &mut JSContext,
388    subject: &Element,
389    relations: LinkRelations,
390    hyperlink_suffix: Option<String>,
391) {
392    // Step 1: If subject cannot navigate, then return.
393    if subject.cannot_navigate() {
394        return;
395    }
396
397    // Step 2: Let targetAttributeValue be the empty string.
398    // This is done below.
399
400    // Step 3: If subject is an a or area element, then set targetAttributeValue to the
401    //         result of getting an element's target given subject.
402    //
403    // Also allow the user to open links in a new WebView by pressing either the meta or
404    // control key (depending on the platform).
405    let document = subject.owner_document();
406    let target_attribute_value =
407        if subject.is::<HTMLAreaElement>() || subject.is::<HTMLAnchorElement>() {
408            if document
409                .event_handler()
410                .alternate_action_keyboard_modifier_active()
411            {
412                Some("_blank".into())
413            } else {
414                get_element_target(subject, None)
415            }
416        } else {
417            None
418        };
419
420    // Step 4: Let urlRecord be the result of encoding-parsing a URL given subject's href
421    //         attribute value, relative to subject's node document.
422    // Step 5: If urlRecord is failure, then return.
423    // TODO: Implement this.
424
425    // Step 6: Let noopener be the result of getting an element's noopener with subject,
426    //         urlRecord, and targetAttributeValue.
427    let noopener = relations.get_element_noopener(target_attribute_value.as_ref());
428
429    // Step 7: Let targetNavigable be the first return value of applying the rules for
430    //         choosing a navigable given targetAttributeValue, subject's node navigable, and
431    //         noopener.
432    let window = document.window();
433    let source = document.browsing_context().unwrap();
434    let (maybe_chosen, history_handling) = match target_attribute_value {
435        Some(name) => {
436            let (maybe_chosen, new) = source.choose_browsing_context(cx, name, noopener);
437            let history_handling = if new {
438                NavigationHistoryBehavior::Replace
439            } else {
440                NavigationHistoryBehavior::Push
441            };
442            (maybe_chosen, history_handling)
443        },
444        None => (Some(window.window_proxy()), NavigationHistoryBehavior::Push),
445    };
446
447    // Step 8: If targetNavigable is null, then return.
448    let chosen = match maybe_chosen {
449        Some(proxy) => proxy,
450        None => return,
451    };
452
453    if let Some(target_document) = chosen.document() {
454        let target_window = target_document.window();
455        // Step 9: Let urlString be the result of applying the URL serializer to urlRecord.
456        // TODO: Implement this.
457
458        let mut href = subject
459            .get_attribute_string_value(&local_name!("href"))
460            .unwrap();
461
462        // Step 10: If hyperlinkSuffix is non-null, then append it to urlString.
463        if let Some(suffix) = hyperlink_suffix {
464            href.push_str(&suffix);
465        }
466        let Ok(url) = document.encoding_parse_a_url(&href) else {
467            return;
468        };
469
470        // Step 11: Let referrerPolicy be the current state of subject's referrerpolicy content attribute.
471        let referrer_policy = referrer_policy_for_element(subject);
472
473        // Step 12: If subject's link types includes the noreferrer keyword, then set
474        //          referrerPolicy to "no-referrer".
475        let referrer = if relations.contains(LinkRelations::NO_REFERRER) {
476            Referrer::NoReferrer
477        } else {
478            target_window.as_global_scope().get_referrer()
479        };
480
481        // Step 13: Navigate targetNavigable to urlString using subject's node document,
482        //          with referrerPolicy set to referrerPolicy, userInvolvement set to
483        //          userInvolvement, and sourceElement set to subject.
484        let secure = target_window.as_global_scope().is_secure_context();
485        let load_data = LoadData::new(
486            LoadOrigin::Script(document.origin().snapshot()),
487            url,
488            document.about_base_url(),
489            Some(window.pipeline_id()),
490            referrer,
491            referrer_policy,
492            Some(secure),
493            Some(document.insecure_requests_policy()),
494            document.has_trustworthy_ancestor_origin(),
495            document.creation_sandboxing_flag_set_considering_parent_iframe(),
496        );
497        let target = Trusted::new(target_window);
498        let task = task!(navigate_follow_hyperlink: move |cx| {
499            debug!("following hyperlink to {}", load_data.url);
500            navigate(cx, &target.root(), history_handling, false, load_data);
501        });
502        target_document
503            .owner_global()
504            .task_manager()
505            .dom_manipulation_task_source()
506            .queue(task);
507    };
508}