css.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. module.exports = function(hljs) {
  2. var FUNCTION_LIKE = {
  3. begin: /[\w-]+\(/, returnBegin: true,
  4. contains: [
  5. {
  6. className: 'built_in',
  7. begin: /[\w-]+/
  8. },
  9. {
  10. begin: /\(/, end: /\)/,
  11. contains: [
  12. hljs.APOS_STRING_MODE,
  13. hljs.QUOTE_STRING_MODE,
  14. hljs.CSS_NUMBER_MODE,
  15. ]
  16. }
  17. ]
  18. }
  19. var ATTRIBUTE = {
  20. className: 'attribute',
  21. begin: /\S/, end: ':', excludeEnd: true,
  22. starts: {
  23. endsWithParent: true, excludeEnd: true,
  24. contains: [
  25. FUNCTION_LIKE,
  26. hljs.CSS_NUMBER_MODE,
  27. hljs.QUOTE_STRING_MODE,
  28. hljs.APOS_STRING_MODE,
  29. hljs.C_BLOCK_COMMENT_MODE,
  30. {
  31. className: 'number', begin: '#[0-9A-Fa-f]+'
  32. },
  33. {
  34. className: 'meta', begin: '!important'
  35. }
  36. ]
  37. }
  38. }
  39. var AT_IDENTIFIER = '@[a-z-]+' // @font-face
  40. var AT_MODIFIERS = "and or not only"
  41. var MEDIA_TYPES = "all print screen speech"
  42. var AT_PROPERTY_RE = /@\-?\w[\w]*(\-\w+)*/ // @-webkit-keyframes
  43. var IDENT_RE = '[a-zA-Z-][a-zA-Z0-9_-]*';
  44. var RULE = {
  45. begin: /(?:[A-Z\_\.\-]+|--[a-zA-Z0-9_-]+)\s*:/, returnBegin: true, end: ';', endsWithParent: true,
  46. contains: [
  47. ATTRIBUTE
  48. ]
  49. };
  50. return {
  51. case_insensitive: true,
  52. illegal: /[=\/|'\$]/,
  53. contains: [
  54. hljs.C_BLOCK_COMMENT_MODE,
  55. {
  56. className: 'selector-id', begin: /#[A-Za-z0-9_-]+/
  57. },
  58. {
  59. className: 'selector-class', begin: /\.[A-Za-z0-9_-]+/
  60. },
  61. {
  62. className: 'selector-attr',
  63. begin: /\[/, end: /\]/,
  64. illegal: '$',
  65. contains: [
  66. hljs.APOS_STRING_MODE,
  67. hljs.QUOTE_STRING_MODE,
  68. ]
  69. },
  70. {
  71. className: 'selector-pseudo',
  72. begin: /:(:)?[a-zA-Z0-9\_\-\+\(\)"'.]+/
  73. },
  74. // matching these here allows us to treat them more like regular CSS
  75. // rules so everything between the {} gets regular rule highlighting,
  76. // which is what we want for page and font-face
  77. {
  78. begin: '@(page|font-face)',
  79. lexemes: AT_IDENTIFIER,
  80. keywords: '@page @font-face'
  81. },
  82. {
  83. begin: '@', end: '[{;]', // at_rule eating first "{" is a good thing
  84. // because it doesn’t let it to be parsed as
  85. // a rule set but instead drops parser into
  86. // the default mode which is how it should be.
  87. illegal: /:/, // break on Less variables @var: ...
  88. returnBegin: true,
  89. contains: [
  90. {
  91. className: 'keyword',
  92. begin: AT_PROPERTY_RE
  93. },
  94. {
  95. begin: /\s/, endsWithParent: true, excludeEnd: true,
  96. relevance: 0,
  97. keywords: AT_MODIFIERS,
  98. contains: [
  99. {
  100. begin: /[a-z-]+:/,
  101. className:"attribute"
  102. },
  103. hljs.APOS_STRING_MODE,
  104. hljs.QUOTE_STRING_MODE,
  105. hljs.CSS_NUMBER_MODE
  106. ]
  107. }
  108. ]
  109. },
  110. {
  111. className: 'selector-tag', begin: IDENT_RE,
  112. relevance: 0
  113. },
  114. {
  115. begin: '{', end: '}',
  116. illegal: /\S/,
  117. contains: [
  118. hljs.C_BLOCK_COMMENT_MODE,
  119. RULE,
  120. ]
  121. }
  122. ]
  123. };
  124. };