Class: Pakyow::Mailer::StyleInliner Private

Inherits:
Object
  • Object
show all
Defined in:
lib/pakyow/mailer/style_inliner.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Inlines styles into html content.

Instance Method Summary collapse

Constructor Details

#initialize(doc_or_html, stylesheets: []) ⇒ StyleInliner

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of StyleInliner.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pakyow/mailer/style_inliner.rb', line 12

def initialize(doc_or_html, stylesheets: [])
  @css_parser = CssParser::Parser.new.tap do |parser|
    stylesheets.each do |stylesheet|
      parser.load_string!(stylesheet.read)
    end
  end

  @doc = case doc_or_html
  when Oga::XML::Document
    doc_or_html
  when Oga::XML::Element
    doc_or_html
  else
    Oga.parse_html(doc_or_html.to_s)
  end
end

Instance Method Details

#inlinedObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



29
30
31
32
33
34
35
36
37
# File 'lib/pakyow/mailer/style_inliner.rb', line 29

def inlined
  @css_parser.each_selector(:all) do |selector, declaration|
    @doc.css(selector).each do |node|
      node.set(:style, [declaration, node.get(:style).to_s].join(" "))
    end
  end

  @doc.to_xml
end