Skip to content

Commit

Permalink
Merge 139495f into e79f4cd
Browse files Browse the repository at this point in the history
  • Loading branch information
Azhovan authored Jan 11, 2024
2 parents e79f4cd + 139495f commit 6203dce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions js/modules/k6/html/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,18 @@ func (e Element) IsSameNode(v goja.Value) bool {
return false
}

// Selection returns a Selection object based on the current Element.
//
// This function is used to create a Selection object that represents the same HTML
// content as the Element. It is useful for performing operations or manipulations
// on the HTML content within the scope of this Element.
//
// Example:
// sel := element.Selection()
func (e Element) Selection() Selection {
return *e.sel
}

func (e Element) GetElementsByClassName(name string) []goja.Value {
return elemList(Selection{e.sel.rt, e.sel.sel.Find("." + name), e.sel.URL})
}
Expand Down
9 changes: 9 additions & 0 deletions js/modules/k6/html/element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ func TestElement(t *testing.T) {
assert.Contains(t, nodes[3].Export().(Element).TextContent(), "Maecenas augue ligula")
}
})
t.Run("Selection", func(t *testing.T) {
t.Parallel()
rt := getTestRuntimeWithDoc(t, testHTMLElem)

v, err := rt.RunString(`doc.find('div').get(0).selection().find('h2').text()`)
if assert.NoError(t, err) {
assert.Equal(t, "Nullam id nisi eget ex pharetra imperdiet.", v.String())
}
})
t.Run("ClassList", func(t *testing.T) {
t.Parallel()
rt := getTestRuntimeWithDoc(t, testHTMLElem)
Expand Down

0 comments on commit 6203dce

Please sign in to comment.