use std::str::FromStr;
use content_security_policy::{Policy, PolicyDisposition, PolicySource};
use dom_struct::dom_struct;
use html5ever::{LocalName, Prefix, local_name};
use js::context::JSContext;
use js::rust::HandleObject;
use net_traits::ReferrerPolicy;
use paint_api::viewport_description::ViewportDescription;
use servo_config::pref;
use style::str::HTML_SPACE_CHARACTERS;
use crate::dom::bindings::codegen::Bindings::HTMLMetaElementBinding::HTMLMetaElementMethods;
use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::document::Document;
use crate::dom::element::attributes::storage::AttrRef;
use crate::dom::element::{AttributeMutation, Element};
use crate::dom::html::htmlelement::HTMLElement;
use crate::dom::html::htmlheadelement::HTMLHeadElement;
use crate::dom::node::virtualmethods::VirtualMethods;
use crate::dom::node::{BindContext, Node, NodeTraits, UnbindContext};
#[dom_struct]
pub(crate) struct HTMLMetaElement {
htmlelement: HTMLElement,
}
impl HTMLMetaElement {
fn new_inherited(
local_name: LocalName,
prefix: Option<Prefix>,
document: &Document,
) -> HTMLMetaElement {
HTMLMetaElement {
htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
}
}
pub(crate) fn new(
cx: &mut js::context::JSContext,
local_name: LocalName,
prefix: Option<Prefix>,
document: &Document,
proto: Option<HandleObject>,
) -> DomRoot<HTMLMetaElement> {
Node::reflect_node_with_proto(
cx,
Box::new(HTMLMetaElement::new_inherited(local_name, prefix, document)),
document,
proto,
)
}
fn process_attributes(&self, cx: &mut JSContext) {
let element = self.upcast::<Element>();
if let Some(ref name) = element.get_name() {
let name = name.to_ascii_lowercase();
let name = name.trim_matches(HTML_SPACE_CHARACTERS);
if name == "referrer" {
self.apply_referrer();
}
if name == "viewport" {
self.parse_and_send_viewport_if_necessary(cx);
}
} else if !self.HttpEquiv().is_empty() {
match self.HttpEquiv().to_ascii_lowercase().as_str() {
"refresh" => self.declarative_refresh(),
"content-security-policy" => self.apply_csp_list(),
_ => {},
}
}
}
fn process_referrer_attribute(&self) {
let element = self.upcast::<Element>();
if let Some(ref name) = element.get_name() {
let name = name.to_ascii_lowercase();
let name = name.trim_matches(HTML_SPACE_CHARACTERS);
if name == "referrer" {
self.apply_referrer();
}
}
}
fn apply_referrer(&self) {
let doc = self.owner_document();
let meta_node = self.upcast::<Node>();
if !meta_node.is_in_a_document_tree() {
return;
}
if self.upcast::<Element>().get_name() != Some(atom!("referrer")) {
return;
}
if let Some(content) = self
.upcast::<Element>()
.get_attribute_string_value(&local_name!("content"))
.filter(|value| !value.is_empty())
{
doc.set_referrer_policy(ReferrerPolicy::from_with_legacy(&content));
}
}
fn parse_and_send_viewport_if_necessary(&self, cx: &mut JSContext) {
if !pref!(viewport_meta_enabled) {
return;
}
if !self.owner_window().is_top_level() {
return;
}
let element = self.upcast::<Element>();
let Some(content) = element.get_attribute_string_value(&local_name!("content")) else {
return;
};
if let Ok(viewport) = ViewportDescription::from_str(&content) {
let initial_scale = viewport.initial_scale.get();
let window = self.owner_window();
window.paint_api().viewport(window.webview_id(), viewport);
window
.get_or_init_visual_viewport(cx)
.update_scale(initial_scale);
}
}
fn apply_csp_list(&self) {
if self
.upcast::<Node>()
.GetParentElement()
.is_none_or(|parent| !parent.is::<HTMLHeadElement>())
{
return;
};
let Some(content) = self
.upcast::<Element>()
.get_attribute_string_value(&local_name!("content"))
else {
return;
};
if content.is_empty() {
return;
}
let mut policy = Policy::parse(&content, PolicySource::Meta, PolicyDisposition::Enforce);
policy.directive_set.retain(|directive| {
!matches!(
directive.name.as_str(),
"report-uri" | "frame-ancestors" | "sandbox"
)
});
self.owner_document().enforce_csp_policy(policy);
}
fn declarative_refresh(&self) {
if !self.upcast::<Node>().is_in_a_document_tree() {
return;
}
let content = self.Content();
if !content.is_empty() {
self.owner_document().shared_declarative_refresh_steps(
&content.as_bytes(),
true,
);
}
}
}
impl HTMLMetaElementMethods<crate::DomTypeHolder> for HTMLMetaElement {
make_getter!(Name, "name");
make_atomic_setter!(SetName, "name");
make_getter!(Content, "content");
make_setter!(SetContent, "content");
make_getter!(HttpEquiv, "http-equiv");
make_atomic_setter!(SetHttpEquiv, "http-equiv");
make_getter!(Scheme, "scheme");
make_setter!(SetScheme, "scheme");
}
impl VirtualMethods for HTMLMetaElement {
fn super_type(&self) -> Option<&dyn VirtualMethods> {
Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
}
fn bind_to_tree(&self, cx: &mut JSContext, context: &BindContext) {
if let Some(s) = self.super_type() {
s.bind_to_tree(cx, context);
}
if context.tree_connected {
self.process_attributes(cx);
}
}
fn attribute_mutated(
&self,
cx: &mut js::context::JSContext,
attr: AttrRef<'_>,
mutation: AttributeMutation,
) {
if let Some(s) = self.super_type() {
s.attribute_mutated(cx, attr, mutation);
}
self.process_referrer_attribute();
}
fn unbind_from_tree(&self, cx: &mut js::context::JSContext, context: &UnbindContext) {
if let Some(s) = self.super_type() {
s.unbind_from_tree(cx, context);
}
if context.tree_connected {
self.process_referrer_attribute();
}
}
}