Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ A ***reporter*** is a function that accepts exactly 1 argument of type `LintWarn
Bootlint exports a `bootlint` property on the global `window` object.
In a browser environment, the following public APIs are available:

* `bootlint.lintCurrentDocument(reporter)`: Lints the HTML of the current document and calls the `reporter()` function repeatedly with each lint problem as an argument.
* `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()`: Lints the HTML of the current document and reports the linting results to the user.
* `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`)

In a browser environment, after Bootlint has loaded, `bootlint.showLintReportForCurrentDocument()` will be invoked once.
In a browser environment, after Bootlint has loaded, `bootlint.showLintReportForCurrentDocument([])` will be executed once.

### Node.js

Expand All @@ -75,14 +78,15 @@ 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
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)`: Lints the given HTML for a webpage and returns the linting results.
* `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 function that should accept exactly 1 argument. It will be called 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`)

## Contributing
Expand Down
27 changes: 18 additions & 9 deletions src/bootlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,15 @@ var cheerio = require('cheerio');
});
});

exports._lint = function ($, reporter) {
exports._lint = function ($, reporter, disabledIdList) {
var disabledIdSet = {};
disabledIdList.forEach(function (disabledId) {
disabledIdSet[disabledId] = true;
});
Object.keys(allLinters).sort().forEach(function (linterId) {
allLinters[linterId]($, reporter);
if (!disabledIdSet[linterId]) {
allLinters[linterId]($, reporter);
}
});
};
if (IN_NODE_JS) {
Expand All @@ -607,11 +613,12 @@ var cheerio = require('cheerio');
* Lints the given HTML.
* @param {string} html The HTML to lint
* @param reporter Function to call with each lint problem
* @param {string[]} disabledIds Array of string IDs of linters to disable
* @returns {undefined} Nothing
*/
exports.lintHtml = function (html, reporter) {
exports.lintHtml = function (html, reporter, disabledIds) {
var $ = cheerio.load(html);
this._lint($, reporter);
this._lint($, reporter, disabledIds);
};
}
else {
Expand All @@ -621,18 +628,20 @@ var cheerio = require('cheerio');
/**
* Lints the HTML of the current document.
* @param reporter Function to call with each lint problem
* @param {string[]} disabledIds Array of string IDs of linters to disable
* @returns {undefined} Nothing
*/
exports.lintCurrentDocument = function (reporter) {
this._lint($, reporter);
exports.lintCurrentDocument = function (reporter, disabledIds) {
this._lint($, reporter, disabledIds);
};
/**
* Lints the HTML of the current document.
* 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().
* @param {string[]} disabledIds Array of string IDs of linters to disable
* @returns {undefined} Nothing
*/
exports.showLintReportForCurrentDocument = function () {
exports.showLintReportForCurrentDocument = function (disabledIds) {
var seenLint = false;
var reporter = function (lint) {
if (!seenLint) {
Expand All @@ -643,13 +652,13 @@ var cheerio = require('cheerio');
}
console.warn("bootlint:", lint.id, lint.message);
};
this.lintCurrentDocument(reporter);
this.lintCurrentDocument(reporter, disabledIds);
};
/*eslint-disable no-undef, block-scoped-var */
window.bootlint = exports;
/*eslint-enable no-undef, block-scoped-var */
$(function () {
exports.showLintReportForCurrentDocument();
exports.showLintReportForCurrentDocument([]);
});
})();
}
Expand Down
3 changes: 2 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var bootlint = require('./bootlint.js');

var totalErrCount = 0;
var totalFileCount = 0;
var disabledIds = [];
var patterns = process.argv.slice(2);
patterns.forEach(function (pattern) {
var filenames = glob.sync(pattern);
Expand All @@ -27,7 +28,7 @@ patterns.forEach(function (pattern) {
console.log(filename + ":", err);
return;
}
bootlint.lintHtml(html, reporter);
bootlint.lintHtml(html, reporter, disabledIds);
totalFileCount++;
});
});
Expand Down
11 changes: 9 additions & 2 deletions test/bootlint_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ function utf8Fixture(name) {
function utf16Fixture(name) {
return fs.readFileSync(_fixtureNameToFilepath(name), {encoding: 'utf16le'});
}
function lintHtml(html) {
function lintHtml(html, disabledIds) {
var lints = [];
var reporter = function (lint) {
lints.push(lint.message);
};
bootlint.lintHtml(html, reporter);
bootlint.lintHtml(html, reporter, disabledIds || []);
return lints;
}
/*
Expand Down Expand Up @@ -62,6 +62,13 @@ exports.bootlint = {
'should not complain when the legacy-compatibility HTML5 doctype is used.');
test.done();
},
'disabling lint checks': function (test) {
test.expect(1);
test.deepEqual(lintHtml(utf8Fixture('bs-v2.html'), ['E002', 'E013']),
[],
'should complain when Bootstrap v2 grid classes are present.');
test.done();
},
'UTF-8 charset meta tag': function (test) {
test.expect(3);
test.deepEqual(lintHtml(utf8Fixture('charset/utf8.html')),
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/generic-qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
var reporter = function (lint) {
lints.push(lint.message);
};
bootlint.lintCurrentDocument(reporter);
bootlint.lintCurrentDocument(reporter, []);
return lints;
}

Expand Down