simple_selector 0.0.1

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.
data/Changelog ADDED
@@ -0,0 +1,2 @@
1
+ 0.0.1 (February 26, 2012)
2
+ Initial release.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Jacek Mikrut
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ SimpleSelector
2
+ ==============
3
+
4
+ SimpleSelector is a Ruby gem that **represents a CSS-like selector**, calculates its **specificity** and tells if it **matches** given HTML-like tags.
5
+
6
+ Content
7
+ -------
8
+
9
+ The selector content can consist of **tag names**, **IDs** and **class names**. An example:
10
+
11
+ ```ruby
12
+ SimpleSelector.new("tag_a#id_a.class_a #id_b .class_c.class_d")
13
+ ```
14
+
15
+ Specificity
16
+ -----------
17
+
18
+ #### Calculation rules
19
+
20
+ The specificity of a selector is calculated according to rules similar to the standard CSS rules.
21
+
22
+ The specificity is four numbers: **a,b,c,d**.
23
+
24
+ **The rules respected by this gem are**:
25
+
26
+ * **a** is always 0;
27
+ * **b** is the number of ID attributes in the selector;
28
+ * **c** is the number of class names in the selector;
29
+ * **d** is the number of tag names in the selector.
30
+
31
+ For example, a selector `tag#id1.class1 #id2.class2.class3` has specificity equal to `0,2,3,1`.
32
+
33
+ ```ruby
34
+ SimpleSelector.new("tag#id1.class1 #id2.class2.class3").specificity
35
+ => #<SimpleSelector::Specificity "0,2,3,1">
36
+ ```
37
+
38
+ #### Comparison
39
+
40
+ Specificity allows selector comparison.
41
+
42
+ Two selector specificities are compared by succesively comparing their corresponding numbers, from left to right.
43
+
44
+ Tag matching
45
+ ------------
46
+
47
+ A SimpleSelector object tells if it matches given HTML-like tags.
48
+
49
+ The tag is expected to be represented by an object which responds to :name, :id, :class_names and :parent messages. An example:
50
+
51
+ ```ruby
52
+ Tag = Struct.new(:name, :id, :class_names, :parent)
53
+
54
+ # Tag objects corresponding to the structure:
55
+ # <section id="content"><message class="success">...</message></section>
56
+ content_tag = Tag.new("section", "content", [], nil)
57
+ message_tag = Tag.new("message", nil, ["success"], content_tag)
58
+ ```
59
+
60
+ And now:
61
+
62
+ ```ruby
63
+ SimpleSelector.new("section#content message.success").match?(message_tag)
64
+ => true
65
+ ```
66
+
67
+ Installation
68
+ ------------
69
+
70
+ As a Ruby gem, SimpleSelector can be installed either by running
71
+
72
+ ```bash
73
+ gem install simple_selector
74
+ ```
75
+
76
+ or adding
77
+
78
+ ```ruby
79
+ gem "simple_selector"
80
+ ```
81
+
82
+ to the Gemfile and then invoking `bundle install`.
83
+
84
+ License
85
+ -------
86
+
87
+ License is included in the LICENSE file.
@@ -0,0 +1,66 @@
1
+ require "simple_selector/version"
2
+
3
+ require "simple_selector/specificity"
4
+ require "simple_selector/segment"
5
+
6
+ class SimpleSelector
7
+
8
+ def initialize(string=nil)
9
+ @segments = []
10
+ @specificity = Specificity.new
11
+ concat(string) unless string.nil?
12
+ end
13
+
14
+ attr_reader :specificity
15
+
16
+ def concat(string)
17
+ string.scan(/[\w.#]+/).map { |s| Segment.new(s) }.each do |segment|
18
+ @segments << segment
19
+ @specificity += segment.specificity
20
+ end
21
+ self
22
+ end
23
+
24
+ def +(string)
25
+ duplicate.concat(string)
26
+ end
27
+
28
+ def match?(tag)
29
+ return true if @segments.none?
30
+ return false unless @segments.last.match?(tag)
31
+
32
+ index = @segments.size - 2
33
+ current_tag = tag
34
+
35
+ while index >= 0 && current_tag = current_tag.parent
36
+ if @segments[index].match?(current_tag)
37
+ index -= 1
38
+ next
39
+ end
40
+ end
41
+ index == -1
42
+ end
43
+
44
+ def empty?
45
+ @segments.none?
46
+ end
47
+
48
+ def to_s
49
+ @segments.map { |segment| segment.to_s }.join(" ")
50
+ end
51
+
52
+ def inspect
53
+ "#<#{self.class} #{to_s.inspect}>"
54
+ end
55
+
56
+ def ==(other)
57
+ to_s == other.to_s
58
+ end
59
+
60
+ def duplicate
61
+ d = dup
62
+ d.instance_variable_set( "@segments", @segments.dup)
63
+ d.instance_variable_set("@specificity", @specificity.dup)
64
+ d
65
+ end
66
+ end
@@ -0,0 +1,38 @@
1
+ class SimpleSelector
2
+ class Segment
3
+
4
+ def initialize(string)
5
+ @tag_name = string.lstrip.slice(/^\w+/)
6
+ @id = string.slice(/(?<=\#)\w+/)
7
+ @class_names = string.scan(/(?<=\.)\w+/)
8
+ end
9
+
10
+ attr_reader :tag_name, :id, :class_names
11
+
12
+ def specificity
13
+ @specificity ||= Specificity.new(
14
+ 0,
15
+ id ? 1 : 0,
16
+ class_names.count,
17
+ tag_name ? 1 : 0
18
+ )
19
+ end
20
+
21
+ def match?(tag)
22
+ return false if tag_name && tag_name != tag.name
23
+ return false if id && id != tag.id
24
+ return false if (class_names - tag.class_names).any?
25
+ true
26
+ end
27
+
28
+ def to_s
29
+ "#{tag_name}" +
30
+ (id ? "##{id}" : "") +
31
+ class_names.map { |class_name| ".#{class_name}" }.join
32
+ end
33
+
34
+ def inspect
35
+ "#<#{self.class} #{to_s.inspect}>"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,32 @@
1
+ class SimpleSelector
2
+ class Specificity
3
+
4
+ include Comparable
5
+
6
+ def initialize(a=0, b=0, c=0, d=0)
7
+ @a, @b, @c, @d = a, b, c, d
8
+ end
9
+
10
+ attr_accessor :a, :b, :c, :d
11
+
12
+ def <=>(other)
13
+ [:a, :b, :c, :d].each do |name|
14
+ result = send(name) <=> other.send(name)
15
+ return result unless result == 0
16
+ end
17
+ 0
18
+ end
19
+
20
+ def +(other)
21
+ self.class.new(a + other.a, b + other.b, c + other.c, d + other.d)
22
+ end
23
+
24
+ def to_s
25
+ "#{a},#{b},#{c},#{d}"
26
+ end
27
+
28
+ def inspect
29
+ "#<#{self.class} #{to_s.inspect}>"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ class SimpleSelector
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_selector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jacek Mikrut
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-26 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &76532010 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *76532010
25
+ description: Represents a CSS-like selector, calculates its specificity and tells
26
+ if it matches given HTML-like tags.
27
+ email: jacekmikrut.software@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/simple_selector.rb
33
+ - lib/simple_selector/specificity.rb
34
+ - lib/simple_selector/version.rb
35
+ - lib/simple_selector/segment.rb
36
+ - README.md
37
+ - LICENSE
38
+ - Changelog
39
+ homepage: http://github.com/jacekmikrut/simple_selector
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.12
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Represents a CSS-like selector, calculates its specificity and tells if it
63
+ matches given HTML-like tags.
64
+ test_files: []