html-lint.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. // Depends on htmlhint.js from http://htmlhint.com/js/htmlhint.js
  4. // declare global: HTMLHint
  5. (function(mod) {
  6. if (typeof exports == "object" && typeof module == "object") // CommonJS
  7. mod(require("../../lib/codemirror"), require("htmlhint"));
  8. else if (typeof define == "function" && define.amd) // AMD
  9. define(["../../lib/codemirror", "htmlhint"], mod);
  10. else // Plain browser env
  11. mod(CodeMirror, window.HTMLHint);
  12. })(function(CodeMirror, HTMLHint) {
  13. "use strict";
  14. var defaultRules = {
  15. "tagname-lowercase": true,
  16. "attr-lowercase": true,
  17. "attr-value-double-quotes": true,
  18. "doctype-first": false,
  19. "tag-pair": true,
  20. "spec-char-escape": true,
  21. "id-unique": true,
  22. "src-not-empty": true,
  23. "attr-no-duplication": true
  24. };
  25. CodeMirror.registerHelper("lint", "html", function(text, options) {
  26. var found = [];
  27. if (HTMLHint && !HTMLHint.verify) HTMLHint = HTMLHint.HTMLHint;
  28. if (!HTMLHint) HTMLHint = window.HTMLHint;
  29. if (!HTMLHint) {
  30. if (window.console) {
  31. window.console.error("Error: HTMLHint not found, not defined on window, or not available through define/require, CodeMirror HTML linting cannot run.");
  32. }
  33. return found;
  34. }
  35. var messages = HTMLHint.verify(text, options && options.rules || defaultRules);
  36. for (var i = 0; i < messages.length; i++) {
  37. var message = messages[i];
  38. var startLine = message.line - 1, endLine = message.line - 1, startCol = message.col - 1, endCol = message.col;
  39. found.push({
  40. from: CodeMirror.Pos(startLine, startCol),
  41. to: CodeMirror.Pos(endLine, endCol),
  42. message: message.message,
  43. severity : message.type
  44. });
  45. }
  46. return found;
  47. });
  48. });