Skip to content

Commit

Permalink
feat: html module support Last func,
Browse files Browse the repository at this point in the history
  • Loading branch information
aooohan committed Feb 23, 2024
1 parent 2d75a9b commit e8cb133
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ $ node -v

> If you have installed `vfox`, you can view all available plugins with the `vfox available` command.
[![plugins](https://skillicons.dev/icons?i=java,nodejs,flutter,dotnet,python,dart,golang,maven,zig,deno&theme=light)](https://github.com/version-fox/version-fox-plugins)
[![plugins](https://skillicons.dev/icons?i=java,kotlin,nodejs,flutter,dotnet,python,dart,golang,maven,zig,deno&theme=light)](https://github.com/version-fox/version-fox-plugins)

For more details, see the [version-fox-plugins](https://github.com/version-fox/version-fox-plugins)

Expand Down
10 changes: 10 additions & 0 deletions internal/module/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var selectionMethods = map[string]lua.LGFunction{
"html": selectionHtml,
"find": selectionFind,
"first": selectionFirst,
"last": selectionLast,
"each": selectionEach,
"attr": selectionAttr,
"eq": selectionEq,
Expand Down Expand Up @@ -120,6 +121,15 @@ func selectionFirst(state *lua.LState) int {
return 1
}

func selectionLast(state *lua.LState) int {
s := checkSelection(state)
ud := state.NewUserData()
ud.Value = s.Last()
state.SetMetatable(ud, state.GetTypeMetatable(luaSelectionTypeName))
state.Push(ud)
return 1
}

func selectionFind(state *lua.LState) int {
s := checkSelection(state)
selector := state.CheckString(2)
Expand Down
10 changes: 10 additions & 0 deletions internal/module/html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ func TestEq(t *testing.T) {
evalLua(str, t)
}

func TestLast(t *testing.T) {
const str = `
local html = require("html")
local doc = html.parse("<html><body><div id='t2' name='123'>456</div><div>222</div></body></html>")
local s = doc:find("div"):last()
print(s:text() == "222")
`
evalLua(str, t)
}

func evalLua(str string, t *testing.T) {
s := lua.NewState()
defer s.Close()
Expand Down

0 comments on commit e8cb133

Please sign in to comment.