Skip to content

Commit

Permalink
LibWeb: Update implementation of Range::partially_contains_node()
Browse files Browse the repository at this point in the history
This accurately reflects the spec it's implementing. This algorithm is
used in 5 spots in the spec but the old buggy behavior was never
triggered:

  * In both ::extract() and ::clone_the_contents(), invocations to this
    method are guarded by a check to see if the start node is the
    inclusive ancestor of the end node, or vice versa - effectively
    resulting in the inequality checks to be accidentally correct.

  * In ::surround_contents(), we forego the usage of this algorithm as
    stated in the spec, but instead use a correct and more optimized
    version that simply compares the start and end nodes.

A lot of words to say: no functional changes :^)
  • Loading branch information
gmta committed Jan 9, 2025
1 parent 36190e3 commit 514d098
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions Libraries/LibWeb/DOM/Range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2020, the SerenityOS developers.
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
* Copyright (c) 2022-2023, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
* Copyright (c) 2024-2025, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
Expand Down Expand Up @@ -807,12 +807,9 @@ bool Range::contains_node(GC::Ref<Node> node) const
// https://dom.spec.whatwg.org/#partially-contained
bool Range::partially_contains_node(GC::Ref<Node> node) const
{
// A node is partially contained in a live range if it’s an inclusive ancestor of the live range’s start node but not its end node, or vice versa.
if (node->is_inclusive_ancestor_of(m_start_container) && node != m_end_container)
return true;
if (node->is_inclusive_ancestor_of(m_end_container) && node != m_start_container)
return true;
return false;
// A node is partially contained in a live range if it’s an inclusive ancestor of the live range’s start node but
// not its end node, or vice versa.
return node->is_inclusive_ancestor_of(m_start_container) != node->is_inclusive_ancestor_of(m_end_container);
}

// https://dom.spec.whatwg.org/#dom-range-insertnode
Expand Down

0 comments on commit 514d098

Please sign in to comment.