Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/inline-style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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

Expand Down