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' }
* { :type => "strip", :selector => 'h1' }

Direct Known Subclasses

Remove, RemoveClass, Strip, Unwrap, UnwrapRoot

Defined Under Namespace

Classes: Remove, RemoveClass, Strip, Unwrap, UnwrapRoot

Constant Summary collapse

TRANSFORMS =

Supported dom_transform types.

{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rule) ⇒ DomTransform

Creates a DOM Transform.



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

def initialize(rule)
  @rule = rule
end

Class Method Details

.create(rule) ⇒ Object

Creates a DOM Transform as per rule.



54
55
56
57
58
59
60
# File 'lib/sitediff/sanitize/dom_transform.rb', line 54

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

Registers a DOM Transform plugin.



48
49
50
# File 'lib/sitediff/sanitize/dom_transform.rb', line 48

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

Instance Method Details

#apply(node) ⇒ Object

Applies the transformation to a DOM node.



42
43
44
# File 'lib/sitediff/sanitize/dom_transform.rb', line 42

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

#targets(node, &block) ⇒ Object

TODO: Document what this method does.



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

def targets(node, &block)
  selectors = to_array(@rule['selector'])
  selectors.each do |sel|
    node.css(sel).each(&block)
  end
end

#to_array(val) ⇒ Object

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



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

def to_array(val)
  [val].flatten
end