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:

config.js
module.exports = {
removeAttributes: ['data-src']
}

Input:

src/templates/example.html
<img src="example.jpg" data-src alt="">

Output:

<img src="example.jpg" alt="">

By name and value

If you know the exact name and value, you may pass them to the array as an object:

config.js
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:

config.js
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

app.js
const {removeAttributes} = require('@maizzle/framework')
const options = [
'id',
{name: 'role', value: 'article'}
]
const html = await removeAttributes(`<div id="" style="" role="article"></div>`, options)
Copyright © 2024 Maizzle SRLBrand policy
Edit this page on GitHub