script/dom/html/
htmlbaseelement.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
5use dom_struct::dom_struct;
6use html5ever::{LocalName, Prefix, local_name, ns};
7use js::rust::HandleObject;
8use servo_url::ServoUrl;
9
10use crate::dom::attr::Attr;
11use crate::dom::bindings::cell::DomRefCell;
12use crate::dom::bindings::codegen::Bindings::HTMLBaseElementBinding::HTMLBaseElementMethods;
13use crate::dom::bindings::inheritance::Castable;
14use crate::dom::bindings::root::DomRoot;
15use crate::dom::bindings::str::DOMString;
16use crate::dom::document::Document;
17use crate::dom::element::{AttributeMutation, Element};
18use crate::dom::globalscope::GlobalScope;
19use crate::dom::html::htmlelement::HTMLElement;
20use crate::dom::node::{BindContext, Node, NodeTraits, UnbindContext};
21use crate::dom::security::csp::CspReporting;
22use crate::dom::virtualmethods::VirtualMethods;
23use crate::script_runtime::CanGc;
24
25#[dom_struct]
26pub(crate) struct HTMLBaseElement {
27    htmlelement: HTMLElement,
28
29    /// <https://html.spec.whatwg.org/multipage/#frozen-base-url>
30    #[no_trace]
31    frozen_base_url: DomRefCell<Option<ServoUrl>>,
32}
33
34impl HTMLBaseElement {
35    fn new_inherited(
36        local_name: LocalName,
37        prefix: Option<Prefix>,
38        document: &Document,
39    ) -> HTMLBaseElement {
40        HTMLBaseElement {
41            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
42            frozen_base_url: Default::default(),
43        }
44    }
45
46    pub(crate) fn new(
47        local_name: LocalName,
48        prefix: Option<Prefix>,
49        document: &Document,
50        proto: Option<HandleObject>,
51        can_gc: CanGc,
52    ) -> DomRoot<HTMLBaseElement> {
53        Node::reflect_node_with_proto(
54            Box::new(HTMLBaseElement::new_inherited(local_name, prefix, document)),
55            document,
56            proto,
57            can_gc,
58        )
59    }
60
61    pub(crate) fn clear_frozen_base_url(&self) {
62        *self.frozen_base_url.borrow_mut() = None;
63    }
64
65    /// <https://html.spec.whatwg.org/multipage/#set-the-frozen-base-url>
66    pub(crate) fn set_frozen_base_url(&self) {
67        // Step 1. Let document be element's node document.
68        let document = self.owner_document();
69        // Step 2. Let urlRecord be the result of parsing the value of element's href content attribute
70        // with document's fallback base URL, and document's character encoding. (Thus, the base element isn't affected by itself.)
71        let attr = self
72            .upcast::<Element>()
73            .get_attribute(&ns!(), &local_name!("href"));
74        let Some(href_value) = attr.as_ref().map(|attr| attr.value()) else {
75            unreachable!("Must always have a href set when setting frozen base URL");
76        };
77        let document_fallback_url = document.fallback_base_url();
78        let url_record = document_fallback_url.join(&href_value).ok();
79        // Step 3. If any of the following are true:
80        if
81        // urlRecord is failure;
82        url_record.as_ref().is_none_or(|url_record|
83            // urlRecord's scheme is "data" or "javascript"; or
84            url_record.scheme() == "data" || url_record.scheme() == "javascript"
85            // running Is base allowed for Document? on urlRecord and document returns "Blocked",
86            || !document
87                .get_csp_list()
88                .is_base_allowed_for_document(
89                    document.window().upcast::<GlobalScope>(),
90                    &url_record.clone().into_url(),
91                    &document.origin().immutable().clone().into_url_origin(),
92                ))
93        {
94            // then set element's frozen base URL to document's fallback base URL and return.
95            *self.frozen_base_url.borrow_mut() = Some(document_fallback_url);
96            return;
97        }
98        // Step 4. Set element's frozen base URL to urlRecord.
99        *self.frozen_base_url.borrow_mut() = url_record;
100        // Step 5. Respond to base URL changes given document.
101        // TODO
102    }
103
104    /// <https://html.spec.whatwg.org/multipage/#frozen-base-url>
105    pub(crate) fn frozen_base_url(&self) -> ServoUrl {
106        self.frozen_base_url
107            .borrow()
108            .clone()
109            .expect("Must only retrieve frozen base URL for valid base elements")
110    }
111}
112
113impl HTMLBaseElementMethods<crate::DomTypeHolder> for HTMLBaseElement {
114    /// <https://html.spec.whatwg.org/multipage/#dom-base-href>
115    fn Href(&self) -> DOMString {
116        // Step 1. Let document be element's node document.
117        let document = self.owner_document();
118
119        // Step 2. Let url be the value of the href attribute of this element, if it has one, and the empty string otherwise.
120        let attr = self
121            .upcast::<Element>()
122            .get_attribute(&ns!(), &local_name!("href"));
123        let value = attr.as_ref().map(|attr| attr.value());
124        let url = value.as_ref().map_or("", |value| &**value);
125
126        // Step 3. Let urlRecord be the result of parsing url with document's fallback base URL,
127        // and document's character encoding. (Thus, the base element isn't affected by other base elements or itself.)
128        let url_record = document.fallback_base_url().join(url);
129
130        match url_record {
131            Err(_) => {
132                // Step 4. If urlRecord is failure, return url.
133                url.into()
134            },
135            Ok(url_record) => {
136                // Step 5. Return the serialization of urlRecord.
137                url_record.into_string().into()
138            },
139        }
140    }
141
142    // https://html.spec.whatwg.org/multipage/#dom-base-href
143    make_setter!(SetHref, "href");
144}
145
146impl VirtualMethods for HTMLBaseElement {
147    fn super_type(&self) -> Option<&dyn VirtualMethods> {
148        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
149    }
150
151    fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
152        self.super_type()
153            .unwrap()
154            .attribute_mutated(attr, mutation, can_gc);
155
156        // https://html.spec.whatwg.org/multipage/#frozen-base-url
157        if *attr.local_name() == local_name!("href") {
158            // > The base element is the first base element in tree order with an href content attribute in its Document,
159            // > and its href content attribute is changed.
160            if self.frozen_base_url.borrow().is_some() && !mutation.is_removal() {
161                self.set_frozen_base_url();
162            } else {
163                // > The base element becomes the first base element in tree order with an href content attribute in its Document.
164                let document = self.owner_document();
165                document.refresh_base_element();
166            }
167        }
168    }
169
170    fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
171        self.super_type().unwrap().bind_to_tree(context, can_gc);
172        // https://html.spec.whatwg.org/multipage/#frozen-base-url
173        // > The base element becomes the first base element in tree order with an href content attribute in its Document.
174        let document = self.owner_document();
175        document.refresh_base_element();
176    }
177
178    fn unbind_from_tree(&self, context: &UnbindContext, can_gc: CanGc) {
179        self.super_type().unwrap().unbind_from_tree(context, can_gc);
180        // https://html.spec.whatwg.org/multipage/#frozen-base-url
181        // > The base element becomes the first base element in tree order with an href content attribute in its Document.
182        let document = self.owner_document();
183        document.refresh_base_element();
184    }
185}