You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.9KB

  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) {
  28. if(typeof HTMLHint.default !== 'undefined') {
  29. HTMLHint = HTMLHint.default;
  30. } else {
  31. HTMLHint = HTMLHint.HTMLHint;
  32. }
  33. }
  34. if (!HTMLHint) HTMLHint = window.HTMLHint;
  35. if (!HTMLHint) {
  36. if (window.console) {
  37. window.console.error("Error: HTMLHint not found, not defined on window, or not available through define/require, CodeMirror HTML linting cannot run.");
  38. }
  39. return found;
  40. }
  41. var messages = HTMLHint.verify(text, options && options.rules || defaultRules);
  42. for (var i = 0; i < messages.length; i++) {
  43. var message = messages[i];
  44. var startLine = message.line - 1, endLine = message.line - 1, startCol = message.col - 1, endCol = message.col;
  45. found.push({
  46. from: CodeMirror.Pos(startLine, startCol),
  47. to: CodeMirror.Pos(endLine, endCol),
  48. message: message.message,
  49. severity : message.type
  50. });
  51. }
  52. return found;
  53. });
  54. });