bootlint/README.md

193 строки
11 KiB
Markdown
Исходник Обычный вид История

2014-07-21 07:38:33 +04:00
# Bootlint
2014-07-11 13:24:56 +04:00
[![NPM version](https://badge.fury.io/js/bootlint.svg)](http://badge.fury.io/js/bootlint)
[![Build Status](https://img.shields.io/travis/twbs/bootlint/master.svg)](https://travis-ci.org/twbs/bootlint)
2014-10-18 01:34:17 +04:00
[![Coverage Status](https://img.shields.io/coveralls/twbs/bootlint.svg?branch=master)](https://coveralls.io/r/twbs/bootlint)
![Development Status :: 5 - Production/Stable](https://img.shields.io/badge/maturity-stable-green.svg "Development Status :: 5 - Production/Stable")
2015-05-18 09:08:50 +03:00
[![MIT License](https://img.shields.io/github/license/twbs/bootlint.svg)](https://github.com/twbs/bootlint/blob/master/LICENSE)
2014-09-24 02:24:19 +04:00
[![Dependency Status](https://david-dm.org/twbs/bootlint.svg)](https://david-dm.org/twbs/bootlint)
[![devDependency Status](https://david-dm.org/twbs/bootlint/dev-status.svg)](https://david-dm.org/twbs/bootlint#info=devDependencies)
2014-07-02 18:55:13 +04:00
An HTML [linter](http://en.wikipedia.org/wiki/Lint_(software)) for [Bootstrap](http://getbootstrap.com) projects
## What's Bootlint?
Bootlint is a tool that checks for several common HTML mistakes in webpages that are using [Bootstrap](http://getbootstrap.com) 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`](https://validator.github.io/validator/), [grunt-html](https://www.npmjs.org/package/grunt-html), or [grunt-html-validation](https://www.npmjs.org/package/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.
2014-07-11 09:42:45 +04:00
## Getting Started
### Via Grunt
2015-03-03 16:47:18 +03:00
To use Bootlint with [Grunt](http://gruntjs.com/), use the official Grunt plugin: [grunt-bootlint](https://github.com/twbs/grunt-bootlint)
2014-07-21 07:38:33 +04:00
### On the command line
Install the module with: `npm install -g bootlint`
2014-07-11 09:42:45 +04:00
2014-07-21 07:38:33 +04:00
Run it on some HTML files:
```shell
2014-07-21 07:38:33 +04:00
$ bootlint /path/to/some/webpage.html another_webpage.html [...]
2014-07-11 09:42:45 +04:00
```
2014-07-21 07:38:33 +04:00
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](https://github.com/twbs/bootlint/wiki). Here's an example:
```shell
$ bootlint -d W002,E020 /path/to/some/webpage.html another_webpage.html [...]
```
The CLI will also process `stdin` input which means that you can pipe into Bootlint:
```shell
$ cat mypage.html | bootlint
```
Or you could use a heredoc (mostly useful for quick testing):
```shell
$ bootlint << EOF
<button class="btn btn-default">Is this correct Bootstrap markup, Bootlint?</button>
EOF
```
2014-07-11 09:42:45 +04:00
### In the browser
Use the following [bookmarklet](https://en.wikipedia.org/wiki/Bookmarklet) that's powered by [BootstrapCDN](http://www.bootstrapcdn.com/#bootlint_tab):
2015-03-18 23:03:43 +03:00
```js
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)})();
```
2014-07-21 07:38:33 +04:00
Then check the JavaScript console for lint warning messages.
2014-10-15 02:37:24 +04:00
You can also manually download [the browser-ready version of Bootlint](https://github.com/twbs/bootlint/blob/master/dist/browser/bootlint.js).
2014-12-31 10:11:55 +03:00
Bootlint is also available as a unofficial third-party web service at [bootlint.com](http://www.bootlint.com/) which lints your page simply by entering a URL, similar to the [W3C Markup Validation Service](http://validator.w3.org/). **Please note** that we do *not* operate this service and that it may use an outdated version of Bootlint. Therefore it is not the recommended way to use Bootlint.
2014-10-03 11:07:59 +04:00
## Lint problem explanations
For detailed explanations of each lint problem, [look up the IDs](https://github.com/twbs/bootlint/wiki) (for example, [`E001`](https://github.com/twbs/bootlint/wiki/E001) or [`W002`](https://github.com/twbs/bootlint/wiki/W002)) in [our wiki](https://github.com/twbs/bootlint/wiki).
2014-07-21 07:38:33 +04:00
## API Documentation
2014-07-26 00:16:22 +04:00
Bootlint is a [CommonJS module](http://wiki.commonjs.org/wiki/Modules/1.1).
2014-07-21 07:38:33 +04:00
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
* (**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
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.
2014-07-21 07:38:33 +04:00
### 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, alertOpts)`: Lints the HTML of the current document and reports the linting results to the user. Each warning will be output individually using `console.warn()`.
* `disabledIds` is an array of string linter IDs to disable
* `alertOpts` is an optional options object with the following properties:
* `hasProblems` (type: `boolean`; default: `true`) - `window.alert()` a single general notification message to the user if there are any lint problems?
* `problemFree` (type: `boolean`; default: `true`) - `window.alert()` a notification message to the user if the document has no lint problems?
2014-07-21 07:38:33 +04:00
* Returns nothing (i.e. `undefined`)
### Node.js
Example:
2015-03-18 23:03:43 +03:00
```js
2014-07-21 07:38:33 +04:00
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
2014-07-21 07:38:33 +04:00
```
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`)
2014-07-11 09:42:45 +04:00
2014-10-22 01:29:30 +04:00
### HTTP API
2014-10-22 04:53:49 +04:00
Bootlint can also be run as an HTTP server that exposes a very simple API. Use `npm run start` to run the server.
2014-10-22 01:29:30 +04:00
By default, it runs on port `7070`. Set the `$PORT` environment variable to change which port it uses.
2014-10-22 04:53:49 +04:00
2014-10-22 01:29:30 +04:00
POST an HTML document to `/` and the document's lint problems will be returned as JSON.
2014-10-22 04:53:49 +04:00
2014-10-22 01:29:30 +04:00
The endpoint accepts an optional querystring argument named `disable`, whose value is a comma-separated list of linter IDs to disable.
Example:
```http
2014-10-22 01:29:30 +04:00
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"
},
...
]
```
2014-07-11 09:42:45 +04:00
## 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](http://gruntjs.com/).
2014-07-11 09:42:45 +04:00
2014-09-24 08:59:50 +04:00
_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!_
2014-07-11 09:42:45 +04:00
## Release History
2014-09-27 00:44:52 +04:00
See the [GitHub Releases page](https://github.com/twbs/bootlint/releases) for detailed changelogs.
* (next release) - `master`
2015-02-24 06:43:28 +03:00
* 2015-02-23 - v0.11.0: Adds several new lint checks. Improves stdin handling. Bumps dependency versions.
2015-03-18 23:03:43 +03:00
* 2015-01-21 - v0.10.0: By default, the in-browser version now `alert()`s when no lint problems are found. Adds validity check for carousel control & indicator targets.
* 2015-01-07 - v0.9.2: Fixes a problem when using the CLI via node's `child_process.exec`.
2014-12-20 01:08:14 +03:00
* 2014-12-19 - v0.9.1: Fixes a W013 false positive.
2014-12-18 08:40:50 +03:00
* 2014-12-18 - v0.9.0: Fixes several small bugs and tweaks a few existing checks. Adds 4 new lint checks.
2014-11-07 12:20:54 +03:00
* 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-02 04:28:06 +03:00
* 2014-11-01 - v0.7.0: Tweaks lint message texts. Adds 1 new lint check.
2014-10-31 21:22:14 +03:00
* 2014-10-31 - v0.6.0: Fixes crash bug. Adds some new lint checks. Adds HTTP API.
2015-02-24 06:43:28 +03:00
* 2014-10-16 - v0.5.0: Adds several new features. Add official bookmarklet. Disables auto-lint-on-load in browser. Tweaks some checks. **Not backward compatible**
* 2014-10-07 - v0.4.0: Adds checks for correct Glyphicon usage and correct modal DOM structure; fixes `.panel-footer` false positive
2014-09-27 00:44:52 +04:00
* 2014-09-26 - v0.3.0: Several bug fixes and enhancements. **Not backward compatible**
2014-09-24 04:45:04 +04:00
* 2014-09-23 - v0.2.0: First formal release. Announcement: http://blog.getbootstrap.com/2014/09/23/bootlint/
2014-07-11 09:42:45 +04:00
## License
2015-02-20 22:20:48 +03:00
Copyright (c) 2014-2015 Christopher Rebert. Licensed under the MIT License.