Serenity Operating System
1/*
2 * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibWeb/Bindings/Intrinsics.h>
8#include <LibWeb/CSS/Parser/Parser.h>
9#include <LibWeb/HTML/HTMLTableCellElement.h>
10#include <LibWeb/HTML/Parser/HTMLParser.h>
11
12namespace Web::HTML {
13
14HTMLTableCellElement::HTMLTableCellElement(DOM::Document& document, DOM::QualifiedName qualified_name)
15 : HTMLElement(document, move(qualified_name))
16{
17}
18
19HTMLTableCellElement::~HTMLTableCellElement() = default;
20
21JS::ThrowCompletionOr<void> HTMLTableCellElement::initialize(JS::Realm& realm)
22{
23 MUST_OR_THROW_OOM(Base::initialize(realm));
24 set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLTableCellElementPrototype>(realm, "HTMLTableCellElement"));
25
26 return {};
27}
28
29void HTMLTableCellElement::apply_presentational_hints(CSS::StyleProperties& style) const
30{
31 for_each_attribute([&](auto& name, auto& value) {
32 if (name == HTML::AttributeNames::bgcolor) {
33 auto color = Color::from_string(value);
34 if (color.has_value())
35 style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value()));
36 return;
37 }
38 if (name == HTML::AttributeNames::align) {
39 if (value.equals_ignoring_ascii_case("center"sv) || value.equals_ignoring_ascii_case("middle"sv)) {
40 style.set_property(CSS::PropertyID::TextAlign, CSS::IdentifierStyleValue::create(CSS::ValueID::LibwebCenter));
41 } else {
42 if (auto parsed_value = parse_css_value(CSS::Parser::ParsingContext { document() }, value.view(), CSS::PropertyID::TextAlign))
43 style.set_property(CSS::PropertyID::TextAlign, parsed_value.release_nonnull());
44 }
45 return;
46 }
47 if (name == HTML::AttributeNames::width) {
48 if (auto parsed_value = parse_nonzero_dimension_value(value))
49 style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
50 return;
51 } else if (name == HTML::AttributeNames::height) {
52 if (auto parsed_value = parse_nonzero_dimension_value(value))
53 style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
54 return;
55 }
56 });
57}
58
59unsigned int HTMLTableCellElement::col_span() const
60{
61 return attribute(HTML::AttributeNames::colspan).to_uint().value_or(1);
62}
63
64void HTMLTableCellElement::set_col_span(unsigned int value)
65{
66 MUST(set_attribute(HTML::AttributeNames::colspan, DeprecatedString::number(value)));
67}
68
69unsigned int HTMLTableCellElement::row_span() const
70{
71 return attribute(HTML::AttributeNames::rowspan).to_uint().value_or(1);
72}
73
74void HTMLTableCellElement::set_row_span(unsigned int value)
75{
76 MUST(set_attribute(HTML::AttributeNames::rowspan, DeprecatedString::number(value)));
77}
78
79Optional<ARIA::Role> HTMLTableCellElement::default_role() const
80{
81 // TODO: For td:
82 // role=cell if the ancestor table element is exposed as a role=table
83 // role=gridcell if the ancestor table element is exposed as a role=grid or treegrid
84 // No corresponding role if the ancestor table element is not exposed as a role=table, grid or treegrid
85 // For th:
86 // role=columnheader, rowheader or cell if the ancestor table element is exposed as a role=table
87 // role=columnheader, rowheader or gridcell if the ancestor table element is exposed as a role=grid or treegrid
88 // No corresponding role if the ancestor table element is not exposed as a role=table, grid or treegrid
89 // https://www.w3.org/TR/html-aria/#el-td
90 // https://www.w3.org/TR/html-aria/#el-th
91 return {};
92}
93
94}