Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove empty non-boolean attributes #624

Conversation

dylanarmstrong
Copy link
Contributor

@dylanarmstrong dylanarmstrong commented Jun 5, 2023

Summary

This causes sanitize-html to remove empty attributes that are known to be non-boolean. It provides an exhaustive list of all known attributes taken from: https://html.spec.whatwg.org/multipage/indices.html#attributes-3

  • User controlled via options.nonBooleanAttributes
  • Enabled by default, and can be disabled by passing in nonBooleanAttributes: []
  • Can handle all empty attributes via nonBooleanAttributes: ['*']

Closes #123

What are the specific steps to test this change?

Test with empty attributes such as:

<input checked type="checkbox" />

Will not remove anything.

<input checked form type="checkbox" />

Will transform into the first example.

What kind of change does this PR introduce?

  • Bug fix
  • New feature
  • Refactor
  • Documentation
  • Build-related changes
  • Other

Make sure the PR fulfills these requirements:

  • It includes a) the existing issue ID being resolved, b) a convincing reason for adding this feature, or c) a clear description of the bug it resolves
  • The changelog is updated
  • Related documentation has been updated
  • Related tests have been updated

If adding a new feature without an already open issue, it's best to open a feature request issue first and wait for approval before working on it.

Copy link
Member

@boutell boutell left a comment

Choose a reason for hiding this comment

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

Great stuff. Minor documentation requests 🙏

README.md Outdated
@@ -167,6 +212,22 @@ allowedTags: false,
allowedAttributes: false
```

#### "What if I want to allow empty attributes?"
Copy link
Member

Choose a reason for hiding this comment

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

This implies they are not allowed by default. They are allowed, just only where it makes sense.

This could be a better heading:

"What if I want to allow empty attributes, even for cases like href that normally don't make sense?"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated


```js
nonBooleanAttributes: ['*']
```
Copy link
Member

Choose a reason for hiding this comment

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

Add a note that this will break common valid cases like checked and selected so for most ordinary HTML use cases it is best to avoid making this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

@dylanarmstrong
Copy link
Contributor Author

Thanks for feedback, updated documentation.

Copy link
Member

@boutell boutell left a comment

Choose a reason for hiding this comment

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

Good stuff, thank you!

@boutell boutell merged commit c8e02df into apostrophecms:main Jun 9, 2023
@dylanarmstrong dylanarmstrong deleted the feat/remove-empty-non-boolean-attributes branch June 9, 2023 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Empty attributes
2 participants