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::context::JSContext;
10use js::rust::HandleObject;
11
12use crate::dom::bindings::codegen::Bindings::HTMLTitleElementBinding::HTMLTitleElementMethods;
13use crate::dom::bindings::inheritance::Castable;
14use crate::dom::bindings::root::DomRoot;
15use crate::dom::bindings::str::DOMString;
16use crate::dom::document::Document;
17use crate::dom::html::htmlelement::HTMLElement;
18use crate::dom::node::{BindContext, ChildrenMutation, Node};
19use crate::dom::virtualmethods::VirtualMethods;
20use crate::script_runtime::CanGc;
21
22#[dom_struct]
23pub(crate) struct HTMLTitleElement {
24    htmlelement: HTMLElement,
25    popped: Cell<bool>,
26}
27
28impl HTMLTitleElement {
29    fn new_inherited(
30        local_name: LocalName,
31        prefix: Option<Prefix>,
32        document: &Document,
33    ) -> HTMLTitleElement {
34        HTMLTitleElement {
35            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
36            popped: Cell::new(false),
37        }
38    }
39
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, cx: &mut JSContext, value: DOMString) {
73        self.upcast::<Node>()
74            .set_text_content_for_element(cx, Some(value))
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, cx: &mut JSContext, mutation: &ChildrenMutation) {
84        if let Some(s) = self.super_type() {
85            s.children_changed(cx, 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, cx: &mut JSContext, context: &BindContext) {
96        if let Some(s) = self.super_type() {
97            s.bind_to_tree(cx, context);
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}