From 8ce4392e550a9ba8628cb1b6a11cee8e3546f7a4 Mon Sep 17 00:00:00 2001 From: ljie-PI Date: Sun, 22 Dec 2013 23:05:04 +0800 Subject: [PATCH] make it work when there's a dot in the attribute selector --- src/htmlparser/ElementNode.lua | 18 ++++++++++++++++-- tst/init.lua | 4 +++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/htmlparser/ElementNode.lua b/src/htmlparser/ElementNode.lua index a44ba0a..22ee7c6 100644 --- a/src/htmlparser/ElementNode.lua +++ b/src/htmlparser/ElementNode.lua @@ -149,11 +149,25 @@ local function select(self, s) childrenonly = false if part == "*" then goto nextpart end local excludes, filter = Set:new() - for t, w in string.gmatch(part, + local halfword = "" + for t, w, c in string.gmatch(part, "([:%[#.]?)" .. -- t = an optional :, [, #, or . "([^:%(%[#.%]%)]+)" .. -- w = 1 or more of anything not :, (, [, #, ., ], or ) - "%]?%)?" -- followed by an uncaptured optional ] and/or ) + "(%]?)%)?" -- followed by an uncaptured optional ] and/or ) ) do + -- this if..elseif.. block will match the pattern like "[src='aaa.jpg']" + if t == "[" and c ~= "]" then + halfword = t .. w + goto nextw + elseif c == "" and halfword ~= "" then + halfword = halfword .. t .. w + goto nextw + elseif t ~= "[" and c == "]" then + halfword = halfword .. t .. w .. c + t, w = "[", string.sub(halfword, 2, -2) + halfword = "" + end + if t == ":" then filter = w goto nextw end local matched = match(t, w) if filter == "not" then diff --git a/tst/init.lua b/tst/init.lua index 6606086..e6c17b7 100644 --- a/tst/init.lua +++ b/tst/init.lua @@ -85,7 +85,7 @@ function test_attr_equal() + a10 a11="a11.js.jpg"> ]]) assert_equal(1, #tree.nodes, "top level") assert(tree("[a1='']")[1], "a1=''") @@ -102,6 +102,8 @@ function test_attr_equal() assert(tree("[a9='a9']")[1], "a9='a9'") assert(tree("[a10='']")[1], "a10=''") assert(tree("[a10=]")[1], "a10=") + -- An excepton for a7. Some times we may select javascript or img nodes with attr selector [src="a.js"] or [src="a.jpg"] + assert(tree("[a11='a11.js.jpg']")[1], "a11=") end function test_attr_notequal()