Module: StreamParser::HTML

Defined in:
lib/stream_parser/html.rb

Defined Under Namespace

Classes: Tag

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'lib/stream_parser/html.rb', line 5

def self.included(base)
  base.include(StreamParser)
end

Instance Method Details

#html_tag_valueObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/stream_parser/html.rb', line 96

def html_tag_value
  gobble(/\s+/)
  case peek(1)
  when '"', "'"
    quote_char = next_char
    forward(1)
    quoted_value(quote_char)
  else
    scan_until(/[^>\s\/=]+/)[0]
  end
end

#next_end_tag(name) ⇒ Object



108
109
110
# File 'lib/stream_parser/html.rb', line 108

def next_end_tag(name)
  scan_until(/<\/\s*li>/)
end

#next_tag(old_index: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stream_parser/html.rb', line 9

def next_tag(old_index: nil)
  old_index ||= @index
  return unless scan_until(/<\s*/)
  start_index = @index-1

  while peek(3) == '!--'
    forward(3)
    scan_until(/-->\s*/)
    scan_until(/<\s*/)
  end
  
  # HTMLComment.new(pre_match)
  if peek(1) == '/'
    scan_until(/[^>\s\/]+/)
    scan_tag(match, old_index: old_index, start_index: start_index, closing: true)
  else
    scan_until(/[^>\s\/]+/)
    scan_tag(match, old_index: old_index, start_index: start_index) 
  end
end

#scan_for_closing_tagObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/stream_parser/html.rb', line 39

def scan_for_closing_tag
  old_index = @index
  heap = []
  
  tag = next_tag
  puts tag.inspect
  while tag && !(tag.closing? && heap.empty?)
    if !tag.closing? && !tag.self_closing?
      heap << tag
    elsif !tag.self_closing?
      heap.pop
    end
    tag = next_tag(old_index: old_index)
  end
  @old_index = old_index
  tag
end

#scan_for_tag(name, closing: nil, **attributes) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/stream_parser/html.rb', line 30

def scan_for_tag(name, closing: nil, **attributes)
  old_index ||= @index
  tag = next_tag
  while tag && !tag.match(name: name, closing: closing, attributes: attributes)
    tag = next_tag(old_index: old_index)
  end
  tag
end

#scan_tag(name, closing: false, old_index:, start_index:) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/stream_parser/html.rb', line 57

def scan_tag(name, closing: false, old_index:, start_index:)
  tag = Tag.new(name, closing)
  
  while !eos?
    gobble(/\s+/)
    key = case peek(1)
    when '>'
      forward(1)
      @old_index = old_index
      @match = @source[start_index...@index]
      return tag
    when '/'
      forward(1)
      gobble(/\s*\>/)
      @old_index = old_index
      @match = @source[start_index...@index]
      tag.self_closing = true
      return tag
    when '"', "'"
      quote_char = next_char
      forward(1)
      quoted_value(quote_char)
    else
      scan_until(/[^>\s\/=]+/)[0]
    end
    
    tag[key] = if next?(/\s*=/)
      gobble(/\s*=/)
      html_tag_value
    else
      true
    end
  end

  @old_index = old_index
  @match = @source[start_index...@index]
  tag
end