-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Make html.Selection.map return []goja.Value instead of []string #2534
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
olegbespalov
previously approved these changes
May 17, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
mstoykov
reviewed
May 18, 2022
Here's an example script demonstrating it at play import { parseHTML } from 'k6/html'
const data = `
<ListAllMyBucketsResult>
<Buckets>
<Bucket>
<CreationDate>myCreationDateTimestamp</CreationDate>
<Name>myBucketName</Name>
</Bucket>
</Buckets>
<Owner>
<DisplayName>string</DisplayName>
<ID>string</ID>
</Owner>
</ListAllMyBucketsResult>`
export default function () {
const buckets = parseHTML(data)
.find('Buckets')
.children()
.map(function (_, bucket) {
let bucketObj = {}
bucket.children().each(function (_, elem) {
switch (elem.nodeName()) {
case 'name':
Object.assign(bucketObj, { name: elem.textContent() })
break
case 'creationdate':
Object.assign(bucketObj, { creationDate: elem.textContent() })
break
}
})
return bucketObj
})
console.log(JSON.stringify(buckets)) // Prints [{"creationDate":"timestamp","name":"string"}]
console.log(buckets[0].name) // Prints "myBucketName"
console.log(buckets[0].creationDate) // Prints "myCreationDateTimestamp"
} cc @mstoykov |
mstoykov
reviewed
Jun 9, 2022
oleiade
force-pushed
the
fix/html_parse
branch
2 times, most recently
from
June 9, 2022 09:06
b7df16c
to
82118c6
Compare
mstoykov
requested changes
Jun 9, 2022
mstoykov
approved these changes
Jun 10, 2022
olegbespalov
approved these changes
Jun 10, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR attempts to address #2471.
The existing
Html.Selection.map
function would return a[]string
under the hood. This behavior would result in effectively obfuscating any type that's not a string during the parsing of an HTML or XML document. This PR modifies the behavior and signature of the function to fit what we have documented. The return value should be anArray
of values on the JS script side, a[]goja.Value
in the Go implementation of the library.The reason why we returned
[]string
in the first place is because goquery, the library we depend on to perform HTML/XML parsing, own map method does so. To work around the limitation, I adopted an approach consisting in serializing the values with map over to JSON, and deserialize them before we return from theMap
function.It works, but I'm open to any other approach that might be simpler or more performant 🙇🏻