Remove undefined CSS class attributes from your HTML.
This Gem will, given a set of HTML files and a set of CSS files, remove any mentions in the class attributes of HTML objects classes that are not defined in the CSS files.
Given the following files
index.html
<html>
<body>
<h1 class="happy sad">Heading</h1>
<p class="red blue">Hello</p>
</body>
</html>
app.css
.red {
color: red;
}
.happy {
color: black;
}
Running CSSDeadClass on those two files will change index.html
to the following, remove calls to .sad
and .blue
, as they do not exist:
<html>
<body>
<h1 class="happy">Heading</h1>
<p class="red">Hello</p>
</body>
</html>
Install using gem css_dead_class
.
CSSDeadClass takes three options:
An array of file paths to CSS files to scan.
An array of file paths to HTML files to scan.
An array of CSS classes to keep, regardless of presence in the HTML files. Can either be of the form .classname
or classname
.
Sample usage in a ruby program:
require 'css_dead_classes'
css_deadfiles = CSSDeadClass.new({
html_files: Dir.glob("**/*.html"),
css_files: Dir.glob("**/*.css"),
classes_to_keep: [
".no-js",
"blue"
]
})
css_deadfiles.parse