Skip to content

fix: removeAttr case-insensitive for HTML documents - #5313

Closed
rafaumeu wants to merge 2 commits into
cheeriojs:mainfrom
rafaumeu:fix/removeattr-case-insensitive-5257
Closed

fix: removeAttr case-insensitive for HTML documents#5313
rafaumeu wants to merge 2 commits into
cheeriojs:mainfrom
rafaumeu:fix/removeattr-case-insensitive-5257

Conversation

@rafaumeu

Copy link
Copy Markdown

Problem (#5257)

.removeAttr() did not lowercase the attribute name before removing it from elem.attribs. Since htmlparser2 stores all attribute names in lowercase (per HTML spec), calling .removeAttr("CLASS") would fail to remove a class attribute.

const $ = cheerio.load('<div class="test"></div>');
$('div').removeAttr('CLASS');
console.log($('div').attr('class')); // Should be undefined, returns "test"

Fix

Lowercase the attribute name when not in xmlMode, matching the behavior of attr() and the HTML spec. XML mode preserves case sensitivity.

Testing

  • All 129 existing attribute tests pass
  • Added 2 new tests: case-insensitive removal in HTML mode, case-sensitive removal in XML mode
  • Biome lint passes

HTML attribute names are case-insensitive and htmlparser2 stores them in
lowercase. removeAttr() now lowercases the attribute name when not in
xmlMode, matching the behavior of attr() and the HTML spec.

XML mode preserves case sensitivity as expected.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 03e1f0eaab

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/api/attributes.ts
if (this.options.xmlMode) {
removeAttribute(elem, attrName);
} else {
removeAttribute(elem, 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 Avoid lowercasing SVG attribute names

In an HTML document that contains SVG, parse5 stores adjusted foreign-content attributes such as viewBox under their canonical camel-case names, so $('svg').attr('viewBox') can read them. This branch lowercases every name whenever xmlMode is false, so $('svg').removeAttr('viewBox') tries to delete viewbox and leaves the original viewBox attribute in place. Please only apply case-folding to HTML elements, or otherwise remove the actual matching key case-insensitively for HTML attributes.

Useful? React with 👍 / 👎.

…eriojs#5073)

When an HTML boolean attribute has a value (e.g. hidden="until-found"),
.attr() now returns the actual value instead of normalizing to the
attribute name. Pure boolean attributes (empty value) still return the
attribute name.

Fixes cheeriojs#5073

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b005bf4522

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/api/attributes.ts
Comment on lines +70 to +73
if (!xmlMode && rboolean.test(name) && value === '') {
return name;
}
return value;

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 boolean-attribute presence semantics

When an HTML boolean attribute is present with a non-empty value such as disabled="false" or readonly="custom", this now returns the literal value instead of the attribute name. That regresses the existing Cheerio/jQuery-style contract documented above this helper and used by the existing boolean-attribute tests: these attributes are true by presence, so callers using .attr('disabled') to detect presence now see misleading values even though .prop('disabled') remains true. Please limit the value-preserving special case to hidden="until-found" (or remove hidden from the boolean set) rather than changing all boolean attributes.

Useful? React with 👍 / 👎.

@fb55

fb55 commented Aug 7, 2026

Copy link
Copy Markdown
Member

Thanks! Duplicate of #5290.

@fb55 fb55 closed this Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants