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