Class: REHTML::REXMLBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rehtml/builder.rb

Constant Summary collapse

EMPTY_TAGS =
Set.new %w[area base br col embed hr img input keygen link meta param source track wbr isindex basefont]
CDATA_TAGS =
Set.new %w[script style textarea xmp title]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



7
8
9
# File 'lib/rehtml/builder.rb', line 7

def doc
  @doc
end

Instance Method Details

#append(node) ⇒ Object

append node to document



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rehtml/builder.rb', line 19

def append(node)
  if node.is_a?(EndTag)
    return if empty_tag?(node.name)
    po = @pos
    while po.parent and po.name != node.name
      po = po.parent
    end
    if po.name == node.name
      @pos = po.parent
    end
  else
    rexml = to_rexml(node)

    # if node is second root element, add root element wrap html tag
    if rexml.is_a?(REXML::Element) and @pos == @doc and @doc.root
      if @doc.root.name != 'html'
        html = REXML::Element.new
        html.name = "html"
        i = @doc.root.index_in_parent-1
        while pos = @doc.delete_at(i)
          @doc.delete_element(pos) if pos.is_a?(REXML::Element)
          html << pos
        end
        @doc << html
        @pos = html
      end
      @pos = @doc.root
    end
    @pos << rexml
    if rexml.is_a?(REXML::Element) and !empty_tag?(node.name) and !node.empty?
      @pos = rexml
    end
  end
end

#parse(tokenizer) ⇒ Object

build document use tokenizer



10
11
12
13
14
15
16
# File 'lib/rehtml/builder.rb', line 10

def parse(tokenizer)
  @doc = REXML::Document.new
  @pos = @doc
  while node=tokenizer.next
    append(node)
  end
end