script/dom/html/
htmltablecolelement.rs1use dom_struct::dom_struct;
6use html5ever::{LocalName, Prefix, local_name, ns};
7use js::rust::HandleObject;
8use style::attr::{AttrValue, LengthOrPercentageOrAuto};
9
10use crate::dom::attr::Attr;
11use crate::dom::bindings::codegen::Bindings::HTMLTableColElementBinding::HTMLTableColElementMethods;
12use crate::dom::bindings::inheritance::Castable;
13use crate::dom::bindings::root::{DomRoot, LayoutDom};
14use crate::dom::bindings::str::DOMString;
15use crate::dom::document::Document;
16use crate::dom::element::{AttributeMutation, Element, LayoutElementHelpers};
17use crate::dom::html::htmlelement::HTMLElement;
18use crate::dom::node::{Node, NodeDamage};
19use crate::dom::virtualmethods::VirtualMethods;
20use crate::script_runtime::CanGc;
21
22#[dom_struct]
23pub(crate) struct HTMLTableColElement {
24 htmlelement: HTMLElement,
25}
26
27impl HTMLTableColElement {
28 fn new_inherited(
29 local_name: LocalName,
30 prefix: Option<Prefix>,
31 document: &Document,
32 ) -> HTMLTableColElement {
33 HTMLTableColElement {
34 htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
35 }
36 }
37
38 #[cfg_attr(crown, allow(crown::unrooted_must_root))]
39 pub(crate) fn new(
40 local_name: LocalName,
41 prefix: Option<Prefix>,
42 document: &Document,
43 proto: Option<HandleObject>,
44 can_gc: CanGc,
45 ) -> DomRoot<HTMLTableColElement> {
46 let n = Node::reflect_node_with_proto(
47 Box::new(HTMLTableColElement::new_inherited(
48 local_name, prefix, document,
49 )),
50 document,
51 proto,
52 can_gc,
53 );
54
55 n.upcast::<Node>().set_weird_parser_insertion_mode();
56 n
57 }
58}
59
60impl HTMLTableColElementMethods<crate::DomTypeHolder> for HTMLTableColElement {
61 make_uint_getter!(Span, "span", 1);
63 make_clamped_uint_setter!(SetSpan, "span", 1, 1000, 1);
67
68 make_getter!(Width, "width");
70
71 make_dimension_setter!(SetWidth, "width");
73}
74
75pub(crate) trait HTMLTableColElementLayoutHelpers<'dom> {
76 fn get_span(self) -> Option<u32>;
77 fn get_width(self) -> LengthOrPercentageOrAuto;
78}
79
80impl<'dom> HTMLTableColElementLayoutHelpers<'dom> for LayoutDom<'dom, HTMLTableColElement> {
81 fn get_span(self) -> Option<u32> {
82 self.upcast::<Element>()
83 .get_attr_for_layout(&ns!(), &local_name!("span"))
84 .map(AttrValue::as_uint)
85 }
86
87 fn get_width(self) -> LengthOrPercentageOrAuto {
88 self.upcast::<Element>()
89 .get_attr_for_layout(&ns!(), &local_name!("width"))
90 .map(AttrValue::as_dimension)
91 .cloned()
92 .unwrap_or(LengthOrPercentageOrAuto::Auto)
93 }
94}
95
96impl VirtualMethods for HTMLTableColElement {
97 fn super_type(&self) -> Option<&dyn VirtualMethods> {
98 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
99 }
100
101 fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
102 if let Some(super_type) = self.super_type() {
103 super_type.attribute_mutated(attr, mutation, can_gc);
104 }
105
106 if matches!(*attr.local_name(), local_name!("span")) {
107 self.upcast::<Node>().dirty(NodeDamage::Other);
108 }
109 }
110
111 fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
112 match attr.local_name() {
113 &local_name!("width") => true,
114 _ => self
115 .super_type()
116 .unwrap()
117 .attribute_affects_presentational_hints(attr),
118 }
119 }
120
121 fn parse_plain_attribute(&self, local_name: &LocalName, value: DOMString) -> AttrValue {
122 match *local_name {
123 local_name!("span") => {
124 let mut attr = AttrValue::from_u32(value.into(), 1);
125 if let AttrValue::UInt(_, ref mut val) = attr {
126 *val = (*val).clamp(1, 1000);
130 }
131 attr
132 },
133 local_name!("width") => AttrValue::from_dimension(value.into()),
134 _ => self
135 .super_type()
136 .unwrap()
137 .parse_plain_attribute(local_name, value),
138 }
139 }
140}