Remove attributes
Maizzle can automatically remove attributes from your HTML.
By default, it removes empty style
and class
attributes that are sometimes left over after the CSS inlining process.
Usage
You may configure which attributes to remove through the removeAttributes
array.
Empty values
To remove attributes with no values, specify the attribute name as a string:
module.exports = {
removeAttributes: ['data-src']
}
Input:
<img src="example.jpg" data-src alt="">
Output:
<img src="example.jpg" alt="">
style
and
class
attributes, no need to add them yourself.
By name and value
If you know the exact name and value of the attribute, you may pass them as an object:
module.exports = {
removeAttributes: [
{name: 'id', value: 'test'}
]
}
Input:
<div style="color: #000" id="test">Test</div>
Output:
<div style="color: #000">Test</div>
With a RegExp
You may also use a regular expression for the value
.
All attributes with a value matching the regex will be removed:
module.exports = {
removeAttributes: [
{name: 'data-id', value: /\d/}
]
}
Input:
<div data-id="test"></div>
<div data-id="99"></div>
Output:
<div data-id="test"></div>
<div></div>
API
const {removeAttributes} = require('@maizzle/framework')
const options = [
'id',
{name: 'role', value: 'article'}
]
const html = await removeAttributes(`<div id="" style="" role="article"></div>`, options)