html-lint.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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')
  7. // CommonJS
  8. mod(require('../../lib/codemirror'), require('htmlhint'))
  9. else if (typeof define == 'function' && define.amd)
  10. // AMD
  11. define(['../../lib/codemirror', 'htmlhint'], mod)
  12. // Plain browser env
  13. else mod(CodeMirror, window.HTMLHint)
  14. })(function (CodeMirror, HTMLHint) {
  15. 'use strict'
  16. var defaultRules = {
  17. 'tagname-lowercase': true,
  18. 'attr-lowercase': true,
  19. 'attr-value-double-quotes': true,
  20. 'doctype-first': false,
  21. 'tag-pair': true,
  22. 'spec-char-escape': true,
  23. 'id-unique': true,
  24. 'src-not-empty': true,
  25. 'attr-no-duplication': true,
  26. }
  27. CodeMirror.registerHelper('lint', 'html', function (text, options) {
  28. var found = []
  29. if (HTMLHint && !HTMLHint.verify) {
  30. if (typeof HTMLHint.default !== 'undefined') {
  31. HTMLHint = HTMLHint.default
  32. } else {
  33. HTMLHint = HTMLHint.HTMLHint
  34. }
  35. }
  36. if (!HTMLHint) HTMLHint = window.HTMLHint
  37. if (!HTMLHint) {
  38. if (window.console) {
  39. window.console.error('Error: HTMLHint not found, not defined on window, or not available through define/require, CodeMirror HTML linting cannot run.')
  40. }
  41. return found
  42. }
  43. var messages = HTMLHint.verify(text, (options && options.rules) || defaultRules)
  44. for (var i = 0; i < messages.length; i++) {
  45. var message = messages[i]
  46. var startLine = message.line - 1,
  47. endLine = message.line - 1,
  48. startCol = message.col - 1,
  49. endCol = message.col
  50. found.push({
  51. from: CodeMirror.Pos(startLine, startCol),
  52. to: CodeMirror.Pos(endLine, endCol),
  53. message: message.message,
  54. severity: message.type,
  55. })
  56. }
  57. return found
  58. })
  59. })