Problem/Motivation

We are deprecating underscore as an API core provides, and limiting its use to within modules with Backbone integrations. It is already marked internal, and the library has been renamed to internal.underscore (Backbone has also internal and will be eventually deprecated.)

Underscore API documentation:
https://underscorejs.org/

The only two non-deprecated uses of underscore in core are in the following libraries:

drupal.editor.admin:
  version: VERSION
  js:
    js/editor.admin.js: {}
  dependencies:
    - core/jquery
    - core/once
    - core/jquery.once.bc
    - core/drupal
    - core/internal.underscore
drupal.filter.filter_html.admin:
  version: VERSION
  js:
    filter.filter_html.admin.js: {}
  dependencies:
    - core/jquery
    - core/once
    - core/jquery.once.bc
    - core/internal.underscore

Underscore functions replacements

keys -> Object.keys
omit -> Object.keys + Array.prototype.forEach to build a new object
has -> Object.prototype.hasOwnPropertyOf
reduce -> Array.prototype.reduce or use Object.keys + Array.prototype.forEach
indexOf -> String.prototype.IndexOf
each -> Array.prototype.forEach
every -> Array.prototype.every
some -> Array.prototype.some
pluck -> Array.prototype.map + object destructuring
union -> use object destructuring to return a combined object
isEmpty -> compare Object.keys length
intersection -> Array.prototype.filter based on Array.prototype.includes on the second array

Needs replacement pattern: difference

Proposed resolution

Replace the use of underscore APIs in the above two libraries with vanilla JavaScript, and remove underscore from the respective library definitions.

Remaining tasks

TBD

User interface changes

N/A

API changes

The drupal.editor.admin and drupal.filter.filter_html.admin libraries no longer depend on underscore.

Data model changes

N/A

Release notes snippet

The drupal.editor.admin and drupal.filter.filter_html.admin libraries no longer depend on underscore.

CommentFileSizeAuthor
#32 3270395-31-94x.patch30.25 KBbnjmnm
#31 3270395-31-10x.patch28.32 KBbnjmnm

Issue fork drupal-3270395

Command icon 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

xjm created an issue. See original summary.

xjm’s picture

Issue summary: View changes
mradcliffe’s picture

Issue summary: View changes

I added some details about what functions need to be replaced in those files.

difference, intersection and isEmpty (for objects) are probably the most complex to code for.

mradcliffe’s picture

I'll create an initial issue fork for 9.4.x and work on it for a bit.

mradcliffe’s picture

Issue summary: View changes

Some of the underscore usage of _.every, _.pluck, _.some, _.omit are hard for me to understand exactly what's going on, but I think I was able to get it right. The underscore (or lodash) documentation isn't very clear or verbose for optional arguments.

We really need to add api documentation for some of the objects used in this file.

I'm going to stop working on this for now.

mradcliffe’s picture

Status: Active » Needs work

I forgot to update the status to Needs work.

xjm’s picture

Issue summary: View changes

murilohp made their first commit to this issue’s fork.

murilohp’s picture

Status: Needs work » Needs review

Regarding the difference method, during the development I used You-Dont-Need-Lodash-Underscore as a reference for it, so you'll see a combination of reduce and filter. I'm moving this to NR to see what you guys think about the solution.

xjm’s picture

Issue tags: +Drupal 9.4 target

Nice work!

nod_’s picture

Status: Needs review » Needs work

couple of things, IE11 compatibility issue, and adding a local helper for difference method

murilohp’s picture

Status: Needs work » Needs review

Thanks for the review @nod_!.

murilohp’s picture

Sorry for my delay, when you comment just on the MR, I don't get any notification on my dashboard.

xjm’s picture

Commenting for dashboard notification. ;)

murilohp’s picture

Thanks @xjm!

murilohp’s picture

Regarding the CR, recently I created Underscore library is deprecated, do you think a new CR is necessary here?

nod_’s picture

Status: Needs review » Needs work

We're missing some test coverage here, some of the replacements are just not functional.

Been at it for a couple of hours but I can't find how to trigger the paths that have problems. I looked at contrib and this whole filter_html api doesn't seems to be used at all outside ckeditor 4 and that is going away. Gives a strong "dead code" vibe here.

CKE5 bypasses all of that and does the work on the PHP side so it's not even going to be used afterwards.

murilohp’s picture

We're missing some test coverage here, some of the replacements are just not functional.

So should we write some new tests for both files here? And which replacements are just not functional?

Been at it for a couple of hours but I can't find how to trigger the paths that have problems.

Just for you know, I focused only on filter.filter_html.admin.js, and looking at the core/modules/filter/src/Plugin/Filter/FilterHtml.php file, you can see the filter/drupal.filter.filter_html.admin library being attached, so my idea was to move into /admin/config/content/formats/manage/full_html and test there using CKE4.

I looked at contrib and this whole filter_html api doesn't seems to be used at all outside ckeditor 4 and that is going away. Gives a strong "dead code" vibe here.

That's really sad, I spent a lot of time trying to replace the underscore usage there, but that's fine, it's good to know that CKE5 will be able handle this. Testing here with CKE5, I can confirm, the filter_html is not being used, it works with CKE4 only.

xjm’s picture

@murilohp It's still important to provide clean replacements even for CKEditor 4 integration code, because we need to continue to support CKEditor 4 until the end of 2023 in Drupal 9 and in contrib for the Drupal 10 upgrade path. So it's still totally worth it; we need it for nearly two years. :)

It does mean we could potentially open a followup to deprecate it and remove it from D10.

nod_’s picture

Fixed most of the issues with the replacement. The difference function was not working as intended (it removed the keys of the object and ended up breaking the generated html filter settings).

murilohp’s picture

@nod_ do you have any suggestions to properly replace the _.difference? It's really weird to know the function is actually breaking, at least on the /admin/config/content/formats/manage/full_html page, I printed all the objects before and after changing the difference function, and the results were the same. Maybe I was just missing something.

We'll need tests to cover this right? You just removed the underscore dependency from core/modules/filter/filter.filter_html.admin.es6.js, the code is still calling _.difference and somehow all the tests passed.

xjm’s picture

Status: Needs work » Needs review

I guess this is NR now?

Can someone check for resolved threads and either mark them resolved or put in a thumbs-up emoji so that I can do so with my committer powers? :)

nod_’s picture

Status: Needs review » Needs work

Still one function left to replace

nod_’s picture

Status: Needs work » Needs review

ok, put the code back as it was mostly. The error I ran into is present in the underscore code as well, so it's actually working as expected :)

To reproduce the error with backbone and the replacement:

  1. Create a new text format from scratch
  2. Select CKEditor as the text editor
  3. Check "limit allowed HTML tags…" checkbox
  4. Manually add a new tag in the "Allowed HTML tags" textarea, for example <kbd>
  5. In the CKEditor toolbar configuration, drag the "Underline" button in the active toolbar
  6. The "Allowed HTML tags" field is now: <0 href hreflang> <1> <2> <3> <4 cite> <5> <6 type> <7 start type> <8> <9> <10> <11> <12 id> <13 id> <14 id> <15 id> <16 id> <17 src alt data-entity-type data-entity-uuid> <18> <strong> <em> <a href> <ul> <li> <ol> <blockquote> <img src alt data-entity-type data-entity-uuid> <u>

Can someone open a follow up with those steps to fix the issue?

The replacement is actually doing the exact same thing so +1 from me. I'd say it's RTBC now.

murilohp’s picture

I had the same issue locally @_nod, I thought I was doing something wrong. Anyway the followup is created: #3275054: It's not possible to drag an item from CKE and change the "Allowed HTML tags" on a text format

I've also updated the code to fix the CS errors

murilohp’s picture

Re: #20
Thanks for the explanation @xjm! I've created a followup issue to remove the files on D10, #3275080: Remove unused javascript from filter module.

wim leers’s picture

Status: Needs review » Needs work

I ended up simply +1'ing all (two) of @bnjmnm's remarks 🙈 😅

P.S.: I think all of this JS will disappear once CKEditor 4 is removed from Drupal 10. CKEditor 5 does not rely on this JS to update the filter_html configuration. It was the only way to make this work at the time of CKEditor 4, because it was technically impossible to have metadata about which HTML elements (tag, attributes, attribute values) a specific CKEditor 4 feature supported. Which meant we needed a hidden CKEditor 4 instance in the admin UI to get that metadata live, instead of declaratively (in a YAML file/annotation). With CKEditor 5, that did become possible, and so it's been implemented that way. It's much more thoroughly tested.

Therefore I think the follow-up that @nod_ is asking for in #25 may not be necessary. This JS will simply no longer be used. And at the time it was written, it was not possible to execute JS in tests for Drupal core. Writing such tests for it today would not only be a nightmare, but also pointless (because it is no longer used).

nod_’s picture

Status: Needs work » Needs review

added the dep.

Technically the whole thing is a public api. I haven't found a use in contrib so probably safe to remove it, but it should be deprecated at least no?

wim leers’s picture

Status: Needs review » Reviewed & tested by the community

haven't found a use in contrib so probably safe to remove it, but it should be deprecated at least no?

Yep — but definitely out of scope here!

bnjmnm’s picture

StatusFileSize
new28.32 KB
bnjmnm’s picture

StatusFileSize
new30.25 KB
bnjmnm’s picture

Issue summary: View changes

  • bnjmnm committed dca093d on 9.4.x
    Issue #3270395 by murilohp, nod_, mradcliffe, xjm, Wim Leers: Remove use...

  • bnjmnm committed 3831a78 on 10.0.x
    Issue #3270395 by murilohp, nod_, mradcliffe, xjm, Wim Leers: Remove use...
bnjmnm’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs change record

Committed to 9.4.x and 10.0.x. Change record published. I haven't seen @murilohp in an issue I've worked on before - so thank you in particular for your work on this!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

xjm’s picture