Class: HTML5small::Minifier

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/html5small/minifier.rb

Constant Summary collapse

PRE_TAGS =

Elements in which whitespace is significant, so can’t be normalised

[:pre, :style, :script, :textarea]
FLOW_ELEMENTS =

Elements representing flow content

%w{a abbr address area article aside audio b bdo blockquote br
 button canvas cite code command datalist del details dfn div
 dl em embed fieldset figure footer form h1 h2 h3 h4 h5 h6 header
 hgroup hr i iframe img input ins kbd keygen label link
 map mark math menu meta meter nav noscript object ol output
 p pre progress q ruby samp script section select small span
 strong style sub sup svg table textarea time ul var video wbr
}.map(&:to_sym)
BLOCK_ELEMENTS =

BLock-level elements

%w{address article aside audio blockquote canvas dd div dl fieldset
 figcaption figure footer form h1 h2 h3 h4 h5 h6 header hgroup hr
 li noscript ol output p pre section table thead tfoot tr ul video
}.map(&:to_sym)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMinifier

Returns a new instance of Minifier.



27
28
29
30
31
# File 'lib/html5small/minifier.rb', line 27

def initialize
  @buf, @text_node = '', ''
  @stack = [], @prev_tag
  @entities = HTMLEntities.new :expanded
end

Instance Attribute Details

#bufObject

Returns the value of attribute buf.



25
26
27
# File 'lib/html5small/minifier.rb', line 25

def buf
  @buf
end

#entitiesObject

Returns the value of attribute entities.



25
26
27
# File 'lib/html5small/minifier.rb', line 25

def entities
  @entities
end

#text_nodeObject

Returns the value of attribute text_node.



25
26
27
# File 'lib/html5small/minifier.rb', line 25

def text_node
  @text_node
end

Instance Method Details

#cdata_block(string) ⇒ Object



63
64
65
# File 'lib/html5small/minifier.rb', line 63

def cdata_block string
  text_node << string
end

#characters(chars) ⇒ Object



67
68
69
# File 'lib/html5small/minifier.rb', line 67

def characters chars
  text_node << chars
end

#comment(string) ⇒ Object



56
57
58
59
60
61
# File 'lib/html5small/minifier.rb', line 56

def comment string
  # I.E "conditional comments" should be retained as-is
  if string =~ /\[if\s+lt\s+IE\s+\d+\]/i
    buf << "<!--#{string}-->"
  end
end

#end_element(name) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/html5small/minifier.rb', line 47

def end_element name
  name = normalise_name name
  dump_text_node
  buf.rstrip! if is_block_element?(name) and not in_pre_element?
  @stack.pop
  @prev_tag = name
  buf << "</#{name}>"
end

#start_documentObject

HTML5 documents begin with the doctype



34
35
36
# File 'lib/html5small/minifier.rb', line 34

def start_document
  buf << "<!doctype html>"
end

#start_element(name, attrs = []) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/html5small/minifier.rb', line 38

def start_element name, attrs = []
  name = normalise_name name
  dump_text_node
  @stack.push name
  @prev_tag = name
  buf.rstrip! if is_block_element?(name) and not in_pre_element?
  buf << "<#{name}" + format_attributes(attrs, name) + ">"
end