class Prawn::Markup::Normalizer

Normalizes HTML markup:

* assert that self-closing tags are always closed
* replace html entities with their UTF-8 correspondent string
* normalize white space
* wrap entire content into <root> tag

Constants

REPLACE_ENTITIES
SELF_CLOSING_ELEMENTS

Attributes

html[R]

Public Class Methods

new(html) click to toggle source
# File lib/prawn/markup/support/normalizer.rb, line 19
def initialize(html)
  @html = html.dup
end

Public Instance Methods

normalize() click to toggle source
# File lib/prawn/markup/support/normalizer.rb, line 23
def normalize
  close_self_closing_elements
  normalize_spaces
  replace_html_entities
  "<body>#{html}</body>"
end

Private Instance Methods

close_self_closing_elements() click to toggle source
# File lib/prawn/markup/support/normalizer.rb, line 32
def close_self_closing_elements
  html.gsub!(/<(#{SELF_CLOSING_ELEMENTS.join('|')})[^>]*>/i) do |tag|
    tag[-1] = '/>' unless tag.end_with?('/>')
    tag
  end
end
normalize_spaces() click to toggle source
# File lib/prawn/markup/support/normalizer.rb, line 39
def normalize_spaces
  html.gsub!(/\s+/, ' ')
end
replace_html_entities() click to toggle source
# File lib/prawn/markup/support/normalizer.rb, line 43
def replace_html_entities
  REPLACE_ENTITIES.each do |entity, string|
    html.gsub!(/&#{entity};/, string)
  end
end