Bootlint

用于 Bootstrap 项目的 HTML linter。(HTML linter for Bootstrap projects)

Github stars Tracking Chart

Bootlint

Bootstrap 项目 HTML linter

什么是 Bootlint?

Bootlint 是一个工具,它可以检查在使用 Bootstrap 以相当“普通”方式使用的网页中的几个常见 HTML 错误。 Vanilla Bootstrap 的组件/小部件要求它们的 DOM 部分符合特定的结构。 Bootlint 会检查 Bootstrap 组件的实例是否具有正确构造的HTML。 最佳的 Bootstrap 使用还需要您的页面包括某些元标签<meta>,HTML5 doctype声明等。 Bootlint检查这些是否存在。

注意事项

Bootlint 假设您的网页已经是有效的 HTML5。如果您需要检查 HTML5 有效性,我们推荐使用 vnu.jar ,或 grunt-html

Bootlint 假定您在网页中使用了 Bootstrap 的默认类名,而不是利用 Less 或 Sass 的“mixins”功能将它们映射到自定义类名。如果您使用 mixin,Bootlint 可能会报告一些误报警告。但是,即使您普遍使用 mixin,也可以使用一些 Bootlint 检查。

入门

Via Grunt

要通过 Grunt 使用 Bootlint,请使用官方的 Grunt 插件:grunt-bootlint

Via Gulp

如果您想通过 Gulp 使用Bootlint,则可以使用非官方的 Gulp插件:gulp-bootlint

在命令行上

安装模块: npm install -g bootlint

在一些 HTML 文件上运行它:

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

这将输出适用于每个文件的 lint 警告。

CLI 还接受 --disable(或 -d )选项以禁用某些 lint 检查。 --disable 接受以逗号分隔的 lint 问题 ID 列表。这里有一个例子:

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

CLI 还将处理 stdin 输入,这意味着您可以通过管道导入 Bootlint:

cat mypage.html, bootlint

或者你可以使用 heredoc(主要用于快速测试):

bootlint << EOF
<button class="btn btn-default">Is this correct Bootstrap markup, Bootlint?</button>
EOF

在浏览器中

Bootlint 可以直接在浏览器中运行!这是通过使用 书签来实现的,它将 bootlint 附加到活动页面的主体。在浏览器中直接运行 bootlint 有几个好处。它们包括:

  1. 在 AJAX 请求完成后评估页面标记。
  2. 评估动态创建的服务器端页面(例如:CMS)。
  3. 评估没有构建脚本的网页/网站。

如何安装书签

请按照以下说明启动并运行:

  1. 在您的浏览器中创建新书签
  2. 将名称/标题设置为等于易记的东西。例如:运行Bootlint
  3. 将网址设置为
javascript:(function(){var s=document.createElement("script");s.onload=function(){bootlint.showLintReportForCurrentDocument([]);};s.src="https://stackpath.bootstrapcdn.com/bootlint/latest/bootlint.min.js";document.body.appendChild(s)})();

注意:上面的代码片段将确保您始终运行最新版本的 bootlint。如果您想引用特定版本的 bootlint,请参阅 BootstrapCDN 。复制网址并更新 s.src="PASTE-ME-HERE"。

如何使用书签

  1. 点击上面创建的书签
  2. 出现一个弹出窗口,通知您是否检测到问题
  3. 如果存在问题,请打开开发者工具并选择控制台标签

其他选项

浏览器就绪脚本

您可以手动下载 Bootlint 的浏览器就绪版本

第三方服务

Bootlint 还可以在 bootlint.com 上作为非官方的第三方网站服务提供,只需输入网址即可屏蔽您的网页,类似于 W3C标记验证服务请注意我们不是运行此服务,并且它可能会使用过时的 Bootlint 版本。因此它不是使用Bootlint的推荐方式。

Lint 问题解释

有关每个 lint 问题的详细说明,请查找ID (例如,E001 W002 )位于 我们的 wiki 中。

API 文档

Bootlint 是一个 CommonJS 模块

Bootlint 代表使用 LintError 和 LintWarning 类报告的 lint 问题:

  • LintWarning
    • 代表潜在的错误。它可能有误报。
    • 构造函数: LintWarning(id, message, elements)
    • 属性:
      • id -- 此类型 lint 问题的唯一字符串 ID。形式为“W###”(例如“W123”)。
      • message -- 描述问题的可读字符串
      • elements -- 引用 DOM 元素的 jQuery 或 Cheerio 集合,指向文档中的所有问题位置
        • 仅在 Node.js 下可用):当从底层 HTML 解析器(大多数情况下)可用时,集合中的DOM元素将具有 .startLocation 属性,该属性是一个 Location (见下文),表示元素在文档的 HTML 源代码中的位置
  • LintError
    • 表示错误。根据上述“注意事项”部分所述的假设,不应该有任何误报。
    • 构造函数: LintError(id,message,elements)
    • 属性:
      • id - 此类型lint问题的唯一字符串ID。形式为“E ###”(例如“E123”)。
      • message - 描述问题的可读字符串
      • elements - 引用DOM元素的jQuery或Cheerio集合,指向文档中的所有问题位置
        • 仅在 Node.js 下可用):当从底层HTML解析器(大多数情况下)可用时,集合中的 DOM 元素将具有 .startLocation 属性,该属性是 Location (见下文),指示元素在文档的 HTML 源代码中的位置

Bootlint 定义了以下公用类:

  • Location(仅在 Node.js 下可用
    • 代表 HTML 源代码中的位置
    • 构造函数:Location(line, column)
    • 属性:
      • line -- 基于0的行号
      • column -- 基于0的列号

reporter 是一个函数,它接受1个 LintWarning 或 LintError 类型的参数。其返回值被忽略。它应该以某种方式记录问题或将其显示给用户。

浏览器

Bootlint 在全局窗口对象上导出 bootlint 属性。 在浏览器环境中,可以使用以下公共 API:

  • bootlint.lintCurrentDocument(reporter, disabledIds):提示当前文档的 HTML,并以每个 lint 问题作为参数重复调用 reporter() 函数。
    • reporter 是 reporter 功能(参见上面的定义)。它将会以每个lint问题作为参数被重复调用。
    • disabledIds 是要禁用的字符串 linter ID 的数组
    • 不返回任何内容(即 undefined )
  • bootlint.showLintReportForCurrentDocument(disabledIds, alertOpts):提示当前文档的 HTML 并向用户报告 linting 结果。每个警告将使用 console.warn()。
      单独输出
    • disabledIds 是禁用的字符串 linter ID 的数组
    • alertOpts 是一个具有以下属性的可选选项对象:
      • hasProblems (type: boolean; default: true) -- window.alert() 如果有任何绒毛问题,通知消息给用户
      • problemFree (type: boolean; default: true) -- window.alert()通知消息给用户如果文件没有绒毛问题?
    • 不返回任何内容(即 undefined )
  • Node.js

    示例:

    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
    

    在 Node.js 环境中,Bootlint 公开以下公共 API:

    • bootlint.lintHtml(html,reporter,disabledIds):为网页提供给定的 HTML 并返回 linting 结果。
      • html 是作为字符串的 lint 的 HTML
      • reporter 是一个 reporter 函数(参见上面的定义)。它将会以每个 lint 问题作为参数被重复调用。
      • disabledIds 是禁用的字符串 linter ID 的数组
      • 不返回任何内容(即 undefined )

    HTTP API

    Bootlint 也可以作为一个 HTTP 服务器运行,它提供了一个非常简单的 API。使用 npm run start 来运行服务器。

    默认情况下,它在端口 7070 上运行。设置 $PORT 环境变量以更改它使用的端口。

    将 HTML 文档发布到 /,并且文档的 lint 问题将作为 JSON 返回。

    端点接受一个名为 disable 的可选 querystring 参数,其值是逗号分隔的要禁用的linter ID 列表。

    示例:

    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"
        },
        ...
      ]
    

    贡献

    项目的编码风格在 ESLint 配置中进行了规划。为任何新的或更改的功能添加单元测试。使用 npm 脚本测试你的代码。

    另外,请不要编辑 “dist” 子目录中的文件,因为它们是通过 npm run dist 生成的。您可以在 “src” 子目录中找到源代码!

    许可证

    版权所有(c)2014-2017 Bootlint 作者。根据 MIT 许可证授权。

    (First edition: vz edited at 2019.08.23)

    Main metrics

    Overview
    Name With Ownertwbs/bootlint
    Primary LanguageJavaScript
    Program languageJavaScript (Language Count: 2)
    PlatformLinux, Mac, Windows
    License:MIT License
    所有者活动
    Created At2014-07-02 14:55:13
    Pushed At2022-01-27 15:10:29
    Last Commit At2022-01-27 17:09:24
    Release Count31
    Last Release Namev1.1.0 (Posted on )
    First Release Namev0.1.0 (Posted on )
    用户参与
    Stargazers Count2.4k
    Watchers Count136
    Fork Count310
    Commits Count715
    Has Issues Enabled
    Issues Count235
    Issue Open Count39
    Pull Requests Count181
    Pull Requests Open Count8
    Pull Requests Close Count49
    项目设置
    Has Wiki Enabled
    Is Archived
    Is Fork
    Is Locked
    Is Mirror
    Is Private

    Bootlint

    NPM version
    Build Status
    Coverage Status
    Development Status :: 5 - Production/Stable
    MIT License
    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 or grunt-html.

    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.

    Via Gulp

    If you want to use Bootlint with Gulp, there is an unofficial Gulp plugin: gulp-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 [...]
    

    The CLI will also process stdin input which means that you can pipe into Bootlint:

    cat mypage.html, bootlint
    

    Or you could use a heredoc (mostly useful for quick testing):

    bootlint << EOF
    <button class="btn btn-default">Is this correct Bootstrap markup, Bootlint?</button>
    EOF
    

    In the browser

    Bootlint can run directly in the browser! This is accomplished by using a bookmarklet, which appends bootlint to the body of the active page. There are a few nice benefits of running bootlint directly in the browser. They include:

    1. Evaluating page markup after AJAX requests complete.
    2. Evaluating pages that are dynamically created server-side (ex: CMS).
    3. Evaluating pages/sites that do not have a build script.

    How to install the bookmarklet

    Please follow the instructions below to get up and running:

    1. Create a new bookmark in your browser
    2. Set the name/title equal to something that is easy to remember. Ex: Run Bootlint
    3. Set the URL equal to
    javascript:(function(){var s=document.createElement("script");s.onload=function(){bootlint.showLintReportForCurrentDocument([]);};s.src="https://stackpath.bootstrapcdn.com/bootlint/latest/bootlint.min.js";document.body.appendChild(s)})();
    

    Note: The snippet above will ensure you are always running the latest version of bootlint. If you want to reference a specific version of bootlint please see the BootstrapCDN. Copy the URL and update s.src="PASTE-ME-HERE".

    How to use the bookmarklet

    1. Click the bookmark you created above
    2. A popup will appear informing you if issues were detected
    3. If issues exist, please open the developer tools and select the console tab

    Alternative Options

    Browser ready script

    You can 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
          • (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.

    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?
      • 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. See https://github.com/twbs/bootlint-server.

    Contributing

    The project's coding style is laid out in the ESLint configuration. Add unit tests for any new or changed functionality. Lint and test your code using the npm scripts.

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

    Release History

    See the GitHub Releases page for detailed changelogs.

    • (next release) - master
    • 2015-11-25 - v0.14.2: Fix critical CLI bug introduced in v0.14.0 and add tests to prevent its recurrence. Update current Bootstrap version to v3.3.6.
    • 2015-11-16 - v0.14.1: Forgot to regenerate browser version when tagging v0.14.0
    • 2015-11-16 - v0.14.0: Adds 3 new lint checks.
    • 2015-11-15 - v0.13.0: Removes E036. Adds a few new checks. Bumps dependency versions.
    • 2015-03-16 - v0.12.0: Adds warning if Bootstrap v4 is detected (since Bootlint is currently only compatible with Bootstrap v3). Minor fixes to some existing lint checks.
    • 2015-02-23 - v0.11.0: Adds several new lint checks. Improves stdin handling. Bumps dependency versions.
    • 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-19 - v0.9.1: Fixes a W013 false positive.
    • 2014-12-18 - v0.9.0: Fixes several small bugs and tweaks a few existing checks. Adds 4 new lint checks.
    • 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: Adds several new features. Adds 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-26 - v0.3.0: Several bug fixes and enhancements. Not backward compatible
    • 2014-09-23 - v0.2.0: First formal release. Announcement: https://blog.getbootstrap.com/2014/09/23/bootlint/

    License

    Copyright (c) 2014-2019 The Bootlint Authors. Licensed under the MIT License.