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

#1099@minor: Add support for currentScript. #1100

Merged

Conversation

ckhampus
Copy link
Contributor

Fixes #1099

@ckhampus ckhampus force-pushed the task/1099-add-document-current-script branch from 1c0174a to cd32a53 Compare September 27, 2023 08:19
Copy link
Owner

@capricorn86 capricorn86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! 🌟

I found some things. Otherwise it looks good.

WindowErrorUtility.captureError(this.ownerDocument.defaultView, () =>
this.ownerDocument.defaultView.eval(textContent)
);
try {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not do a try and catch here. Thats why we have the WindowErrorUtility.captureError() method that only should be used if this.ownerDocument.defaultView.happyDOM.settings.disableErrorCapturing is set to false.

If error capturing is disabled, we want the error to be thrown and catched by the framework using Happy DOM.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also add the this.ownerDocument['_currentScript'] = * logic to HTMLScriptElementUtility when it is evaluating the code.

Copy link
Contributor Author

@ckhampus ckhampus Sep 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback!

We should not do a try and catch here. Thats why we have the WindowErrorUtility.captureError() method that only should be used if this.ownerDocument.defaultView.happyDOM.settings.disableErrorCapturing is set to false.

If error capturing is disabled, we want the error to be thrown and catched by the framework using Happy DOM.

This try does not actually catch any errors because there isn't a catch-block, and it just has a finally-block so we can set this.ownerDocument['_currentScript'] to null. So any errors should passed through as before.

We should also add the this.ownerDocument['_currentScript'] = * logic to HTMLScriptElementUtility when it is evaluating the code.

I will fix that! 👍

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The try block slows down execution a lot as it will still create a new context for each try to capture errors.

If we encounter and error when this.ownerDocument.defaultView.happyDOM.settings.disableErrorCapturing is set to true, I think it is expected that the execution stops anyway.

You can see a performance test of try/finally here:
Link to test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I see, thanks for the link and info. Didn't know that. I will change it then 🙂

@capricorn86 capricorn86 merged commit b8f3ab6 into capricorn86:master Sep 30, 2023
@capricorn86
Copy link
Owner

I have fixed the last things and merged it now 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for document.currentScript
2 participants