lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Bug: $getSelectionStyleValueForProperty will report different values for backward selection when 'null' passed as default

Open samuelpecher opened this issue 1 month ago • 0 comments

Lexical version: 0.38.2

Steps To Reproduce

  1. Have a plain TextNode followed by styled TextNode (using $patchStyle({ color: "red" }) for example)
  2. Select both backwards and use $getSelectionStyleValueForProperty(selection, "color"): the expected "" will be returned
  3. Select both forwards and use $getSelectionStyleValueForProperty(selection, "color"): the style in the second TextNode will be returned

The current behavior

if null is passed as the default value to

  • Selection forwards from an unstyled TextNode to a styled TextNode returns the style from the styled TextNode
  • Selection backwards from a styled TextNode to a precesing unstyled TextNode returns ""

The expected behavior

Selection of styled and unstyled TextNodes return "" no matter selection when null is the default

Root cause

In $getSelectionStyleValueForProperty this block returns different values depending on node order when the default value is null.

      if (styleValue === null) {
        styleValue = nodeStyleValue;
      } else if (styleValue !== nodeStyleValue) {
        // multiple text nodes are in the selection and they don't all
        // have the same style.
        styleValue = '';
        break;
      }
  • In the forwards iteration, from unstyled -> styled, styleValue remains null and therefore the style overwrites the null default and is returned rather than "".
  • In the backwards iteration, from styled -> unstyled, the unstyled node's nodeStyleValue of nullwill not match the precedingstyledValue` from the styled node and return "" correctly.

samuelpecher avatar Nov 27 '25 20:11 samuelpecher