former 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/former.gemspec CHANGED
@@ -15,6 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
16
  gem.require_paths = ["lib"]
17
17
  gem.add_dependency("nokogiri", ">= 1.6.0")
18
+ gem.add_dependency("csspool", ">= 3.0.0")
18
19
  gem.add_development_dependency("rake")
19
20
  gem.add_development_dependency("rdoc")
20
21
  end
@@ -25,7 +25,7 @@ module Former
25
25
  @html.traverse_prefix do |e|
26
26
  (matches[e] || []).each do |query|
27
27
  if query[:block].nil? or query[:block].call(e)
28
- @editable << Element.new(e, query[:query], @editable.length)
28
+ @editable << Element.new(e, query[:query], @editable.length, query[:args])
29
29
  end
30
30
  end
31
31
  end
@@ -66,15 +66,21 @@ module Former
66
66
  }
67
67
  end
68
68
 
69
- def self.attr(elem, attr, &block)
69
+ def self.attr(elem, attr, args=nil, &block)
70
70
  @queries ||= {}
71
71
  @queries[elem] ||= []
72
- @queries[elem] << { :query => attr, :block => block }
72
+ @queries[elem] << { :query => attr, :block => block, :args => args }
73
73
  end
74
74
 
75
75
  def self.text(*elems, &block)
76
76
  attr("text()", :text, &block) if elems.length == 0
77
77
  elems.each { |elem| attr(elem, :text, &block) }
78
78
  end
79
+
80
+ def self.style_url(property, &block)
81
+ attr("[@style]", :style_url, { :property => property }) { |elem|
82
+ elem['style'].include? property and (block_given? ? block.call(elem) : true)
83
+ }
84
+ end
79
85
  end
80
86
  end
@@ -1,31 +1,41 @@
1
1
  require 'json'
2
+ require 'csspool'
2
3
 
3
4
  module Former
4
5
  class Element
5
6
  attr_reader :node, :query
6
7
 
7
- def initialize(node, query, index)
8
+ def initialize(node, query, index, args=nil)
8
9
  @node = node
9
10
  @query = query
10
11
  @index = index
12
+ @args = args || {}
11
13
  end
12
14
 
13
15
  def to_json
14
16
  h = {
15
- :value => (@query == :text) ? @node.text : @node[@query],
17
+ :value => value,
16
18
  :nodename => @node.name
17
19
  }
18
- h[:attr] = @query unless @query == :text
20
+ h[:attr] = @query unless (@query == :text or @query == :style_url)
19
21
  h.to_json
20
22
  end
21
23
 
22
24
  def value
23
- @query == :text ? @node.content : @node[@query]
25
+ if @query == :text
26
+ @node.text
27
+ elsif @query == :style_url
28
+ style_url(@node['style']) { |exp| return exp.value }
29
+ else
30
+ @node[@query]
31
+ end
24
32
  end
25
33
 
26
34
  def value=(value)
27
35
  if @query == :text
28
36
  @node.content = value
37
+ elsif @query == :style_url
38
+ @node['style'] = style_url(@node['style']) { |exp| exp.value = value }
29
39
  else
30
40
  @node[@query] = value
31
41
  end
@@ -34,5 +44,21 @@ module Former
34
44
  def to_html
35
45
  @node.to_html
36
46
  end
47
+
48
+ private
49
+
50
+ def style_url(rules)
51
+ begin
52
+ rset = CSSPool.CSS("e { #{rules} }").rule_sets.first
53
+ decs = rset.declarations.select { |d| d.property == @args[:property] }
54
+ decs.each do |dec|
55
+ dec.expressions.select { |e| e.is_a? CSSPool::Terms::URI }.each { |d| yield d }
56
+ end
57
+ css = rset.to_minified_css
58
+ css.slice(4, css.length - 6)
59
+ rescue Racc::ParseError
60
+ rules
61
+ end
62
+ end
37
63
  end
38
64
  end
@@ -1,3 +1,3 @@
1
1
  module Former
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
data/test/builder_test.rb CHANGED
@@ -7,6 +7,11 @@ class Parser < Former::Builder
7
7
  text "p", "a.important"
8
8
  end
9
9
 
10
+ class StyleParser < Former::Builder
11
+ style_url "background-image"
12
+ attr "img", :src
13
+ end
14
+
10
15
  class AllTextParser < Former::Builder
11
16
  attr "a.important", :href
12
17
  attr("img", :src)
@@ -25,6 +30,23 @@ class BuilderTest < Test::Unit::TestCase
25
30
  @parser = Parser.new @html_txt
26
31
  end
27
32
 
33
+ def test_style_url
34
+ p = StyleParser.new "<div style=\"background-image: url('http://blah.com/image.jpg');\">blah</div>"
35
+ assert_equal p.length, 1
36
+ p[0] = "http://another.com/image.jpg"
37
+ assert_equal p.to_html, "<div style='background-image: url(\"http://another.com/image.jpg\");'>blah</div>"
38
+
39
+ p = Parser.new "<div style=\"color: #ffffff;\">blah</div>"
40
+ assert_equal p.length, 0
41
+
42
+ p = StyleParser.new "<div style=\"background-image: url('http://blah.com/image.jpg');\"><img src=\"/pic.jpg\" /></div>"
43
+ assert_equal p.length, 2
44
+ p[0] = "http://another.com/image.jpg"
45
+ assert_equal p.to_html, "<div style='background-image: url(\"http://another.com/image.jpg\");'><img src=\"/pic.jpg\"></div>"
46
+ p[1] = "/blah.jpg"
47
+ assert_equal p.to_html, "<div style='background-image: url(\"http://another.com/image.jpg\");'><img src=\"/blah.jpg\"></div>"
48
+ end
49
+
28
50
  def test_ignore_blank_fields
29
51
  p = AllTextParserNotBlank.new "<p>\n</p><h1> </h1><p> some text </p>"
30
52
  assert_equal p.length, 1
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: former
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-08 00:00:00.000000000 Z
12
+ date: 2014-03-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: 1.6.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: csspool
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.0.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 3.0.0
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: rake
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -94,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
110
  version: '0'
95
111
  segments:
96
112
  - 0
97
- hash: 4051511182999902248
113
+ hash: -3383286526901818744
98
114
  required_rubygems_version: !ruby/object:Gem::Requirement
99
115
  none: false
100
116
  requirements:
@@ -103,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
119
  version: '0'
104
120
  segments:
105
121
  - 0
106
- hash: 4051511182999902248
122
+ hash: -3383286526901818744
107
123
  requirements: []
108
124
  rubyforge_project:
109
125
  rubygems_version: 1.8.25