Skip to content

Conversation

@rap1ds
Copy link

@rap1ds rap1ds commented Dec 2, 2025

The parent-selector-re matches only a single & or & followed by
non-whitespace characters (\S). Because of this, it doesn't recognize
i.e. combinator selectors that include whitespace.

This issue originates from
noprompt@d9248e4#diff-849a8061f274bbe17201c9450e913c145c6fd6945f89403ab7dc49fa98344ea3
where parent-selector-re was changed from #"^&.+|^&$" to
#"^&(?:\S+)?$". The old regex did allow whitespaces.

The commit message doesn't tell why the regex was changed. The only
reason I could come up is that since the parent selecter regex is used
to extract the reference, it was changed to not match selectors where
the reference part is whitespace only i.e. "& " (single ampersand
followed by space).

Example: calling extract-reference with the old regex:

(defn- extract-reference
  "Extract the selector portion of a parent selector reference."
  [selector]
  (when-let [reference (->> (last selector)
                            (util/to-str)
                            (re-find #"^&.+|^&$"))]
    (apply str (rest reference))))

(extract-reference ["a" "& "])
;; #=> " "

Example: calling extract-reference with the new regex:

(defn- extract-reference
  "Extract the selector portion of a parent selector reference."
  [selector]
  (when-let [reference (->> (last selector)
                            (util/to-str)
                            (re-find #"^&(?:\S+)?$"))]
    (apply str (rest reference))))

(extract-reference ["a" "& "])
;; #=> nil

I think it makes sense that extract-reference does NOT return
whitespace-only strings. It feels logical that nil is returned if
there really isn't reference part.

However, if extract-reference returns nil, it signifies to the
caller that this is not a parent selector, which is incorrect in case of
selector like ["a" "& "]. It would make sense to decouple the two
responsibilities, checking if the selector is a parent selector and
extracting the reference part

This commit makes following changes:

  • Revert to the old parent-selector-re, which allows whitespaces
  • Add a new parent-selector? predicate to check if the selector is a
    parent selector
  • Change extract-reference so that it returns nil for selectors with
    whitespace only reference.

Fixes:

The `parent-selector-re` matches only a single `&` or `&` followed by
non-whitespace characters (`\S`). Because of this, it doesn't recognize
i.e. combinator selectors that include whitespace.

This issue originates from
noprompt@d9248e4#diff-849a8061f274bbe17201c9450e913c145c6fd6945f89403ab7dc49fa98344ea3
where `parent-selector-re` was changed from `#"^&.+|^&$"` to
`#"^&(?:\S+)?$"`. The old regex did allow whitespaces.

The commit message doesn't tell why the regex was changed. The only
reason I could come up is that since the parent selecter regex is used
to extract the reference, it was changed to not match selectors where
the reference part is whitespace only i.e. `"& "` (single ampersand
followed by space).

Example: calling `extract-reference` with the old regex:

```
(defn- extract-reference
  "Extract the selector portion of a parent selector reference."
  [selector]
  (when-let [reference (->> (last selector)
                            (util/to-str)
                            (re-find #"^&.+|^&$"))]
    (apply str (rest reference))))

(extract-reference ["a" "& "])
;; #=> " "
```

Example: calling `extract-reference` with the new regex:

```
(defn- extract-reference
  "Extract the selector portion of a parent selector reference."
  [selector]
  (when-let [reference (->> (last selector)
                            (util/to-str)
                            (re-find #"^&(?:\S+)?$"))]
    (apply str (rest reference))))

(extract-reference ["a" "& "])
;; #=> nil
```

I think it makes sense that `extract-reference` does NOT return
whitespace-only strings. It feels logical that `nil` is returned if
there really isn't reference part.

However, if `extract-reference` returns `nil`, it signifies to the
caller that this is not a parent selector, which is incorrect in case of
selector like `["a" "& "]`. It would make sense to decouple the two
responsibilities, checking if the selector is a parent selector and
extracting the reference part

This commit makes following changes:

- Revert to the old parent-selector-re, which allows whitespaces
- Add a new `parent-selector?` predicate to check if the selector is a
  parent selector
- Change `extract-reference` so that it returns `nil` for selectors with
  whitespace only reference.

Fixes:

* noprompt#187
* noprompt#208
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant