forked from highlightjs/highlight.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss.js
137 lines (134 loc) · 3.74 KB
/
css.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Language: CSS
Category: common, css, web
Website: https://developer.mozilla.org/en-US/docs/Web/CSS
*/
// @ts-ignore
import * as css from "./lib/css-shared.js";
/** @type LanguageFn */
export default function(hljs) {
const regex = hljs.regex;
const modes = css.MODES(hljs);
const VENDOR_PREFIX = { begin: /-(webkit|moz|ms|o)-(?=[a-z])/ };
const AT_MODIFIERS = "and or not only";
const AT_PROPERTY_RE = /@-?\w[\w]*(-\w+)*/; // @-webkit-keyframes
const IDENT_RE = '[a-zA-Z-][a-zA-Z0-9_-]*';
const STRINGS = [
hljs.APOS_STRING_MODE,
hljs.QUOTE_STRING_MODE
];
return {
name: 'CSS',
case_insensitive: true,
illegal: /[=|'\$]/,
keywords: { keyframePosition: "from to" },
classNameAliases: {
// for visual continuity with `tag {}` and because we
// don't have a great class for this?
keyframePosition: "selector-tag" },
contains: [
modes.BLOCK_COMMENT,
VENDOR_PREFIX,
// to recognize keyframe 40% etc which are outside the scope of our
// attribute value mode
modes.CSS_NUMBER_MODE,
{
className: 'selector-id',
begin: /#[A-Za-z0-9_-]+/,
relevance: 0
},
{
className: 'selector-class',
begin: '\\.' + IDENT_RE,
relevance: 0
},
modes.ATTRIBUTE_SELECTOR_MODE,
{
className: 'selector-pseudo',
variants: [
{ begin: ':(' + css.PSEUDO_CLASSES.join('|') + ')' },
{ begin: ':(:)?(' + css.PSEUDO_ELEMENTS.join('|') + ')' }
]
},
// we may actually need this (12/2020)
// { // pseudo-selector params
// begin: /\(/,
// end: /\)/,
// contains: [ hljs.CSS_NUMBER_MODE ]
// },
modes.CSS_VARIABLE,
{
className: 'attribute',
begin: '\\b(' + css.ATTRIBUTES.join('|') + ')\\b'
},
// attribute values
{
begin: /:/,
end: /[;}{]/,
contains: [
modes.BLOCK_COMMENT,
modes.HEXCOLOR,
modes.IMPORTANT,
modes.CSS_NUMBER_MODE,
...STRINGS,
// needed to highlight these as strings and to avoid issues with
// illegal characters that might be inside urls that would tigger the
// languages illegal stack
{
begin: /(url|data-uri)\(/,
end: /\)/,
relevance: 0, // from keywords
keywords: { built_in: "url data-uri" },
contains: [
...STRINGS,
{
className: "string",
// any character other than `)` as in `url()` will be the start
// of a string, which ends with `)` (from the parent mode)
begin: /[^)]/,
endsWithParent: true,
excludeEnd: true
}
]
},
modes.FUNCTION_DISPATCH
]
},
{
begin: regex.lookahead(/@/),
end: '[{;]',
relevance: 0,
illegal: /:/, // break on Less variables @var: ...
contains: [
{
className: 'keyword',
begin: AT_PROPERTY_RE
},
{
begin: /\s/,
endsWithParent: true,
excludeEnd: true,
relevance: 0,
keywords: {
$pattern: /[a-z-]+/,
keyword: AT_MODIFIERS,
attribute: css.MEDIA_FEATURES.join(" ")
},
contains: [
{
begin: /[a-z-]+(?=:)/,
className: "attribute"
},
...STRINGS,
modes.CSS_NUMBER_MODE
]
}
]
},
{
className: 'selector-tag',
begin: '\\b(' + css.TAGS.join('|') + ')\\b'
}
]
};
}