html_surgeon 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 34619a8d9e7430b304e686b0a3dbdfb3d02ea140
4
- data.tar.gz: 672e8ff3f2e8f2fecb3b9121ab8a54d870175d40
3
+ metadata.gz: a5cc452a219671d16b9eb20cac5a1163b99771ae
4
+ data.tar.gz: b38ec70aa5f76edee83f9ca0a37871cdf7dbd455
5
5
  SHA512:
6
- metadata.gz: ef23b46708e249599fe9d28924f4ff7b2566fc5c01c55e055ff791d6680f1ef6355102ed278010bdcedcf4461814ae9c2b7629f1289c75fb8b1224f35e2e8c02
7
- data.tar.gz: 79e27faa58ffaf5aff691105ba67433eaf6d31d26105010eed840e42f049b9a98b2e8f3ff7e60c4a441b67741fd2815c8fb0384f78f946a5ca4a176e9c0b14ad
6
+ metadata.gz: a2929310917817e2405d1432568f00e87e195d818e98ea3bc6143ce117674e9bc0e6a4b6d89b783168ba5f47e30a165ba42e692eb90856e82e7e7dd63dc77f62
7
+ data.tar.gz: 255c2c883ee2e9a901109aa622f830c0c58b64a9d178c90cc174b57dd3f99bc7561475db5d6c40d4b0dc24b84525bc596e83dfdefde5d98604b14bd580445eae
data/README.md CHANGED
@@ -123,7 +123,7 @@ surgeon.css('.lol').replace_tag_name('span').add_css_class('hey').run
123
123
  surgeon.html # =>
124
124
  # <div>
125
125
  # <h1>Something</h1>
126
- # <span id="1" class="lol to-be-changed hey" data-surgeon-audit='[{"change_set":"830e96dc-fa07-40ce-8968-ea5c55ec4b84","changed_at":"2015-07-02T12:52:43.874Z","type":"replace_tag_name","old":"div","new":"span"},{"change_set":"830e96dc-fa07-40ce-8968-ea5c55ec4b84","changed_at":"2015-07-02T12:52:43.874Z","type":"add_css_class","existed_before":false,"class":"hey"}]'>1</span>
126
+ # <span id="1" class="lol to-be-changed hey" data-surgeon-audit='[{"change_set":"830e96dc-fa07-40ce-8968-ea5c55ec4b84","changed_at":"2015-07-02T12:52:43.874Z","type":"replace_tag_name","old":"div","new":"span"},{"change_set":"830e96dc-fa07-40ce-8968-ea5c55ec4b84","changed_at":"2015-07-02T12:52:43.874Z","type":"add_css_class","class":"hey"}]'>1</span>
127
127
  # <span>Other</span>
128
128
  # <div id="2" class="another to-be-changed">
129
129
  # <ul>
@@ -149,7 +149,6 @@ the attribute's value (formatted) is:
149
149
  "change_set":"830e96dc-fa07-40ce-8968-ea5c55ec4b84",
150
150
  "changed_at":"2015-07-02T12:52:43.874Z",
151
151
  "type":"add_css_class",
152
- "existed_before":false,
153
152
  "class":"hey"
154
153
  }
155
154
  ]
@@ -197,6 +196,12 @@ surgeon.css('div.to-be-changed').replace_tag_name('article')
197
196
  surgeon.css('div.to-be-changed').add_css_class('applied-some-stuff')
198
197
  ```
199
198
 
199
+ ### Remove Attribute
200
+
201
+ ```ruby
202
+ surgeon.css('div.to-be-changed').remove_attribute('style')
203
+ ```
204
+
200
205
  ## Rollback
201
206
 
202
207
  the surgeon can be used to revert any audited rollback. We can select what changes to rollback based on:
@@ -270,6 +275,11 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/eturin
270
275
  ## CHANGESET
271
276
 
272
277
 
278
+ ### v0.7.0
279
+
280
+ - added `remove_attribute` change.
281
+ - changes will be skipped if not needed on that node (will not do anything, nor be added to the audit).
282
+
273
283
  ### v0.6.0
274
284
 
275
285
  - *WARNING: BREAKING API CHANGE*: now `rollback` and `clear_audit` return the number of changes performed
data/lib/html_surgeon.rb CHANGED
@@ -11,6 +11,7 @@ require 'html_surgeon/change_set'
11
11
  require 'html_surgeon/change'
12
12
  require 'html_surgeon/changes'
13
13
  require 'html_surgeon/changes/add_css_class'
14
+ require 'html_surgeon/changes/remove_attribute'
14
15
  require 'html_surgeon/changes/replace_tag_name'
15
16
  require 'html_surgeon/node_services/audit_cleaner'
16
17
  require 'html_surgeon/node_services/reverser'
@@ -14,13 +14,19 @@ module HtmlSurgeon
14
14
  end
15
15
 
16
16
  def apply(node)
17
+ return false unless applicable?(node)
18
+
17
19
  auditor = auditor_class.new(node)
18
20
 
19
21
  auditor.add_change(audit_data(node))
20
22
  apply_in node
21
23
  auditor.apply
22
24
 
23
- self
25
+ true
26
+ end
27
+
28
+ def applicable?(_node)
29
+ raise AbstractMethodError, "a lazy developer has not implemented this method in #{self.class}"
24
30
  end
25
31
 
26
32
  def auditor_class
@@ -39,7 +45,7 @@ module HtmlSurgeon
39
45
  }
40
46
  end
41
47
 
42
- def audit_data(node)
48
+ def audit_data(_node)
43
49
  raise AbstractMethodError, "a lazy developer has not implemented this method in #{self.class}"
44
50
  end
45
51
 
@@ -84,14 +84,16 @@ module HtmlSurgeon
84
84
  end
85
85
  end
86
86
 
87
- do_apply_on_node(node)
88
- changed_nodes << node
87
+ applied = do_apply_on_node(node)
88
+ changed_nodes << node if applied
89
89
  end
90
90
 
91
91
  def do_apply_on_node(node)
92
- change_list.each do |change|
92
+ map = change_list.map do |change|
93
93
  change.apply(node)
94
94
  end
95
+
96
+ map.any? { |result| !!result }
95
97
  end
96
98
  end
97
99
  end
@@ -5,7 +5,7 @@ module HtmlSurgeon
5
5
 
6
6
  CLASS_ATTRIBUTE = 'class'.freeze
7
7
  CLASS_SEPARATOR = ' '.freeze
8
- CLASS_XPATH = './/@class'
8
+ CLASS_XPATH = './/@class'.freeze
9
9
 
10
10
  attr_reader :css_class
11
11
 
@@ -20,33 +20,36 @@ module HtmlSurgeon
20
20
  end
21
21
 
22
22
  private
23
+ def applicable?(node)
24
+ !self.class.has_class? node, css_class
25
+ end
26
+
23
27
  def apply_in(node)
24
- self.class.add_class_to_node css_class, node
28
+ self.class.add_class_to_node node, css_class
25
29
  end
26
30
 
27
- def audit_data(node)
28
- basic_audit_data.merge type: AUDIT_TYPE,
29
- existed_before: self.class.has_class?(node, css_class),
30
- class: css_class
31
+ def audit_data(_node)
32
+ basic_audit_data.merge type: AUDIT_TYPE,
33
+ class: css_class
31
34
  end
32
35
 
33
- def self.add_class_to_node(css_class, node)
36
+ def self.add_class_to_node(node, css_class)
34
37
  classes = node_classes node
35
38
  classes << css_class unless classes.include? css_class
36
- set_classes_in_node(classes, node)
39
+ set_classes_in_node(node, classes)
37
40
  end
38
41
 
39
- def self.remove_class_to_node(css_class, node)
42
+ def self.remove_class_to_node(node, css_class)
40
43
  classes = node_classes node
41
44
  classes.delete css_class
42
45
  if classes.empty?
43
46
  node.xpath(CLASS_XPATH).remove
44
47
  else
45
- set_classes_in_node(classes, node)
48
+ set_classes_in_node(node, classes)
46
49
  end
47
50
  end
48
51
 
49
- def self.set_classes_in_node(classes, node)
52
+ def self.set_classes_in_node(node, classes)
50
53
  node.set_attribute(CLASS_ATTRIBUTE, classes.join(CLASS_SEPARATOR))
51
54
  end
52
55
 
@@ -59,8 +62,7 @@ module HtmlSurgeon
59
62
  end
60
63
 
61
64
  def self.revert(node, change_definition)
62
- return if change_definition[:existed_before]
63
- remove_class_to_node(change_definition[:class], node)
65
+ remove_class_to_node(node, change_definition[:class])
64
66
  end
65
67
 
66
68
  module ChangeSetMethods
@@ -0,0 +1,60 @@
1
+ module HtmlSurgeon
2
+ module Changes
3
+ class RemoveAttribute < Change
4
+ AUDIT_TYPE = :remove_attribute
5
+
6
+ attr_reader :attribute
7
+
8
+ def initialize(attribute:, **other)
9
+ @attribute = attribute
10
+
11
+ super **other
12
+ end
13
+
14
+ def log
15
+ "remove attribute #{attribute}"
16
+ end
17
+
18
+ private
19
+ def applicable?(node)
20
+ self.class.has_attribute?(node, attribute)
21
+ end
22
+
23
+ def apply_in(node)
24
+ self.class.remove_attribute node, attribute
25
+ end
26
+
27
+ def audit_data(node)
28
+ basic_audit_data.merge type: AUDIT_TYPE,
29
+ attribute: attribute,
30
+ value: self.class.attribute_in_node(node, attribute)
31
+ end
32
+
33
+ def self.remove_attribute(node, attribute)
34
+ node.xpath(".//@#{attribute}").remove
35
+ end
36
+
37
+ def self.set_attribute_in_node(node, attribute, value)
38
+ node.set_attribute(attribute, value)
39
+ end
40
+
41
+ def self.has_attribute?(node, attribute)
42
+ node.has_attribute?(attribute) && attribute_in_node(node, attribute).present?
43
+ end
44
+
45
+ def self.attribute_in_node(node, attribute)
46
+ node.get_attribute(attribute).to_s
47
+ end
48
+
49
+ def self.revert(node, change_definition)
50
+ set_attribute_in_node(node, change_definition[:attribute], change_definition[:value])
51
+ end
52
+
53
+ module ChangeSetMethods
54
+ def remove_attribute(attribute)
55
+ chain_add_change Changes::RemoveAttribute.new(change_set: self, attribute: attribute)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -16,6 +16,11 @@ module HtmlSurgeon
16
16
  end
17
17
 
18
18
  private
19
+
20
+ def applicable?(node)
21
+ node.name.to_s != new_tag_name.to_s
22
+ end
23
+
19
24
  def apply_in(node)
20
25
  node.name = new_tag_name
21
26
  end
@@ -1,3 +1,3 @@
1
1
  module HtmlSurgeon
2
- VERSION = "0.6.0"
2
+ VERSION = '0.7.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_surgeon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Turiño
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-06 00:00:00.000000000 Z
11
+ date: 2015-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -145,6 +145,7 @@ files:
145
145
  - lib/html_surgeon/change_set.rb
146
146
  - lib/html_surgeon/changes.rb
147
147
  - lib/html_surgeon/changes/add_css_class.rb
148
+ - lib/html_surgeon/changes/remove_attribute.rb
148
149
  - lib/html_surgeon/changes/replace_tag_name.rb
149
150
  - lib/html_surgeon/node_services/audit_cleaner.rb
150
151
  - lib/html_surgeon/node_services/reverser.rb