Problem/Motivation
As mentioned in the parent issue #3238306: [META] Where possible, refactor existing jQuery uses to vanillaJS to reduce jQuery footprint, we are working towards reducing our jQuery footprint. One of the ways to accomplish this is to reduce the number of jQuery features used in Drupal core. We have added eslint rules that identify specific features and fail tests when those features are in use.
There are (or will be) individual issues for each jQuery-use eslint rule. This one is specific to jquery/no-parse-html, which targets the jQuery parseHTML function.
Steps to reproduce
Proposed resolution
Remaining tasks
Confirm if there are any other considerations as per #5, #6, #11, #14.
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #15 | 3238870-nr-bot.txt | 90 bytes | needs-review-queue-bot |
Issue fork drupal-3238870
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #4
mstrelan commentedI didn't have any luck with the suggested
createHTMLDocument()but was at least able to get\Drupal\FunctionalJavascriptTests\Ajax\CommandsTest::testAjaxCommandspassing with this approach. Let's see if anything else fails.Comment #5
nod_There are security considerations to take care of with replacing parseHTML: https://github.com/jquery/jquery/blob/main/src/core/parseHTML.js
a straightforward replacement is unlikely to be suitable.
We might need to extract jquery's logic in vanilla js somehow
Comment #6
mstrelan commented@nod_ are those considerations applicable in this one case that $.parseHTML is called? Or is it bypassed because we're passing
truefor thekeepScriptsparam?Comment #11
larowlanI think @mstrelan is right here, we're using the third argument to parseHTML so we're not getting any filtering anyway
Comment #12
mstrelan commentedComment #13
smustgrave commentedRefactor looks good and applying locally doesn't seem to break anything.
Comment #14
lauriiiWould be great if we could get confirmation from @nod_ or @justafish on this. The
$.parseHtmlis using jQuerys own way of handling fragments which at least provides an API for filtering HTML. We could probably manage this by publishing a CR but it would be good to get confirmation that there aren't any other considerations.Comment #15
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #16
mstrelan commentedI don't understand what the bot wants me to do.
"Patch does not apply"
"This does not mean that the patch needs to be re-rolled or the MR rebased."
If both are true then this should not be "Needs work".
Anyway, I've merged 11.x in to the branch and pushed it back up. Setting back to NR. Updated remaining tasks.
Comment #17
nod_@mstrelan the bot comment is there to discourage credit farming from people from doing rerolls without addressing anything else that needs to be addressed and leave the issue stalled.
Spent some time looking at the replacement, still not sure it's safe. We do keep scripts although the scripts are not executed at parse time, they're executed when we add the elements to the DOM after the parsing. Here they would be executed during the parsing. I'm not convinced it's a problem, especially since we don't add JS with that codepath anymore (since the introduction of the add_js command). So we could try to set keepScripts to false and see what happens with the testbot first. That would be a good indication this is not a concern anymore. In any case the behavior change should be in a change notice somewhere.
If we don't have scripts to take care of, next is prefiltering html as lauriii pointed out. While is not a very scientific way of looking at this, I've never seen it used and Drupal is in charge of making things 'safe' before it gets to the JS so a developer will probably reach out to some PHP before trying to figure out this jquery API.
There is some code related to text nodes parents in jQuery, that may introduce a memory leak. It's over text nodes and Drupal is hardly a single page app when used with the native ajax framework so probably not a huge deal but would be good to get some data on whether this doesn't cause issues with very large text nodes.
Comment #20
shubh_ commentedsame here as well raised a MR for this issue but the problem is that it is failing pipeline and when fixing one test case will result in failing another test case and viceversa..
Comment #21
nod_can someone please try this here?
Comment #23
kostyashupenkoI have used new MR 6957, because 1251 have huge rebase with conflicts (i decided to not waste the time - that's the only reason).
Basically in my commit i was trying to resolve #17 and provide more safe variant of replacement
Comment #24
shubh_ commentedComment #25
thebumik commentedLooks good to me, installed and tested it locally (no issues so far).
Please have @nod_ double-check as well.
Comment #28
nod_Opened a MR to test #17 and #21, we need to make sure we don't need to care about scripts in there.
Comment #29
nod_ok so it's just the specific test that fails, not other functionality in core
Comment #30
nod_So yeah code from !1251 was enough given the way we call this, we don't need the extra options since we always call the function with the same parameters
Security considerations I talked about in #5 don't apply here since we always call the function with the document parameter, and I haven't seen the
htmlPrefilter"hook" jquery provide used in core/contrib. This is not where security-related things should be happening.As #14 mentioned we could use a change record to explain that htmlPrefilter is not available anymore for ajax content.
Comment #32
shubh_ commentedAcknowledged feedbacks, not sure why pipeline failing checks, as said in #29
Comment #33
smustgrave commentedReran failing javascript test and fails every time
Also was tagged for CR so moving to NW for those.
Comment #34
nod_It's about the asset size change since the code changed
Comment #35
shubh_ commented@nod_ All test passed...pls review
Comment #36
smustgrave commentedStill appears to need a CR per #14 thanks.
Comment #38
mstrelan commentedNot sure why you keep adding empty comments Pravesh_Poonia.
Also not sure what the CR is for. None of the other jquery removal issues have CRs, and we're not adding or changing any APIs.
Comment #39
shubh_ commentedleaning on @mstrelan as per #38
Comment #40
smustgrave commentedBelieve the CR was a leftover tag actually so will remove it.
Seems all threads and feedback have been addressed and gone through a few rounds of review. Going to mark it.
Comment #41
nod_Feedback from #30 was not addressed, comments in MR
Comment #42
shubh_ commentedComment #43
smustgrave commentedBelieve feedback has been addressed here.
Comment #48
nod_Committed and pushed b4a0261008 to 11.x and cd700017d1 to 11.0.x and 2d482e076f to 10.4.x. Thanks!
Comment #50
b_sharpe commentedJust leaving a note here in case someone else ran into my problem after updating to 10.4+, this breaks functionality in which an ajax response might not be a valid child of a div whereas with jquery it was working. For example if you were returning a table row into an already existing table a
<tr>is not a valid child of a div so it will be stripped from the response.I don't necessarily think this warrants a bug ticket as it's probably the right way to go in the future, but it caused me some headache so felt I should leave a note here.