From b715a27017f1b59f674e314012d38546748fa388 Mon Sep 17 00:00:00 2001 From: Jabar Asadi Date: Thu, 28 Dec 2023 23:15:44 +0100 Subject: [PATCH] feat: enabling the extraction of a specific selection from an element --- js/modules/k6/html/element.go | 12 ++++++++++++ js/modules/k6/html/element_test.go | 9 +++++++++ js/modules/k6/html/html.go | 18 ++++++++++++++++++ js/modules/k6/html/html_test.go | 12 ++++++++++++ 4 files changed, 51 insertions(+) diff --git a/js/modules/k6/html/element.go b/js/modules/k6/html/element.go index ad80a701546b..b8b655ff3a4d 100644 --- a/js/modules/k6/html/element.go +++ b/js/modules/k6/html/element.go @@ -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 Selection{rt: e.sel.rt, sel: e.sel.sel, URL: e.sel.URL} +} + func (e Element) GetElementsByClassName(name string) []goja.Value { return elemList(Selection{e.sel.rt, e.sel.sel.Find("." + name), e.sel.URL}) } diff --git a/js/modules/k6/html/element_test.go b/js/modules/k6/html/element_test.go index 144f21eb8ef3..f1f2fd03e314 100644 --- a/js/modules/k6/html/element_test.go +++ b/js/modules/k6/html/element_test.go @@ -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) diff --git a/js/modules/k6/html/html.go b/js/modules/k6/html/html.go index 180567a9c3a6..955edbe01e97 100644 --- a/js/modules/k6/html/html.go +++ b/js/modules/k6/html/html.go @@ -280,6 +280,24 @@ func (s Selection) Last() Selection { return Selection{s.rt, s.sel.Last(), s.URL} } +// FromElement creates a new Selection based on a specified Element. +// +// This function is useful for creating a new Selection object that +// encapsulates the same HTML structure as the provided Element. It +// is particularly helpful when you need to perform further operations +// or manipulations on the HTML content within the scope of this Element. +// +// Example: +// sel := existingSelection.FromElement(jsElement) +func (s Selection) FromElement(v goja.Value) Selection { + elm, ok := v.Export().(Element) + if !ok { + return s.emptySelection() + } + + return Selection{rt: s.rt, sel: elm.sel.sel, URL: s.URL} +} + func (s Selection) Contents() Selection { return Selection{s.rt, s.sel.Contents(), s.URL} } diff --git a/js/modules/k6/html/html_test.go b/js/modules/k6/html/html_test.go index 19f3e912de98..5d7463543647 100644 --- a/js/modules/k6/html/html_test.go +++ b/js/modules/k6/html/html_test.go @@ -269,6 +269,18 @@ func TestParseHTML(t *testing.T) { assert.Equal(t, "form1", v.Export()) } }) + t.Run("FromElement", func(t *testing.T) { + t.Parallel() + rt := getTestRuntimeWithDoc(t, testHTML) + + _, err := rt.RunString(`var body = doc.find("body").get(0)`) + assert.NoError(t, err) + + v, err := rt.RunString(`doc.fromElement(body).find('#top').text()`) + if assert.NoError(t, err) { + assert.Equal(t, "Lorem ipsum", v.String()) + } + }) t.Run("Contents", func(t *testing.T) { t.Parallel() rt := getTestRuntimeWithDoc(t, testHTML)