highlight.js icon indicating copy to clipboard operation
highlight.js copied to clipboard

fix(ruby): properly highlight %r{} regex literals containing nested patterns

Open j-erasmus opened this issue 6 months ago • 2 comments

Summary

This fixes an issue where %r{}-style regular expressions in Ruby were not correctly highlighted when they contained nested patterns or complex escape sequences. Patterns like (\.{2}|\A/) would either break highlighting or be ignored entirely due to improper handling of nesting inside %r{}.

Resolves: https://github.com/highlightjs/highlight.js/issues/4281


Root Cause

Highlight.js previously treated %r{} regex literals as flat strings. This caused the parser to:

  • Fail on nested constructs like {2} (quantifiers)
  • Break on position anchors like \A
  • Terminate the regex early or skip highlighting entirely

Fix

  • Introduces a top-level REGEXP mode to match %r{}, %r(), %r[], %r!, etc. anywhere in Ruby code
  • Enables nested brace parsing inside %r{} using contains: ['self']
  • Removes %r variants from the legacy RE_STARTERS_RE block (which still handles /.../ contexts)
  • Keeps other regex behavior intact and scoped

Screenshots

Before After
Screenshot 2025-06-27 at 10 39 06 Screenshot 2025-06-27 at 10 39 24

j-erasmus avatar Jun 27 '25 08:06 j-erasmus

101142167176

@.***

Jacques Erasmus @.***> 于 2025年7月4日周五 19:49写道:

@.**** commented on this pull request.

In src/languages/ruby.js https://github.com/highlightjs/highlight.js/pull/4286#discussion_r2185158611 :

@@ -227,6 +227,27 @@ export default function(hljs) { ] };

  • const REGEXP = {
  • className: 'regexp',
  • contains: [
  •  hljs.BACKSLASH_ESCAPE,
    
  •  SUBST,
    
  •  {
    
  •    begin: /\{/, end: /\}/,
    

@joshgoebel https://github.com/joshgoebel this should now be fixed, I also added some tests to verify these cases.

— Reply to this email directly, view it on GitHub https://github.com/highlightjs/highlight.js/pull/4286#discussion_r2185158611, or unsubscribe https://github.com/notifications/unsubscribe-auth/BK2N76VR5FECLYCE47MX7X33GZS4NAVCNFSM6AAAAACAIM4ROGVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDSOBWHA3DINRWHA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

Yang062 avatar Jul 04 '25 18:07 Yang062

@joshgoebel any updates here? I've addressed the comments you left previously. Please take a look/merge when you get a chance 🙇

j-erasmus avatar Sep 15 '25 07:09 j-erasmus