Coverage Report

Created: 2026-06-07 07:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/HTML/HTMLTableElement.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3
 * Copyright (c) 2021, Adam Hodgen <ant1441@gmail.com>
4
 *
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#include <LibWeb/Bindings/HTMLTableElementPrototype.h>
9
#include <LibWeb/Bindings/Intrinsics.h>
10
#include <LibWeb/CSS/Parser/Parser.h>
11
#include <LibWeb/CSS/StyleProperties.h>
12
#include <LibWeb/CSS/StyleValues/CSSColorValue.h>
13
#include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
14
#include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
15
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
16
#include <LibWeb/DOM/ElementFactory.h>
17
#include <LibWeb/DOM/HTMLCollection.h>
18
#include <LibWeb/HTML/HTMLTableColElement.h>
19
#include <LibWeb/HTML/HTMLTableElement.h>
20
#include <LibWeb/HTML/HTMLTableRowElement.h>
21
#include <LibWeb/HTML/Numbers.h>
22
#include <LibWeb/HTML/Parser/HTMLParser.h>
23
#include <LibWeb/Namespace.h>
24
25
namespace Web::HTML {
26
27
JS_DEFINE_ALLOCATOR(HTMLTableElement);
28
29
HTMLTableElement::HTMLTableElement(DOM::Document& document, DOM::QualifiedName qualified_name)
30
0
    : HTMLElement(document, move(qualified_name))
31
0
{
32
0
}
33
34
0
HTMLTableElement::~HTMLTableElement() = default;
35
36
void HTMLTableElement::initialize(JS::Realm& realm)
37
0
{
38
0
    Base::initialize(realm);
39
0
    WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLTableElement);
40
0
}
41
42
void HTMLTableElement::visit_edges(Cell::Visitor& visitor)
43
0
{
44
0
    Base::visit_edges(visitor);
45
0
    visitor.visit(m_rows);
46
0
    visitor.visit(m_t_bodies);
47
0
}
48
49
static unsigned parse_border(StringView value)
50
0
{
51
0
    return value.to_number<unsigned>().value_or(0);
52
0
}
53
54
void HTMLTableElement::apply_presentational_hints(CSS::StyleProperties& style) const
55
0
{
56
0
    for_each_attribute([&](auto& name, auto& value) {
57
0
        if (name == HTML::AttributeNames::width) {
58
0
            if (auto parsed_value = parse_nonzero_dimension_value(value))
59
0
                style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
60
0
            return;
61
0
        }
62
0
        if (name == HTML::AttributeNames::height) {
63
0
            if (auto parsed_value = parse_dimension_value(value))
64
0
                style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
65
0
            return;
66
0
        }
67
0
        if (name == HTML::AttributeNames::align) {
68
0
            if (value.equals_ignoring_ascii_case("center"sv)) {
69
0
                style.set_property(CSS::PropertyID::MarginLeft, CSS::CSSKeywordValue::create(CSS::Keyword::Auto));
70
0
                style.set_property(CSS::PropertyID::MarginRight, CSS::CSSKeywordValue::create(CSS::Keyword::Auto));
71
0
            } else if (auto parsed_value = parse_css_value(CSS::Parser::ParsingContext { document() }, value, CSS::PropertyID::Float)) {
72
0
                style.set_property(CSS::PropertyID::Float, parsed_value.release_nonnull());
73
0
            }
74
0
            return;
75
0
        }
76
0
        if (name == HTML::AttributeNames::background) {
77
0
            if (auto parsed_value = document().parse_url(value); parsed_value.is_valid())
78
0
                style.set_property(CSS::PropertyID::BackgroundImage, CSS::ImageStyleValue::create(parsed_value));
79
0
            return;
80
0
        }
81
0
        if (name == HTML::AttributeNames::bgcolor) {
82
            // https://html.spec.whatwg.org/multipage/rendering.html#tables-2:rules-for-parsing-a-legacy-colour-value
83
0
            auto color = parse_legacy_color_value(value);
84
0
            if (color.has_value())
85
0
                style.set_property(CSS::PropertyID::BackgroundColor, CSS::CSSColorValue::create_from_color(color.value()));
86
0
            return;
87
0
        }
88
0
        if (name == HTML::AttributeNames::cellspacing) {
89
0
            if (auto parsed_value = parse_dimension_value(value))
90
0
                style.set_property(CSS::PropertyID::BorderSpacing, parsed_value.release_nonnull());
91
0
            return;
92
0
        }
93
0
        if (name == HTML::AttributeNames::border) {
94
0
            auto border = parse_border(value);
95
0
            if (!border)
96
0
                return;
97
0
            auto apply_border_style = [&](CSS::PropertyID style_property, CSS::PropertyID width_property, CSS::PropertyID color_property) {
98
0
                auto legacy_line_style = CSS::CSSKeywordValue::create(CSS::Keyword::Outset);
99
0
                style.set_property(style_property, legacy_line_style);
100
0
                style.set_property(width_property, CSS::LengthStyleValue::create(CSS::Length::make_px(border)));
101
0
                style.set_property(color_property, CSS::CSSColorValue::create_from_color(Color(128, 128, 128)));
102
0
            };
103
0
            apply_border_style(CSS::PropertyID::BorderLeftStyle, CSS::PropertyID::BorderLeftWidth, CSS::PropertyID::BorderLeftColor);
104
0
            apply_border_style(CSS::PropertyID::BorderTopStyle, CSS::PropertyID::BorderTopWidth, CSS::PropertyID::BorderTopColor);
105
0
            apply_border_style(CSS::PropertyID::BorderRightStyle, CSS::PropertyID::BorderRightWidth, CSS::PropertyID::BorderRightColor);
106
0
            apply_border_style(CSS::PropertyID::BorderBottomStyle, CSS::PropertyID::BorderBottomWidth, CSS::PropertyID::BorderBottomColor);
107
0
        }
108
0
    });
109
0
}
110
111
void HTMLTableElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value)
112
0
{
113
0
    Base::attribute_changed(name, old_value, value);
114
0
    if (name == HTML::AttributeNames::cellpadding) {
115
0
        if (value.has_value())
116
0
            m_padding = max(0, parse_integer(value.value()).value_or(0));
117
0
        else
118
0
            m_padding = 1;
119
120
0
        return;
121
0
    }
122
0
}
123
124
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-caption
125
JS::GCPtr<HTMLTableCaptionElement> HTMLTableElement::caption()
126
0
{
127
    // The caption IDL attribute must return, on getting, the first caption element child of the table element,
128
    // if any, or null otherwise.
129
0
    return first_child_of_type<HTMLTableCaptionElement>();
130
0
}
131
132
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-caption
133
WebIDL::ExceptionOr<void> HTMLTableElement::set_caption(HTMLTableCaptionElement* caption)
134
0
{
135
    // On setting, the first caption element child of the table element, if any, must be removed,
136
    // and the new value, if not null, must be inserted as the first node of the table element.
137
0
    delete_caption();
138
139
0
    if (caption)
140
0
        TRY(pre_insert(*caption, first_child()));
141
142
0
    return {};
143
0
}
144
145
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-createcaption
146
JS::NonnullGCPtr<HTMLTableCaptionElement> HTMLTableElement::create_caption()
147
0
{
148
0
    auto maybe_caption = caption();
149
0
    if (maybe_caption) {
150
0
        return *maybe_caption;
151
0
    }
152
153
0
    auto caption = DOM::create_element(document(), TagNames::caption, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
154
0
    MUST(pre_insert(caption, first_child()));
155
0
    return static_cast<HTMLTableCaptionElement&>(*caption);
156
0
}
157
158
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-deletecaption
159
void HTMLTableElement::delete_caption()
160
0
{
161
0
    auto maybe_caption = caption();
162
0
    if (maybe_caption) {
163
0
        maybe_caption->remove(false);
164
0
    }
165
0
}
166
167
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-thead
168
JS::GCPtr<HTMLTableSectionElement> HTMLTableElement::t_head()
169
0
{
170
    // The tHead IDL attribute must return, on getting, the first thead element child of the table element,
171
    // if any, or null otherwise.
172
0
    for (auto* child = first_child(); child; child = child->next_sibling()) {
173
0
        if (is<HTMLTableSectionElement>(*child)) {
174
0
            auto table_section_element = &verify_cast<HTMLTableSectionElement>(*child);
175
0
            if (table_section_element->local_name() == TagNames::thead)
176
0
                return table_section_element;
177
0
        }
178
0
    }
179
180
0
    return nullptr;
181
0
}
182
183
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-thead
184
WebIDL::ExceptionOr<void> HTMLTableElement::set_t_head(HTMLTableSectionElement* thead)
185
0
{
186
    // If the new value is neither null nor a thead element, then a "HierarchyRequestError" DOMException must be thrown instead.
187
0
    if (thead && thead->local_name() != TagNames::thead)
188
0
        return WebIDL::HierarchyRequestError::create(realm(), "Element is not thead"_string);
189
190
    // On setting, if the new value is null or a thead element, the first thead element child of the table element,
191
    // if any, must be removed,
192
0
    delete_t_head();
193
194
0
    if (!thead)
195
0
        return {};
196
197
    // and the new value, if not null, must be inserted immediately before the first element in the table element
198
    // that is neither a caption element nor a colgroup element, if any,
199
    // or at the end of the table if there are no such elements.
200
201
    // We insert the new thead after any <caption> or <colgroup> elements
202
0
    DOM::Node* child_to_insert_before = nullptr;
203
0
    for (auto* child = first_child(); child; child = child->next_sibling()) {
204
0
        if (!is<HTMLElement>(*child))
205
0
            continue;
206
0
        if (is<HTMLTableCaptionElement>(*child))
207
0
            continue;
208
0
        if (is<HTMLTableColElement>(*child)) {
209
0
            auto table_col_element = &verify_cast<HTMLTableColElement>(*child);
210
0
            if (table_col_element->local_name() == TagNames::colgroup)
211
0
                continue;
212
0
        }
213
214
        // We have found an element which is not a <caption> or <colgroup>, we'll insert before this
215
0
        child_to_insert_before = child;
216
0
        break;
217
0
    }
218
219
0
    TRY(pre_insert(*thead, child_to_insert_before));
220
221
0
    return {};
222
0
}
223
224
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-createthead
225
JS::NonnullGCPtr<HTMLTableSectionElement> HTMLTableElement::create_t_head()
226
0
{
227
0
    auto maybe_thead = t_head();
228
0
    if (maybe_thead)
229
0
        return *maybe_thead;
230
231
0
    auto thead = DOM::create_element(document(), TagNames::thead, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
232
233
    // We insert the new thead after any <caption> or <colgroup> elements
234
0
    DOM::Node* child_to_insert_before = nullptr;
235
0
    for (auto* child = first_child(); child; child = child->next_sibling()) {
236
0
        if (!is<HTMLElement>(*child))
237
0
            continue;
238
0
        if (is<HTMLTableCaptionElement>(*child))
239
0
            continue;
240
0
        if (is<HTMLTableColElement>(*child)) {
241
0
            auto table_col_element = &verify_cast<HTMLTableColElement>(*child);
242
0
            if (table_col_element->local_name() == TagNames::colgroup)
243
0
                continue;
244
0
        }
245
246
        // We have found an element which is not a <caption> or <colgroup>, we'll insert before this
247
0
        child_to_insert_before = child;
248
0
        break;
249
0
    }
250
251
0
    MUST(pre_insert(thead, child_to_insert_before));
252
253
0
    return static_cast<HTMLTableSectionElement&>(*thead);
254
0
}
255
256
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-deletethead
257
void HTMLTableElement::delete_t_head()
258
0
{
259
0
    auto maybe_thead = t_head();
260
0
    if (maybe_thead) {
261
0
        maybe_thead->remove(false);
262
0
    }
263
0
}
264
265
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-tfoot
266
JS::GCPtr<HTMLTableSectionElement> HTMLTableElement::t_foot()
267
0
{
268
    // The tFoot IDL attribute must return, on getting, the first tfoot element child of the table element,
269
    // if any, or null otherwise.
270
0
    for (auto* child = first_child(); child; child = child->next_sibling()) {
271
0
        if (is<HTMLTableSectionElement>(*child)) {
272
0
            auto table_section_element = &verify_cast<HTMLTableSectionElement>(*child);
273
0
            if (table_section_element->local_name() == TagNames::tfoot)
274
0
                return table_section_element;
275
0
        }
276
0
    }
277
278
0
    return nullptr;
279
0
}
280
281
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-tfoot
282
WebIDL::ExceptionOr<void> HTMLTableElement::set_t_foot(HTMLTableSectionElement* tfoot)
283
0
{
284
    // If the new value is neither null nor a tfoot element, then a "HierarchyRequestError" DOMException must be thrown instead.
285
0
    if (tfoot && tfoot->local_name() != TagNames::tfoot)
286
0
        return WebIDL::HierarchyRequestError::create(realm(), "Element is not tfoot"_string);
287
288
    // On setting, if the new value is null or a tfoot element, the first tfoot element child of the table element,
289
    // if any, must be removed,
290
0
    delete_t_foot();
291
292
    // and the new value, if not null, must be inserted at the end of the table.
293
0
    if (tfoot) {
294
0
        TRY(append_child(*tfoot));
295
0
    }
296
297
0
    return {};
298
0
}
299
300
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-createtfoot
301
JS::NonnullGCPtr<HTMLTableSectionElement> HTMLTableElement::create_t_foot()
302
0
{
303
0
    auto maybe_tfoot = t_foot();
304
0
    if (maybe_tfoot)
305
0
        return *maybe_tfoot;
306
307
0
    auto tfoot = DOM::create_element(document(), TagNames::tfoot, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
308
0
    MUST(append_child(tfoot));
309
0
    return static_cast<HTMLTableSectionElement&>(*tfoot);
310
0
}
311
312
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-deletetfoot
313
void HTMLTableElement::delete_t_foot()
314
0
{
315
0
    auto maybe_tfoot = t_foot();
316
0
    if (maybe_tfoot) {
317
0
        maybe_tfoot->remove(false);
318
0
    }
319
0
}
320
321
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-tbodies
322
JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableElement::t_bodies()
323
0
{
324
    // The tBodies attribute must return an HTMLCollection rooted at the table node,
325
    // whose filter matches only tbody elements that are children of the table element.
326
0
    if (!m_t_bodies) {
327
0
        m_t_bodies = DOM::HTMLCollection::create(*this, DOM::HTMLCollection::Scope::Children, [](DOM::Element const& element) {
328
0
            return element.local_name() == TagNames::tbody;
329
0
        });
330
0
    }
331
0
    return *m_t_bodies;
332
0
}
333
334
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-createtbody
335
JS::NonnullGCPtr<HTMLTableSectionElement> HTMLTableElement::create_t_body()
336
0
{
337
0
    auto tbody = DOM::create_element(document(), TagNames::tbody, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
338
339
    // We insert the new tbody after the last <tbody> element
340
0
    DOM::Node* child_to_insert_before = nullptr;
341
0
    for (auto* child = last_child(); child; child = child->previous_sibling()) {
342
0
        if (!is<HTMLElement>(*child))
343
0
            continue;
344
0
        if (is<HTMLTableSectionElement>(*child)) {
345
0
            auto table_section_element = &verify_cast<HTMLTableSectionElement>(*child);
346
0
            if (table_section_element->local_name() == TagNames::tbody) {
347
                // We have found an element which is a <tbody> we'll insert after this
348
0
                child_to_insert_before = child->next_sibling();
349
0
                break;
350
0
            }
351
0
        }
352
0
    }
353
354
0
    MUST(pre_insert(tbody, child_to_insert_before));
355
356
0
    return static_cast<HTMLTableSectionElement&>(*tbody);
357
0
}
358
359
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-rows
360
JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableElement::rows()
361
0
{
362
0
    HTMLTableElement* table_node = this;
363
    // FIXME:  The elements in the collection must be ordered such that those elements whose parent is a thead are
364
    //         included first, in tree order, followed by those elements whose parent is either a table or tbody
365
    //         element, again in tree order, followed finally by those elements whose parent is a tfoot element,
366
    //         still in tree order.
367
    // How do you sort HTMLCollection?
368
369
0
    if (!m_rows) {
370
0
        m_rows = DOM::HTMLCollection::create(*this, DOM::HTMLCollection::Scope::Descendants, [table_node](DOM::Element const& element) {
371
            // Only match TR elements which are:
372
            // * children of the table element
373
            // * children of the thead, tbody, or tfoot elements that are themselves children of the table element
374
0
            if (!is<HTMLTableRowElement>(element)) {
375
0
                return false;
376
0
            }
377
0
            if (element.parent_element() == table_node)
378
0
                return true;
379
380
0
            if (element.parent_element() && (element.parent_element()->local_name() == TagNames::thead || element.parent_element()->local_name() == TagNames::tbody || element.parent_element()->local_name() == TagNames::tfoot)
381
0
                && element.parent()->parent() == table_node) {
382
0
                return true;
383
0
            }
384
385
0
            return false;
386
0
        });
387
0
    }
388
0
    return *m_rows;
389
0
}
390
391
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-insertrow
392
WebIDL::ExceptionOr<JS::NonnullGCPtr<HTMLTableRowElement>> HTMLTableElement::insert_row(WebIDL::Long index)
393
0
{
394
0
    auto rows = this->rows();
395
0
    auto rows_length = rows->length();
396
397
0
    if (index < -1 || index > (long)rows_length) {
398
0
        return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than the number of rows"_string);
399
0
    }
400
0
    auto& tr = static_cast<HTMLTableRowElement&>(*TRY(DOM::create_element(document(), TagNames::tr, Namespace::HTML)));
401
0
    if (rows_length == 0 && !has_child_of_type<HTMLTableRowElement>()) {
402
0
        auto tbody = TRY(DOM::create_element(document(), TagNames::tbody, Namespace::HTML));
403
0
        TRY(tbody->append_child(tr));
404
0
        TRY(append_child(tbody));
405
0
    } else if (rows_length == 0) {
406
0
        auto tbody = last_child_of_type<HTMLTableRowElement>();
407
0
        TRY(tbody->append_child(tr));
408
0
    } else if (index == -1 || index == (long)rows_length) {
409
0
        auto parent_of_last_tr = rows->item(rows_length - 1)->parent_element();
410
0
        TRY(parent_of_last_tr->append_child(tr));
411
0
    } else {
412
0
        rows->item(index)->parent_element()->insert_before(tr, rows->item(index));
413
0
    }
414
0
    return JS::NonnullGCPtr(tr);
415
0
}
416
417
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-deleterow
418
WebIDL::ExceptionOr<void> HTMLTableElement::delete_row(WebIDL::Long index)
419
0
{
420
0
    auto rows = this->rows();
421
0
    auto rows_length = rows->length();
422
423
    // 1. If index is less than −1 or greater than or equal to the number of elements in the rows collection, then throw an "IndexSizeError" DOMException.
424
0
    if (index < -1 || index >= (long)rows_length)
425
0
        return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than or equal to the number of rows"_string);
426
427
    // 2. If index is −1, then remove the last element in the rows collection from its parent, or do nothing if the rows collection is empty.
428
0
    if (index == -1) {
429
0
        if (rows_length == 0)
430
0
            return {};
431
432
0
        auto row_to_remove = rows->item(rows_length - 1);
433
0
        row_to_remove->remove(false);
434
0
        return {};
435
0
    }
436
437
    // 3. Otherwise, remove the indexth element in the rows collection from its parent.
438
0
    auto row_to_remove = rows->item(index);
439
0
    row_to_remove->remove(false);
440
0
    return {};
441
0
}
442
443
unsigned int HTMLTableElement::border() const
444
0
{
445
0
    return parse_border(get_attribute_value(HTML::AttributeNames::border));
446
0
}
447
448
unsigned int HTMLTableElement::padding() const
449
0
{
450
0
    return m_padding;
451
0
}
452
453
}