Class: Gotenberg::Rails::HtmlPreprocessor

Inherits:
Object
  • Object
show all
Defined in:
lib/gotenberg/rails/html_preprocessor.rb

Constant Summary collapse

URL_ATTRIBUTES =
%w[action href poster src].freeze
DATA_URL_ATTRIBUTES =
{
  "object" => %w[data]
}.freeze
SRCSET_ATTRIBUTES =
%w[srcset].freeze
SKIPPED_SCHEMES =
%w[cid data javascript mailto tel].freeze
CSS_URL_PATTERN =
/url\(\s*(['"]?)([^'")]+)\1\s*\)/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html, display_url:) ⇒ HtmlPreprocessor

Returns a new instance of HtmlPreprocessor.



19
20
21
22
# File 'lib/gotenberg/rails/html_preprocessor.rb', line 19

def initialize(html, display_url:)
  @html = html.to_s
  @display_url = display_url
end

Instance Attribute Details

#display_urlObject (readonly)

Returns the value of attribute display_url.



17
18
19
# File 'lib/gotenberg/rails/html_preprocessor.rb', line 17

def display_url
  @display_url
end

#htmlObject (readonly)

Returns the value of attribute html.



17
18
19
# File 'lib/gotenberg/rails/html_preprocessor.rb', line 17

def html
  @html
end

Instance Method Details

#callObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gotenberg/rails/html_preprocessor.rb', line 24

def call
  return html if display_url.nil? || display_url.to_s.empty?

  document = Nokogiri::HTML5(html)
  base_url = document.at_css("base[href]")&.[]("href") || display_url

  preprocess_url_attributes(document, base_url)
  preprocess_srcset_attributes(document, base_url)
  preprocess_css_urls(document, base_url)

  document.to_html
end