michal-h21 / LuaXML

Fork of LuaXML (originally Paul Chakravarti)
14 stars 9 forks source link

"nth-child" does not work #2

Closed rolfn closed 5 years ago

rolfn commented 5 years ago

I try CSS selection with the library luaxml-cssquery in the following way:

\documentclass{article}
\usepackage{luacode}
\begin{luacode*}
sample = [[
<data>
  <items>
    <item>foo</item>
    <item>bar</item>
    <item>baz</item>
  </items>
</data>
]]

local cssobj = require 'luaxml-cssquery'
local domobj = require 'luaxml-domobject'
local dom = domobj.parse(sample)
local css = cssobj()

local sel = [[
data
items
item:nth-child(2)
]]
css:add_selector(sel, function(obj)
  print('\n########## ' .. obj:get_text())
end)

dom:traverse_elements(function(el)
  local querylist = css:match_querylist(el)
  css:apply_querylist(el, querylist)
end)
\end{luacode*}

\begin{document}
\end{document}

Instead of one result bar I get three results (foo, bar, and baz). What is wrong here? Thanks in advance.

Rolf

michal-h21 commented 5 years ago

I've added basic support for the nth-child selector - only numbered selector works, not odd, even or anything more advanced. It seems to work with your example.

Best, Michal

rolfn commented 5 years ago

I tried it again with my example. It doesn't work:

luaxml-cssquery.lua:64: attempt to call method 'get_siblings' (a nil value)
stack traceback:
        ./luaxml-cssquery.lua:64: in function 'make_nth'
        ./luaxml-cssquery.lua:82: in function <./luaxml-cssquery.lua:58>
        (...tail calls...)
        ./luaxml-cssquery.lua:123: in function 'test_object'
        ./luaxml-cssquery.lua:137: in function 'match_query'
        ./luaxml-cssquery.lua:147: in function 'match_querylist'
        [\directlua]:26: in function 'fn'
        ./luaxml-domobject.lua:343: in function 'traverse_elements'
        ./luaxml-domobject.lua:347: in function 'traverse_elements'
        [\directlua]:25: in main chunk.
\luacode@dbg@exec ...code@maybe@printdbg {#1} #1 }

Where is the method get_siblings defined?

michal-h21 commented 5 years ago

Ah, sorry, I forgot to push fixes in luaxml-domobject.lua to the repo. It should work now.

rolfn commented 5 years ago

Yes, it works now. Many thanks.

Greetings from Berlin Rolf