mirror of
https://github.com/servo/servo.git
synced 2025-06-08 00:23:30 +00:00
132 lines
4 KiB
Rust
132 lines
4 KiB
Rust
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
use dom_struct::dom_struct;
|
|
use html5ever::{LocalName, Prefix};
|
|
use js::rust::HandleObject;
|
|
use style::str::HTML_SPACE_CHARACTERS;
|
|
|
|
use crate::dom::attr::Attr;
|
|
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::{AttributeMutation, Element};
|
|
use crate::dom::htmlelement::HTMLElement;
|
|
use crate::dom::htmlheadelement::HTMLHeadElement;
|
|
use crate::dom::node::{BindContext, Node, UnbindContext};
|
|
use crate::dom::virtualmethods::VirtualMethods;
|
|
|
|
#[dom_struct]
|
|
pub 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),
|
|
}
|
|
}
|
|
|
|
#[allow(unrooted_must_root)]
|
|
pub fn new(
|
|
local_name: LocalName,
|
|
prefix: Option<Prefix>,
|
|
document: &Document,
|
|
proto: Option<HandleObject>,
|
|
) -> DomRoot<HTMLMetaElement> {
|
|
Node::reflect_node_with_proto(
|
|
Box::new(HTMLMetaElement::new_inherited(local_name, prefix, document)),
|
|
document,
|
|
proto,
|
|
)
|
|
}
|
|
|
|
fn process_attributes(&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 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();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <https://html.spec.whatwg.org/multipage/#meta-referrer>
|
|
fn apply_referrer(&self) {
|
|
if let Some(parent) = self.upcast::<Node>().GetParentElement() {
|
|
if let Some(head) = parent.downcast::<HTMLHeadElement>() {
|
|
head.set_document_referrer();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl HTMLMetaElementMethods for HTMLMetaElement {
|
|
// https://html.spec.whatwg.org/multipage/#dom-meta-name
|
|
make_getter!(Name, "name");
|
|
|
|
// https://html.spec.whatwg.org/multipage/#dom-meta-name
|
|
make_atomic_setter!(SetName, "name");
|
|
|
|
// https://html.spec.whatwg.org/multipage/#dom-meta-content
|
|
make_getter!(Content, "content");
|
|
|
|
// https://html.spec.whatwg.org/multipage/#dom-meta-content
|
|
make_setter!(SetContent, "content");
|
|
}
|
|
|
|
impl VirtualMethods for HTMLMetaElement {
|
|
fn super_type(&self) -> Option<&dyn VirtualMethods> {
|
|
Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
|
|
}
|
|
|
|
fn bind_to_tree(&self, context: &BindContext) {
|
|
if let Some(ref s) = self.super_type() {
|
|
s.bind_to_tree(context);
|
|
}
|
|
|
|
if context.tree_connected {
|
|
self.process_attributes();
|
|
}
|
|
}
|
|
|
|
fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation) {
|
|
if let Some(s) = self.super_type() {
|
|
s.attribute_mutated(attr, mutation);
|
|
}
|
|
|
|
self.process_referrer_attribute();
|
|
}
|
|
|
|
fn unbind_from_tree(&self, context: &UnbindContext) {
|
|
if let Some(ref s) = self.super_type() {
|
|
s.unbind_from_tree(context);
|
|
}
|
|
|
|
if context.tree_connected {
|
|
self.process_referrer_attribute();
|
|
}
|
|
}
|
|
}
|