mirror of
https://github.com/msva/lua-htmlparser.git
synced 2024-11-04 23:34:20 +00:00
improved tpl-resisting and testing
This commit is contained in:
parent
6f45c76030
commit
b5362e0e55
@ -4,12 +4,13 @@ sudo: false
|
||||
|
||||
env:
|
||||
global:
|
||||
- LUAROCKS=2.3.0
|
||||
- LUAROCKS=3.1.3
|
||||
matrix:
|
||||
- LUA=lua5.1
|
||||
- LUA=lua5.2
|
||||
- LUA=lua5.3
|
||||
- LUA=luajit # latest stable version (2.0.4)
|
||||
- LUA=lua5.4
|
||||
- LUA=luajit # latest stable version
|
||||
- LUA=luajit2.0 # current head of 2.0 branch
|
||||
- LUA=luajit2.1 # current head of 2.1 branch
|
||||
|
||||
|
@ -2,13 +2,13 @@
|
||||
|
||||
# A script for setting up environment for travis-ci testing.
|
||||
# Sets up Lua and Luarocks.
|
||||
# LUA must be "lua5.1", "lua5.2" or "luajit".
|
||||
# LUA must be "lua5.x" or "luajit".
|
||||
# luajit2.0 - master v2.0
|
||||
# luajit2.1 - master v2.1
|
||||
|
||||
set -eufo pipefail
|
||||
|
||||
LUAJIT_VERSION="2.0.4"
|
||||
LUAJIT_VERSION="2.0.5"
|
||||
LUAJIT_BASE="LuaJIT-$LUAJIT_VERSION"
|
||||
|
||||
source .travis/platform.sh
|
||||
@ -62,14 +62,14 @@ else
|
||||
|
||||
if [ "$LUA" == "lua5.1" ]; then
|
||||
curl http://www.lua.org/ftp/lua-5.1.5.tar.gz | tar xz
|
||||
cd lua-5.1.5;
|
||||
elif [ "$LUA" == "lua5.2" ]; then
|
||||
curl http://www.lua.org/ftp/lua-5.2.4.tar.gz | tar xz
|
||||
cd lua-5.2.4;
|
||||
elif [ "$LUA" == "lua5.3" ]; then
|
||||
curl http://www.lua.org/ftp/lua-5.3.2.tar.gz | tar xz
|
||||
cd lua-5.3.2;
|
||||
curl http://www.lua.org/ftp/lua-5.3.5.tar.gz | tar xz
|
||||
elif [ "$LUA" == "lua5.4" ]; then
|
||||
curl http://www.lua.org/work/lua-5.4.0-alpha-rc2.tar.gz | tar xz
|
||||
fi
|
||||
cd lua-5*
|
||||
|
||||
# Build Lua without backwards compatibility for testing
|
||||
perl -i -pe 's/-DLUA_COMPAT_(ALL|5_2)//' src/Makefile
|
||||
|
@ -109,40 +109,36 @@ local function parse(text,limit) -- {{{
|
||||
end
|
||||
-- g }}}
|
||||
|
||||
-- templaters {{{
|
||||
text=text:gsub(
|
||||
[=[(=[%s]-)(['"])]=].. -- only match attr.values, and not random strings between two random quoting marks
|
||||
[=[([^%2<>]+)]=]..
|
||||
[=[([^%2>]-)]=]..
|
||||
[=[(%2)]=],
|
||||
function(...)return g(4,...)end
|
||||
) -- Escape "<" inside attr.values (see issue #50)
|
||||
text=text:gsub(
|
||||
[=[(=[%s]-)(['"])]=].. -- only match attr.values, and not random strings between two random quoting marks
|
||||
[=[([^%2<>]+)]=]..
|
||||
[=[([^%2<]-)]=]..
|
||||
[=[(%2)]=],
|
||||
function(...)return g(4,...)end
|
||||
) -- Escape ">" inside attr.values (see issue #50)
|
||||
--[[
|
||||
]]
|
||||
text = text:gsub(
|
||||
"(<[^!])".. -- Comments aren't templaters placeholders
|
||||
"([^>]-)"..
|
||||
-- tpl-placeholders and attributes {{{
|
||||
text=text
|
||||
:gsub(
|
||||
"(=[%s]-)".. -- only match attr.values, and not random strings between two random apostrophs
|
||||
"(%b'')",
|
||||
function(...)return g(2,...)end
|
||||
)
|
||||
:gsub(
|
||||
"(=[%s]-)".. -- same for "
|
||||
'(%b"")',
|
||||
function(...)return g(2,...)end
|
||||
) -- Escape "<"/">" inside attr.values (see issue #50)
|
||||
:gsub(
|
||||
"(<".. -- Match "<",
|
||||
(opts.tpl_skip_pattern or "[^!]").. -- with exclusion pattern (for example, to ignore comments, which aren't template placeholders, but can legally contain "<"/">" inside.
|
||||
")([^>]+)".. -- If matched, we want to escape '<'s if we meet them inside tag
|
||||
"(>)",
|
||||
function(...)return g(2,...)end
|
||||
) -- scan for a second "<", inside "<>" (if it shows before ">"), until it inside the comment or CDATA
|
||||
text=text:gsub(
|
||||
"("..tpr["<"]..")"..
|
||||
"([^%w%s])"..
|
||||
"([^%2]-)"..
|
||||
"(%2)"..
|
||||
"(>)"..
|
||||
"([^>]-)"..
|
||||
"(>)", -- Comments and CDATA aren't templaters placeholders
|
||||
)
|
||||
:gsub(
|
||||
"("..
|
||||
(tpr["<"] or "__FAILED__").. -- Here we search for "<", we escaped in previous gsub (and don't break things if we have no escaping replacement)
|
||||
")("..
|
||||
(opts.tpl_marker_pattern or "[^%w%s]").. -- Capture templating symbol
|
||||
")([%g%s]-)".. -- match placeholder's content
|
||||
"(%2)(>)".. -- placeholder's tail
|
||||
"([^>]*>)", -- remainings
|
||||
function(...)return g(5,...)end
|
||||
) -- try to find matching ">" for previous replace
|
||||
-- templaters }}}
|
||||
)
|
||||
-- }}}
|
||||
end -- }}}
|
||||
|
||||
local index = 0
|
||||
|
11
tst/init.lua
11
tst/init.lua
@ -309,8 +309,13 @@ end
|
||||
|
||||
function test_loop_limit()
|
||||
local tree = htmlparser.parse([[
|
||||
<a id='a >b'>moo</a>
|
||||
<a id='c> d'>moo</a>
|
||||
<a id='e > f'>moo</a>
|
||||
<a id="g >h">moo</a>
|
||||
<a id="i> j">moo</a>
|
||||
<a id="k > l">moo</a>
|
||||
<a id='1>2'>moo</a>
|
||||
<a id='2>3'>moo</a>
|
||||
<b id='foo<bar'>moo</b>
|
||||
<img <%tpl%> foo=bar></img>
|
||||
<img <%tpl%> />
|
||||
@ -319,10 +324,8 @@ function test_loop_limit()
|
||||
<i <=moo=>>k</i>
|
||||
<s <-foo->>o</s>
|
||||
<div <*bar*>></div>
|
||||
<p>
|
||||
<a id="unclosed>Element"> with unclosed attribute</a>
|
||||
</p>
|
||||
<div data-pic="aa<%=image_url%>bb" ></div>
|
||||
]]) -- issue#42
|
||||
assert(1==1)
|
||||
assert(#tree.nodes==17)
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user