script/dom/html/
htmlmetaelement.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 std::str::FromStr;
6
7use compositing_traits::viewport_description::ViewportDescription;
8use dom_struct::dom_struct;
9use html5ever::{LocalName, Prefix, local_name, ns};
10use js::rust::HandleObject;
11use servo_config::pref;
12use style::str::HTML_SPACE_CHARACTERS;
13
14use crate::dom::attr::Attr;
15use crate::dom::bindings::codegen::Bindings::HTMLMetaElementBinding::HTMLMetaElementMethods;
16use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
17use crate::dom::bindings::inheritance::Castable;
18use crate::dom::bindings::root::DomRoot;
19use crate::dom::bindings::str::DOMString;
20use crate::dom::document::{Document, determine_policy_for_token};
21use crate::dom::element::{AttributeMutation, Element};
22use crate::dom::html::htmlelement::HTMLElement;
23use crate::dom::html::htmlheadelement::HTMLHeadElement;
24use crate::dom::node::{BindContext, Node, NodeTraits, UnbindContext};
25use crate::dom::virtualmethods::VirtualMethods;
26use crate::script_runtime::CanGc;
27
28#[dom_struct]
29pub(crate) struct HTMLMetaElement {
30    htmlelement: HTMLElement,
31}
32
33impl HTMLMetaElement {
34    fn new_inherited(
35        local_name: LocalName,
36        prefix: Option<Prefix>,
37        document: &Document,
38    ) -> HTMLMetaElement {
39        HTMLMetaElement {
40            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
41        }
42    }
43
44    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
45    pub(crate) fn new(
46        local_name: LocalName,
47        prefix: Option<Prefix>,
48        document: &Document,
49        proto: Option<HandleObject>,
50        can_gc: CanGc,
51    ) -> DomRoot<HTMLMetaElement> {
52        Node::reflect_node_with_proto(
53            Box::new(HTMLMetaElement::new_inherited(local_name, prefix, document)),
54            document,
55            proto,
56            can_gc,
57        )
58    }
59
60    fn process_attributes(&self) {
61        let element = self.upcast::<Element>();
62        if let Some(ref name) = element.get_name() {
63            let name = name.to_ascii_lowercase();
64            let name = name.trim_matches(HTML_SPACE_CHARACTERS);
65            if name == "referrer" {
66                self.apply_referrer();
67            }
68            if name == "viewport" {
69                self.parse_and_send_viewport_if_necessary();
70            }
71        // https://html.spec.whatwg.org/multipage/#attr-meta-http-equiv
72        } else if !self.HttpEquiv().is_empty() {
73            // TODO: Implement additional http-equiv candidates
74            match self.HttpEquiv().to_ascii_lowercase().as_str() {
75                "refresh" => self.declarative_refresh(),
76                "content-security-policy" => self.apply_csp_list(),
77                _ => {},
78            }
79        }
80    }
81
82    fn process_referrer_attribute(&self) {
83        let element = self.upcast::<Element>();
84        if let Some(ref name) = element.get_name() {
85            let name = name.to_ascii_lowercase();
86            let name = name.trim_matches(HTML_SPACE_CHARACTERS);
87
88            if name == "referrer" {
89                self.apply_referrer();
90            }
91        }
92    }
93
94    /// <https://html.spec.whatwg.org/multipage/#meta-referrer>
95    fn apply_referrer(&self) {
96        let doc = self.owner_document();
97        // From spec: For historical reasons, unlike other standard metadata names, the processing model for referrer
98        // is not responsive to element removals, and does not use tree order. Only the most-recently-inserted or
99        // most-recently-modified meta element in this state has an effect.
100        // 1. If element is not in a document tree, then return.
101        let meta_node = self.upcast::<Node>();
102        if !meta_node.is_in_a_document_tree() {
103            return;
104        }
105
106        // 2. If element does not have a name attribute whose value is an ASCII case-insensitive match for "referrer",
107        // then return.
108        if self.upcast::<Element>().get_name() != Some(atom!("referrer")) {
109            return;
110        }
111
112        // 3. If element does not have a content attribute, or that attribute's value is the empty string, then return.
113        let content = self
114            .upcast::<Element>()
115            .get_attribute(&ns!(), &local_name!("content"));
116        if let Some(attr) = content {
117            let attr = attr.value();
118            let attr_val = attr.trim();
119            if !attr_val.is_empty() {
120                doc.set_referrer_policy(determine_policy_for_token(attr_val));
121            }
122        }
123    }
124
125    /// <https://drafts.csswg.org/css-viewport/#parsing-algorithm>
126    fn parse_and_send_viewport_if_necessary(&self) {
127        if !pref!(viewport_meta_enabled) {
128            return;
129        }
130
131        // Skip processing if this isn't the top level frame
132        if !self.owner_window().is_top_level() {
133            return;
134        }
135        let element = self.upcast::<Element>();
136        let Some(content) = element.get_attribute(&ns!(), &local_name!("content")) else {
137            return;
138        };
139
140        if let Ok(viewport) = ViewportDescription::from_str(&content.value()) {
141            self.owner_window()
142                .compositor_api()
143                .viewport(self.owner_window().webview_id(), viewport);
144        }
145    }
146
147    /// <https://html.spec.whatwg.org/multipage/#attr-meta-http-equiv-content-security-policy>
148    fn apply_csp_list(&self) {
149        if let Some(parent) = self.upcast::<Node>().GetParentElement() {
150            if let Some(head) = parent.downcast::<HTMLHeadElement>() {
151                head.set_content_security_policy();
152            }
153        }
154    }
155
156    /// <https://html.spec.whatwg.org/multipage/#shared-declarative-refresh-steps>
157    fn declarative_refresh(&self) {
158        if !self.upcast::<Node>().is_in_a_document_tree() {
159            return;
160        }
161
162        // 2
163        let content = self.Content();
164        // 1
165        if !content.is_empty() {
166            // 3
167            self.owner_document()
168                .shared_declarative_refresh_steps(content.as_bytes());
169        }
170    }
171}
172
173impl HTMLMetaElementMethods<crate::DomTypeHolder> for HTMLMetaElement {
174    // https://html.spec.whatwg.org/multipage/#dom-meta-name
175    make_getter!(Name, "name");
176
177    // https://html.spec.whatwg.org/multipage/#dom-meta-name
178    make_atomic_setter!(SetName, "name");
179
180    // https://html.spec.whatwg.org/multipage/#dom-meta-content
181    make_getter!(Content, "content");
182
183    // https://html.spec.whatwg.org/multipage/#dom-meta-content
184    make_setter!(SetContent, "content");
185
186    // https://html.spec.whatwg.org/multipage/#dom-meta-httpequiv
187    make_getter!(HttpEquiv, "http-equiv");
188    // https://html.spec.whatwg.org/multipage/#dom-meta-httpequiv
189    make_atomic_setter!(SetHttpEquiv, "http-equiv");
190
191    // https://html.spec.whatwg.org/multipage/#dom-meta-scheme
192    make_getter!(Scheme, "scheme");
193    // https://html.spec.whatwg.org/multipage/#dom-meta-scheme
194    make_setter!(SetScheme, "scheme");
195}
196
197impl VirtualMethods for HTMLMetaElement {
198    fn super_type(&self) -> Option<&dyn VirtualMethods> {
199        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
200    }
201
202    fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
203        if let Some(s) = self.super_type() {
204            s.bind_to_tree(context, can_gc);
205        }
206
207        if context.tree_connected {
208            self.process_attributes();
209        }
210    }
211
212    fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
213        if let Some(s) = self.super_type() {
214            s.attribute_mutated(attr, mutation, can_gc);
215        }
216
217        self.process_referrer_attribute();
218    }
219
220    fn unbind_from_tree(&self, context: &UnbindContext, can_gc: CanGc) {
221        if let Some(s) = self.super_type() {
222            s.unbind_from_tree(context, can_gc);
223        }
224
225        if context.tree_connected {
226            self.process_referrer_attribute();
227        }
228    }
229}