rails/rails — pull request #3592 by avakhov (2011-11-10 07:43:13+00:00)
Title: Fix and simplify highlight regexp

hightlight text helper method is implemented via too complicated regexp. We can simplify it and fix hightlighting of tag names as a bonus:

``` ruby
highlight("

abc div

", "div", :highlighter => '\1')
# => should be "

abc div

"
```

I tested my solution on 1.8.7, 1.9.2, 1.9.3. All tests passed well.

Explanation:

Dan Weinand (91eeb0ff119d34d0fcdb44d3d7fcbb7924208e05) modified regexp to not highlight text inside tags. His regexp /(#{match})(?!(?:[^<]*?)(?:["'])[^<>]*>)/i tries to find something not <, quote and nearest >. If matching is positive it means we are inside tag and should ignore highlighting.

This is too complicated criteria. To be sure we are outside tag, nearest '<' symbol should be before nearest '>' (if they exist). My regexp solves all tasks of Dan and additionaly highlight correct tag names.