class SiteDiff::Sanitizer::DomTransform
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' }
Constants
- TRANSFORMS
Supported dom_transform types.
Public Class Methods
create(rule)
click to toggle source
Creates a DOM Transform as per rule.
# 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
new(rule)
click to toggle source
Creates a DOM Transform.
# File lib/sitediff/sanitize/dom_transform.rb, line 21 def initialize(rule) @rule = rule end
register(name)
click to toggle source
Registers a DOM Transform plugin.
# File lib/sitediff/sanitize/dom_transform.rb, line 48 def self.register(name) TRANSFORMS[name] = self end
Public Instance Methods
apply(node)
click to toggle source
Applies the transformation to a DOM node.
# File lib/sitediff/sanitize/dom_transform.rb, line 42 def apply(node) targets(node) { |t| process(t) } end
targets(node) { |n| ... }
click to toggle source
TODO: Document what this method does.
# File lib/sitediff/sanitize/dom_transform.rb, line 33 def targets(node) selectors = to_array(@rule['selector']) selectors.each do |sel| node.css(sel).each { |n| yield n } end end
to_array(val)
click to toggle source
Often an array or scalar are both ok values. Turn either into an array.
# File lib/sitediff/sanitize/dom_transform.rb, line 27 def to_array(val) [val].flatten end