Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enabling the extraction of a specific selection from an element #3519

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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