Class: Hpricot::Scrub

Inherits:
Object
  • Object
show all
Defined in:
lib/hpricot_scrub/hpricot_scrub.rb

Class Method Summary collapse

Class Method Details

.keep_attribute?(parent_element, key, value, attribute_rule) ⇒ Boolean

Takes:

An element
An attribute key found in that element
The attribute value attached to the key
An attribute rule

Checks the rule aginst the attribute and returns:

true = the attribute should be kept
false = the attribute should NOT be kept

Acceptable attribute rules are:

true: Keep the attribute without inspection
a String: Attribute value must be the same as the string
an Array: Attribute key must exist in the array
a Regexp: Attribute value must match the regexp
a Hash: The attribute key is found in the hash, and the value is considered a new rule and follows these same rules via recursion
a Proc: The Proc is called with arguments (parent_element, key, value), the returned value is considered a new rule and follows these same rules via recursion
otherwise: Remove the attribute

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/hpricot_scrub/hpricot_scrub.rb', line 91

def self.keep_attribute?(parent_element, key, value, attribute_rule)

  if attribute_rule == true
    keep = true
  elsif attribute_rule.is_a?(String)
    keep = (attribute_rule == value)
  elsif attribute_rule.is_a?(Array)
    keep = attribute_rule.include?(key)
  elsif attribute_rule.is_a?(Regexp)
    keep = attribute_rule.match(value)
  elsif attribute_rule.is_a?(Hash)
    # Allow hash value to be new rule via recursion
    new_rule = attribute_rule[key]
    keep = keep_attribute?(parent_element, key, value, new_rule)
  elsif attribute_rule.is_a?(Proc)
    # Allow the proc to return a new rule - recurse:
    new_rule = attribute_rule.call(parent_element, key, value)
    keep = keep_attribute?(parent_element, key, value, new_rule)
  else
    # Err on the side of caution
    keep = false
  end

  return keep

end

.normalize_config(config) ⇒ Object

:nodoc:#



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hpricot_scrub/hpricot_scrub.rb', line 15

def self.normalize_config(config) #:nodoc:#
  config = {} unless config.is_a?(Hash)

  return config if config[:normalized]

  config = {

    # Legacy config keys:
    :remove_tags => [],
    :allow_tags => [],
    :allow_attributes => [],

    # New fine-grained hotness:
    :elem_rules => {
      "script"  =>  false,
      "style"  =>  false
    },
    :default_elem_rule => :strip,
    :default_comment_rule => false,
    :default_attribute_rule => false

  }.merge(config)

  #
  # Merge+delete legacy config keys
  #
  # :remove_tags -> :elem_rules (false)
  (config.delete(:remove_tags) || []).each do |tag|
    config[:elem_rules][tag] = false unless config[:elem_rules].has_key?(tag)
  end
  # :allow_tags -> :elem_rules (true)
  (config.delete(:allow_tags) || []).each do |tag|
    config[:elem_rules][tag] = true unless config[:elem_rules].has_key?(tag)
  end
  # :allow_attributes -> :default_attribute_rule (procs)
  (config.delete(:allow_attributes) || []).each do |attribute|
    #
    # Add it to the default attribute rule
    #
    old_rule = config[:default_attribute_rule]
    config[:default_attribute_rule] = Proc.new do |parent_element, key, value|
      if key == attribute
        true
      else
        Scrub::keep_attribute?(parent_element, key, value, old_rule)
      end
    end
  end

  config[:normalized] = true
  return config
end