diff --git a/Libraries/LibWeb/DOM/Range.cpp b/Libraries/LibWeb/DOM/Range.cpp index 448f8e470df..276783da5f2 100644 --- a/Libraries/LibWeb/DOM/Range.cpp +++ b/Libraries/LibWeb/DOM/Range.cpp @@ -556,29 +556,24 @@ Utf16String Range::to_string() const // 2. If this’s start node is this’s end node and it is a Text node, // then return the substring of that Text node’s data beginning at this’s start offset and ending at this’s end offset. - if (start_container() == end_container() && is(*start_container())) { - auto const& text = static_cast(*start_container()); - return MUST(text.substring_data(start_offset(), end_offset() - start_offset())); - } + auto* start_text = as_if(*start_container()); + if (start_text && start_container() == end_container()) + return MUST(start_text->substring_data(start_offset(), end_offset() - start_offset())); // 3. If this’s start node is a Text node, then append the substring of that node’s data from this’s start offset until the end to s. - if (is(*start_container())) { - auto const& text = static_cast(*start_container()); - builder.append(MUST(text.substring_data(start_offset(), text.length_in_utf16_code_units() - start_offset()))); - } + if (start_text) + builder.append(MUST(start_text->substring_data(start_offset(), start_text->length_in_utf16_code_units() - start_offset()))); // 4. Append the concatenation of the data of all Text nodes that are contained in this, in tree order, to s. - for_each_contained([&](GC::Ref node) { - if (is(*node)) - builder.append(static_cast(*node).data()); + for_each_contained([&](GC::Ref node) { + if (auto* text_node = as_if(*node)) + builder.append(text_node->data()); return IterationDecision::Continue; }); // 5. If this’s end node is a Text node, then append the substring of that node’s data from its start until this’s end offset to s. - if (is(*end_container())) { - auto const& text = static_cast(*end_container()); - builder.append(MUST(text.substring_data(0, end_offset()))); - } + if (auto* end_text = as_if(*end_container())) + builder.append(MUST(end_text->substring_data(0, end_offset()))); // 6. Return s. return builder.to_utf16_string();