msva / lua-htmlparser

An HTML parser for lua.
231 stars 44 forks source link

Bug: nil index error if parent child selector + any voidelement #55

Closed keiviv closed 3 years ago

keiviv commented 4 years ago

Hi @msva, huge thanks for your parser! Found a small, but annoying bug.

Bug Description

Any tag listed in voidelements.lua casues the "Attempt to index a nil value" error if using a parent child selector.

Bug Reproduction

Shortest working example:

local htmlparser = require 'htmlparser'
local html = [[
<br><!-- ← This single tag causes nil index error. No error without it. -->
<div class="parent">
   <div class="child">msva rulit, kogda ne spit!</div>
</div>
]]
local root = htmlparser.parse(html)
local bug = root('.parent .child') -- Error only if a pair. No error if single.
print(bug[1]:getcontent())

Temp Workaround

Until fixed, remove all unpaired tags that are listed in voidelements.lua from the html to be processed:

html = html:gsub('<br>', ''):gsub('<br />', ''):gsub('<hr>', ''):gsub('etc', '')
boston2029 commented 3 years ago

That's weird.

msva commented 3 years ago

@keiviv Hi there! Sorry for delay. It should be fixed now.

@boston2029 What exactly?