• Stars
    star
    351
  • Rank 120,906 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Audit the CSS on a page to see what elements are using styles from the style guide and which styles are overriding them

Style Guide Auditing Tool

Audit the CSS on a page to see what elements are using styles from the style guide and which styles are overriding them.

IMPORTANT: Dropbox changed how their Public folder worked and removed all previous links to files hosted in it (which is where the Bookmarklet script is kept). All previous versions (1.0.1 and below) of the bookmarklet will no longer work until you've updated to the latest code.

How it works

The code parses all the style sheets on the page and keeps track of every rule and how it affects all elements. When you run the audit, it takes this information and looks at which rules from other style sheets are overriding the rules from the style guide. Any elements that have an override are highlighted.

If you click on any highlighted element, the tool will show you which rules from which style sheets are providing the overrides.

The tool will also outline each element that is using a rule from the style guide. This helps to highlight any elements that should be using the style guide but aren't. Rules that purely use element selectors (no class selector in the rule) will be ignored since those are always applied so long as the style guide is being used on the page.

How to use

Out of the box, the tool provides two global functions: parseStyleSheets() and auditStyleGuide(). Running parseStyleSheets() will parse the style sheets on the page. After the function completes, you can run auditStyleGuide() to run the audit.

auditStyleGuide() takes two parameters, the first is a string that is the name of the style guide to audit. It uses fuzzy matching to find the name for cases when your compiled style sheet uses hashes, or if you use a minified version on production and a non-minified version on localhost.

// matches any styleSheet that contains the text 'pattern-lib':
// localhost/css/pattern-lib.css
// http://myDomain/styles/pattern-lib.min.css
// http://myDomain/styles/pattern-lib-17D8401NDL.css,
auditStyleGuide('pattern-lib');

The second parameter is an array of style sheets to ignore from the audit. This is useful if you are using a global style sheet like Bootstrap and don't care to know if it overrides any of your style guide's rules. Again, it uses fuzzy matching for the name.

// ignores any styleSheet that contains the text 'bootstrap' or 'normalize'
auditStyleGuide('pattern-lib', ['bootstrap','normalize']);

Customizing the audit

The tool also allows you to customize the audit. You can pass an array of custom rules to auditStyleGuide() as the third parameter that will highlight any element that fails the rule.

For example, let's say you wanted to validate the accessibility of your page by ensuring that all anchor tags navigate and are not just there for JavaScript events. You could write a rule that checks that all anchor tags have a valid href property and run the rule with the audit. Any anchor tags that do not have a valid href will be highlighted.

var customRules = [{
  type: 'my-custom-rule',
  selector: 'a[href^="javascript"], a[href="#"], a:not([href])',
  description: 'Anchor tags that do not navigate should be buttons.'
}];

auditStyleGuide('pattern-lib', ['bootstrap'], customRules);

A rule consists of three properties:

  • type: a slug that identifies the rule. This slug will be added to the data-style-audit attribute of any element that fails the rule. This is useful if you want to give those elements your own styling.
  • selector: how to identify any elements that do not pass the rule (using document.querySelectorAll)
  • description: the text to display in the audit results for any element that fails the rule.

Using the bookmarklet

You can also create a bookmarklet that will run the entire audit for you on any page. Just edit src/run.js to include any custom rules and the style sheet to audit, and then run gulp scripts. This will create bookmarklet.js that you can then copy into your favorites bar.

This is an excellent tool to give to designers who can then help run the audits of the site on their own.

IMPORTANT - If you are going to create your own custom bookmarklet, please create a branch on your fork where you will push the changes. I will not accept any pull requests that change src/run.js or bookmarklet.js, so if you need to submit a fix, you must do so on a clean branch without custom bookmarklet changes. More info.

More Repositories

1

kontra

A lightweight JavaScript gaming micro-library, optimized for js13kGames.
JavaScript
948
star
2

livingcss

Parse comments in your CSS to generate a living style guide using Markdown, Handlebars, Polymer, and Prism syntax highlighter.
Handlebars
240
star
3

galaxian-canvas-game

A Galaxian Style HTML5 Canvas Game. Hosts all the files for the five part tutorial series on http://blog.sklambert.com/galaxian-html5-game/.
JavaScript
114
star
4

html-tagged-template

Proposal to improve the DOM creation API so developers have a cleaner, simpler, and secure interface to DOM creation and manipulation.
JavaScript
81
star
5

endless-runner-html5-game

An Endless Runner Style HTML5 Canvas Game. Hosts all the files for the two part tutorial series on http://blog.sklambert.com/endless-runner-html5-game/.
JavaScript
76
star
6

kontra-asset-loader

HTML5 game asset loader. Part of the Kontra.js game library.
JavaScript
17
star
7

js13k-2018

https://straker.github.io/js13k-2018/
JavaScript
10
star
8

gulp-livingcss

Gulp wrapper for the LivingCSS Style Guide Generator
JavaScript
9
star
9

digsim

Digital Circuit Simulator
JavaScript
7
star
10

js13k-packer

Optimally package js13kGames files for upload.
JavaScript
5
star
11

browser-driver-manager

A cli for managing Chrome browsers and drivers. Especially useful to keep Chrome and ChromeDriver versions in-sync for continuous integration
JavaScript
5
star
12

10k-arcade-cabinet

10k Arkade is an arcade cabinet that lets you play a few games for under 10k. Made for the 2016 10k Apart competition.
JavaScript
5
star
13

responsiveCanvas

An experiment in responsive and adaptive canvas design
JavaScript
4
star
14

kontra-asteroids

A simple Asteroids game made in Kontra.js.
JavaScript
3
star
15

rollup-plugin-kontra

Rollup plugin for Kontra.js to remove unused code from Classes
JavaScript
3
star
16

tic-tac-toe

Tic-Tac-Toe vs an AI using AB Negamax Game Tree algorithm. https://sklambert.com/tictactoe/
JavaScript
3
star
17

chronos

HTML5 turn based combat game.
CSS
3
star
18

audio-dash

Audio Dash - Ride the Waves of Your Music
JavaScript
3
star
19

terraform

Terraform is a musical puzzle game where you try into terraform a desolate planet to something habitable. Made for the 2014 js13k game jam.
JavaScript
3
star
20

spaceship-repair-factory

An factory automation game about repairing spaceships in the Asteroid Belt.
JavaScript
2
star
21

qLite

qLite is a tiny, API-compatible subset of q.js
JavaScript
1
star
22

dotfiles

My personal dotfiles setup to configure any new Mac, based on https://github.com/nicksp/dotfiles
Shell
1
star
23

tiled-automap-example

A working Tiled AutoMap example
1
star
24

a11y-enhancer

A JavaScript library for making widgets and components accessible
JavaScript
1
star
25

reverse-td

Reverse Tower Defense where you play as the creep trying to get past the towers. Made for the 2015 js13k game jam.
JavaScript
1
star
26

somber

JavaScript
1
star
27

js13k-2023

JavaScript
1
star
28

straker

1
star
29

js13k-2021

JavaScript
1
star
30

js13k-2022

JavaScript
1
star