summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/HTML/HTMLTableElement.cpp
blob: 69af3df3aa8dd7a4bb08fa4f7b42fde83cddac25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2021, Adam Hodgen <ant1441@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibWeb/CSS/Parser/DeprecatedCSSParser.h>
#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/DOM/HTMLCollection.h>
#include <LibWeb/HTML/HTMLTableColElement.h>
#include <LibWeb/HTML/HTMLTableElement.h>
#include <LibWeb/HTML/HTMLTableRowElement.h>
#include <LibWeb/Namespace.h>

namespace Web::HTML {

HTMLTableElement::HTMLTableElement(DOM::Document& document, QualifiedName qualified_name)
    : HTMLElement(document, move(qualified_name))
{
}

HTMLTableElement::~HTMLTableElement()
{
}

void HTMLTableElement::apply_presentational_hints(CSS::StyleProperties& style) const
{
    for_each_attribute([&](auto& name, auto& value) {
        if (name == HTML::AttributeNames::width) {
            if (auto parsed_value = parse_html_length(document(), value))
                style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
            return;
        }
        if (name == HTML::AttributeNames::height) {
            if (auto parsed_value = parse_html_length(document(), value))
                style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
            return;
        }
        if (name == HTML::AttributeNames::bgcolor) {
            auto color = Color::from_string(value);
            if (color.has_value())
                style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value()));
            return;
        }
    });
}

RefPtr<HTMLTableCaptionElement> HTMLTableElement::caption()
{
    return first_child_of_type<HTMLTableCaptionElement>();
}

void HTMLTableElement::set_caption(HTMLTableCaptionElement* caption)
{
    // FIXME: This is not always the case, but this function is currently written in a way that assumes non-null.
    VERIFY(caption);

    // FIXME: The spec requires deleting the current caption if caption is null
    //        Currently the wrapper generator doesn't send us a nullable value
    delete_caption();

    pre_insert(*caption, first_child());
}

NonnullRefPtr<HTMLTableCaptionElement> HTMLTableElement::create_caption()
{
    auto maybe_caption = caption();
    if (maybe_caption) {
        return *maybe_caption;
    }

    auto caption = DOM::create_element(document(), TagNames::caption, Namespace::HTML);
    pre_insert(caption, first_child());
    return caption;
}

void HTMLTableElement::delete_caption()
{
    auto maybe_caption = caption();
    if (maybe_caption) {
        maybe_caption->remove(false);
    }
}

RefPtr<HTMLTableSectionElement> HTMLTableElement::t_head()
{
    for (auto* child = first_child(); child; child = child->next_sibling()) {
        if (is<HTMLTableSectionElement>(*child)) {
            auto table_section_element = &verify_cast<HTMLTableSectionElement>(*child);
            if (table_section_element->local_name() == TagNames::thead)
                return table_section_element;
        }
    }

    return nullptr;
}

DOM::ExceptionOr<void> HTMLTableElement::set_t_head(HTMLTableSectionElement* thead)
{
    // FIXME: This is not always the case, but this function is currently written in a way that assumes non-null.
    VERIFY(thead);

    if (thead->local_name() != TagNames::thead)
        return DOM::HierarchyRequestError::create("Element is not thead");

    // FIXME: The spec requires deleting the current thead if thead is null
    //        Currently the wrapper generator doesn't send us a nullable value
    delete_t_head();

    // We insert the new thead after any <caption> or <colgroup> elements
    DOM::Node* child_to_append_after = nullptr;
    for (auto* child = first_child(); child; child = child->next_sibling()) {
        if (!is<HTMLElement>(*child))
            continue;
        if (is<HTMLTableCaptionElement>(*child))
            continue;
        if (is<HTMLTableColElement>(*child)) {
            auto table_col_element = &verify_cast<HTMLTableColElement>(*child);
            if (table_col_element->local_name() == TagNames::colgroup)
                continue;
        }

        // We have found an element which is not a <caption> or <colgroup>, we'll insert before this
        child_to_append_after = child;
        break;
    }

    pre_insert(*thead, child_to_append_after);

    return {};
}

NonnullRefPtr<HTMLTableSectionElement> HTMLTableElement::create_t_head()
{
    auto maybe_thead = t_head();
    if (maybe_thead)
        return *maybe_thead;

    auto thead = DOM::create_element(document(), TagNames::thead, Namespace::HTML);

    // We insert the new thead after any <caption> or <colgroup> elements
    DOM::Node* child_to_append_after = nullptr;
    for (auto* child = first_child(); child; child = child->next_sibling()) {
        if (!is<HTMLElement>(*child))
            continue;
        if (is<HTMLTableCaptionElement>(*child))
            continue;
        if (is<HTMLTableColElement>(*child)) {
            auto table_col_element = &verify_cast<HTMLTableColElement>(*child);
            if (table_col_element->local_name() == TagNames::colgroup)
                continue;
        }

        // We have found an element which is not a <caption> or <colgroup>, we'll insert before this
        child_to_append_after = child;
        break;
    }

    pre_insert(thead, child_to_append_after);

    return thead;
}

void HTMLTableElement::delete_t_head()
{
    auto maybe_thead = t_head();
    if (maybe_thead) {
        maybe_thead->remove(false);
    }
}

RefPtr<HTMLTableSectionElement> HTMLTableElement::t_foot()
{
    for (auto* child = first_child(); child; child = child->next_sibling()) {
        if (is<HTMLTableSectionElement>(*child)) {
            auto table_section_element = &verify_cast<HTMLTableSectionElement>(*child);
            if (table_section_element->local_name() == TagNames::tfoot)
                return table_section_element;
        }
    }

    return nullptr;
}

DOM::ExceptionOr<void> HTMLTableElement::set_t_foot(HTMLTableSectionElement* tfoot)
{
    // FIXME: This is not always the case, but this function is currently written in a way that assumes non-null.
    VERIFY(tfoot);

    if (tfoot->local_name() != TagNames::tfoot)
        return DOM::HierarchyRequestError::create("Element is not tfoot");

    // FIXME: The spec requires deleting the current tfoot if tfoot is null
    //        Currently the wrapper generator doesn't send us a nullable value
    delete_t_foot();

    // We insert the new tfoot at the end of the table
    append_child(*tfoot);

    return {};
}

NonnullRefPtr<HTMLTableSectionElement> HTMLTableElement::create_t_foot()
{
    auto maybe_tfoot = t_foot();
    if (maybe_tfoot)
        return *maybe_tfoot;

    auto tfoot = DOM::create_element(document(), TagNames::tfoot, Namespace::HTML);
    append_child(tfoot);
    return tfoot;
}

void HTMLTableElement::delete_t_foot()
{
    auto maybe_tfoot = t_foot();
    if (maybe_tfoot) {
        maybe_tfoot->remove(false);
    }
}

NonnullRefPtr<DOM::HTMLCollection> HTMLTableElement::t_bodies()
{
    return DOM::HTMLCollection::create(*this, [](DOM::Element const& element) {
        return element.local_name() == TagNames::tbody;
    });
}

NonnullRefPtr<HTMLTableSectionElement> HTMLTableElement::create_t_body()
{
    auto tbody = DOM::create_element(document(), TagNames::tbody, Namespace::HTML);

    // We insert the new tbody after the last <tbody> element
    DOM::Node* child_to_append_after = nullptr;
    for (auto* child = last_child(); child; child = child->previous_sibling()) {
        if (!is<HTMLElement>(*child))
            continue;
        if (is<HTMLTableSectionElement>(*child)) {
            auto table_section_element = &verify_cast<HTMLTableSectionElement>(*child);
            if (table_section_element->local_name() == TagNames::tbody) {
                // We have found an element which is a <tbody> we'll insert after this
                child_to_append_after = child->next_sibling();
                break;
            }
        }
    }

    pre_insert(tbody, child_to_append_after);

    return tbody;
}

NonnullRefPtr<DOM::HTMLCollection> HTMLTableElement::rows()
{
    HTMLTableElement* table_node = this;
    // FIXME:  The elements in the collection must be ordered such that those elements whose parent is a thead are
    //         included first, in tree order, followed by those elements whose parent is either a table or tbody
    //         element, again in tree order, followed finally by those elements whose parent is a tfoot element,
    //         still in tree order.
    // How do you sort HTMLCollection?

    return DOM::HTMLCollection::create(*this, [table_node](DOM::Element const& element) {
        // Only match TR elements which are:
        // * children of the table element
        // * children of the thead, tbody, or tfoot elements that are themselves children of the table element
        if (!is<HTMLTableRowElement>(element)) {
            return false;
        }
        if (element.parent_element() == table_node)
            return true;

        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)
            && element.parent()->parent() == table_node) {
            return true;
        }

        return false;
    });
}

DOM::ExceptionOr<NonnullRefPtr<HTMLTableRowElement>> HTMLTableElement::insert_row(long index)
{
    auto rows = this->rows();
    auto rows_length = rows->length();

    if (index < -1 || index >= (long)rows_length) {
        return DOM::IndexSizeError::create("Index is negative or greater than the number of rows");
    }
    auto tr = static_cast<NonnullRefPtr<HTMLTableRowElement>>(DOM::create_element(document(), TagNames::tr, Namespace::HTML));
    if (rows_length == 0 && !has_child_of_type<HTMLTableRowElement>()) {
        auto tbody = DOM::create_element(document(), TagNames::tbody, Namespace::HTML);
        tbody->append_child(tr);
        append_child(tbody);
    } else if (rows_length == 0) {
        auto tbody = last_child_of_type<HTMLTableRowElement>();
        tbody->append_child(tr);
    } else if (index == -1 || index == (long)rows_length) {
        auto parent_of_last_tr = rows->item(rows_length - 1)->parent_element();
        parent_of_last_tr->append_child(tr);
    } else {
        rows->item(index)->parent_element()->insert_before(tr, rows->item(index));
    }
    return tr;
}

DOM::ExceptionOr<void> HTMLTableElement::delete_row(long index)
{
    auto rows = this->rows();
    auto rows_length = rows->length();

    if (index < -1 || index >= (long)rows_length) {
        return DOM::IndexSizeError::create("Index is negative or greater than the number of rows");
    }
    if (index == -1 && rows_length > 0) {
        auto row_to_remove = rows->item(rows_length - 1);
        row_to_remove->remove(false);
    } else {
        auto row_to_remove = rows->item(index);
        row_to_remove->remove(false);
    }

    return {};
}

}