Skip to content

Commit c3862a2

Browse files
Fix for markup attributes (#1752)
This fixes some issues related to markup (XML/HTML) attributes. See the PR for more detail.
1 parent 5fd7577 commit c3862a2

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

components/prism-markup.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Prism.languages.markup = {
44
'doctype': /<!DOCTYPE[\s\S]+?>/i,
55
'cdata': /<!\[CDATA\[[\s\S]*?]]>/i,
66
'tag': {
7-
pattern: /<\/?(?!\d)[^\s>\/=$<%]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">=]+))?)*\s*\/?>/i,
7+
pattern: /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/i,
88
greedy: true,
99
inside: {
1010
'tag': {
@@ -15,12 +15,12 @@ Prism.languages.markup = {
1515
}
1616
},
1717
'attr-value': {
18-
pattern: /=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">=]+)/i,
18+
pattern: /=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+)/i,
1919
inside: {
2020
'punctuation': [
2121
/^=/,
2222
{
23-
pattern: /(^|[^\\])["']/,
23+
pattern: /^(\s*)["']|["']$/,
2424
lookbehind: true
2525
}
2626
]

components/prism-markup.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prism.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ Prism.languages.markup = {
557557
'doctype': /<!DOCTYPE[\s\S]+?>/i,
558558
'cdata': /<!\[CDATA\[[\s\S]*?]]>/i,
559559
'tag': {
560-
pattern: /<\/?(?!\d)[^\s>\/=$<%]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">=]+))?)*\s*\/?>/i,
560+
pattern: /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/i,
561561
greedy: true,
562562
inside: {
563563
'tag': {
@@ -568,12 +568,12 @@ Prism.languages.markup = {
568568
}
569569
},
570570
'attr-value': {
571-
pattern: /=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">=]+)/i,
571+
pattern: /=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+)/i,
572572
inside: {
573573
'punctuation': [
574574
/^=/,
575575
{
576-
pattern: /(^|[^\\])["']/,
576+
pattern: /^(\s*)["']|["']$/,
577577
lookbehind: true
578578
}
579579
]

tests/languages/markup/tag_attribute_feature.test

+35-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
bar
55
baz">
66
<div foo:bar=42>
7-
<div foo="=\"=">
7+
<div foo = 42 bar = "42">
8+
<div foo="=\"bar=baz/>
89

910
----------------------------------------------------
1011

@@ -61,12 +62,12 @@ baz">
6162
["attr-name", [
6263
"class"
6364
]],
64-
["attr-value", [
65-
["punctuation", "="],
66-
["punctuation", "\""],
67-
"foo\r\nbar\r\nbaz",
68-
["punctuation", "\""]
69-
]],
65+
["attr-value", [
66+
["punctuation", "="],
67+
["punctuation", "\""],
68+
"foo\r\nbar\r\nbaz",
69+
["punctuation", "\""]
70+
]],
7071
["punctuation", ">"]
7172
]],
7273

@@ -91,20 +92,43 @@ baz">
9192
["punctuation", "<"],
9293
"div"
9394
]],
94-
["attr-name", [
95-
"foo"
95+
["attr-name", ["foo"]],
96+
["attr-value", [
97+
["punctuation", "="],
98+
" 42"
9699
]],
100+
["attr-name", ["bar"]],
97101
["attr-value", [
98102
["punctuation", "="],
99103
["punctuation", "\""],
100-
"=\\\"=",
104+
"42",
101105
["punctuation", "\""]
102106
]],
103107
["punctuation", ">"]
108+
]],
109+
110+
["tag", [
111+
["tag", [
112+
["punctuation", "<"],
113+
"div"
114+
]],
115+
["attr-name", ["foo"]],
116+
["attr-value", [
117+
["punctuation", "="],
118+
["punctuation", "\""],
119+
"=\\",
120+
["punctuation", "\""]
121+
]],
122+
["attr-name", ["bar"]],
123+
["attr-value", [
124+
["punctuation", "="],
125+
"baz/"
126+
]],
127+
["punctuation", ">"]
104128
]]
105129
]
106130

107131
----------------------------------------------------
108132

109133
Checks for single-quoted, double-quoted and unquoted attributes, attributes without value and
110-
namespaced attributes.
134+
namespaced attributes.

0 commit comments

Comments
 (0)