Skip to content

element.cloneNode can cause problems with custom elements #4996

@WilcoFiers

Description

@WilcoFiers

Axe-core uses element.cloneNode in a couple places. Essentially for two reasons:

  1. to get the element's HTML without its content a shallow clone is used
  2. To get the attributes where element.attributes is set for silly reasons <input name=attribute>

Calling element.cloneNode can invoke the constructor of custom elements. I don't think for scenario 2 that's avoidable, but for scenario 1 it is. Instead of cloning, we can do the following:

elmOnlyHtml = elm.outerHTML.replace(elm.innerHTML, '')

UPDATE

The solve here is just to rewrite truncateElement to in a way that doesn't require cloneNode. I would also refactor the code. This getSource code should be its own function with its own tests. It'd be nice to make the params configurable. This is a nice function to have:

axe.utils.getSource(elm, { maxLength: 300, attrLimit: 20 })

Metadata

Metadata

Assignees

Labels

coreIssues in the core code (lib/core)criticalCritical priorityfixBug fixessupport

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions