roadie-5.2.1/0000755000004100000410000000000014563757014013026 5ustar www-datawww-dataroadie-5.2.1/.gitignore0000644000004100000410000000013314563757014015013 0ustar www-datawww-data.DS_Store .yardoc .rspec .ruby-version Gemfile.lock doc tmp pkg .bundle .history vendor/ roadie-5.2.1/.standard.yml0000644000004100000410000000002214563757014015421 0ustar www-datawww-dataruby_version: 2.7 roadie-5.2.1/.github/0000755000004100000410000000000014563757014014366 5ustar www-datawww-dataroadie-5.2.1/.github/workflows/0000755000004100000410000000000014563757014016423 5ustar www-datawww-dataroadie-5.2.1/.github/workflows/main.yml0000644000004100000410000000227114563757014020074 0ustar www-datawww-dataname: Main on: push: branches: - main - master pull_request: types: [opened, synchronize, reopened] jobs: base: name: Ruby ${{ matrix.ruby }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: ruby: - "2.7" - "3.0" - "3.1" - "3.2" steps: - name: Checkout code uses: actions/checkout@v4 # This setup is not compatible with the way Travis CI was # setup, so the cache will only work for the root folder. - name: Setup Ruby uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} bundler-cache: true - name: Rake run: bundle exec rake - uses: codecov/codecov-action@v3 lint: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 # This setup is not compatible with the way Travis CI was # setup, so the cache will only work for the root folder. - name: Setup Ruby uses: ruby/setup-ruby@v1 with: ruby-version: "3.2" bundler-cache: true - name: standardrb run: bundle exec standardrb roadie-5.2.1/lib/0000755000004100000410000000000014563757014013574 5ustar www-datawww-dataroadie-5.2.1/lib/roadie.rb0000644000004100000410000000137214563757014015367 0ustar www-datawww-data# frozen_string_literal: true module Roadie end require "roadie/version" require "roadie/errors" require "roadie/utils" require "roadie/deduplicator" require "roadie/stylesheet" require "roadie/selector" require "roadie/style_property" require "roadie/style_attribute_builder" require "roadie/style_block" require "roadie/asset_provider" require "roadie/provider_list" require "roadie/filesystem_provider" require "roadie/null_provider" require "roadie/net_http_provider" require "roadie/cached_provider" require "roadie/path_rewriter_provider" require "roadie/asset_scanner" require "roadie/markup_improver" require "roadie/url_generator" require "roadie/url_rewriter" require "roadie/null_url_rewriter" require "roadie/inliner" require "roadie/document" roadie-5.2.1/lib/roadie/0000755000004100000410000000000014563757014015037 5ustar www-datawww-dataroadie-5.2.1/lib/roadie/null_url_rewriter.rb0000644000004100000410000000062214563757014021143 0ustar www-datawww-data# frozen_string_literal: true module Roadie # @api private # Null Object for the URL rewriter role. # # Used whenever client does not pass any URL options and no URL rewriting # should take place. class NullUrlRewriter def initialize(generator = nil) # Intentionally left blank. end def transform_dom(dom) end def transform_css(css) css end end end roadie-5.2.1/lib/roadie/style_property.rb0000644000004100000410000000261414563757014020473 0ustar www-datawww-data# frozen_string_literal: true module Roadie # @api private # Domain object for a CSS property such as "color: red !important". # # @attr_reader [String] property name of the property (such as "font-size"). # @attr_reader [String] value value of the property (such as "5px solid green"). # @attr_reader [Boolean] important if the property is "!important". # @attr_reader [Integer] specificity specificity of parent {Selector}. Used to compare/sort. class StyleProperty include Comparable attr_reader :value, :important, :specificity # @todo Rename #property to #name attr_reader :property def initialize(property, value, important, specificity) @property = property @value = value @important = important @specificity = specificity end def important? @important end # Compare another {StyleProperty}. Important styles are "greater than" # non-important ones; otherwise the specificity declares order. def <=>(other) if important == other.important specificity <=> other.specificity else important ? 1 : -1 end end def to_s [property, value_with_important].join(":") end def inspect "#{self} (#{specificity})" end private def value_with_important if important "#{value} !important" else value end end end end roadie-5.2.1/lib/roadie/cached_provider.rb0000644000004100000410000000467114563757014020515 0ustar www-datawww-data# frozen_string_literal: true module Roadie # @api public # The {CachedProvider} wraps another provider (or {ProviderList}) and caches # the response from it. # # The default cache store is a instance-specific hash that lives for the # entire duration of the instance. If you want to share hash between # instances, pass your own hash-like object. Just remember to not allow this # cache to grow without bounds, which a shared hash would do. # # Not found assets are not cached currently, but it's possible to extend this # class in the future if there is a need for it. Remember this if you have # providers with very slow failures. # # The cache store must accept {Roadie::Stylesheet} instances, and return such # instances when fetched. It must respond to `#[name]` and `#[name]=` to # retrieve and set entries, respectively. The `#[name]=` method also needs to # return the instance again. # # @example Global cache # Application.asset_cache = Hash.new # slow_provider = MyDatabaseProvider.new(Application) # provider = Roadie::CachedProvider.new(slow_provider, Application.asset_cache) # # @example Custom cache store # class MyRoadieMemcacheStore # def initialize(memcache) # @memcache = memcache # end # # def [](path) # css = memcache.read("assets/#{path}/css") # if css # name = memcache.read("assets/#{path}/name") || "cached #{path}" # Roadie::Stylesheet.new(name, css) # end # end # # def []=(path, stylesheet) # memcache.write("assets/#{path}/css", stylesheet.to_s) # memcache.write("assets/#{path}/name", stylesheet.name) # stylesheet # You need to return the set Stylesheet # end # end # class CachedProvider # The cache store used by this instance. attr_reader :cache # @param upstream [an asset provider] The wrapped asset provider # @param cache [#[], #[]=] The cache store to use. def initialize(upstream, cache = {}) @upstream = upstream @cache = cache end def find_stylesheet(name) cache_fetch(name) do @upstream.find_stylesheet(name) end end def find_stylesheet!(name) cache_fetch(name) do @upstream.find_stylesheet!(name) end end private def cache_fetch(name) cache[name] || cache[name] = yield rescue CssNotFound cache[name] = nil raise end end end roadie-5.2.1/lib/roadie/style_block.rb0000644000004100000410000000333214563757014017677 0ustar www-datawww-data# frozen_string_literal: true require "forwardable" module Roadie # @api private # A style block is the combination of a {Selector} and a list of {StyleProperty}. class StyleBlock extend Forwardable attr_reader :selector, :properties, :media # @param [Selector] selector # @param [Array] properties # @param [Array] media Array of media types, e.g. # @media screen, print and (max-width 800px) will become # ['screen', 'print and (max-width 800px)'] def initialize(selector, properties, media) @selector = selector @properties = properties @media = media.map(&:to_s) end # @!method specificity # @see Selector#specificity def_delegators :selector, :specificity # @!method selector_string # @see Selector#to_s def_delegator :selector, :to_s, :selector_string # Checks whether the media query can be inlined # @see inlineable_media # @return {Boolean} def inlinable? inlinable_media? && selector.inlinable? end # String representation of the style block. This is valid CSS and can be # used in the DOM. # @return {String} def to_s # NB - leave off redundant final semicolon - see https://www.w3.org/TR/CSS2/syndata.html#declaration "#{selector}{#{properties.map(&:to_s).join(";")}}" end private # A media query cannot be inlined if it contains any advanced rules # e.g. @media only screen {...} is ok to inline but # @media only screen and (max-width: 600px) {...} cannot be inlined # @return {Boolean} def inlinable_media? @media.none? { |media_query| media_query.include? "(" } end end end roadie-5.2.1/lib/roadie/null_provider.rb0000644000004100000410000000112314563757014020245 0ustar www-datawww-data# frozen_string_literal: true module Roadie # An asset provider that returns empty stylesheets for any name. # # Use it to ignore missing assets or in your tests when you need a provider # but you do not care what it contains or that it is even referenced at all. class NullProvider def find_stylesheet(name) empty_stylesheet end def find_stylesheet!(name) empty_stylesheet end def to_s inspect end def inspect "#<#{self.class}>" end private def empty_stylesheet Stylesheet.new "(null)", "" end end end roadie-5.2.1/lib/roadie/net_http_provider.rb0000644000004100000410000000625214563757014021130 0ustar www-datawww-data# frozen_string_literal: true require "set" require "uri" require "net/http" module Roadie # @api public # External asset provider that downloads stylesheets from some other server # using Ruby's built-in {Net::HTTP} library. # # You can pass a whitelist of hosts that downloads are allowed on. # # @example Allowing all downloads # provider = Roadie::NetHttpProvider.new # # @example Only allowing your own app domains # provider = Roadie::NetHttpProvider.new( # whitelist: ["myapp.com", "assets.myapp.com", "www.myapp.com"] # ) class NetHttpProvider attr_reader :whitelist # @option options [Array] :whitelist ([]) A list of host names that downloads are allowed from. Empty set means everything is allowed. def initialize(options = {}) @whitelist = host_set(Array(options.fetch(:whitelist, []))) end def find_stylesheet(url) find_stylesheet!(url) rescue CssNotFound nil end def find_stylesheet!(url) response = download(url) if response.is_a? Net::HTTPSuccess Stylesheet.new(url, response_body(response)) else raise CssNotFound.new( css_name: url, message: "Server returned #{response.code}: #{truncate response.body}", provider: self ) end rescue Timeout::Error raise CssNotFound.new(css_name: url, message: "Timeout", provider: self) end def to_s inspect end def inspect "#<#{self.class} whitelist: #{whitelist.inspect}>" end private def host_set(hosts) hosts.each { |host| validate_host(host) }.to_set end def validate_host(host) if host.nil? || host.empty? || host == "." || host.include?("/") raise ArgumentError, "#{host.inspect} is not a valid hostname" end end def download(url) url = "https:#{url}" if url.start_with?("//") uri = URI.parse(url) if access_granted_to?(uri.host) get_response(uri) else raise CssNotFound.new( css_name: url, message: "#{uri.host} is not part of whitelist!", provider: self ) end end def get_response(uri) if RUBY_VERSION >= "2.0.0" Net::HTTP.get_response(uri) else Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == "https")) do |http| http.request(Net::HTTP::Get.new(uri.request_uri)) end end end def access_granted_to?(host) whitelist.empty? || whitelist.include?(host) end def truncate(string) if string.length > 50 string[0, 49] + "…" else string end end def response_body(response) # Make sure we respect encoding because Net:HTTP will encode body as ASCII by default # which will break if the response is not compatible. supplied_charset = response.type_params["charset"] body = response.body if supplied_charset body.force_encoding(supplied_charset).encode!("UTF-8") else # Default to UTF-8 when server does not specify encoding as that is the # most common charset. body.force_encoding("UTF-8") end end end end roadie-5.2.1/lib/roadie/document.rb0000644000004100000410000001771014563757014017210 0ustar www-datawww-data# frozen_string_literal: true module Roadie # The main entry point for Roadie. A document represents a working unit and # is built with the input HTML and the configuration options you need. # # A Document must never be used from two threads at the same time. Reusing # Documents is discouraged. # # Stylesheets are added to the HTML from three different sources: # 1. Stylesheets inside the document ( + HTML scanner = AssetScanner.new dom, normal_provider, external_provider stylesheets = scanner.find_css expect(stylesheets).to have(2).stylesheets expect(stylesheets[0].to_s).to include("green") expect(stylesheets[1].to_s).to include("red") expect(stylesheets.first.name).to eq("(inline)") end it "does not find any embedded stylesheets marked for ignoring" do dom = dom_document <<-HTML HTML scanner = AssetScanner.new dom, normal_provider, external_provider expect(scanner.find_css).to have(1).stylesheet end it "finds normal referenced stylesheets through the normal provider" do stylesheet = double "A stylesheet" expect(normal_provider).to receive(:find_stylesheet!).with("/some/url.css").and_return stylesheet dom = dom_fragment %() scanner = AssetScanner.new dom, normal_provider, external_provider expect(scanner.find_css).to eq([stylesheet]) end it "finds external referenced stylesheets through the external provider" do stylesheet = double "A stylesheet" external_provider = TestProvider.new expect(external_provider).to receive(:find_stylesheet!).with("//example.com/style.css").and_return stylesheet dom = dom_fragment %() scanner = AssetScanner.new dom, normal_provider, external_provider expect(scanner.find_css).to eq([stylesheet]) end it "ignores referenced print stylesheets" do dom = dom_fragment %() expect(normal_provider).not_to receive(:find_stylesheet!) scanner = AssetScanner.new dom, normal_provider, external_provider expect(scanner.find_css).to eq([]) end it "does not look for externally referenced stylesheets from empty ProviderList" do external_provider = ProviderList.empty dom = dom_fragment %() expect(external_provider).not_to receive(:find_stylesheet!) scanner = AssetScanner.new dom, normal_provider, external_provider expect(scanner.find_css).to eq([]) end it "does not look for ignored referenced stylesheets" do dom = dom_fragment %() expect(normal_provider).not_to receive(:find_stylesheet!) scanner = AssetScanner.new dom, normal_provider, external_provider expect(scanner.find_css).to eq([]) end it "ignores HTML comments and CDATA sections" do # TinyMCE posts invalid CSS. We support that just to be pragmatic. dom = dom_fragment %() scanner = AssetScanner.new dom, normal_provider, external_provider stylesheet = scanner.find_css.first expect(stylesheet.to_s).to include("green") expect(stylesheet.to_s).not_to include("!--") expect(stylesheet.to_s).not_to include("CDATA") end it "does not pick up scripts generating styles" do dom = dom_fragment <<-HTML HTML scanner = AssetScanner.new dom, normal_provider, external_provider expect(scanner.find_css).to eq([]) end end describe "extracting" do it "returns the stylesheets found, and removes them from the DOM" do dom = dom_document <<-HTML Hello world! HTML normal_provider = TestProvider.new "/some/url.css" => "body { color: green; }" scanner = AssetScanner.new dom, normal_provider, external_provider stylesheets = scanner.extract_css expect(stylesheets).to have(2).stylesheets expect(stylesheets[0].to_s).to include("span") expect(stylesheets[1].to_s).to include("body") expect(dom).to have_selector("html > head > title") expect(dom).to have_selector("html > body > style.totally-ignored") expect(dom).to have_selector("link.totally-ignored") expect(dom).to have_selector("link[media=print]") expect(dom).not_to have_selector("html > head > style") expect(dom).not_to have_selector("html > head > link[href='/some/url.css']") end it "removes external references if provider is not empty" do dom = dom_document <<-HTML HTML external_provider = ProviderList.wrap(NullProvider.new) scanner = AssetScanner.new dom, normal_provider, external_provider stylesheets = scanner.extract_css expect(stylesheets).to have(1).stylesheets expect(dom).to_not have_selector("link[href*=some]") expect(dom).to have_selector("link[href*=other]") end end end end roadie-5.2.1/spec/lib/roadie/markup_improver_spec.rb0000644000004100000410000000476314563757014022564 0ustar www-datawww-data# frozen_string_literal: true require "spec_helper" module Roadie describe MarkupImprover do def improve(html) dom = Nokogiri::HTML.parse html MarkupImprover.new(dom, html).improve dom end describe "automatic doctype" do it "inserts a HTML5 doctype if no doctype is present" do expect(improve("").internal_subset.to_xml).to eq("") end it "does not insert duplicate doctypes" do html = improve("").to_html expect(html.scan("DOCTYPE").size).to eq(1) end it "leaves other doctypes alone" do dtd = "" html = "#{dtd}" expect(improve(html).internal_subset.to_xml.strip).to eq(dtd) end end describe "basic HTML structure" do it "inserts a element as the root" do expect(improve("")).to have_selector("html") expect(improve("

Hey!

")).to have_selector("html h1") expect(improve("").css("html").size).to eq(1) end it "inserts if not present" do expect(improve("")).to have_selector("html > head + body") expect(improve("")).to have_selector("html > head") expect(improve("Foo")).to have_selector("html > head") expect(improve("").css("head").size).to eq(1) end it "inserts if not present" do expect(improve("

Hey!

")).to have_selector("html > body > h1") expect(improve("

Hey!

")).to have_selector("html > body > h1") expect(improve("

Hey!

").css("body").size).to eq(1) end end describe "charset declaration" do it "is inserted if missing" do dom = improve("") expect(dom).to have_selector("head meta") meta = dom.at_css("head meta") expect(meta["http-equiv"]).to eq("Content-Type") expect(meta["content"]).to eq("text/html; charset=UTF-8") end it "is left alone when predefined" do expect(improve(<<-HTML).xpath("//meta")).to have(1).item HTML end end end end roadie-5.2.1/spec/lib/roadie/url_rewriter_spec.rb0000644000004100000410000000651314563757014022062 0ustar www-datawww-data# frozen_string_literal: true require "spec_helper" require "shared_examples/url_rewriter" module Roadie describe UrlRewriter do let(:generator) { double("URL generator") } subject(:rewriter) { UrlRewriter.new(generator) } it_behaves_like "url rewriter" describe "transforming DOM trees" do def dom_document(html) Nokogiri::HTML.parse html end it "rewrites all a[href]" do expect(generator).to receive(:generate_url).with("some/path").and_return "http://foo.com/" dom = dom_document <<-HTML Some path HTML expect { rewriter.transform_dom dom }.to change { dom.at_css("a")["href"] }.to "http://foo.com/" end it "rewrites relative img[src]" do expect(generator).to receive(:generate_url).with("some/path.jpg").and_return "http://foo.com/image.jpg" dom = dom_document <<-HTML HTML expect { rewriter.transform_dom dom }.to change { dom.at_css("img")["src"] }.to "http://foo.com/image.jpg" end it "rewrites url() directives inside style attributes" do expect(generator).to receive(:generate_url).with("some/path.jpg").and_return "http://foo.com/image.jpg" dom = dom_document <<-HTML
HTML expect { rewriter.transform_dom dom }.to change { dom.at_css("div")["style"] }.to 'background-image: url("http://foo.com/image.jpg");' end it "skips elements with data-roadie-ignore attributes" do allow(generator).to receive(:generate_url).and_return("http://example.com") dom = dom_document <<-HTML Image
HTML rewriter.transform_dom dom expect(generator).not_to have_received(:generate_url) end end describe "transforming css" do it "rewrites all url() directives" do expect(generator).to receive(:generate_url).with("some/path.jpg").and_return "http://foo.com/image.jpg" css = "body { background: top url(some/path.jpg) #eee; }" transformed_css = rewriter.transform_css css expect(transformed_css).to eq "body { background: top url(http://foo.com/image.jpg) #eee; }" end it "correctly identifies URLs with single quotes" do expect(generator).to receive(:generate_url).with("images/foo.png").and_return "x" rewriter.transform_css "url('images/foo.png')" end it "correctly identifies URLs with double quotes" do expect(generator).to receive(:generate_url).with("images/foo.png").and_return "x" rewriter.transform_css 'url("images/foo.png")' end it "correctly identifies URLs with parenthesis inside them" do expect(generator).to receive(:generate_url).with("images/map_(large_(extra)).png").and_return "x" rewriter.transform_css "url(images/map_(large_(extra)).png)" end end end end roadie-5.2.1/spec/lib/roadie/filesystem_provider_spec.rb0000644000004100000410000000516214563757014023432 0ustar www-datawww-data# frozen_string_literal: true require "spec_helper" require "roadie/rspec" require "shared_examples/asset_provider" module Roadie describe FilesystemProvider do let(:fixtures_path) { File.expand_path "../../../fixtures", __FILE__ } subject(:provider) { FilesystemProvider.new(fixtures_path) } it_behaves_like "roadie asset provider", valid_name: "stylesheets/green.css", invalid_name: "foo" it "takes a path" do expect(FilesystemProvider.new("/tmp").path).to eq("/tmp") end it "defaults to the current working directory" do expect(FilesystemProvider.new.path).to eq(Dir.pwd) end it "shows the given path in string representation" do expect(provider.to_s).to include provider.path.to_s expect(provider.inspect).to include provider.path.to_s end describe "finding stylesheets" do it "finds files in the path" do full_path = File.join(fixtures_path, "stylesheets", "green.css") file_contents = File.read full_path stylesheet = provider.find_stylesheet("stylesheets/green.css") expect(stylesheet).not_to be_nil expect(stylesheet.name).to eq(full_path) expect(stylesheet.to_s).to eq(Stylesheet.new("", file_contents).to_s) end it "returns nil on non-existant files" do expect(provider.find_stylesheet("non/existant.css")).to be_nil end it "finds files inside the base path when using absolute paths" do full_path = File.join(fixtures_path, "stylesheets", "green.css") expect(provider.find_stylesheet("/stylesheets/green.css").name).to eq(full_path) end it "does not read files above the base directory" do expect { provider.find_stylesheet("../#{File.basename(__FILE__)}") }.to raise_error FilesystemProvider::InsecurePathError end end describe "finding stylesheets with query strings" do it "ignores the query string" do full_path = File.join(fixtures_path, "stylesheets", "green.css") file_contents = File.read full_path stylesheet = provider.find_stylesheet("/stylesheets/green.css?time=111") expect(stylesheet).not_to be_nil expect(stylesheet.name).to eq(full_path) expect(stylesheet.to_s).to eq(Stylesheet.new("", file_contents).to_s) end it "shows that the query string is ignored inside raised errors" do provider.find_stylesheet!("/foo.css?query-string") fail "No error was raised" rescue CssNotFound => error expect(error.css_name).to eq("foo.css") expect(error.to_s).to include("/foo.css?query-string") end end end end roadie-5.2.1/spec/lib/roadie/inliner_spec.rb0000644000004100000410000002067114563757014020776 0ustar www-datawww-data# frozen_string_literal: true require "spec_helper" module Roadie describe Inliner do before { @stylesheet = "" } def use_css(css) @stylesheet = Stylesheet.new("example", css) end def rendering(html, stylesheet = @stylesheet) dom = Nokogiri::HTML.parse html Inliner.new([stylesheet], dom).inline dom end describe "inlining styles" do it "inlines simple attributes" do use_css "p { color: green }" expect(rendering("

")).to have_styling("color" => "green") end it "keeps multiple versions of the same property to support progressive enhancement" do # https://github.com/premailer/css_parser/issues/44 pending "css_parser issue #44" use_css "p { color: #eee; color: rgba(255, 255, 255, 0.9); }" expect(rendering("

")).to have_styling( [["color", "green"], ["color", "rgba(255, 255, 255, 0.9)"]] ) end it "de-duplicates identical styles" do use_css ' p { color: green; } .message { color: blue; } .positive { color: green; } ' expect(rendering('

')).to have_styling( [["color", "blue"], ["color", "green"]] ) end it "inlines browser-prefixed attributes" do use_css "p { -vendor-color: green }" expect(rendering("

")).to have_styling("-vendor-color" => "green") end it "inlines CSS3 attributes" do use_css "p { border-radius: 2px; }" expect(rendering("

")).to have_styling("border-radius" => "2px") end it "keeps the order of the styles that are inlined" do use_css "h1 { padding: 2px; margin: 5px; }" expect(rendering("

")).to have_styling([["padding", "2px"], ["margin", "5px"]]) end it "combines multiple selectors into one" do use_css 'p { color: green; } .tip { float: right; }' expect(rendering('

')).to have_styling([["color", "green"], ["float", "right"]]) end it "uses the attributes with the highest specificity when conflicts arises" do use_css ".safe { color: green; } p { color: red; }" expect(rendering('

')).to have_styling([["color", "red"], ["color", "green"]]) end it "sorts styles by specificity order" do use_css 'p { important: no; } #important { important: very; } .important { important: yes; }' expect(rendering('

')).to have_styling([ %w[important no], %w[important yes] ]) expect(rendering('

')).to have_styling([ %w[important no], %w[important yes], %w[important very] ]) end it "supports multiple selectors for the same rules" do use_css "p, a { color: green; }" rendering("

").tap do |document| expect(document).to have_styling("color" => "green").at_selector("p") expect(document).to have_styling("color" => "green").at_selector("a") end end it "keeps !important properties" do use_css "a { text-decoration: underline !important; } a.hard-to-spot { text-decoration: none; }" expect(rendering('')).to have_styling([ ["text-decoration", "none"], ["text-decoration", "underline !important"] ]) end it "combines with already present inline styles" do use_css "p { color: green }" expect(rendering('

')).to have_styling([["color", "green"], ["font-size", "1.1em"]]) end it "does not override inline styles" do use_css "p { text-transform: uppercase; color: red }" # The two color properties are kept to make css fallbacks work correctly expect(rendering('

')).to have_styling([ ["text-transform", "uppercase"], ["color", "red"], ["color", "green"] ]) end it "does not apply link and dynamic pseudo selectors" do use_css " p:active { color: red } p:focus { color: red } p:hover { color: red } p:link { color: red } p:target { color: red } p:visited { color: red } p.active { width: 100%; } " expect(rendering('

')).to have_styling("width" => "100%") end it "does not crash on any pseudo element selectors" do use_css " p.some-element { width: 100%; } p::some-element { color: red; } " expect(rendering('

')).to have_styling("width" => "100%") end it "warns on selectors that crash Nokogiri" do dom = Nokogiri::HTML.parse "

" stylesheet = Stylesheet.new "foo.css", "p[%^=foo] { color: red; }" inliner = Inliner.new([stylesheet], dom) expect(Utils).to receive(:warn).with( %(Cannot inline "p[%^=foo]" from "foo.css" stylesheet. If this is valid CSS, please report a bug.) ) inliner.inline end it "works with nth-child" do use_css " p { color: red; } p:nth-child(2n) { color: green; } " result = rendering("

") expect(result).to have_styling([["color", "red"]]).at_selector("p:first") expect(result).to have_styling([["color", "red"], ["color", "green"]]).at_selector("p:last") end context "with uninlinable selectors" do before do allow(Roadie::Utils).to receive(:warn) end it "puts them in a new Hello world! HTML result = Nokogiri::HTML.parse document.transform expect(result).to have_selector("body > a > span") expect(result).not_to have_selector("[data-roadie-ignore]") end end end roadie-5.2.1/spec/lib/roadie/provider_list_spec.rb0000644000004100000410000001007414563757014022217 0ustar www-datawww-data# frozen_string_literal: true require "spec_helper" require "roadie/rspec" module Roadie describe ProviderList do let(:test_provider) { TestProvider.new } subject(:provider) { ProviderList.new([test_provider]) } it_behaves_like "roadie asset provider", valid_name: "valid", invalid_name: "invalid" do let(:test_provider) { TestProvider.new "valid" => "" } end it "finds using all given providers" do first = TestProvider.new "foo.css" => "foo { color: green; }" second = TestProvider.new "bar.css" => "bar { color: green; }" provider = ProviderList.new [first, second] expect(provider.find_stylesheet("foo.css").to_s).to include "foo" expect(provider.find_stylesheet("bar.css").to_s).to include "bar" expect(provider.find_stylesheet("baz.css")).to be_nil end it "is enumerable" do expect(provider).to be_kind_of(Enumerable) expect(provider).to respond_to(:each) expect(provider.each.to_a).to eq([test_provider]) end it "has a size" do expect(provider.size).to eq(1) expect(provider).not_to be_empty end it "has a first and a last element" do providers = [double("1"), double("2"), double("3")] list = ProviderList.new(providers) expect(list.first).to eq(providers.first) expect(list.last).to eq(providers.last) end it "can have providers pushed and popped" do other = double "Some other provider" expect { provider.push other provider << other }.to change(provider, :size).by(2) expect { expect(provider.pop).to eq(other) }.to change(provider, :size).by(-1) end it "can have providers shifted and unshifted" do other = double "Some other provider" expect { provider.unshift other }.to change(provider, :size).by(1) expect { expect(provider.shift).to eq(other) }.to change(provider, :size).by(-1) end it "has a readable string represenation" do provider = double("Provider", to_s: "Some provider") sublist = ProviderList.new([provider, provider]) list = ProviderList.new([provider, sublist, provider]) expect(list.to_s).to eq(<<~TEXT) ProviderList: [ \tSome provider, \tProviderList: [ \t\tSome provider, \t\tSome provider \t], \tSome provider ] TEXT end it "raises a readable error message" do provider = double("Provider", to_s: "Some provider") allow(provider).to receive(:find_stylesheet!).and_raise( CssNotFound.new( css_name: "style.css", message: "I tripped", provider: provider ) ) sublist = ProviderList.new([provider, provider]) list = ProviderList.new([provider, sublist, provider]) expect { list.find_stylesheet!("style.css") }.to raise_error { |error| expect(error.message).to eq(<<~TEXT) Could not find stylesheet "style.css": All providers failed Used providers: \tSome provider: I tripped \tSome provider: I tripped \tSome provider: I tripped \tSome provider: I tripped TEXT } end describe "wrapping" do it "creates provider lists with the arguments" do expect(ProviderList.wrap(test_provider)).to be_instance_of(ProviderList) expect(ProviderList.wrap(test_provider, test_provider).size).to eq(2) end it "flattens arrays" do expect(ProviderList.wrap([test_provider, test_provider], test_provider).size).to eq(3) expect(ProviderList.wrap([test_provider, test_provider]).size).to eq(2) end it "combines with providers from other lists" do other_list = ProviderList.new([test_provider, test_provider]) expect(ProviderList.wrap(test_provider, other_list).size).to eq(3) end it "returns the passed list if only a single ProviderList is passed" do other_list = ProviderList.new([test_provider]) expect(ProviderList.wrap(other_list)).to eql other_list end end end end roadie-5.2.1/spec/lib/roadie/selector_spec.rb0000644000004100000410000000346414563757014021157 0ustar www-datawww-data# frozen_string_literal: true require "spec_helper" module Roadie describe Selector do it "can be coerced into String" do expect("I love " + Selector.new("html")).to eq("I love html") end it "can be inlined when simple" do expect(Selector.new("html body #main p.class")).to be_inlinable end it "cannot be inlined when containing pseudo functions" do %w[ p:active p:focus p:hover p:link p:target p:visited p:-ms-input-placeholder p:-moz-placeholder p:before p:after p:enabled p:disabled p:checked p:host ].each do |bad_selector| expect(Selector.new(bad_selector)).not_to be_inlinable end expect(Selector.new("p.active")).to be_inlinable end it "cannot be inlined when containing pseudo elements" do expect(Selector.new("p::some-element")).not_to be_inlinable end it "cannot be inlined when selector is an at-rule" do expect(Selector.new("@keyframes progress-bar-stripes")).not_to be_inlinable end it "has a calculated specificity" do selector = "html p.active.nice #main.deep-selector" expect(Selector.new(selector).specificity).to eq(CssParser.calculate_specificity(selector)) end it "can be told about the specificity at initialization" do selector = "html p.active.nice #main.deep-selector" expect(Selector.new(selector, 1337).specificity).to eq(1337) end it "is equal to other selectors when they match the same things" do expect(Selector.new("foo")).to eq(Selector.new("foo ")) expect(Selector.new("foo")).not_to eq("foo") end it "strips the given selector" do expect(Selector.new(" foo \n").to_s).to eq(Selector.new("foo").to_s) end end end roadie-5.2.1/spec/lib/roadie/style_attribute_builder_spec.rb0000644000004100000410000000262214563757014024263 0ustar www-datawww-data# frozen_string_literal: true require "spec_helper" module Roadie describe StyleAttributeBuilder do it "sorts the added properties" do builder = StyleAttributeBuilder.new builder << StyleProperty.new("color", "green", true, 1) builder << StyleProperty.new("font-size", "110%", false, 15) builder << StyleProperty.new("color", "red", false, 15) expect(builder.attribute_string).to eq "font-size:110%;color:red;color:green !important" end it "preserves the order of added attributes with the same specificity" do builder = StyleAttributeBuilder.new builder << StyleProperty.new("color", "pink", false, 50) builder << StyleProperty.new("color", "red", false, 50) builder << StyleProperty.new("color", "green", false, 50) # We need one different element to trigger the problem with Ruby's # unstable sort builder << StyleProperty.new("background", "white", false, 1) expect(builder.attribute_string).to eq "background:white;color:pink;color:red;color:green" end it "removes duplicate properties" do builder = StyleAttributeBuilder.new builder << StyleProperty.new("color", "pink", false, 10) builder << StyleProperty.new("color", "green", false, 20) builder << StyleProperty.new("color", "pink", false, 50) expect(builder.attribute_string).to eq "color:green;color:pink" end end end roadie-5.2.1/spec/lib/roadie/null_provider_spec.rb0000644000004100000410000000116614563757014022220 0ustar www-datawww-data# frozen_string_literal: true require "spec_helper" require "shared_examples/asset_provider" module Roadie describe NullProvider do it_behaves_like "asset provider role" def expect_empty_stylesheet(stylesheet) expect(stylesheet).not_to be_nil expect(stylesheet.name).to eq("(null)") expect(stylesheet).to have(0).blocks expect(stylesheet.to_s).to be_empty end it "finds an empty stylesheet for every name" do expect_empty_stylesheet NullProvider.new.find_stylesheet("omg wtf bbq") expect_empty_stylesheet NullProvider.new.find_stylesheet!("omg wtf bbq") end end end roadie-5.2.1/spec/lib/roadie/style_property_spec.rb0000644000004100000410000000350114563757014022433 0ustar www-datawww-data# frozen_string_literal: true require "spec_helper" module Roadie describe StyleProperty do it "is initialized with a property, value, if it is marked as important, and the specificity" do StyleProperty.new("color", "green", true, 45).tap do |declaration| expect(declaration.property).to eq("color") expect(declaration.value).to eq("green") expect(declaration).to be_important expect(declaration.specificity).to eq(45) end end describe "string representation" do it "is the property and the value joined with a colon" do expect(StyleProperty.new("color", "green", false, 1).to_s).to eq("color:green") expect(StyleProperty.new("font-size", "1.1em", false, 1).to_s).to eq("font-size:1.1em") end it "contains the !important flag when set" do expect(StyleProperty.new("color", "green", true, 1).to_s).to eq("color:green !important") end end describe "comparing" do def declaration(specificity, important = false) StyleProperty.new("color", "green", important, specificity) end it "compares on specificity" do expect(declaration(5)).to eq(declaration(5)) expect(declaration(4)).to be < declaration(5) expect(declaration(6)).to be > declaration(5) end context "with an important declaration" do it "is less than the important declaration regardless of the specificity" do expect(declaration(99, false)).to be < declaration(1, true) end it "compares like normal when both declarations are important" do expect(declaration(5, true)).to eq(declaration(5, true)) expect(declaration(4, true)).to be < declaration(5, true) expect(declaration(6, true)).to be > declaration(5, true) end end end end end roadie-5.2.1/spec/lib/roadie/path_rewriter_provider_spec.rb0000644000004100000410000000242014563757014024117 0ustar www-datawww-data# frozen_string_literal: true require "spec_helper" require "roadie/rspec" require "shared_examples/asset_provider" module Roadie describe PathRewriterProvider do let(:upstream) { TestProvider.new "good.css" => "body { color: green; }" } subject(:provider) do PathRewriterProvider.new(upstream) do |path| path.gsub("well", "good") end end it_behaves_like "roadie asset provider", valid_name: "well.css", invalid_name: "bad" it "does not call the upstream provider if block returns nil" do provider = PathRewriterProvider.new(upstream) { nil } expect(upstream).to_not receive(:find_stylesheet) expect(upstream).to_not receive(:find_stylesheet!) expect(provider.find_stylesheet("foo")).to be_nil expect { provider.find_stylesheet!("foo") }.to raise_error(CssNotFound, /nil/) end it "does not call the upstream provider if block returns false" do provider = PathRewriterProvider.new(upstream) { false } expect(upstream).to_not receive(:find_stylesheet) expect(upstream).to_not receive(:find_stylesheet!) expect(provider.find_stylesheet("foo")).to be_nil expect { provider.find_stylesheet!("foo") }.to raise_error(CssNotFound, /false/) end end end roadie-5.2.1/spec/lib/roadie/style_block_spec.rb0000644000004100000410000000304014563757014021637 0ustar www-datawww-data# frozen_string_literal: true require "spec_helper" module Roadie describe StyleBlock do it "has a selector and a list of properties" do properties = [] selector = double "Selector" block = StyleBlock.new(selector, properties, [:all]) expect(block.selector).to eq(selector) expect(block.properties).to eq(properties) end it "delegates #specificity to the selector" do selector = double "Selector", specificity: 45 expect(StyleBlock.new(selector, [], [:all]).specificity).to eq(45) end it "delegates #selector_string to selector#to_s" do selector = double "Selector", to_s: "yey" expect(StyleBlock.new(selector, [], [:all]).selector_string).to eq("yey") end it "has a string representation" do properties = [double(to_s: "bar"), double(to_s: "baz")] expect(StyleBlock.new(double(to_s: "foo"), properties, [:all]).to_s).to eq("foo{bar;baz}") end describe "#inlinable" do context "when no media include feature condition" do it "delegates #inlinable? to the selector" do selector = double "Selector", inlinable?: "maybe" expect(StyleBlock.new(selector, [], [:all]).inlinable?).to eq("maybe") end end context "when one of media queries includes feature condition" do it "returns false" do selector = double "Selector", inlinable?: "maybe" expect(StyleBlock.new(selector, [], [:all, :"screen (min-width: 300px)"]).inlinable?).to be(false) end end end end end roadie-5.2.1/spec/fixtures/0000755000004100000410000000000014563757014015631 5ustar www-datawww-dataroadie-5.2.1/spec/fixtures/big_em.css0000644000004100000410000000003014563757014017556 0ustar www-datawww-dataem { font-size: 200%; } roadie-5.2.1/spec/fixtures/stylesheets/0000755000004100000410000000000014563757014020205 5ustar www-datawww-dataroadie-5.2.1/spec/fixtures/stylesheets/green.css0000644000004100000410000000002714563757014022016 0ustar www-datawww-databody { color: green; } roadie-5.2.1/spec/support/0000755000004100000410000000000014563757014015474 5ustar www-datawww-dataroadie-5.2.1/spec/support/have_styling_matcher.rb0000644000004100000410000000266414563757014022230 0ustar www-datawww-data# frozen_string_literal: true RSpec::Matchers.define :have_styling do |rules| normalized_rules = StylingExpectation.new(rules) chain(:at_selector) { |selector| @selector = selector } match { |document| @selector ||= "body > *:first" normalized_rules == styles_at_selector(document) } description { "have styles #{normalized_rules.inspect} at selector #{@selector.inspect}" } failure_message { |document| "expected styles at #{@selector.inspect} to be:\n#{normalized_rules}\nbut was:\n#{styles_at_selector(document)}" } failure_message_when_negated { "expected styles at #{@selector.inspect} to not be:\n#{normalized_rules}" } def styles_at_selector(document) expect(document).to have_selector(@selector) StylingExpectation.new document.at_css(@selector)["style"] end end class StylingExpectation def initialize(styling) case styling when String then @rules = parse_rules(styling) when Array then @rules = styling when Hash then @rules = styling.to_a when nil then @rules = [] else fail "I don't understand #{styling.inspect}!" end end def ==(other) rules == other.rules end def to_s rules.to_s end protected attr_reader :rules private def parse_rules(css) css.split(";").map { |property| parse_property(property) } end def parse_property(property) rule, value = property.split(":", 2).map(&:strip) [rule, value] end end roadie-5.2.1/spec/support/have_attribute_matcher.rb0000644000004100000410000000156614563757014022542 0ustar www-datawww-data# frozen_string_literal: true RSpec::Matchers.define :have_attribute do |attribute| @selector = "body > *:first" chain :at_selector do |selector| @selector = selector end match do |document| name, expected = attribute.first expected == attribute(document, name) end description { "have attribute #{attribute.inspect} at selector #{@selector.inspect}" } failure_message do |document| name, expected = attribute.first "expected #{name} attribute at #{@selector.inspect} to be #{expected.inspect} but was #{attribute(document, name).inspect}" end failure_message_when_negated do |document| name, expected = attribute.first "expected #{name} attribute at #{@selector.inspect} to not be #{expected.inspect}" end def attribute(document, attribute_name) node = document.css(@selector).first node && node[attribute_name] end end roadie-5.2.1/spec/support/have_selector_matcher.rb0000644000004100000410000000047414563757014022354 0ustar www-datawww-data# frozen_string_literal: true RSpec::Matchers.define :have_selector do |selector| match { |document| !document.css(selector).empty? } failure_message { "expected document to have selector #{selector.inspect}" } failure_message_when_negated { "expected document to not have selector #{selector.inspect}" } end roadie-5.2.1/spec/support/have_node_matcher.rb0000644000004100000410000000123614563757014021456 0ustar www-datawww-data# frozen_string_literal: true RSpec::Matchers.define :have_node do |selector| chain(:with_attributes) { |attributes| @attributes = attributes } match do |document| node = document.at_css(selector) if @attributes node && match_attributes(node.attributes) else node end end failure_message { "expected document to #{name_to_sentence}#{expected_to_sentence}" } failure_message_when_negated { "expected document to not #{name_to_sentence}#{expected_to_sentence}" } def match_attributes(node_attributes) attributes = node_attributes.map { |name, attribute| [name, attribute.value] }.to_h @attributes == attributes end end roadie-5.2.1/spec/support/test_provider.rb0000644000004100000410000000046114563757014020713 0ustar www-datawww-data# frozen_string_literal: true class TestProvider include Roadie::AssetProvider def initialize(files = {}) @files = files @default = files[:default] end def find_stylesheet(name) contents = @files.fetch(name, @default) Roadie::Stylesheet.new name, contents if contents end end roadie-5.2.1/spec/support/have_xpath_matcher.rb0000644000004100000410000000045114563757014021653 0ustar www-datawww-data# frozen_string_literal: true RSpec::Matchers.define :have_xpath do |xpath| match { |document| !document.xpath(xpath).empty? } failure_message { "expected document to have xpath #{xpath.inspect}" } failure_message_when_negated { "expected document to not have xpath #{xpath.inspect}" } end roadie-5.2.1/spec/integration_spec.rb0000644000004100000410000005227314563757014017653 0ustar www-datawww-data# frozen_string_literal: true require "spec_helper" describe "Roadie functionality" do describe "on full documents" do def parse_html(html) Nokogiri::HTML.parse(html) end it "adds missing structure" do html = "

Hello world!

".encode("Shift_JIS") document = Roadie::Document.new(html) result = document.transform expect(result).to include("") expect(result).to include("") expect(result).to include("") expect(result).to include("") expect(result).to include(" Hello world!

Hello world!

Check out these awesome prices!

HTML document.add_css <<-CSS em { color: red; } h1 { text-align: center; } CSS result = parse_html document.transform expect(result).to have_styling("text-align" => "center").at_selector("h1") expect(result).to have_styling("color" => "red").at_selector("p > em") end it "stores styles that cannot be inlined in the " do document = Roadie::Document.new <<-HTML

Hello world!

Check out these awesome prices!

HTML css = <<-CSS em:hover { color: red; } p:fung-shuei { color: spirit; } CSS document.add_css css expect(Roadie::Utils).to receive(:warn).with(/fung-shuei/) result = parse_html document.transform expect(result).to have_selector("html > head > style") styles = result.at_css("html > head > style").text expect(styles).to include Roadie::Stylesheet.new("", css).to_s end it "does not strip :root pseudo-class" do document = Roadie::Document.new <<-HTML Hello world!

Hello world!

HTML css = <<-CSS :root { --color: red; } CSS document.add_css css result = parse_html document.transform expect(result).to have_selector("html > head > style") styles = result.at_css("html > head > style").text expect(styles).to include Roadie::Stylesheet.new("", css).to_s end it "can be configured to skip styles that cannot be inlined" do document = Roadie::Document.new <<-HTML

Hello world!

Check out these awesome prices!

HTML css = <<-CSS em:hover { color: red; } p:fung-shuei { color: spirit; } CSS document.add_css css document.keep_uninlinable_css = false expect(Roadie::Utils).to receive(:warn).with(/fung-shuei/) result = parse_html document.transform expect(result).to_not have_selector("html > head > style") end it "inlines css from disk" do document = Roadie::Document.new <<-HTML Hello world!

Hello world!

Check out these awesome prices!

HTML result = parse_html document.transform expect(result).to have_styling("font-size" => "200%").at_selector("p > em") end it "crashes when stylesheets cannot be found, unless using NullProvider" do document = Roadie::Document.new <<-HTML HTML expect { document.transform }.to raise_error(Roadie::CssNotFound, /does_not_exist\.css/) document.asset_providers << Roadie::NullProvider.new expect { document.transform }.to_not raise_error end it "ignores external css if no external providers are added" do document = Roadie::Document.new <<-HTML Hello world!

Hello world!

Check out these awesome prices!

HTML document.external_asset_providers = [] result = parse_html document.transform expect(result).to have_selector("head > link") expect(result).to have_styling([]).at_selector("p > em") end it "inlines external css if configured" do document = Roadie::Document.new <<-HTML Hello world!

Hello world!

Check out these awesome prices!

HTML document.external_asset_providers = TestProvider.new( "http://example.com/big_em.css" => "em { font-size: 200%; }" ) result = parse_html document.transform expect(result).to have_styling("font-size" => "200%").at_selector("p > em") expect(result).to_not have_selector("head > link") end it "does not inline the same properties several times" do document = Roadie::Document.new <<-HTML

Hello world

HTML document.asset_providers = TestProvider.new("hello.css" => <<-CSS) p { color: red; } .hello { color: red; } .world { color: red; } CSS result = parse_html document.transform expect(result).to have_styling([ ["color", "red"] ]).at_selector("p") end it "makes URLs absolute" do document = Roadie::Document.new <<-HTML About us HTML document.asset_providers = TestProvider.new( "/style.css" => "a { background: url(/assets/link-abcdef1234567890.png); }" ) document.url_options = {host: "myapp.com", scheme: "https", path: "rails/app/"} result = parse_html document.transform expect(result.at_css("a")["href"]).to eq("https://myapp.com/rails/app/about_us") expect(result.at_css("img")["src"]).to eq("https://myapp.com/rails/app/assets/about_us-abcdef1234567890.png") expect(result).to have_styling( "background" => 'url("https://myapp.com/rails/app/assets/bg-abcdef1234567890.png")' ).at_selector("body") expect(result).to have_styling( "background" => "url(https://myapp.com/rails/app/assets/link-abcdef1234567890.png)" ).at_selector("a") end it "does not change URLs of ignored elements, but still inlines styles on them" do document = Roadie::Document.new <<-HTML About us Unsubscribe HTML document.url_options = {host: "myapp.com", scheme: "https", path: "rails/app/"} result = parse_html document.transform expect(result.at_css("a.one")["href"]).to eq("https://myapp.com/rails/app/about_us") expect(result.at_css("a.two")["href"]).to eq("$UNSUBSCRIBE_URL") expect(result).to have_styling("color" => "green").at_selector("a.one") expect(result).to have_styling("color" => "green").at_selector("a.two") end it "allows custom callbacks during inlining" do document = Roadie::Document.new <<-HTML Hello world HTML document.before_transformation = proc { |dom| dom.at_css("body")["class"] = "roadie" } document.after_transformation = proc { |dom| dom.at_css("span").remove } result = parse_html document.transform expect(result.at_css("body")["class"]).to eq("roadie") expect(result.at_css("span")).to be_nil end it "does not add whitespace between table cells" do document = Roadie::Document.new <<-HTML
One1
Two2
HTML result = document.transform expect(result).to include("One1") expect(result).to include("Two2") end it "doesn't inline styles in media queries with features" do document = Roadie::Document.new <<-HTML
HTML document.asset_providers = TestProvider.new( "/style.css" => <<-CSS .colorful { color: green; } @media screen and (max-width 600px) { .colorful { color: red; } } CSS ) result = parse_html document.transform expect(result).to have_styling("color" => "green").at_selector(".colorful") end it "puts non-inlineable media queries in the head" do document = Roadie::Document.new <<-HTML
HTML document.asset_providers = TestProvider.new( "/style.css" => <<-CSS .colorful { color: green; } @media screen and (max-width 800px) { .colorful { color: blue; } } @media screen, print and (max-width 800px) { .colorful { color: blue; } } CSS ) result = parse_html document.transform styles = result.at_css("html > head > style").text expected_result = <<-CSS @media screen and (max-width 800px) { .colorful{color:blue} } @media screen, print and (max-width 800px) { .colorful{color:blue} } CSS expected_result = expected_result.gsub(/\s+/, " ").strip actual_result = styles.gsub(/\s+/, " ").strip expect(actual_result).to eq(expected_result) end it "groups non-inlineable media queries in the head by default" do document = Roadie::Document.new <<-HTML
HTML document.asset_providers = TestProvider.new( "/style.css" => <<-CSS .colorful { color: green; } @media screen and (max-width 600px) { .colorful { color: red; width: 600px; } } @media screen and (max-width 600px) { .colorful-2 { color: red; width: 600px; } } CSS ) result = parse_html document.transform styles = result.at_css("html > head > style").text expected_result = <<-CSS @media screen and (max-width 600px) { .colorful{color:red;width:600px} .colorful-2{color:red;width:600px} } CSS expected_result = expected_result.gsub(/\s+/, " ").strip actual_result = styles.gsub(/\s+/, " ").strip expect(actual_result).to eq(expected_result) end it "adds XML declaration into XHTML with no serialization options prohibiting it" do document = Roadie::Document.new <<-HTML Greetings HTML document.mode = :xhtml document.serialization_options = 0 result = document.transform expect(result).to match(/\A<\?xml[^>]*?>/i) end it "does not add XML declaration into XHTML with serialization options prohibiting it" do document = Roadie::Document.new <<-HTML Greetings HTML document.mode = :xhtml document.serialization_options = Nokogiri::XML::Node::SaveOptions::NO_DECLARATION result = document.transform expect(result).not_to match(/\A<\?xml[^>]*?>/i) end describe "if merge_media_queries is set to false" do it "doesn't group non-inlineable media queries in the head" do document = Roadie::Document.new <<-HTML
HTML document.merge_media_queries = false document.asset_providers = TestProvider.new( "/style.css" => <<-CSS .colorful { color: green; } @media screen and (max-width 600px) { .colorful { color: red; width: 600px; } } @media screen and (max-width 600px) { .colorful-2 { color: red; width: 600px; } } CSS ) result = parse_html document.transform styles = result.at_css("html > head > style").text expected_result = <<-CSS @media screen and (max-width 600px) { .colorful{color:red;width:600px} } @media screen and (max-width 600px) { .colorful-2{color:red;width:600px} } CSS expected_result = expected_result.gsub(/\s+/, " ").strip actual_result = styles.gsub(/\s+/, " ").strip expect(actual_result).to eq(expected_result) end end end describe "on partial documents" do def parse_html(html) Nokogiri::HTML.fragment(html) end it "does not add structure" do html = "

Hello world!

".encode("Shift_JIS") document = Roadie::Document.new(html) result = document.transform_partial expect(result).to eq(html) end it "inlines given css" do document = Roadie::Document.new <<-HTML

Hello world!

Check out these awesome prices!

HTML document.add_css <<-CSS em { color: red; } h1 { text-align: center; } CSS result = parse_html document.transform_partial expect(result).to have_styling("text-align" => "center").at_selector("h1") expect(result).to have_styling("color" => "red").at_selector("p > em") end it "stores styles that cannot be inlined in a new HTML document.asset_providers = TestProvider.new( "/style.css" => "a { background: url(/assets/link-abcdef1234567890.png); }" ) document.url_options = {host: "myapp.com", scheme: "https", path: "rails/app/"} result = parse_html document.transform_partial expect(result.at_css("a")["href"]).to eq("https://myapp.com/rails/app/about_us") expect(result.at_css("img")["src"]).to eq( "https://myapp.com/rails/app/assets/about_us-abcdef1234567890.png" ) expect(result).to have_styling( "background" => 'url("https://myapp.com/rails/app/assets/bg-abcdef1234567890.png")' ).at_selector("div") expect(result).to have_styling( "background" => "url(https://myapp.com/rails/app/assets/link-abcdef1234567890.png)" ).at_selector("a") end it "allows custom callbacks during inlining" do document = Roadie::Document.new <<-HTML

Hello world

HTML document.before_transformation = proc { |dom| dom.at_css("p")["class"] = "roadie" } document.after_transformation = proc { |dom| dom.at_css("span").remove } result = parse_html document.transform_partial expect(result.at_css("p")["class"]).to eq("roadie") expect(result.at_css("span")).to be_nil end it "does not add whitespace between table cells" do document = Roadie::Document.new <<-HTML
One1
Two2
HTML result = document.transform_partial expect(result).to include("One1") expect(result).to include("Two2") end end end roadie-5.2.1/.yardopts0000644000004100000410000000004214563757014014670 0ustar www-datawww-data--no-private --files Changelog.md roadie-5.2.1/roadie.gemspec0000644000004100000410000000210114563757014015630 0ustar www-datawww-data# roadie.gemspec # frozen_string_literal: true $:.push File.expand_path("../lib", __FILE__) require "roadie/version" Gem::Specification.new do |s| s.name = "roadie" s.version = Roadie::VERSION s.platform = Gem::Platform::RUBY s.authors = ["Magnus Bergmark"] s.email = ["magnus.bergmark@gmail.com"] s.homepage = "http://github.com/Mange/roadie" s.summary = "Making HTML emails comfortable for the Ruby rockstars" s.description = "Roadie tries to make sending HTML emails a little less painful by inlining stylesheets and rewriting relative URLs for you." s.license = "MIT" s.required_ruby_version = ">= 2.7" s.add_dependency "nokogiri", "~> 1.15" s.add_dependency "css_parser", "~> 1.4" s.add_development_dependency "rake" s.add_development_dependency "rspec", "~> 3.0" s.add_development_dependency "rspec-collection_matchers", "~> 1.0" s.add_development_dependency "webmock", "~> 3.0" s.add_development_dependency "standardrb" s.extra_rdoc_files = %w[README.md Changelog.md] s.require_paths = %w[lib] s.files = `git ls-files`.split("\n") end roadie-5.2.1/Rakefile0000644000004100000410000000032114563757014014467 0ustar www-datawww-data# frozen_string_literal: true require "bundler/setup" Bundler::GemHelper.install_tasks desc "Run specs" task :spec do sh "bundle exec rspec -f progress" end desc "Default: Run specs" task default: :spec roadie-5.2.1/.solargraph.yml0000644000004100000410000000023514563757014015771 0ustar www-datawww-data--- include: - "**/*.rb" exclude: - spec/**/* - test/**/* - vendor/**/* - ".bundle/**/*" require: [] domains: [] require_paths: [] max_files: 5000 roadie-5.2.1/Gemfile0000644000004100000410000000064514563757014014326 0ustar www-datawww-data# frozen_string_literal: true source "https://rubygems.org" gemspec # Added here so it does not show up on the Gemspec; I only want it for CI builds gem "codecov", group: :test, require: false # Not actually required to run the tests for the gem, but a real convenience # for local development. gem "standard", group: [:test, :development], require: false gem "solargraph", group: [:test, :development], require: false roadie-5.2.1/LICENSE0000644000004100000410000000211714563757014014034 0ustar www-datawww-dataCopyright (c) 2009-2016 Magnus Bergmark, Jim Neath / Purify, and contributors. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. roadie-5.2.1/README.md0000644000004100000410000006074114563757014014315 0ustar www-datawww-dataRoadie ====== [![Code Climate](https://codeclimate.com/github/Mange/roadie.png)](https://codeclimate.com/github/Mange/roadie) [![Code coverage status](https://codecov.io/github/Mange/roadie/coverage.svg?branch=master)](https://codecov.io/github/Mange/roadie?branch=master) [![Gem](https://img.shields.io/gem/v/roadie.svg)](https://rubygems.org/gems/roadie) [![Passive maintenance](https://img.shields.io/badge/maintenance-Passive-yellow.svg)][passive] ||| |---|---| | :warning: | This gem is now in [passive maintenance mode][passive]. [(more)][passive] | > Making HTML emails comfortable for the Ruby rockstars Roadie tries to make sending HTML emails a little less painful by inlining stylesheets and rewriting relative URLs for you inside your emails. How does it work? ----------------- Email clients have bad support for stylesheets, and some of them blocks stylesheets from downloading. The easiest way to handle this is to work with inline styles (`style="..."`), but that is error prone and hard to work with as you cannot use classes and/or reuse styling over your HTML. This gem makes this easier by automatically inlining stylesheets into the document. You give Roadie your CSS, or let it find it by itself from the `` and ` ``` Roadie will use the given asset providers to look for the actual CSS that is referenced. If you don't change the default, it will use the `Roadie::FilesystemProvider` which looks for stylesheets on the filesystem, relative to the current working directory. Example: ```ruby # /home/user/foo/stylesheets/primary.css body { color: green; } # /home/user/foo/script.rb html = <<-HTML HTML Dir.pwd # => "/home/user/foo" document = Roadie::Document.new html document.transform # => # # # # # ``` If a referenced stylesheet cannot be found, the `#transform` method will raise an `Roadie::CssNotFound` error. If you instead want to ignore missing stylesheets, you can use the `NullProvider`. ### Configuring providers ### You can write your own providers if you need very specific behavior for your app, or you can use the built-in providers. Providers come in two groups: normal and external. Normal providers handle paths without host information (`/style/foo.css`) while external providers handle URLs with host information (`//example.com/foo.css`, `localhost:3001/bar.css`, and so on). The default configuration is to not have any external providers configured, which will cause those referenced stylesheets to be ignored. Adding one or more providers for external assets causes all of them to be searched and inlined, so if you only want this to happen to specific stylesheets you need to add ignore markers to every other styleshheet (see above). Included providers: * `FilesystemProvider` – Looks for files on the filesystem, relative to the given directory unless otherwise specified. * `ProviderList` – Wraps a list of other providers and searches them in order. The `asset_providers` setting is an instance of this. It behaves a lot like an array, so you can push, pop, shift and unshift to it. * `NullProvider` – Does not actually provide anything, it always finds empty stylesheets. Use this in tests or if you want to ignore stylesheets that cannot be found by your other providers (or if you want to force the other providers to never run). * `NetHttpProvider` – Downloads stylesheets using `Net::HTTP`. Can be given a whitelist of hosts to download from. * `CachedProvider` – Wraps another provider (or `ProviderList`) and caches responses inside the provided cache store. * `PathRewriterProvider` – Rewrites the passed path and then passes it on to another provider (or `ProviderList`). If you want to search several locations on the filesystem, you can declare that: ```ruby document.asset_providers = [ Roadie::FilesystemProvider.new(App.root.join("resources", "stylesheets")), Roadie::FilesystemProvider.new(App.root.join("system", "uploads", "stylesheets")), ] ``` #### `NullProvider` #### If you want to ignore stylesheets that cannot be found instead of crashing, push the `NullProvider` to the end: ```ruby # Don't crash on missing assets document.asset_providers << Roadie::NullProvider.new # Don't download assets in tests document.external_asset_providers.unshift Roadie::NullProvider.new ``` **Note:** This will cause the referenced stylesheet to be removed from the source code, so email client will never see it either. #### `NetHttpProvider` #### The `NetHttpProvider` will download the URLs that is is given using Ruby's standard `Net::HTTP` library. You can give it a whitelist of hosts that downloads are allowed from: ```ruby document.external_asset_providers << Roadie::NetHttpProvider.new( whitelist: ["myapp.com", "assets.myapp.com", "cdn.cdnnetwork.co.jp"], ) document.external_asset_providers << Roadie::NetHttpProvider.new # Allows every host ``` #### `CachedProvider` #### You might want to cache providers from working several times. If you are sending several emails quickly from the same process, this might also save a lot of time on parsing the stylesheets if you use in-memory storage such as a hash. You can wrap any other kind of providers with it, even a `ProviderList`: ```ruby document.external_asset_providers = Roadie::CachedProvider.new(document.external_asset_providers, my_cache) ``` If you don't pass a cache backend, it will use a normal `Hash`. The cache store must follow this protocol: ```ruby my_cache["key"] = some_stylesheet_instance # => # my_cache["key"] # => # my_cache["missing"] # => nil ``` **Warning:** The default `Hash` store will never be cleared, so make sure you don't allow the number of unique asset paths to grow too large in a single run. This is especially important if you run Roadie in a daemon that accepts arbritary documents, and/or if you use hash digests in your filenames. Making a new instance of `CachedProvider` will use a new `Hash` instance. You can implement your own custom cache store by implementing the `[]` and `[]=` methods. ```ruby class MyRoadieMemcacheStore def initialize(memcache) @memcache = memcache end def [](path) css = memcache.read("assets/#{path}/css") if css name = memcache.read("assets/#{path}/name") || "cached #{path}" Roadie::Stylesheet.new(name, css) end end def []=(path, stylesheet) memcache.write("assets/#{path}/css", stylesheet.to_s) memcache.write("assets/#{path}/name", stylesheet.name) stylesheet # You need to return the set Stylesheet end end document.external_asset_providers = Roadie::CachedProvider.new( document.external_asset_providers, MyRoadieMemcacheStore.new(MemcacheClient.instance) ) ``` If you are using Rspec, you can test your implementation by using the shared examples for the "roadie cache store" role: ```ruby require "roadie/rspec" describe MyRoadieMemcacheStore do let(:memcache_client) { MemcacheClient.instance } subject { MyRoadieMemcacheStore.new(memcache_client) } it_behaves_like "roadie cache store" do before { memcache_client.clear } end end ``` #### `PathRewriterProvider` #### With this provider, you can rewrite the paths that are searched in order to more easily support another provider. Examples could include rewriting absolute URLs into something that can be found on the filesystem, or to access internal hosts instead of external ones. ```ruby filesystem = Roadie::FilesystemProvider.new("assets") document.asset_providers << Roadie::PathRewriterProvider.new(filesystem) do |path| path.sub('stylesheets', 'css').downcase end document.external_asset_providers = Roadie::PathRewriterProvider.new(filesystem) do |url| if url =~ /myapp\.com/ URI.parse(url).path.sub(%r{^/assets}, '') else url end end ``` You can also wrap a list, for example to implement `external_asset_providers` by composing the normal `asset_providers`: ```ruby document.external_asset_providers = Roadie::PathRewriterProvider.new(document.asset_providers) do |url| URI.parse(url).path end ``` ### Writing your own provider ### Writing your own provider is also easy. You need to provide: * `#find_stylesheet(name)`, returning either a `Roadie::Stylesheet` or `nil`. * `#find_stylesheet!(name)`, returning either a `Roadie::Stylesheet` or raising `Roadie::CssNotFound`. ```ruby class UserAssetsProvider def initialize(user_collection) @user_collection = user_collection end def find_stylesheet(name) if name =~ %r{^/users/(\d+)\.css$} user = @user_collection.find_user($1) Roadie::Stylesheet.new("user #{user.id} stylesheet", user.stylesheet) end end def find_stylesheet!(name) find_stylesheet(name) or raise Roadie::CssNotFound.new( css_name: name, message: "does not match a user stylesheet", provider: self ) end # Instead of implementing #find_stylesheet!, you could also: # include Roadie::AssetProvider # That will give you a default implementation without any error message. If # you have multiple error cases, it's recommended that you implement # #find_stylesheet! without #find_stylesheet and raise with an explanatory # error message. end # Try to look for a user stylesheet first, then fall back to normal filesystem lookup. document.asset_providers = [ UserAssetsProvider.new(app), Roadie::FilesystemProvider.new('./stylesheets'), ] ``` You can test for compliance by using the built-in RSpec examples: ```ruby require 'spec_helper' require 'roadie/rspec' describe MyOwnProvider do # Will use the default `subject` (MyOwnProvider.new) it_behaves_like "roadie asset provider", valid_name: "found.css", invalid_name: "does_not_exist.css" # Extra setup just for these tests: it_behaves_like "roadie asset provider", valid_name: "found.css", invalid_name: "does_not_exist.css" do subject { MyOwnProvider.new(...) } before { stub_dependencies } end end ``` ### Keeping CSS that is impossible to inline Some CSS is impossible to inline properly. `:hover` and `::after` comes to mind. Roadie tries its best to keep these around by injecting them inside a new `

Hello world

``` When hovering over this `

`, the color will not change as the `color: green` rule takes precedence. You can get it to work by adding `!important` to the `:hover` rule. It would be foolish to try to automatically inject `!important` on every rule automatically, so this is a manual process. #### Turning it off #### If you'd rather skip this and have the styles not possible to inline disappear, you can turn off this feature by setting the `keep_uninlinable_css` option to false. ```ruby document.keep_uninlinable_css = false ``` ### Callbacks ### Callbacks allow you to do custom work on documents before they are transformed. The Nokogiri document tree is passed to the callable along with the `Roadie::Document` instance: ```ruby class TrackNewsletterLinks def call(dom, document) dom.css("a").each { |link| fix_link(link) } end def fix_link(link) divider = (link['href'] =~ /?/ ? '&' : '?') link['href'] = link['href'] + divider + 'source=newsletter' end end document.before_transformation = ->(dom, document) { logger.debug "Inlining document with title #{dom.at_css('head > title').try(:text)}" } document.after_transformation = TrackNewsletterLinks.new ``` ### XHTML vs HTML ### You can configure the underlying HTML/XML engine to output XHTML or HTML (which is the default). One usecase for this is that `{` tokens usually gets escaped to `{`, which would be a problem if you then pass the resulting HTML on to some other templating engine that uses those tokens (like Handlebars or Mustache). ```ruby document.mode = :xhtml ``` This will also affect the emitted `` if transforming a full document. Partial documents does not have a ``. Build Status ------------ Tested with Github CI using: * MRI 2.7 * MRI 3.0 * MRI 3.1 * MRI 3.2 Let me know if you want any other runtime supported officially. ### Versioning ### This project follows [Semantic Versioning](http://semver.org/) and has been since version 1.0.0. FAQ --- ### Why is my markup changed in subtle ways? Roadie uses Nokogiri to parse and regenerate the HTML of your email, which means that some unintentional changes might show up. One example would be that Nokogiri might remove your ` `s in some cases. Another example is Nokogiri's lack of HTML5 support, so certain new element might have spaces removed. I recommend you don't use HTML5 in emails anyway because of bad email client support (that includes web mail!). ### I'm getting segmentation faults (or other C-like problems)! What should I do? ### Roadie uses Nokogiri to parse the HTML of your email, so any C-like problems like segfaults are likely in that end. The best way to fix this is to first upgrade libxml2 on your system and then reinstall Nokogiri. Instructions on how to do this on most platforms, see [Nokogiri's official install guide](http://nokogiri.org/tutorials/installing_nokogiri.html). ### What happened to my `@keyframes`? The CSS Parser used in Roadie does not handle keyframes. I don't think any email clients do either, but if you want to keep on trying you can add them manually to a `