Class: Hpricot::Elem

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) remove



151
152
153
# File 'lib/hpricot_scrub/hpricot_scrub.rb', line 151

def remove
  parent.children.delete(self)
end

- (Object) scrub(config = nil)

Scrubs the element according to the given config The relevant config key is :elem_rules. It is expected to be a Hash having String HTML tag names as keys, and a rule as values The rule value dictates what happens to the element. The following logic is used:

 If the rule is false/nil, the element is removed along with all it's children
 If the rule is :strip, the element is stripped (the element itself is deleted and its children are promoted upwards to where it was)
 If the rule is a proc, the proc is called (and given the element itself) - the proc's expected to return a valid rule that matches this documentation
 Otherwise the element is kept

If the element name (HTML tag) was not found in :elem_rules, the default rule in config key :default_elem_rule is used

After the above is done, if the element was kept, it's time to clean up its attributes so scrub_attributes is called. The rule is passed to it as it's assumed to be the attribute rules (see Hpricot::Scrub.keep_attribute?) to apply to the attributes, UNLESS the rule was explicitly “true”, in which case the config key :default_attribute_rule is passed.

This is recursive and will do all the above to all the children of the element as well.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/hpricot_scrub/hpricot_scrub.rb', line 178

def scrub(config = nil)

  config = Scrub::normalize_config(config)

  (children || []).reverse.each do |child|
    child.scrub(config) if child.respond_to?(:scrub)
  end

  rule = config[:elem_rules].has_key?(name) ? config[:elem_rules][name] : config[:default_elem_rule]

  while rule.is_a?(Proc)
    rule = rule.call(self)
  end

  if !rule
    remove
  elsif rule == :strip
    strip
  else
    # Positive rule
    # Keep the element
    # On to attributes
    scrub_attributes(rule == true ? config[:default_attribute_rule] : rule)
  end

  return self
end

- (Object) scrub_attributes(attribute_rule = nil)

Loops over all the attributes on this element, and removes any which Hpricot::Scrub.keep_attribute? returns false for



209
210
211
212
213
214
215
216
# File 'lib/hpricot_scrub/hpricot_scrub.rb', line 209

def scrub_attributes(attribute_rule = nil)
  if raw_attributes
    raw_attributes.each do |key, value|
      remove_attribute(key) unless Scrub.keep_attribute?(self, key, value, attribute_rule)
    end
  end
  return true
end

- (Object) strip



155
156
157
158
159
160
161
# File 'lib/hpricot_scrub/hpricot_scrub.rb', line 155

def strip
  if (i = inner_html) != ""
    swap(i)
  else
    remove
  end
end