make it work when there's a dot in the attribute selector

This commit is contained in:
ljie-PI 2013-12-22 23:05:04 +08:00
parent 34fb7909ec
commit 8ce4392e55
2 changed files with 19 additions and 3 deletions

View File

@ -149,11 +149,25 @@ local function select(self, s)
childrenonly = false childrenonly = false
if part == "*" then goto nextpart end if part == "*" then goto nextpart end
local excludes, filter = Set:new() 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 . "([:%[#.]?)" .. -- t = an optional :, [, #, or .
"([^:%(%[#.%]%)]+)" .. -- w = 1 or more of anything not :, (, [, #, ., ], or ) "([^:%(%[#.%]%)]+)" .. -- w = 1 or more of anything not :, (, [, #, ., ], or )
"%]?%)?" -- followed by an uncaptured optional ] and/or ) "(%]?)%)?" -- followed by an uncaptured optional ] and/or )
) do ) 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 if t == ":" then filter = w goto nextw end
local matched = match(t, w) local matched = match(t, w)
if filter == "not" then if filter == "not" then

View File

@ -85,7 +85,7 @@ function test_attr_equal()
<n a1 a2= a3='' a4="" <n a1 a2= a3='' a4=""
a5='a"5"' a6="a'6'" a7='#.[] :()' a8='|*+-=?$^%&/' a5='a"5"' a6="a'6'" a7='#.[] :()' a8='|*+-=?$^%&/'
a9=a9 a9=a9
a10></n> a10 a11="a11.js.jpg"></n>
]]) ]])
assert_equal(1, #tree.nodes, "top level") assert_equal(1, #tree.nodes, "top level")
assert(tree("[a1='']")[1], "a1=''") assert(tree("[a1='']")[1], "a1=''")
@ -102,6 +102,8 @@ function test_attr_equal()
assert(tree("[a9='a9']")[1], "a9='a9'") assert(tree("[a9='a9']")[1], "a9='a9'")
assert(tree("[a10='']")[1], "a10=''") assert(tree("[a10='']")[1], "a10=''")
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 end
function test_attr_notequal() function test_attr_notequal()