css-lint.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. // Depends on csslint.js from https://github.com/stubbornella/csslint
  4. // declare global: CSSLint
  5. ;(function (mod) {
  6. if (typeof exports == 'object' && typeof module == 'object')
  7. // CommonJS
  8. mod(require('../../lib/codemirror'))
  9. else if (typeof define == 'function' && define.amd)
  10. // AMD
  11. define(['../../lib/codemirror'], mod)
  12. // Plain browser env
  13. else mod(CodeMirror)
  14. })(function (CodeMirror) {
  15. 'use strict'
  16. CodeMirror.registerHelper('lint', 'css', function (text, options) {
  17. var found = []
  18. if (!window.CSSLint) {
  19. if (window.console) {
  20. window.console.error('Error: window.CSSLint not defined, CodeMirror CSS linting cannot run.')
  21. }
  22. return found
  23. }
  24. var results = CSSLint.verify(text, options),
  25. messages = results.messages,
  26. message = null
  27. for (var i = 0; i < messages.length; i++) {
  28. message = messages[i]
  29. var startLine = message.line - 1,
  30. endLine = message.line - 1,
  31. startCol = message.col - 1,
  32. endCol = message.col
  33. found.push({
  34. from: CodeMirror.Pos(startLine, startCol),
  35. to: CodeMirror.Pos(endLine, endCol),
  36. message: message.message,
  37. severity: message.type,
  38. })
  39. }
  40. return found
  41. })
  42. })