class SiteDiff::Sanitizer::Regexp

Regular Expression Object.

Public Class Methods

create(rule) click to toggle source

Creates a RegExp object as per rule.

# File lib/sitediff/sanitize/regexp.rb, line 33
def self.create(rule)
  rule['selector'] ? WithSelector.new(rule) : new(rule)
end
new(rule) click to toggle source

Creates a RegExp object.

# File lib/sitediff/sanitize/regexp.rb, line 9
def initialize(rule)
  @rule = rule
end

Public Instance Methods

applies?(html, _node) click to toggle source

Whether the RegExp applies to the given markup.

# File lib/sitediff/sanitize/regexp.rb, line 21
def applies?(html, _node)
  applies_to_string?(html)
end
apply(html) click to toggle source

Applies the RegExp to the markup.

# File lib/sitediff/sanitize/regexp.rb, line 27
def apply(html)
  gsub!(html)
end
selector?() click to toggle source

Whether the RegExp has a selector.

# File lib/sitediff/sanitize/regexp.rb, line 15
def selector?
  false
end

Protected Instance Methods

applies_to_string?(str) click to toggle source
# File lib/sitediff/sanitize/regexp.rb, line 77
def applies_to_string?(str)
  gsub!(str.dup) != str
end
gsub!(str) click to toggle source
# File lib/sitediff/sanitize/regexp.rb, line 68
def gsub!(str)
  re = ::Regexp.new(@rule['pattern'])
  sub = @rule['substitute'] || ''
  # Expecting a mutation here. Do not reassign the variable str
  # for the purpose of removing UTF-8 encoding errors.
  str.gsub!(re, sub)
  str
end