Skip to content

Commit

Permalink
Tidied up a bit more code and added a few comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jimneath committed Jan 1, 2010
1 parent 1e5ff62 commit 1beb6e1
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/mail_style/inline_styles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,35 @@ def parse_html(html)
# Parse original html
html_document = create_html_document(html)
html_document = absolutize_image_sources(html_document)

element_styles = {}

# Write inline styles
element_styles = {}

css_parser.each_selector do |selector, declaration, specificity|
html_document.css(selector).each do |element|
declaration.to_s.split(';').each do |style|
# Split style in attribute and value
attribute, value = style.split(':').map(&:strip)

# Set element style defaults
element_styles[element] ||= {}
element_styles[element][attribute] ||= {:specificity => 0, :value => ''}
element_styles[element][attribute] ||= { :specificity => 0, :value => '' }

# Update attribute value if specificity is higher than previous values
if element_styles[element][attribute][:specificity] <= specificity
element_styles[element][attribute] = {:specificity => specificity, :value => value}
element_styles[element][attribute] = { :specificity => specificity, :value => value }
end
end
end
end

# Loop through element styles
element_styles.each_pair do |element, attributes|
# Elements current styles
current_style = element['style'].to_s.split(';').sort
new_style = []
attributes.each_pair do |attribute,style|
new_style << "#{attribute}: #{update_image_urls(style[:value])}"
end

# Elements new styles
new_style = attributes.map{|attribute, style| "#{attribute}: #{update_image_urls(style[:value])}"}

# Concat styles
style = (current_style + new_style).compact.uniq.map(&:strip).sort
Expand Down

0 comments on commit 1beb6e1

Please sign in to comment.