Skip to content

Fix CSS identifier fallback escaping in escapeCSSIdentifier function #50

@coderabbitai

Description

@coderabbitai

Problem

The escapeCSSIdentifier function in strot/browser/inject.js has incomplete fallback escaping that can produce invalid CSS selectors when CSS.escape is not available.

Current Issues

The manual fallback escaping is missing several critical cases:

  • Control characters (0x0001-0x001F, 0x007F)
  • Leading digits that need hex escaping
  • Hyphen followed by digit patterns (-digit)
  • NULL character handling

Expected Behavior

The function should use a standards-compliant CSS.escape polyfill when the native CSS.escape method is unavailable, ensuring all edge cases produce valid CSS identifiers.

Technical Details

Current problematic code:

return identifier
  .replace(/^([-0-9])/, "\$1") // escape leading digit or hyphen
  .replace(/\s+/g, "\\ ") // escape whitespace  
  .replace(/[\!"#$%&'()*+,.\/:;<=>?@\[\\\]^`{|}~]/g, "\\$&");

Should be replaced with a robust polyfill implementation that handles all CSS identifier edge cases.

References

Affected Files

  • strot/browser/inject.js (lines ~95-99)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions