How to find unused classes in my HTML?

There are many tools for finding unused CSS rules in style sheets, such as Chrome Audits and the Dust-Me Selectors add-on for Firefox (unfortunately not compatible with Firefox Quantum).

But what about the other way?

How to find classes that are in my HTML that do not exist in my stylesheets?

+27
source share
5 answers

This might be useful: https://code.google.com/p/find-unused-classes/ . According to the description:

It shows classes that exist in css selectors and do not exist in the html page and in verse style.

, , , JavaScript.

+19

, html

var classesEvery = [];
var elementsEvery = document.querySelectorAll('*');
for (var i = 0; i < elementsEvery.length; i++) {
  var classes = elementsEvery[i].className.toString().split(/\s+/);
  for (var j = 0; j < classes.length; j++) {
    var thisClass = classes[j];
    if (thisClass && classesEvery.indexOf(thisClass) === -1)
      classesEvery.push(thisClass);
  }
}

html , Javascript. , CSS, list-selectors. , , Javascript angularJS. HTML js.

var classesEvery = [];
var elementsEvery = document.querySelectorAll('*');
for (var i = 0; i < elementsEvery.length; i++) {
  var classes = elementsEvery[i].className.toString().split(/\s+/);
  for (var j = 0; j < classes.length; j++) {
    var thisClass = classes[j];
    if (thisClass && classesEvery.indexOf(thisClass) === -1)
      classesEvery.push(thisClass);
  }
}
console.log(classesEvery);
.hidden {
  display: none;
}
<!-- Some random HTML code. -->
<div ng-controller="HomeController" class="container hidden">

  <span>Simple Notifications:</span>
  <div class="rows">
    <div class="col-md-2"><button class="btn btn-primary" href ng-click="simple()">Simple notification</button></div>
    <div class="col-md-2"><button class="btn btn-warning" href ng-click="warning()">Warning notification</button></div>
    <div class="col-md-2"><button class="btn btn-success" href ng-click="success()">Success notification</button></div>
    <div class="col-md-2"><button class="btn btn-danger" href ng-click="error()">Error notification</button></div>
    <div class="col-md-2"><button class="btn btn-grimace" href ng-click="wait()">Wait notification</button></div>
    <div class="col-md-2"><button class="btn btn-primary" href ng-click="pop()">Link to other page</button></div>
  </div>
  <hr/>
  <span>Addding Option from Script:</span>
  <div class="rows">
    <div class="col-md-6">
      <Span>With CLose button</span>
      <button class="btn btn-primary" href ng-click="close()">Close Button </button>
    </div>
    <div class="col-md-6">
      <Span>Fade after 1 Sec</span>
      <button class="btn btn-primary" href ng-click="timeout()">Html notification</button>
    </div>
  </div>
  <br/>
  <hr/>
  <span>Custom Notification and Body output type:</span>
  <div class="rows">
    <div class="col-md-3"><button class="btn btn-primary" href ng-click="Custom_temp()">Custom template</button></div>
    <div class="col-md-3"><button class="btn btn-primary" href ng-click="trusted_html()">Trusted HTML</button></div>
    <div class="col-md-3"><button class="btn btn-primary" href ng-click="default_temp()">Default Template</button></div>
    <div class="col-md-3"><button class="btn btn-primary" href ng-click="file_custom_temp()">Including file from Folder</button></div>
  </div>
  <script type="text/ng-template" id="myTemplate.html">
    <div class="Custom_temp_html">
      <p>Notice, custom temlate is not in the list</p>
    </div>
  </script>
</div>
+7

HTML https://github.com/philipwalton/html-inspector :

  • : CSS , HTML. "unused-classes" CSS HTML , .

    , HTML JavaScript, . js-, language- supports- . .

FAQ .

script:

<script src="http://cdnjs.cloudflare.com/ajax/libs/html-inspector/0.8.2/html-inspector.js"></script>

:

HTMLInspector.inspect(["unused-classes"]);

, ( 2017). , ( 2019 ).

, ( 2014).

+6

HTMLElement, :

function unused(e) {
  const s0 = JSON.stringify(window.getComputedStyle(e));
  const c = Array.from(e.classList.values());
  if (c.length !== 0) {
    c.forEach(cc => {
      e.classList.remove(cc);
      const s = JSON.stringify(window.getComputedStyle(e));
      if (s0 === s) {
        console.log('class ${cc} is unused');
      }
      e.classList.add(cc);
    });
  }
  const id = e.id;
  if (id) {
    e.removeAttribute("id");
    const s = JSON.stringify(window.getComputedStyle(e));
    if (s0 === s) {
      console.log('id ${id} is unused');
    }
    e.id = id;
  }
}

, window.getComputedStyle(), , .

(If you want an HTML version with extraneous classes and identifiers removed, change the code so that classes are re-added only if they affect the styling, call the function recursively on all child nodes, and then use DOM to serialize.) XMLSerializer. serializeToString

0
source

All Articles