kostya / myhtml

Fast HTML5 Parser with css selectors for Crystal language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to parse <template> tag

jwoertink opened this issue · comments

When I try to parse the template tag, it just returns nil.

html = Myhtml::Parser.new("<template>test</template>")
body = html.body!

puts body.children.inspect

#=> Myhtml::Iterator::Children(@start_node=Myhtml::Node(:body), @current_node=nil)

The strange thing is that I can make up random tags like <jeremy> and it parses those fine.

html = Myhtml::Parser.new("<jeremy>test</jeremy>")
body = html.body!

puts body.children.inspect
#=> Myhtml::Iterator::Children(@start_node=Myhtml::Node(:body), @current_node=Myhtml::Node(:last_entry))

because it parsed to head, may be you would need fragment parsing for that, or wrap everything in <body>:

html = Myhtml::Parser.new("<template>test</template>")
html.root!.walk_tree { |node, level| puts "#{" " * level * 2}#{node.inspect}" }
Myhtml::Node(:html)
  Myhtml::Node(:head)
    Myhtml::Node(:template)
      Myhtml::Node(:_text, "test")
  Myhtml::Node(:body)

Is there a way to tell if the input has a body tag? Right now the html.body method always returns a body.

html = Myhtml::Parser.new("<template>test</template>")
body = html.body

puts body.inspect
#=> Myhtml::Node(:body)


html = Myhtml::Parser.new("<body><template>test</template></body>")
body = html.body

puts body.inspect
#=> Myhtml::Node(:body)

Even if I walk the tree, I can still get a body even if one isn't passed.

html = Myhtml::Parser.new("<template>test</template>")

html.root!.walk_tree do |node, level|
  if node.tag_name == "body"
    # This is still printed out
    puts "has body"
  end
end

Just for a little background context on what I'm doing here, I'm working on this tool: https://luckyframework.org/convert-html-to-lucky. So I have no clue what the user will enter. It could be a snippet, or an entire document. I'm not able to blindly wrap the input in a <body> because the user might paste in a <body> already. We do this here.