Skip to content

Cleaner may duplicate enforced attrs with preserve-case input #2476

Closed

Description

@jhy

As a follow-up to #2475, I noticed another small Cleaner issue when working with a parsed Document that preserves attribute case.

If an element already has an attribute that matches an enforced attribute, but with different key case, the cleaned output can contain both versions of the attribute instead of a single enforced one.

For example:

Document dirty = Jsoup.parse("<a REL='external'>One</a>", "",
    Parser.htmlParser().settings(ParseSettings.preserveCase));
Cleaner cleaner = new Cleaner(Safelist.none()
    .addTags("a")
    .addEnforcedAttribute("a", "rel", "external"));

cleaner.clean(dirty).body().html();
// was:    <a REL="external" rel="external">One</a>
// should: <a rel="external">One</a>

The same thing can happen with the standard Safelist.basic() handling of rel="nofollow" on external links.

The intended behavior is that when an enforced attribute applies, the cleaned output should contain that attribute once, using the enforced key and value. A case-variant source attribute should be replaced, not duplicated.

Activity

  1. added this to the 1.22.2 milestone on Mar 8, 2026
  2. self-assigned this
    on Mar 8, 2026
  3. added
    bug
    on Mar 8, 2026
  4. added
    fixed
    on Mar 8, 2026
  5. added a commit that references this issue on Mar 8, 2026
    7c32b7e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugfixed

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions