lib/roadie/document.rb in roadie-3.0.5 vs lib/roadie/document.rb in roadie-3.1.0.rc1

- old
+ new

@@ -11,25 +11,30 @@ # 3. The internal stylesheet (see {#add_css}) # # The internal stylesheet is used last and gets the highest priority. The # rest is used in the same order as browsers are supposed to use them. # - # @attr [#call] before_transformation Callback to call just before {#transform}ation is begun. Will be called with the parsed DOM tree. - # @attr [#call] after_transformation Callback to call just before {#transform}ation is completed. Will be called with the current DOM tree. + # @attr [#call] before_transformation Callback to call just before {#transform}ation begins. Will be called with the parsed DOM tree and the {Document} instance. + # @attr [#call] after_transformation Callback to call just before {#transform}ation is completed. Will be called with the current DOM tree and the {Document} instance. class Document - attr_reader :html, :asset_providers + attr_reader :html, :asset_providers, :external_asset_providers # URL options. If none are given no URL rewriting will take place. # @see UrlGenerator#initialize attr_accessor :url_options attr_accessor :before_transformation, :after_transformation + # Should CSS that cannot be inlined be kept in a new `<style>` element in `<head>`? + attr_accessor :keep_uninlinable_css + # @param [String] html the input HTML def initialize(html) + @keep_uninlinable_css = true @html = html @asset_providers = ProviderList.wrap(FilesystemProvider.new) + @external_asset_providers = ProviderList.empty @css = "" end # Append additional CSS to the document's internal stylesheet. # @param [String] new_css @@ -37,13 +42,14 @@ @css << "\n\n" << new_css end # Transform the input HTML and returns the processed HTML. # - # Before the transformation begins, the {#before_transformation} callback will be - # called with the parsed HTML tree, and after all work is complete the - # {#after_transformation} callback will be invoked. + # Before the transformation begins, the {#before_transformation} callback + # will be called with the parsed HTML tree and the {Document} instance, and + # after all work is complete the {#after_transformation} callback will be + # invoked in the same way. # # Most of the work is delegated to other classes. A list of them can be seen below. # # @see MarkupImprover MarkupImprover (improves the markup of the DOM) # @see Inliner Inliner (inlines the stylesheets) @@ -63,27 +69,32 @@ # #dup is called since it fixed a few segfaults in certain versions of Nokogiri dom.dup.to_html end - # Assign new asset providers. The supplied list will be wrapped in a {ProviderList} using {ProviderList.wrap}. + # Assign new normal asset providers. The supplied list will be wrapped in a {ProviderList} using {ProviderList.wrap}. def asset_providers=(list) @asset_providers = ProviderList.wrap(list) end + # Assign new external asset providers. The supplied list will be wrapped in a {ProviderList} using {ProviderList.wrap}. + def external_asset_providers=(list) + @external_asset_providers = ProviderList.wrap(list) + end + private def stylesheet Stylesheet.new "(Document styles)", @css end def improve(dom) MarkupImprover.new(dom, html).improve end def inline(dom) - dom_stylesheets = AssetScanner.new(dom, asset_providers).extract_css - Inliner.new(dom_stylesheets + [stylesheet]).inline(dom) + dom_stylesheets = AssetScanner.new(dom, asset_providers, external_asset_providers).extract_css + Inliner.new(dom_stylesheets + [stylesheet], dom).inline(keep_uninlinable_css) end def rewrite_urls(dom) make_url_rewriter.transform_dom(dom) end @@ -95,9 +106,17 @@ NullUrlRewriter.new end end def callback(callable, dom) - callable.(dom) if callable.respond_to?(:call) + if callable.respond_to?(:call) + # Arity checking is to support the API without bumping a major version. + # TODO: Remove on next major version (v4.0) + if !callable.respond_to?(:parameters) || callable.parameters.size == 1 + callable.(dom) + else + callable.(dom, self) + end + end end end end