lib/deface/applicator.rb in deface-1.7.0 vs lib/deface/applicator.rb in deface-1.8.0

- old
+ new

@@ -4,60 +4,72 @@ # applies all applicable overrides to given source # def apply(source, details, log=true, syntax=:erb) overrides = find(details) - if log && overrides.size > 0 - Rails.logger.debug "\e[1;32mDeface:\e[0m #{overrides.size} overrides found for '#{details[:virtual_path]}'" - end + return source if overrides.empty? - unless overrides.empty? - case syntax - when :haml - #convert haml to erb before parsing before - source = Deface::HamlConverter.new(source.to_param).result - when :slim - source = Deface::SlimConverter.new(source.to_param).result - end + Rails.logger.debug "\e[1;32mDeface:\e[0m #{overrides.size} overrides found for '#{details[:virtual_path]}'" if log - doc = Deface::Parser.convert(source) + apply_overrides( + convert_source(source, syntax: syntax), + overrides: overrides, + log: log + ) + end - overrides.each do |override| - if override.disabled? - Rails.logger.debug("\e[1;32mDeface:\e[0m '#{override.name}' is disabled") if log - next - end + # applies specified overrides to given source + def apply_overrides(source, overrides:, log: true) - override.parsed_document = doc - matches = override.matcher.matches(doc, log) + doc = Deface::Parser.convert(source) - if log - Rails.logger.send(matches.size == 0 ? :error : :debug, "\e[1;32mDeface:\e[0m '#{override.name}' matched #{matches.size} times with '#{override.selector}'") + overrides.each do |override| + if override.disabled? + Rails.logger.debug("\e[1;32mDeface:\e[0m '#{override.name}' is disabled") if log + next + end - # temporarily check and notify on use of old selector styles. - # - if matches.empty? && override.selector.match(/code|erb-loud|erb-silent/) - Rails.logger.error "\e[1;32mDeface: [WARNING]\e[0m Override '#{override.name}' may be using an invalid selector of '#{override.selector}', <code erb-loud|silent> tags are now <erb loud|silent>" - end - end + override.parsed_document = doc + matches = override.matcher.matches(doc, log) - if matches.empty? - override.failure = "failed to match :#{override.action} selector '#{override.selector}'" - else - override.failure = nil - matches.each {|match| override.execute_action match } + if log + Rails.logger.send(matches.size == 0 ? :error : :debug, "\e[1;32mDeface:\e[0m '#{override.name}' matched #{matches.size} times with '#{override.selector}'") + + # temporarily check and notify on use of old selector styles. + # + if matches.empty? && override.selector.match(/code|erb-loud|erb-silent/) + Rails.logger.error "\e[1;32mDeface: [WARNING]\e[0m Override '#{override.name}' may be using an invalid selector of '#{override.selector}', <code erb-loud|silent> tags are now <erb loud|silent>" end end - # Prevents any caching by rails in development mode. - details[:updated_at] = Time.now if Deface.before_rails_6? + if matches.empty? + override.failure = "failed to match :#{override.action} selector '#{override.selector}'" + else + override.failure = nil + matches.each {|match| override.execute_action match } + end + end - source = doc.to_s + source = doc.to_s - Deface::Parser.undo_erb_markup!(source) - end + Deface::Parser.undo_erb_markup!(source) source + end + + # converts the source to a supported syntax (ERB) + def convert_source(source, syntax:) + # convert haml/slim to erb before parsing before + case syntax + when :erb + source + when :haml + Deface::HamlConverter.new(source.to_s).result + when :slim + Deface::SlimConverter.new(source.to_s).result + else + raise "unsupported syntax: #{syntax}" + end end end def execute_action(target_element) validate_original(target_element)