Page MenuHomePhabricator

Replace removeChild() with remove()
Closed, DeclinedPublic

Description

In the early stages of JavaScript it was not possible to remove an element directly. Was necessary:

element.parentNode.removeChild(element);

Later a direct function was added:

element.remove();

All browsers have added support for this function, except Internet Explorer. Now that Internet Explorer is no longer supported and the minimum JavaScript version is ES6, I believe MediaWiki should be updated. It makes the code smaller and more direct.

In a direct search of the MediaWiki source code:
https://codesearch.wmcloud.org/search/?q=removeChild

We see that several scripts use the older, longer method, as an example:
https://gerrit.wikimedia.org/g/mediawiki/core/+/dfd748033e5ab7272ef52f5b5425c28a35e0cbec/resources/lib/vue/vue.global.js#9635

Which is:

remove: (child) => {
  const parent = child.parentNode;
  if (parent) {
    parent.removeChild(child);
  }
}

But it can be simplified to:

remove: (child) => {
  child.remove();
}

Event Timeline

Hi and welcome! I don't think that it's worth the hassle to track this in an epic, huge task like this one (there is no bug as long as the "old" way does not get deprecated and removed from JS). However, I guess patches could be welcome. I'd prefer to close this ticket.

as an example:
https://gerrit.wikimedia.org/g/mediawiki/core/+/dfd748033e5ab7272ef52f5b5425c28a35e0cbec/resources/lib/vue/vue.global.js#9635

That example is not Wikimedia code which was written for MediaWiki. That is code that we imported from upstream, and it should be fixed in upstream first, not here.