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-text, which targets the jQuery text function.
Steps to reproduce
Proposed resolution
Remaining tasks
- In
core/.eslintrc.jquery.jsonChange"jquery/no-text": 0,to"jquery/no-text": 2,to enable eslint checks for uses of jQuery.text(). With this change, you'll be able to see uses of the undesirable jQuery feature by runningyarn lint:core-js-passingfrom thecoredirectory - Add the following lines to
core/scripts/dev/commit-code-check.shso the DrupalCI testing script can catch this jQuery usage on all files, not just those which have changed
# @todo Remove the next chunk of lines before committing. This script only lints # JavaScript files that have changed, so we add this to check all files for # jQuery-specific lint errors. cd "$TOP_LEVEL/core" node ./node_modules/eslint/bin/eslint.js --quiet --config=.eslintrc.passing.json . CORRECTJQS=$? if [ "$CORRECTJQS" -ne "0" ]; then # No need to write any output the node command will do this for us. printf "${red}FAILURE ${reset}: unsupported jQuery usage. See errors above." STATUS=1 FINAL_STATUS=1 fi cd $TOP_LEVEL # @todo end lines to removeAdd the block about 10 lines before the end of the file, just before
if [[ "$FINAL_STATUS" == "1" ]] && [[ "$DRUPALCI" == "1" ]]; then, then remove it once all the jQuery uses have been refactored. - If it's determined to be feasible, refactor those uses of jQuery
.text()to use Vanilla (native) JavaScript instead.
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #36 | 3239123-36-d10.patch | 66.09 KB | hooroomoo |
| #23 | jquery-text-d10-23.patch | 57.41 KB | bnjmnm |
| #21 | jquery-text-d10.patch | 66.08 KB | bnjmnm |
Issue fork drupal-3239123
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 #5
hooroomooAm aware that I used .includes() in core/modules/views_ui/js/views_ui.listing.js.
This is in the hope that #3239509: Add String.includes polyfill to support IE11 and Opera Mini would get committed to core by the time this was reviewed since it was already committed once but de-committed due to the most recent code freeze.
Comment #6
larowlanI've asked if #3239509: Add String.includes polyfill to support IE11 and Opera Mini is still 9.3 eligible, thanks for the reminder
Comment #7
hooroomooComment #8
bnjmnmEvery change has been reviewed. Even those that don't need changes have a (resolved) comment in Gitlab stating that so nobody has to re-review parts already confirmed to be fine.
Comment #10
hooroomooComment #11
bnjmnmSetting to "needs work" - multiple test failures, but apparently didn't automatically set the issue to NW
Comment #12
hooroomooComment #13
hooroomooChecked that nodelist.foreach polyfill dependecy is added where necessary.
Comment #14
nod_few things :)
Comment #15
hooroomooComment #16
nod_Couple of things, 4-5 bugs, one regression few extra changes that are not necessary for this issue.
Looking good!
Comment #17
hooroomooComment #18
nod_Just one little comment and tests fail to fix (need to check if element exists before getting the textContent) and we're good to go! Probably need a D10 patch once it's RTBC too
Great work, thanks you!
Comment #19
hooroomooComment #20
nod_And it is RTBC!
Everything looks good now. Thanks so much for going through that again :) We only need a D10 patch.
(marking rtbc because the issue is against 9.4.x and that code is RTBC)
Comment #21
bnjmnmDrupal 10 patch
Comment #22
nod_D10 patch is good except for the changes to the core.libraries.yml file
Comment #23
bnjmnmIn a patch with that many changes, I'd be shocked if I didn't miss something. Here's the patch without core.libraries
Comment #24
nod_Unfortunately the eslint rule isn't bullet-proof and we missed a few instances of .text() usage
I think it would be fine to have a follow-up though this will avoid having to be careful of the 60+ files that are changed by the current patch.
Leaving RTBC because the patch does fix the issue and eslint rules are passing.
Comment #25
nod_in any case MR + D10 patch are RTBC for me.
Comment #26
hooroomooThere are some .text() usages that were not caught by the linter that nod_ pointed out. Will address.
Comment #27
hooroomooComment #28
nod_Fixes are working as expected and don't see anymore jQuery .text usage!
Thank you :)
Comment #29
lauriiiI don't see any discussion about whether using
Node.textContentis appropriate because it isn't exactly same asjQuery.text().Based on jQuery documentation,
jQuery.text()is escaping any nodes. This means thatjQuery.text( '<p>This is a test.</p>' )would result in<p>This is a test.</p>, whereasNode.textContent = '<p>This is a test.</p>'would result inThis is a test..Comment #30
nod_I believe their documentation is outdated.
In the version of jquery we're using there it no createTextNode involved when using the text function: https://git.drupalcode.org/project/drupal/-/blob/9.4.x/core/assets/vendo...
The code in this patch is doing the same thing as jQuery :)
Comment #31
lauriiiPosted few comments on the MR
Comment #32
hooroomooComment #33
nod_Last problems have been addressed, all good for me
Comment #34
lauriiiThis needs a new Drupal 10 patch
Comment #35
hooroomood10 patch
Comment #36
hooroomooComment #40
lauriiiCommitted 8c62a32 and pushed to 10.0.x. Also committed the MR in 9.4.x. Thanks!