html-lint.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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);
  12. })(function(CodeMirror) {
  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 (!window.HTMLHint) return found;
  28. var messages = HTMLHint.verify(text, options && options.rules || defaultRules);
  29. for (var i = 0; i < messages.length; i++) {
  30. var message = messages[i];
  31. var startLine = message.line - 1, endLine = message.line - 1, startCol = message.col - 1, endCol = message.col;
  32. found.push({
  33. from: CodeMirror.Pos(startLine, startCol),
  34. to: CodeMirror.Pos(endLine, endCol),
  35. message: message.message,
  36. severity : message.type
  37. });
  38. }
  39. return found;
  40. });
  41. });