package plugin import ( "strings" "sync" "github.com/PuerkitoBio/goquery" "github.com/andybalholm/cascadia" lua "github.com/yuin/gopher-lua" "golang.org/x/net/html" ) const nodeType = "dibs.node" // nodeMethods are the methods of doc and of the nodes doc:all() returns. // Each takes an optional CSS selector: without one it works on the node // itself, with one on the first match inside it. var nodeMethods = map[string]lua.LGFunction{ "text": nodeText, "attr": nodeAttr, "exists": nodeExists, "count": nodeCount, "html": nodeHTML, "all": nodeAll, } func registerNode(L *lua.LState) { mt := L.NewTypeMetatable(nodeType) L.SetField(mt, "__index", L.SetFuncs(L.NewTable(), nodeMethods)) } func pushNode(L *lua.LState, sel *goquery.Selection) lua.LValue { ud := L.NewUserData() ud.Value = sel L.SetMetatable(ud, L.GetTypeMetatable(nodeType)) return ud } func checkNode(L *lua.LState) *goquery.Selection { if ud, ok := L.Get(1).(*lua.LUserData); ok { if sel, ok := ud.Value.(*goquery.Selection); ok { return sel } } L.ArgError(1, "node expected (call methods with a colon, as in doc:text(...))") return nil } // target is the node itself, or what the selector in argument arg matches // inside it. func target(L *lua.LState, node *goquery.Selection, arg int) *goquery.Selection { if L.GetTop() < arg || L.Get(arg) == lua.LNil { return node } return node.FindMatcher(selector(L, arg)) } var selectors sync.Map // string -> cascadia.Selector func selector(L *lua.LState, arg int) cascadia.Selector { src := L.CheckString(arg) if s, ok := selectors.Load(src); ok { return s.(cascadia.Selector) } s, err := cascadia.Compile(src) if err != nil { L.ArgError(arg, "invalid selector "+src+": "+err.Error()) return nil } selectors.Store(src, s) return s } // node:text([sel]) is the visible text of the node or its first match, with // whitespace collapsed. It is nil if nothing matches or the text is empty, // so `if doc:text(sel) then` means "has text"; exists tests presence. func nodeText(L *lua.LState) int { s := target(L, checkNode(L), 2).First() text := "" if s.Length() > 0 { text = visibleText(s) } if text == "" { L.Push(lua.LNil) } else { L.Push(lua.LString(text)) } return 1 } // node:attr(name) or node:attr(sel, name) is an attribute value, or nil. func nodeAttr(L *lua.LState) int { node := checkNode(L) s, name := node, L.CheckString(2) if L.GetTop() >= 3 { s, name = target(L, node, 2), L.CheckString(3) } if v, ok := s.First().Attr(name); ok { L.Push(lua.LString(v)) } else { L.Push(lua.LNil) } return 1 } // node:exists(sel) reports whether sel matches anything. func nodeExists(L *lua.LState) int { L.Push(lua.LBool(checkNode(L).FindMatcher(selector(L, 2)).Length() > 0)) return 1 } // node:count(sel) is the number of matches. func nodeCount(L *lua.LState) int { L.Push(lua.LNumber(checkNode(L).FindMatcher(selector(L, 2)).Length())) return 1 } // node:html([sel]) is the outer HTML of the node or its first match, or nil. func nodeHTML(L *lua.LState) int { s := target(L, checkNode(L), 2).First() if s.Length() == 0 { L.Push(lua.LNil) return 1 } out, err := goquery.OuterHtml(s) if err != nil { L.RaiseError("html: %v", err) } L.Push(lua.LString(out)) return 1 } // node:all(sel) is a list of nodes, one per match. func nodeAll(L *lua.LState) int { t := L.NewTable() checkNode(L).FindMatcher(selector(L, 2)).Each(func(_ int, s *goquery.Selection) { t.Append(pushNode(L, s)) }) L.Push(t) return 1 } // hiddenElements never contribute visible text. Amazon puts large inline // scripts inside its availability block, for example. var hiddenElements = map[string]bool{"script": true, "style": true, "noscript": true, "template": true} // inlineElements run into the text around them. Other elements are // separated by a space, so "