From bd77932ff387763de8dcaf2b208994717f414c9e Mon Sep 17 00:00:00 2001 From: Aaron Latham-Ilari Date: Wed, 2 Jul 2014 19:08:03 +1200 Subject: [PATCH] added ignore and only css selectors to the stylesheet parsing --- lib/inline-style.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/inline-style.rb b/lib/inline-style.rb index 89a2d65..802e38f 100644 --- a/lib/inline-style.rb +++ b/lib/inline-style.rb @@ -28,8 +28,10 @@ def self.process html, opts = {} def initialize html, opts = {} @stylesheets_path = opts[:stylesheets_path] || ENV['DOCUMENT_ROOT'] || '.' - @html = html - @dom = String === html ? Nokogiri.HTML(html) : html + @ignore = opts[:ignore] || false + @only = opts[:only] || false + @html = html + @dom = String === html ? Nokogiri.HTML(html) : html end def process @@ -79,8 +81,12 @@ def pre_parsed? # Returns parsed CSS def extract_css - @dom.css('style, link[rel=stylesheet]').collect do |node| + css_search = @only ? @only : 'style, link[rel=stylesheet]' + @dom.css(css_search).collect do |node| + + next if @ignore and node.parent.css(@ignore).include? node next unless /^$|screen|all/ === node['media'].to_s + node.remove if node.name == 'style' @@ -89,6 +95,7 @@ def extract_css uri = %r{^https?://} === node['href'] ? node['href'] : File.join(@stylesheets_path, node['href'].sub(/\?.+$/,'')) open(uri).read end + end.join("\n") end