Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/api/attributes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,22 @@ describe('$(...)', () => {

expect($text('body').html()).toBe(mixedText);
});

it('(KEY) : should be case-insensitive in HTML mode', () => {
const $html = load('<div class="test"></div>');
$html('div').removeAttr('CLASS');
expect($html('div').attr('class')).toBeUndefined();
});

it('(KEY) : should remain case-sensitive in XML mode', () => {
const $xml = load('<Foo Bar="x"></Foo>', { xmlMode: true });
// A differently-cased name must not remove the attribute in XML mode.
$xml('Foo').removeAttr('BAR');
expect($xml('Foo').attr('Bar')).toBe('x');
// The exact name still removes it.
$xml('Foo').removeAttr('Bar');
expect($xml('Foo').attr('Bar')).toBeUndefined();
});
});

describe('.hasClass', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/api/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,8 +880,10 @@ export function removeAttr<T extends AnyNode>(
const attrNames = splitNames(name);

for (const attrName of attrNames) {
// HTML attribute names are case-insensitive; XML mode preserves case.
const lookup = this.options.xmlMode ? attrName : attrName.toLowerCase();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve mixed-case SVG attr removals

In default HTML mode Cheerio parses with parse5, which preserves/adjusts SVG foreign-content attribute names such as viewBox and preserveAspectRatio as mixed-case keys on elem.attribs; lowercasing every requested name when xmlMode is false makes $('svg').removeAttr('viewBox') look for viewbox instead and leave the parsed viewBox attribute in place. This affects common inline SVG markup even though the document is not in XML mode.

Useful? React with 👍 / 👎.

domEach(this, (elem) => {
if (isTag(elem)) removeAttribute(elem, attrName);
if (isTag(elem)) removeAttribute(elem, lookup);
});
}

Expand Down