eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

Rule proposal: prefer `getHTML`/`setHTML` to `.innerHTML`

Open fregante opened this issue 9 months ago • 2 comments

Description

See context for the Sanitizer API in https://fullystacked.net/innerhtml-alternatives/

Fail

main.innerHTML = '<h2>Mambo Nº2</h2>'
console.log(main.innerHTML);

Pass

main.setHTML('<h2>Mambo Nº2</h2>')
console.log(main.getHTML());

Related

  • https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1405

fregante avatar May 09 '24 06:05 fregante

According to MDN all major browsers removed support for <Element>.setHTML() (though not <Element>.getHTML()) again, according to Can I use it's available in about 5% of browsers. It's still in the spec, however that doesn't help if all users should be able to use your website.

Why would you prefer something which breaks the site for most users, especially on recent browser releases, with no apparent benefits by itself (if you don't use sanitizers), over something which has been working since many years?

DEVTomatoCake avatar May 11 '24 04:05 DEVTomatoCake

The linked article has more info on why. The rule might take years to be added and even then it could be left disabled by default until Chrome implements it again. Browsers are quick to update nowadays

fregante avatar May 11 '24 05:05 fregante