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