script/dom/html/
htmltitleelement.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::cell::Cell;
6
7use dom_struct::dom_struct;
8use html5ever::{LocalName, Prefix};
9use js::rust::HandleObject;
10
11use crate::dom::bindings::codegen::Bindings::HTMLTitleElementBinding::HTMLTitleElementMethods;
12use crate::dom::bindings::inheritance::Castable;
13use crate::dom::bindings::root::DomRoot;
14use crate::dom::bindings::str::DOMString;
15use crate::dom::document::Document;
16use crate::dom::html::htmlelement::HTMLElement;
17use crate::dom::node::{BindContext, ChildrenMutation, Node};
18use crate::dom::virtualmethods::VirtualMethods;
19use crate::script_runtime::CanGc;
20
21#[dom_struct]
22pub(crate) struct HTMLTitleElement {
23    htmlelement: HTMLElement,
24    popped: Cell<bool>,
25}
26
27impl HTMLTitleElement {
28    fn new_inherited(
29        local_name: LocalName,
30        prefix: Option<Prefix>,
31        document: &Document,
32    ) -> HTMLTitleElement {
33        HTMLTitleElement {
34            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
35            popped: Cell::new(false),
36        }
37    }
38
39    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
40    pub(crate) fn new(
41        local_name: LocalName,
42        prefix: Option<Prefix>,
43        document: &Document,
44        proto: Option<HandleObject>,
45        can_gc: CanGc,
46    ) -> DomRoot<HTMLTitleElement> {
47        Node::reflect_node_with_proto(
48            Box::new(HTMLTitleElement::new_inherited(
49                local_name, prefix, document,
50            )),
51            document,
52            proto,
53            can_gc,
54        )
55    }
56
57    fn notify_title_changed(&self) {
58        let node = self.upcast::<Node>();
59        if node.is_in_a_document_tree() {
60            node.owner_doc().title_changed();
61        }
62    }
63}
64
65impl HTMLTitleElementMethods<crate::DomTypeHolder> for HTMLTitleElement {
66    // https://html.spec.whatwg.org/multipage/#dom-title-text
67    fn Text(&self) -> DOMString {
68        self.upcast::<Node>().child_text_content()
69    }
70
71    // https://html.spec.whatwg.org/multipage/#dom-title-text
72    fn SetText(&self, value: DOMString, can_gc: CanGc) {
73        self.upcast::<Node>()
74            .set_text_content_for_element(Some(value), can_gc)
75    }
76}
77
78impl VirtualMethods for HTMLTitleElement {
79    fn super_type(&self) -> Option<&dyn VirtualMethods> {
80        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
81    }
82
83    fn children_changed(&self, mutation: &ChildrenMutation) {
84        if let Some(s) = self.super_type() {
85            s.children_changed(mutation);
86        }
87
88        // Notify of title changes only after the initial full parsing
89        // of the element.
90        if self.popped.get() {
91            self.notify_title_changed();
92        }
93    }
94
95    fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
96        if let Some(s) = self.super_type() {
97            s.bind_to_tree(context, can_gc);
98        }
99        let node = self.upcast::<Node>();
100        if context.tree_is_in_a_document_tree {
101            node.owner_doc().title_changed();
102        }
103    }
104
105    fn pop(&self) {
106        if let Some(s) = self.super_type() {
107            s.pop();
108        }
109
110        self.popped.set(true);
111
112        // Initial notification of title change, once the full text
113        // is available.
114        self.notify_title_changed();
115    }
116}