Skip to content

Commit 72f74be

Browse files
committed
Gets the party started
0 parents  commit 72f74be

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Ruby cssesc
2+
===========
3+
4+
A Ruby wrapper for [cssesc][1], a JavaScript library for [escaping text for
5+
use in CSS strings or identifiers][2].
6+
7+
8+
Installation
9+
------------
10+
11+
```bash
12+
gem install cssesc
13+
```
14+
15+
API
16+
-----
17+
18+
### `CSSEsc.escape(value, options = {})`
19+
20+
This function takes a value and returns an escaped version of the value where
21+
any characters that are not printable ASCII symbols are escaped using the
22+
shortest possible (but valid) [escape sequences for use in CSS strings or
23+
identifiers][2].
24+
25+
```ruby
26+
CSSEsc.escape('Ich ♥ Bücher')
27+
# => 'Ich \\2665 B\\FC cher'
28+
```
29+
30+
By default, `CSSEsc.escape` returns a string that can be used as part of a CSS
31+
string. If the target is a CSS identifier rather than a CSS string, use the
32+
`is_identifier: true` setting.
33+
34+
The optional `options` arguments accepts a `Hash` with the exact [options
35+
which JavaScript version cssesc uses][3] only converted to the `snake_case`:
36+
37+
```ruby
38+
CSSEsc.espace('123a2b', {
39+
escape_everything: true
40+
})
41+
```
42+
43+
Dependencies
44+
------------
45+
46+
This library depends on the `cssesc-source` gem which is updated any time a
47+
new version of cssesc is released. (The `cssesc-source` gem's version number
48+
is synced with each official cssesc release.)
49+
50+
### ExecJS
51+
52+
The [ExecJS][4] library is used to automatically choose the best JavaScript
53+
engine for your platform. Check out its [README][5] for a complete list of
54+
supported engines.
55+
56+
[1]: http://github.com/mathiasbynens/cssesc
57+
[2]: http://mathiasbynens.be/notes/css-escapes
58+
[3]: http://github.com/mathiasbynens/cssesc#api
59+
[4]: http://github.com/sstephenson/execjs
60+
[5]: http://github.com/sstephenson/execjs/blob/master/README.md

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.1.0

cssesc.gemspec

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version = File.read(File.expand_path('../VERSION', __FILE__)).strip
2+
3+
Gem::Specification.new do |s|
4+
s.name = 'cssesc'
5+
s.version = version
6+
s.summary = 'Ruby wrapper for a JavaScript library for escaping CSS strings and identifiers'
7+
8+
s.license = 'MIT'
9+
10+
s.author = 'Vadim Borodean'
11+
s.email = 'borodean@gmail.com'
12+
s.homepage = 'http://github.com/borodean/ruby-cssesc'
13+
14+
s.files = Dir['lib/**/*']
15+
16+
s.add_dependency 'activesupport'
17+
s.add_dependency 'cssesc-source'
18+
s.add_dependency 'execjs'
19+
end

lib/cssesc.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'active_support/core_ext/hash/keys'
2+
require 'active_support/core_ext/string/inflections'
3+
require 'cssesc/source'
4+
require 'execjs'
5+
6+
module CSSEsc
7+
source = File.read(CSSEsc::Source.bundled_path)
8+
@cssescjs = ExecJS.compile(source)
9+
10+
def self.escape(value, options = {})
11+
options.transform_keys! { |key| key.to_s.camelize(:lower) }
12+
@cssescjs.call('cssesc', value, options)
13+
end
14+
end

0 commit comments

Comments
 (0)