Module: Cybertrain::Test::HtmlReader
- Defined in:
- cybertrain/test/html_reader.rb
Overview
The reader: HtmlReader.parse(html) answers every element in document order. See the file’s comment for what it reads and refuses.
Defined Under Namespace
Classes: Malformed
Class Method Summary collapse
-
.decode_references(text) ⇒ String
textwith its character references decoded: the named ones in NAMED,&#NN;and&#xHH;(an invalid code point becomes U+FFFD). -
.parse(html) ⇒ Array<HtmlNode>
Every element of
html, in document order; each knows its parent’s position (HtmlNode#node_parent_index) and its children.
Class Method Details
.decode_references(text) ⇒ String
text with its character references decoded: the named ones in
NAMED, &#NN; and &#xHH; (an invalid code point becomes U+FFFD).
Anything else after & stays as written.
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
# File 'cybertrain/test/html_reader.rb', line 589 def self.decode_references(text) s = text.to_s return s if s.index("&").nil? out = +"" n = s.bytesize i = 0 run = 0 while i < n if s.getbyte(i) != 38 # & i += 1 next end semi = i + 1 semi += 1 while semi < n && semi - i <= 32 && reference_byte?(s.getbyte(semi)) if semi >= n || s.getbyte(semi) != 59 || semi == i + 1 # no `;` close by i += 1 next end name = s.byteslice(i + 1, semi - i - 1) value = reference_text(name) if value.empty? i += 1 next end out << s.byteslice(run, i - run) if i > run out << value i = semi + 1 run = i end out << s.byteslice(run, n - run) if n > run out end |
.parse(html) ⇒ Array<HtmlNode>
Every element of html, in document order; each knows its parent’s
position (HtmlNode#node_parent_index) and its children. A binary
(ASCII-8BIT) body is read as UTF-8.
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'cybertrain/test/html_reader.rb', line 255 def self.parse(html) # Read through a fresh local (NOTES rule 62). text = HtmlReader.utf8_text(html.to_s) s = text.to_s nodes = Array.new(0) { HtmlNode.new("", 0, -1, 0) } stack = Array.new(0) { 0 } n = s.bytesize i = 0 run = 0 while i < n b = s.getbyte(i) if b != 60 # < i += 1 next end kind = tag_kind(s, i) if kind == 0 i += 1 next end add_node_texts(nodes, stack, decode_references(s.byteslice(run, i - run))) if i > run # An if chain, not a statement `case kind` # (spikes/phase5/case_nullable_temp.rb): Spinel compiles it as if # the subject were nullable, with an undeclared temporary, in a # program where an earlier statement `case` on a getbyte value # (Html.escape's) got the same temporary number. if kind == 1 # comment close = markup_end(s, i, 1, false) if close < 0 = "unterminated comment at byte #{i}" raise Malformed, end i = close elsif kind == 2 # <!doctype ...>, <?...?>, other <!...> foreign = in_foreign?(nodes, stack) close = markup_end(s, i, 2, foreign) if foreign && s.byteslice(i, 9) == "<![CDATA[" # Text in svg / math, up to `]]>`. if close < 0 = "unterminated CDATA section at byte #{i}" raise Malformed, end add_node_texts(nodes, stack, s.byteslice(i + 9, close - 3 - i - 9)) i = close run = i next end if close < 0 = "unterminated declaration at byte #{i}" raise Malformed, end i = close elsif kind == 3 # end tag i = read_end_tag(s, i, nodes, stack) else # start tag i = read_start_tag(s, i, nodes, stack) end run = i end add_node_texts(nodes, stack, decode_references(s.byteslice(run, n - run))) if n > run unless stack.empty? top = nodes[stack[stack.length - 1]] = "missing end tag </#{top.node_tag}> for <#{top.node_tag}> opened at byte #{top.node_start}#{optional_end_hint(top.node_tag)}" raise Malformed, end nodes end |