HTML linter for Bootstrap projects
Перейти к файлу
Heinrich Fenkart 6dd6777cf2 E034: Fix firing when `.close` button is not preceded by anything
Fixes #181.
2014-11-12 21:29:14 +01:00
bin add more info to the initial www script output 2014-11-03 18:58:49 -08:00
dist/browser E034: Fix firing when `.close` button is not preceded by anything 2014-11-12 21:29:14 +01:00
src E034: Fix firing when `.close` button is not preceded by anything 2014-11-12 21:29:14 +01:00
test E034: Fix firing when `.close` button is not preceded by anything 2014-11-12 21:29:14 +01:00
test-infra Pin `npm-shrinkwrap` to `5.0.0` to support `npm@2.x` 2014-11-12 21:01:38 +01:00
.editorconfig add editorconfig via yeoman 2014-07-11 01:24:21 -07:00
.eslintrc Enable `block-scoped-var` in ESLint 2014-09-26 01:24:35 +02:00
.gitattributes Enforce Unix line endings. 2014-09-25 11:26:58 +03:00
.gitignore add jscoverage instrumented files to gitignore 2014-10-17 14:29:32 -07:00
.jscsrc add new JSCS settings 2014-10-17 15:04:58 -07:00
.jshintrc Fix #92 2014-10-30 16:39:54 -07:00
.travis.yml run coverage/coveralls script as part of Travis CI 2014-10-17 14:30:41 -07:00
CONTRIBUTING.md Fix #132 2014-10-16 18:43:04 -07:00
Dockerfile Dockerfile: Don't use -onbuild because we want npm install --production 2014-11-03 20:19:25 -08:00
Gruntfile.js Add app.js in the lint checks. 2014-10-28 12:12:56 +02:00
LICENSE Update LICENSE 2014-07-12 21:07:06 -07:00
README.md v0.8.0 2014-11-07 01:20:54 -08:00
app.js expose element locations in HTTP API; refs #29 2014-11-07 00:53:07 -08:00
package.json Pin `npm-shrinkwrap` to `5.0.0` to support `npm@2.x` 2014-11-12 21:01:38 +01:00
setup_droplet.sh name docker container 2014-11-04 22:57:34 -08:00

README.md

Bootlint

NPM version Build Status Coverage Status Dependency Status devDependency Status

An HTML linter for Bootstrap projects

What's Bootlint?

Bootlint is a tool that checks for several common HTML mistakes in webpages that are using Bootstrap in a fairly "vanilla" way. Vanilla Bootstrap's components/widgets require their parts of the DOM to conform to certain structures. Bootlint checks that instances of Bootstrap components have correctly-structured HTML. Optimal usage of Bootstrap also requires that your pages include certain <meta> tags, an HTML5 doctype declaration, etc.; Bootlint checks that these are present.

Caveats

Bootlint assumes that your webpage is already valid HTML5. If you need to check HTML5 validity, we recommend tools like vnu.jar, grunt-html, or grunt-html-validation.

Bootlint assumes that you are using Bootstrap's default class names in your webpage, as opposed to taking advantage of the "mixins" functionality of Less or Sass to map them to custom class names. If you are using mixins, Bootlint may report some false-positive warnings. However, there are some Bootlint checks that are applicable even if you are using mixins pervasively.

Getting Started

Via Grunt

To use Bootlint with Grunt, use the official Grunt plugin: grunt-bootlint

On the command line

Install the module with: npm install -g bootlint

Run it on some HTML files:

$ bootlint /path/to/some/webpage.html another_webpage.html [...]

This will output the lint warnings applicable to each file.

The CLI also accepts a --disable (or -d) option to disable certain lint checks. --disable takes a comma-separated list of lint problem IDs. Here's an example:

$ bootlint -d W002,E020 /path/to/some/webpage.html another_webpage.html [...]

In the browser

Use the following bookmarklet that's powered by BootstrapCDN:

javascript:(function(){var s=document.createElement("script");s.onload=function(){bootlint.showLintReportForCurrentDocument([]);};s.src="https://maxcdn.bootstrapcdn.com/bootlint/latest/bootlint.min.js";document.body.appendChild(s)})();

Then check the JavaScript console for lint warning messages.

You can also manually download the browser-ready version of Bootlint.

Lint problem explanations

For detailed explanations of each lint problem, look up the IDs (for example, E001 or W002) in our wiki.

API Documentation

Bootlint is a CommonJS module.

Bootlint represents the lint problems it reports using the LintError and LintWarning classes:

  • LintWarning
    • Represents a potential error. It may have false-positives.
    • Constructor: LintWarning(id, message, elements)
    • Properties:
      • id - Unique string ID for this type of lint problem. Of the form "W###" (e.g. "W123").
      • message - Human-readable string describing the problem
      • elements - jQuery or Cheerio collection of referenced DOM elements pointing to all problem locations in the document
        • (Only available under Node.js): When available from the underlying HTML parser (which is most of the time), the DOM elements in the collection will have a .startLocation property that is a Location (see below) indicating the location of the element in the document's HTML source
  • LintError
    • Represents an error. Under the assumptions explained in the above "Caveats" section, it should never have any false-positives.
    • Constructor: LintError(id, message, elements)
    • Properties:
      • id - Unique string ID for this type of lint problem. Of the form "E###" (e.g. "E123").
      • message - Human-readable string describing the problem
      • elements - jQuery or Cheerio collection of referenced DOM elements pointing to all problem locations in the document

Bootlint defines the following public utility class:

  • Location (Only available under Node.js)
    • Represents a location in the HTML source
    • Constructor: Location(line, column)
    • Properties:
      • line - 0-based line number
      • column - 0-based column number

A reporter is a function that accepts exactly 1 argument of type LintWarning or LintError. Its return value is ignored. It should somehow record the problem or display it to the user.

Browser

Bootlint exports a bootlint property on the global window object. In a browser environment, the following public APIs are available:

  • bootlint.lintCurrentDocument(reporter, disabledIds): Lints the HTML of the current document and calls the reporter() function repeatedly with each lint problem as an argument.
    • reporter is a reporter function (see above for a definition). It will be called repeatedly with each lint problem as an argument.
    • disabledIds is an array of string linter IDs to disable
    • Returns nothing (i.e. undefined)
  • bootlint.showLintReportForCurrentDocument(disabledIds): Lints the HTML of the current document and reports the linting results to the user.
    • If there are any lint warnings, one general notification message will be window.alert()-ed to the user. Each warning will be output individually using console.warn().
    • disabledIds is an array of string linter IDs to disable
    • Returns nothing (i.e. undefined)

Node.js

Example:

var bootlint = require('bootlint');

function reporter(lint) {
    console.log(lint.id, lint.message);
}

bootlint.lintHtml("<!DOCTYPE html><html>...", reporter, []); // calls reporter() repeatedly with each lint problem as an argument

In a Node.js environment, Bootlint exposes the following public API:

  • bootlint.lintHtml(html, reporter, disabledIds): Lints the given HTML for a webpage and returns the linting results.
    • html is the HTML to lint, as a string
    • reporter is a reporter function (see above for a definition). It will be called repeatedly with each lint problem as an argument.
    • disabledIds is an array of string linter IDs to disable
    • Returns nothing (i.e. undefined)

HTTP API

Bootlint can also be run as an HTTP server that exposes a very simple API. Use npm run start to run the server.

By default, it runs on port 7070. Set the $PORT environment variable to change which port it uses.

POST an HTML document to / and the document's lint problems will be returned as JSON.

The endpoint accepts an optional querystring argument named disable, whose value is a comma-separated list of linter IDs to disable.

Example:

Request:
  POST / HTTP/1.1
  Content-Type: text/html

  <!DOCTYPE html>
  ...

Response:
  HTTP/1.1 200 OK
  Content-Type: application/json

  [
    {
      "id": "W003",
      "message": "<head> is missing viewport <meta> tag that enables responsiveness"
    },
    {
      "id": "W005",
      "message": "Unable to locate jQuery, which is required for Bootstrap's JavaScript plugins to work"
    },
    ...
  ]

Contributing

The project's coding style is laid out in the JSHint, ESLint, and JSCS configurations. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Also, please don't edit files in the "dist" subdirectory as they are generated via Grunt. You'll find source code in the "src" subdirectory!

Release History

See the GitHub Releases page for detailed changelogs.

  • 2014-11-07 - v0.8.0: When in a Node.js environment, report the locations of the HTML source code of problematic elements.
  • 2014-11-01 - v0.7.0: Tweaks lint message texts. Adds 1 new lint check.
  • 2014-10-31 - v0.6.0: Fixes crash bug. Adds some new lint checks. Adds HTTP API.
  • 2014-10-16 - v0.5.0: Add several new features. Add official bookmarklet. Disable auto-lint-on-load in browser. Tweak some checks. Not backward compatible
  • 2014-10-07 - v0.4.0: Add checks for correct Glyphicon usage and correct modal DOM structure; fix .panel-footer false positive
  • 2014-09-26 - v0.3.0: Several bug fixes and enhancements. Not backward compatible
  • 2014-09-23 - v0.2.0: First formal release. Announcement: http://blog.getbootstrap.com/2014/09/23/bootlint/

License

Copyright (c) 2014 Christopher Rebert. Licensed under the MIT license.