Class: SiteDiff::Sanitizer::DomTransform

Inherits:
Object
  • Object
show all
Defined in:
lib/sitediff/sanitize/dom_transform.rb

Overview

Currently supported transforms:

* { :type => "unwrap_root" }
* { :type => "unwrap", :selector => "div.field-item" }
* { :type => "remove", :selector => "div.extra-stuff" }
* { :type => "remove_class", :class => 'class1' }

Direct Known Subclasses

Remove, RemoveClass, Unwrap, UnwrapRoot

Defined Under Namespace

Classes: Remove, RemoveClass, Unwrap, UnwrapRoot

Constant Summary collapse

Transforms =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rule) ⇒ DomTransform

Returns a new instance of DomTransform.



17
18
19
# File 'lib/sitediff/sanitize/dom_transform.rb', line 17

def initialize(rule)
  @rule = rule
end

Class Method Details

.create(rule) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/sitediff/sanitize/dom_transform.rb', line 41

def self.create(rule)
  (type = rule['type']) ||
    raise(InvalidSanitization, 'DOM transform needs a type')
  (transform = Transforms[type]) ||
    raise(InvalidSanitization, "No DOM transform named #{type}")
  transform.new(rule)
end

.register(name) ⇒ Object



37
38
39
# File 'lib/sitediff/sanitize/dom_transform.rb', line 37

def self.register(name)
  Transforms[name] = self
end

Instance Method Details

#apply(node) ⇒ Object



33
34
35
# File 'lib/sitediff/sanitize/dom_transform.rb', line 33

def apply(node)
  targets(node) { |t| process(t) }
end

#targets(node) ⇒ Object



26
27
28
29
30
31
# File 'lib/sitediff/sanitize/dom_transform.rb', line 26

def targets(node)
  selectors = to_array(@rule['selector'])
  selectors.each do |sel|
    node.css(sel).each { |n| yield n }
  end
end

#to_array(val) ⇒ Object

Often an array or scalar are both ok values. Turn either into an array.



22
23
24
# File 'lib/sitediff/sanitize/dom_transform.rb', line 22

def to_array(val)
  [val].flatten
end