csstree 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +0 -0
- data/Gemfile +2 -0
- data/README.md +46 -0
- data/Rakefile +2 -0
- data/csstree.gemspec +18 -0
- data/lib/csstree.rb +67 -0
- data/spec/csstree_spec.rb +45 -0
- data/spec/spec_helper.rb +2 -0
- metadata +67 -0
data/.gitignore
ADDED
data/.rspec
ADDED
File without changes
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# CSSTree
|
2
|
+
|
3
|
+
Ultra-simple CSS parser.
|
4
|
+
|
5
|
+
### Parsing CSS
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
tree = CSSTree.parse("body { background-color: #FFFFFF; color: #000000; }")
|
9
|
+
# => #<CSSTree:0x123456>
|
10
|
+
```
|
11
|
+
|
12
|
+
### Finding Rules For A Selector
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
tree.find("body")
|
16
|
+
# => #<OpenStruct background_color="#FFFFFF" color="#000000">
|
17
|
+
```
|
18
|
+
|
19
|
+
### Access Properties
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
tree.find("body").color
|
23
|
+
# => "#000000"
|
24
|
+
```
|
25
|
+
|
26
|
+
### Access Dashed Property Names Using Underscores
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
tree.find("body").background_color
|
30
|
+
# => "#FFFFFF"
|
31
|
+
```
|
32
|
+
|
33
|
+
### Manipulating the Tree
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
tree.find("body").color = "#A3A3A3"
|
37
|
+
# => "#A3A3A3"
|
38
|
+
```
|
39
|
+
|
40
|
+
### Rendering Normal CSS
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
tree.render
|
44
|
+
# => "body { background-color: #FFFFFF; color: #A3A3A3; }
|
45
|
+
```
|
46
|
+
|
data/Rakefile
ADDED
data/csstree.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.dirname(__FILE__) + "/lib/csstree.rb"
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Matte Noble"]
|
6
|
+
gem.email = ["me@mattenoble.com"]
|
7
|
+
gem.description = %q{Ultra-simple CSS parser.}
|
8
|
+
gem.summary = %q{Ultra-simple CSS parser.}
|
9
|
+
|
10
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
11
|
+
gem.files = `git ls-files`.split("\n")
|
12
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
13
|
+
gem.name = "csstree"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = CSSTree::VERSION
|
16
|
+
|
17
|
+
gem.add_development_dependency "rspec", "~> 2.6.0"
|
18
|
+
end
|
data/lib/csstree.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
require "forwardable"
|
3
|
+
|
4
|
+
class CSSTree
|
5
|
+
VERSION = "0.0.1"
|
6
|
+
RULESET_REGEX = /([^{]*){([^}]*)}/
|
7
|
+
|
8
|
+
include Enumerable
|
9
|
+
extend Forwardable
|
10
|
+
|
11
|
+
def_delegators :"@tree", :each, :keys, :values, :[], :[]=, :includes?
|
12
|
+
alias :selectors :keys
|
13
|
+
alias :declarations :values
|
14
|
+
|
15
|
+
def self.parse s
|
16
|
+
new.parse s
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
@tree = {}
|
21
|
+
end
|
22
|
+
|
23
|
+
def parse s
|
24
|
+
@tree = Hash[*trim(s).scan(RULESET_REGEX).collect { |a| parse_block *a }.flatten]
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def find s
|
29
|
+
@tree[s]
|
30
|
+
end
|
31
|
+
|
32
|
+
def render
|
33
|
+
@tree.map { |s,d| "#{s.strip} { #{render_declarations d} }" }.join("\n")
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_s
|
37
|
+
"#<CSSTree:0x#{self.object_id}>"
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def render_declarations d
|
43
|
+
d.marshal_dump.map { |k,v| [deunderscore(k), v].join(": ") }.join("; ") + ";"
|
44
|
+
end
|
45
|
+
|
46
|
+
def parse_declarations d
|
47
|
+
r = d.strip.split(";").map { |d| d.split(":") }.map { |p,v| [underscore(p), v.strip] }
|
48
|
+
OpenStruct.new Hash[r]
|
49
|
+
end
|
50
|
+
|
51
|
+
def parse_block s, d
|
52
|
+
s.split(",").collect { |s| [s.strip, parse_declarations(d)] }.flatten
|
53
|
+
end
|
54
|
+
|
55
|
+
def trim s
|
56
|
+
s.gsub("\n", " ").gsub(/\s{2,}/, " ")
|
57
|
+
end
|
58
|
+
|
59
|
+
def underscore s
|
60
|
+
s.to_s.gsub("-", "_").strip
|
61
|
+
end
|
62
|
+
|
63
|
+
def deunderscore s
|
64
|
+
s.to_s.gsub("_", "-").strip
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe CSSTree do
|
4
|
+
let(:tree) { CSSTree.new }
|
5
|
+
|
6
|
+
it "parses single-line css" do
|
7
|
+
tree.parse "body { color: #000000; }"
|
8
|
+
tree.find("body").color.should == "#000000"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "converts dashed properties to underscores" do
|
12
|
+
tree.parse "body { background-color: #FFFFFF; }"
|
13
|
+
tree.find("body").background_color.should == "#FFFFFF"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "parses multiple lines of css" do
|
17
|
+
tree.parse <<-CSS
|
18
|
+
body { color: #000000; }
|
19
|
+
p { color: #111111; }
|
20
|
+
ul { list-style: none; }
|
21
|
+
CSS
|
22
|
+
tree.selectors.should == %w(body p ul)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "parses rules with multiple selectors" do
|
26
|
+
tree.parse "body, img { margin: 0 }"
|
27
|
+
tree.selectors.should == %w(body img)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "parses multiline rules" do
|
31
|
+
tree.parse <<-CSS
|
32
|
+
body {
|
33
|
+
color: #000000;
|
34
|
+
padding: 10px;
|
35
|
+
}
|
36
|
+
CSS
|
37
|
+
tree.find("body").padding.should == "10px"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "renders css back to text" do
|
41
|
+
tree.parse "body { background: #A2A2A2; }"
|
42
|
+
tree.find("body").background = "#999999"
|
43
|
+
tree.render.should == "body { background: #999999; }"
|
44
|
+
end
|
45
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csstree
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matte Noble
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-20 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &2158231020 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.6.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2158231020
|
25
|
+
description: Ultra-simple CSS parser.
|
26
|
+
email:
|
27
|
+
- me@mattenoble.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- .rspec
|
34
|
+
- Gemfile
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- csstree.gemspec
|
38
|
+
- lib/csstree.rb
|
39
|
+
- spec/csstree_spec.rb
|
40
|
+
- spec/spec_helper.rb
|
41
|
+
homepage:
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.8.6
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Ultra-simple CSS parser.
|
65
|
+
test_files:
|
66
|
+
- spec/csstree_spec.rb
|
67
|
+
- spec/spec_helper.rb
|