csspool-st 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. data/.autotest +16 -0
  2. data/.gemtest +0 -0
  3. data/CHANGELOG.rdoc +87 -0
  4. data/Manifest.txt +62 -0
  5. data/README.rdoc +65 -0
  6. data/Rakefile +50 -0
  7. data/lib/csspool/collection.rb +50 -0
  8. data/lib/csspool/css/charset.rb +13 -0
  9. data/lib/csspool/css/declaration.rb +19 -0
  10. data/lib/csspool/css/document.rb +34 -0
  11. data/lib/csspool/css/document_handler.rb +51 -0
  12. data/lib/csspool/css/import_rule.rb +27 -0
  13. data/lib/csspool/css/media.rb +13 -0
  14. data/lib/csspool/css/parser.rb +1298 -0
  15. data/lib/csspool/css/parser.y +398 -0
  16. data/lib/csspool/css/rule_set.rb +17 -0
  17. data/lib/csspool/css/tokenizer.rb +231 -0
  18. data/lib/csspool/css/tokenizer.rex +97 -0
  19. data/lib/csspool/css.rb +8 -0
  20. data/lib/csspool/node.rb +41 -0
  21. data/lib/csspool/sac/document.rb +35 -0
  22. data/lib/csspool/sac/parser.rb +16 -0
  23. data/lib/csspool/sac.rb +2 -0
  24. data/lib/csspool/selector.rb +36 -0
  25. data/lib/csspool/selectors/additional.rb +6 -0
  26. data/lib/csspool/selectors/attribute.rb +21 -0
  27. data/lib/csspool/selectors/class.rb +11 -0
  28. data/lib/csspool/selectors/id.rb +11 -0
  29. data/lib/csspool/selectors/pseudo_class.rb +13 -0
  30. data/lib/csspool/selectors/simple.rb +22 -0
  31. data/lib/csspool/selectors/type.rb +6 -0
  32. data/lib/csspool/selectors/universal.rb +6 -0
  33. data/lib/csspool/selectors.rb +9 -0
  34. data/lib/csspool/terms/function.rb +17 -0
  35. data/lib/csspool/terms/hash.rb +6 -0
  36. data/lib/csspool/terms/ident.rb +15 -0
  37. data/lib/csspool/terms/number.rb +14 -0
  38. data/lib/csspool/terms/rgb.rb +20 -0
  39. data/lib/csspool/terms/string.rb +6 -0
  40. data/lib/csspool/terms/uri.rb +6 -0
  41. data/lib/csspool/terms.rb +7 -0
  42. data/lib/csspool/visitors/children.rb +50 -0
  43. data/lib/csspool/visitors/comparable.rb +84 -0
  44. data/lib/csspool/visitors/iterator.rb +80 -0
  45. data/lib/csspool/visitors/to_css.rb +248 -0
  46. data/lib/csspool/visitors/visitor.rb +17 -0
  47. data/lib/csspool/visitors.rb +5 -0
  48. data/lib/csspool.rb +21 -0
  49. data/test/css/test_document.rb +13 -0
  50. data/test/css/test_import_rule.rb +42 -0
  51. data/test/css/test_parser.rb +462 -0
  52. data/test/css/test_tokenizer.rb +320 -0
  53. data/test/helper.rb +65 -0
  54. data/test/sac/test_parser.rb +123 -0
  55. data/test/sac/test_properties.rb +43 -0
  56. data/test/sac/test_terms.rb +228 -0
  57. data/test/test_collection.rb +81 -0
  58. data/test/test_parser.rb +91 -0
  59. data/test/test_selector.rb +51 -0
  60. data/test/visitors/test_children.rb +20 -0
  61. data/test/visitors/test_comparable.rb +102 -0
  62. data/test/visitors/test_each.rb +19 -0
  63. data/test/visitors/test_to_css.rb +309 -0
  64. metadata +214 -0
data/.autotest ADDED
@@ -0,0 +1,16 @@
1
+ # -*- ruby -*-
2
+
3
+ begin
4
+ require 'autotest/fsevent'
5
+ rescue LoadError
6
+ end
7
+
8
+ Autotest.add_hook :run_command do |at|
9
+ at.unit_diff = 'cat'
10
+ end
11
+
12
+ Autotest.add_hook :ran_command do |at|
13
+ File.open('/tmp/autotest.txt', 'wb') { |f|
14
+ f.write(at.results.join)
15
+ }
16
+ end
data/.gemtest ADDED
File without changes
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,87 @@
1
+ == 3.1.0
2
+
3
+ * New Features
4
+
5
+ * Support IE safe hacks
6
+ * Support pseudo-elements
7
+ * Support css3 media queries
8
+
9
+ * Bugfixes
10
+
11
+ * Support whitespace after properties
12
+ * Support missing semicolon after last declaration
13
+
14
+ == 3.0.0
15
+
16
+ * New Features
17
+
18
+ * Pure ruby: no longer uses C based back end.
19
+
20
+ == 2.0.0
21
+
22
+ * Bugfixes
23
+
24
+ * Uh... Many.
25
+
26
+ * New Features
27
+
28
+ * Now wraps libcroco via FFI
29
+
30
+ == 0.2.6
31
+
32
+ * Fix comment greediness. [Seth Rasmussen]
33
+
34
+ == 0.2.5
35
+
36
+ * Accepting spaces after error rules.
37
+ * Comments with asterisks tokenize correctly. [Seth Rasmussen]
38
+ * Stop polluting the global namespace with includes. [Thomas Kadauke]
39
+
40
+ == 0.2.4
41
+
42
+ * Fixed error handling on at rules. Thanks Dan for tests!
43
+ * Made specificity an array
44
+ * Made StyleSheet::Rule comparable on specificity
45
+
46
+ == 0.2.3
47
+
48
+ * Fixed a bug where nil selectors on conditional selectors were being visited.
49
+
50
+ == 0.2.2
51
+
52
+ * I suck.
53
+
54
+ == 0.2.1
55
+
56
+ * Recovering from unexpected tokens in the properties section.
57
+
58
+ == 0.2.0
59
+
60
+ * Added CSS::SAC::Parser#parse_rule to parse a single rule.
61
+ * Added CSS::StyleSheet#find_rule for finding a particular rule.
62
+ * Added CSS::StyleSheet#rules_matching for finding all rules matching a node.
63
+ * Added CSS::StyleSheet#create_rule for creating a new rule.
64
+ * Added CSS::StyleSheet#find_all_rules_matching for finding all rules that match
65
+ any node in the passed in document.
66
+ * Added .eql? to selector AST
67
+ * Added .hash to selector AST
68
+ * Added .eql? to LexicalUnits
69
+ * Added .hash to LexicalUnits
70
+ * Added CSS::StyleSheet#to_css
71
+ * Added CSS::StyleSheet#reduce!
72
+ * CSS::StyleSheet is now the default document handler
73
+
74
+ == 0.1.1
75
+
76
+ * Added specificity to selectors.
77
+ * Added == to selector ASTs.
78
+ * Added =~ to selector ASTs. The object passed in to =~ must quack like an
79
+ hpricot node. It must implement the following methods: name, parent, child,
80
+ next_sibling, attributes.
81
+ * Added .to_xpath to the selector AST.
82
+ * Fixed a bug where functions as pseudo classes didn't parse.
83
+
84
+ == 0.1.0
85
+
86
+ * Birthday!
87
+
data/Manifest.txt ADDED
@@ -0,0 +1,62 @@
1
+ .autotest
2
+ CHANGELOG.rdoc
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/csspool.rb
7
+ lib/csspool/collection.rb
8
+ lib/csspool/css.rb
9
+ lib/csspool/css/charset.rb
10
+ lib/csspool/css/declaration.rb
11
+ lib/csspool/css/document.rb
12
+ lib/csspool/css/document_handler.rb
13
+ lib/csspool/css/import_rule.rb
14
+ lib/csspool/css/media.rb
15
+ lib/csspool/css/parser.rb
16
+ lib/csspool/css/parser.y
17
+ lib/csspool/css/rule_set.rb
18
+ lib/csspool/css/tokenizer.rb
19
+ lib/csspool/css/tokenizer.rex
20
+ lib/csspool/node.rb
21
+ lib/csspool/sac.rb
22
+ lib/csspool/sac/document.rb
23
+ lib/csspool/sac/parser.rb
24
+ lib/csspool/selector.rb
25
+ lib/csspool/selectors.rb
26
+ lib/csspool/selectors/additional.rb
27
+ lib/csspool/selectors/attribute.rb
28
+ lib/csspool/selectors/class.rb
29
+ lib/csspool/selectors/id.rb
30
+ lib/csspool/selectors/pseudo_class.rb
31
+ lib/csspool/selectors/simple.rb
32
+ lib/csspool/selectors/type.rb
33
+ lib/csspool/selectors/universal.rb
34
+ lib/csspool/terms.rb
35
+ lib/csspool/terms/function.rb
36
+ lib/csspool/terms/hash.rb
37
+ lib/csspool/terms/ident.rb
38
+ lib/csspool/terms/number.rb
39
+ lib/csspool/terms/rgb.rb
40
+ lib/csspool/terms/string.rb
41
+ lib/csspool/terms/uri.rb
42
+ lib/csspool/visitors.rb
43
+ lib/csspool/visitors/children.rb
44
+ lib/csspool/visitors/comparable.rb
45
+ lib/csspool/visitors/iterator.rb
46
+ lib/csspool/visitors/to_css.rb
47
+ lib/csspool/visitors/visitor.rb
48
+ test/css/test_document.rb
49
+ test/css/test_import_rule.rb
50
+ test/css/test_parser.rb
51
+ test/css/test_tokenizer.rb
52
+ test/helper.rb
53
+ test/sac/test_parser.rb
54
+ test/sac/test_properties.rb
55
+ test/sac/test_terms.rb
56
+ test/test_collection.rb
57
+ test/test_parser.rb
58
+ test/test_selector.rb
59
+ test/visitors/test_children.rb
60
+ test/visitors/test_comparable.rb
61
+ test/visitors/test_each.rb
62
+ test/visitors/test_to_css.rb
data/README.rdoc ADDED
@@ -0,0 +1,65 @@
1
+ = CSSPool
2
+
3
+ * http://csspool.rubyforge.org
4
+ * http://github.com/tenderlove/csspool
5
+
6
+ == DESCRIPTION:
7
+
8
+ CSSPool is a CSS parser. CSSPool provides a SAC interface for parsing CSS as
9
+ well as a document oriented interface for parsing CSS.
10
+
11
+ == FEATURES/PROBLEMS:
12
+
13
+ CSSPool no longer depends on libcroco! There is now a dependency on both the
14
+ {racc}[http://github.com/tenderlove/racc] and
15
+ {rexical}[http://github.com/tenderlove/rexical] gems, so make sure to install
16
+ them first.
17
+
18
+ $ sudo gem install racc
19
+ $ sudo gem install rexical
20
+
21
+ == SYNOPSIS:
22
+
23
+ doc = CSSPool.CSS open('/path/to/css.css')
24
+ doc.rule_sets.each do |rs|
25
+ puts rs.to_css
26
+ end
27
+
28
+ puts doc.to_css
29
+
30
+ == REQUIREMENTS:
31
+
32
+ * racc ("sudo gem install racc")
33
+ * rexical ("sudo gem install rexical")
34
+
35
+ == INSTALL:
36
+
37
+ * sudo gem install csspool
38
+
39
+ == LICENSE:
40
+
41
+ (The MIT License)
42
+
43
+ Copyright (c) 2007-2012
44
+
45
+ * {Aaron Patterson}[http://tenderlovemaking.com]
46
+ * {John Barnette}[http://www.jbarnette.com/]
47
+
48
+ Permission is hereby granted, free of charge, to any person obtaining
49
+ a copy of this software and associated documentation files (the
50
+ 'Software'), to deal in the Software without restriction, including
51
+ without limitation the rights to use, copy, modify, merge, publish,
52
+ distribute, sublicense, and/or sell copies of the Software, and to
53
+ permit persons to whom the Software is furnished to do so, subject to
54
+ the following conditions:
55
+
56
+ The above copyright notice and this permission notice shall be
57
+ included in all copies or substantial portions of the Software.
58
+
59
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
60
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
61
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
62
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
63
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
64
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
65
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ GENERATED_TOKENIZER = "lib/csspool/css/tokenizer.rb"
7
+ GENERATED_PARSER = "lib/csspool/css/parser.rb"
8
+
9
+ Hoe.plugin :git
10
+ Hoe.plugin :bundler
11
+ Hoe.plugin :gemspec
12
+
13
+ Hoe.spec('csspool-st') do
14
+ developer('Aaron Patterson', 'aaronp@rubyforge.org')
15
+ developer('John Barnette', 'jbarnette@rubyforge.org')
16
+ developer('stereobooster', 'stereobooster@gmail.com')
17
+ self.readme_file = 'README.rdoc'
18
+ self.history_file = 'CHANGELOG.rdoc'
19
+ self.extra_rdoc_files = FileList['*.rdoc']
20
+
21
+ %w{racc rexical hoe-git hoe-gemspec hoe-bundler}.each do |dep|
22
+ self.extra_dev_deps << [dep, '>= 0']
23
+ end
24
+ end
25
+
26
+ [:test, :check_manifest, "gem:spec"].each do |task_name|
27
+ Rake::Task[task_name].prerequisites << :compile
28
+ end
29
+
30
+ task :compile => [GENERATED_TOKENIZER, GENERATED_PARSER]
31
+
32
+ file GENERATED_TOKENIZER => "lib/csspool/css/tokenizer.rex" do |t|
33
+ begin
34
+ sh "bundle exec rex -i --independent -o #{t.name} #{t.prerequisites.first}"
35
+ rescue
36
+ abort "need rexical, sudo gem install rexical"
37
+ end
38
+ end
39
+
40
+ file GENERATED_PARSER => "lib/csspool/css/parser.y" do |t|
41
+ begin
42
+ racc = 'bundle exec racc'
43
+ #sh "#{racc} -l -o #{t.name} #{t.prerequisites.first}"
44
+ sh "#{racc} -o #{t.name} #{t.prerequisites.first}"
45
+ rescue
46
+ abort "need racc, sudo gem install racc"
47
+ end
48
+ end
49
+
50
+ # vim: syntax=Ruby
@@ -0,0 +1,50 @@
1
+ module CSSPool
2
+ class Collection
3
+ include Enumerable
4
+
5
+ def initialize &block
6
+ @docs = []
7
+ @block = block
8
+ end
9
+
10
+ def << string
11
+ doc = CSSPool.CSS string
12
+
13
+ import_tree = [[doc]]
14
+
15
+ imported_urls = {}
16
+
17
+ until import_tree.last.all? { |x| x.import_rules.length == 0 }
18
+ level = import_tree.last
19
+ import_tree << []
20
+ level.each do |l|
21
+ l.import_rules.each do |ir|
22
+ next if imported_urls.key? ir.uri
23
+
24
+ new_doc = ir.load(&@block)
25
+
26
+ imported_urls[ir.uri] = ir.load(&@block)
27
+ import_tree.last << new_doc
28
+ end
29
+ end
30
+ end
31
+
32
+ @docs += import_tree.flatten.reverse
33
+ self
34
+ end
35
+
36
+ def length
37
+ @docs.length
38
+ end
39
+
40
+ def [] idx
41
+ @docs[idx]
42
+ end
43
+
44
+ def each &block
45
+ @docs.each(&block)
46
+ end
47
+
48
+ def last; @docs.last; end
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ module CSSPool
2
+ module CSS
3
+ class Charset < CSSPool::Node
4
+ attr_accessor :name
5
+ attr_accessor :parse_location
6
+
7
+ def initialize name, parse_location
8
+ @name = name
9
+ @parse_location = parse_location
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ module CSSPool
2
+ module CSS
3
+ class Declaration < CSSPool::Node
4
+ attr_accessor :property
5
+ attr_accessor :expressions
6
+ attr_accessor :important
7
+ attr_accessor :rule_set
8
+
9
+ alias :important? :important
10
+
11
+ def initialize property, expressions, important, rule_set
12
+ @property = property
13
+ @expressions = expressions
14
+ @important = important
15
+ @rule_set = rule_set
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,34 @@
1
+ module CSSPool
2
+ module CSS
3
+ class Document < Node
4
+ def self.parse string
5
+ unless string && string.length > 0
6
+ return CSSPool::CSS::Document.new
7
+ end
8
+ handler = CSSPool::CSS::DocumentHandler.new
9
+ parser = CSSPool::SAC::Parser.new(handler)
10
+ parser.parse(string)
11
+ handler.document
12
+ end
13
+
14
+ attr_accessor :rule_sets
15
+ attr_accessor :charsets
16
+ attr_accessor :import_rules
17
+ attr_accessor :parent
18
+ attr_accessor :parent_import_rule
19
+
20
+ def initialize
21
+ @rule_sets = []
22
+ @charsets = []
23
+ @import_rules = []
24
+ @parent = nil
25
+ @parent_import_rule = nil
26
+ end
27
+
28
+ def [] selector
29
+ selectors = CSSPool.CSS("#{selector} {}").rule_sets.first.selectors
30
+ rule_sets.find_all { |rs| rs.selectors == selectors}
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,51 @@
1
+ module CSSPool
2
+ module CSS
3
+ class DocumentHandler < CSSPool::SAC::Document
4
+ attr_accessor :document
5
+
6
+ def initialize
7
+ @document = nil
8
+ @media_stack = []
9
+ end
10
+
11
+ def start_document
12
+ @document = CSSPool::CSS::Document.new
13
+ end
14
+
15
+ def charset name, location
16
+ @document.charsets << CSS::Charset.new(name, location)
17
+ end
18
+
19
+ def import_style media_list, uri, ns = nil, loc = {}
20
+ @document.import_rules << CSS::ImportRule.new(
21
+ uri,
22
+ ns,
23
+ media_list.map { |x| CSS::Media.new(x, loc) },
24
+ @document,
25
+ loc
26
+ )
27
+ end
28
+
29
+ def start_selector selector_list
30
+ @document.rule_sets << RuleSet.new(
31
+ selector_list,
32
+ [],
33
+ @media_stack.last || []
34
+ )
35
+ end
36
+
37
+ def property name, exp, important
38
+ rs = @document.rule_sets.last
39
+ rs.declarations << Declaration.new(name, exp, important, rs)
40
+ end
41
+
42
+ def start_media media_list, parse_location = {}
43
+ @media_stack << media_list.map { |x| CSS::Media.new(x, parse_location) }
44
+ end
45
+
46
+ def end_media media_list, parse_location = {}
47
+ @media_stack.pop
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,27 @@
1
+ module CSSPool
2
+ module CSS
3
+ class ImportRule < CSSPool::Node
4
+ attr_accessor :uri
5
+ attr_accessor :namespace
6
+ attr_accessor :media
7
+ attr_accessor :document
8
+ attr_accessor :parse_location
9
+
10
+ def initialize uri, namespace, media, document, parse_location
11
+ @uri = uri
12
+ @namespace = namespace
13
+ @media = media
14
+ @document = document
15
+ @parse_location = parse_location
16
+ end
17
+
18
+ def load
19
+ new_doc = CSSPool.CSS(yield uri.value)
20
+ new_doc.parent_import_rule = self
21
+ new_doc.parent = document
22
+ new_doc.rule_sets.each { |rs| rs.media = media }
23
+ new_doc
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ module CSSPool
2
+ module CSS
3
+ class Media < CSSPool::Node
4
+ attr_accessor :name
5
+ attr_accessor :parse_location
6
+
7
+ def initialize name, parse_location
8
+ @name = name
9
+ @parse_location = parse_location
10
+ end
11
+ end
12
+ end
13
+ end