Method: SGMLParser#parse_starttag

Defined in:
lib/sgml-parser.rb,
lib/web/htmlparser/sgml-parser.rb

#parse_starttag(i) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/sgml-parser.rb', line 174

def parse_starttag(i)
  rawdata = @rawdata
  j = rawdata.index(Endbracket, i + 1)
  return nil unless j
  attrs = []
  if rawdata[i+1] == ?> #
    # SGML shorthand: <> == <last open tag seen>
    k = j
    tag = @lasttag
  else
    match = rawdata.index(Tagfind, i + 1)
    unless match
      raise RuntimeError, 'unexpected call to parse_starttag'
    end
    k = i + 1 + ($&.length)
    tag = $&.downcase
    @lasttag = tag
  end
  while k < j
    break unless rawdata.index(Attrfind, k)
    matched_length = $&.length
    attrname, rest, attrvalue = $1, $2, $3
    if not rest
      attrvalue = '' # was: = attrname
    elsif (attrvalue[0] == ?' && attrvalue[-1] == ?') or
        (attrvalue[0] == ?" && attrvalue[-1,1] == ?")
      attrvalue = attrvalue[1..-2]
    end
    attrs << [attrname.downcase, attrvalue]
    k += matched_length
  end
  if rawdata[j] == ?> #
    j += 1
  end
  finish_starttag(tag, attrs)
  return j
end