Sha256: c0e21e907c9d084b481f5abcbb251b46d5762cc2c41c31706665a92884855d7c

Contents?: true

Size: 1.01 KB

Versions: 4

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module BootstrapEmail
  module Converter
    class HeadStyle < Base
      def build(config)
        @config = config
        doc.at_css('head').add_child(bootstrap_email_head)
      end

      private

      def bootstrap_email_head
        <<-HTML
          <style type="text/css">
            #{purged_css_from_head}
          </style>
        HTML
      end

      def purged_css_from_head
        default, custom = BootstrapEmail::SassCache.compile('bootstrap-head', @config).split('/*! allow_purge_after */')
        custom ||= ''
        # get each CSS declaration
        custom.scan(/\w*\.[\w\-]*[\s\S\n]+?(?=})}{1}/).each do |group|
          # get the first class for each comma separated CSS declaration
          exist = group.scan(/(\.[\w\-]*).*?((,+?)|{+?)/).map(&:first).uniq.any? do |selector|
            !doc.at_css(selector).nil?
          end
          custom.sub!(group, '') unless exist
        end
        (default + custom).gsub(/\n\s*\n+/, "\n")
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bootstrap-email-1.5.1 lib/bootstrap-email/converters/head_style.rb
bootstrap-email-1.5.0 lib/bootstrap-email/converters/head_style.rb
bootstrap-email-1.4.1 lib/bootstrap-email/converters/head_style.rb
bootstrap-email-1.4.0 lib/bootstrap-email/converters/head_style.rb