kostya / lexbor

Fast HTML5 Parser with CSS selectors. This is successor of myhtml and expected to be faster and use less memory.
MIT License
95 stars 14 forks source link

Can't delete all comments from document. #13

Closed MrSorcus closed 3 years ago

MrSorcus commented 3 years ago
require "lexbor"

html = <<-HTML
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Example</title>
  </head>
  <body><!-- foo --> <!-- bar --> <!-- baz --></body>
</html>
HTML

lexbor = Lexbor::Parser.new(html)
lexbor.nodes(:_em_comment).each(&.remove!)
puts lexbor.body!.to_html

Should print <body> </body>, but instead print <body><!-- bar --> <!-- baz --></body>. Or it doesn't work like that?

kostya commented 3 years ago

this works:

nodes = lexbor.nodes(:_em_comment).to_a
nodes.each(&.remove!)
puts lexbor.body!.to_html

I think need to change example: because nodes in this line lexbor.nodes(:_em_comment).each(&.remove!) is iterator, not materialized array. And removing while iterating, something like break things.