Skip to content

Commit

Permalink
Fix Regexp.escape to escape \t, \r, \n, \f characters
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Jan 27, 2014
1 parent 41f196d commit 8027655
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -115,6 +115,8 @@

* Fix some stdlib `Date` methods.

* Fix `Regexp.escape` to properly escape \n, \t, \r, \f characters.

## 0.5.5 2013-11-25

* Fix regression: add `%i[foo bar]` style words back to lexer
Expand Down
8 changes: 7 additions & 1 deletion opal/corelib/regexp.rb
Expand Up @@ -2,7 +2,13 @@ class Regexp
`def._isRegexp = true`

def self.escape(string)
`string.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$| ]/g, '\\$&')`
%x{
return string.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$| ]/g, '\\$&')
.replace(/[\n]/g, '\\n')
.replace(/[\r]/g, '\\r')
.replace(/[\f]/g, '\\f')
.replace(/[\t]/g, '\\t');
}
end

def self.union(*parts)
Expand Down
6 changes: 6 additions & 0 deletions spec/opal/filters/unsupported/encoding.rb
Expand Up @@ -48,3 +48,9 @@
fails "Magic comment is case-insensitive"
fails "Magic comment determines __ENCODING__"
end

opal_filter "Regexp.escape" do
fails "Regexp.escape sets the encoding of the result to US-ASCII if there are only US-ASCII characters present in the input String"
fails "Regexp.escape sets the encoding of the result to the encoding of the String if any non-US-ASCII characters are present in an input String with valid encoding"
fails "Regexp.escape sets the encoding of the result to ASCII-8BIT if any non-US-ASCII characters are present in an input String with invalid encoding"
end
2 changes: 1 addition & 1 deletion spec/opal/rubyspecs
Expand Up @@ -186,7 +186,7 @@ core/time/wednesday_spec
core/time/yday_spec
core/time/year_spec

#core/regexp/escape_spec
core/regexp/escape_spec
core/regexp/match_spec
#core/regexp/quote_spec

Expand Down

0 comments on commit 8027655

Please sign in to comment.