From 9dc07155030461ce90b0a19f4ba924d193082673 Mon Sep 17 00:00:00 2001 From: Patrick Ewing Date: Thu, 22 Oct 2015 10:10:44 -0700 Subject: [PATCH] Allow custom tag HTML5 tags to include hyphens. --- src/htmlparser.lua | 4 ++-- tst/init.lua | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/htmlparser.lua b/src/htmlparser.lua index e58c6b4..1328887 100644 --- a/src/htmlparser.lua +++ b/src/htmlparser.lua @@ -12,7 +12,7 @@ local function parse(text) local openstart, name openstart, tpos, name = string.find(root._text, "<" .. -- an uncaptured starting "<" - "(%w+)" .. -- name = the first word, directly following the "<" + "([%w-]+)" .. -- name = the first word, directly following the "<" "[^>]*>", -- include, but not capture everything up to the next ">" tpos) if not name then break end @@ -51,7 +51,7 @@ local function parse(text) local closeend = tpos while true do local closestart, closing, closename - closestart, closeend, closing, closename = string.find(root._text, "[^<]*<(/?)(%w+)", closeend) + closestart, closeend, closing, closename = string.find(root._text, "[^<]*<(/?)([%w-]+)", closeend) if not closing or closing == "" then break end tag = table.remove(opentags[closename] or {}) or tag -- kludges for the cases of closing void or non-opened tags closestart = string.find(root._text, "<", closestart) diff --git a/tst/init.lua b/tst/init.lua index afdf52a..47a4576 100644 --- a/tst/init.lua +++ b/tst/init.lua @@ -277,3 +277,14 @@ function test_order() assert_equal(i, tonumber(v.name), "notn order") end end + +function test_tagnames_with_hyphens() + local tree = htmlparser.parse([[ + + + + ]]) + assert_equal(1, #tree.nodes, "top level") + assert_equal("tag-name", tree("#9999")[1].name, "#9999") + assert_equal("m", tree("#10000")[1].name, "#10000") +end