Skip to content

Commit

Permalink
Improved escaping of braces in XQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
FIameCaster committed Mar 26, 2024
1 parent 0a1d28a commit 4672340
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
29 changes: 17 additions & 12 deletions package/src/prism/languages/xquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,15 @@ var walkTokens = (tokens, code, position) => {
}
} else if (l && type == 'punctuation') {
last = openedTags[l - 1];
if (content == '{' && code[position - 1] != '{' && code[position + 1] != '{') last[1]++;
if (content == '{') {
// Ignore `{{`
if (code[position + 1] == content) {
tokens[i + 1] = content;
notTagNorBrace = true;
} else {
last[1]++;
}
}
else if (last[1] && content == '}') last[1]--;
else {
notTagNorBrace = true;
Expand All @@ -120,23 +128,20 @@ var walkTokens = (tokens, code, position) => {
plainText = code.slice(start, position + length);
tokens[i] = new Token('plain-text', plainText, plainText);
}
else if (Array.isArray(content)) {
walkTokens(content, code, position);
}
position += length;
}
return tokens;
};

var expression = /\{(?!\{)(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])+\}/;
var exprSrc = [expression.source];
// Allow for two levels of nesting
var expression = [/\{(?!\{)(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})*\}/.source];

tag.pattern = re(/<\/?(?!\d)[^\s/=>$<%]+(?:\s+[^\s/=>]+(?:\s*=\s*(?:"(?:<0>|[^"])*"|'(?:<0>|[^'])*'))?)*\s*\/?>/.source, exprSrc, 'g');
attrValue.pattern = re(/(=\s*)(?:"(?:<0>|[^"])*"|'(?:<0>|[^'])*')/.source, exprSrc, 'g');
tag.pattern = re(/<\/?(?!\d)[^\s/=>$<%]+(?:\s+[^\s/=>]+(?:\s*=\s*(["'])(?:\{\{|<0>|(?!\1)[^{])*\1)?)*\s*\/?>/.source, expression, 'g');
attrValue.pattern = re(/(=\s*)(["'])(?:\{\{|<0>|(?!\2)[^{])*\2/.source, expression, 'g');
attrValue.inside['expression'] = {
// Allow for two levels of nesting
pattern: expression,
inside: xquery,
alias: 'language-xquery'
pattern: re(/((?:^|[^{])(?:\{\{)*)<0>/.source, expression),
lookbehind: true,
alias: 'language-xquery',
inside: xquery
};
delete xquery['markup-bracket'];
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plaintext
{comment{concat(" List of ", $count, " products ")}}
</ul>

<h1>data($prod/name){data($prod/name)}{{data($prod/name)}}</h1>
<h1>data($prod/name){{{data($prod/name)}}}{{data($prod/name)}}</h1>

----------------------------------------------------

Expand Down Expand Up @@ -43,7 +43,7 @@ plaintext
["tag", ["h1"]],
["punctuation", ">"]
]],
["plain-text", "data($prod/name)"],
["plain-text", "data($prod/name){{"],
["punctuation", "{"],
["function", "data"],
["punctuation", "("],
Expand All @@ -52,7 +52,7 @@ plaintext
"name",
["punctuation", ")"],
["punctuation", "}"],
["plain-text", "{{data($prod/name)}}"],
["plain-text", "}}{{data($prod/name)}}"],
["tag", [
["punctuation", "</"],
["tag", ["h1"]],
Expand Down
15 changes: 15 additions & 0 deletions package/src/prism/tests/languages/xquery/tag_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ quan="{$item/@quantity}"/>

<li dep="{substring-after($prod/@dept, "-")}"/>

<li dep="{{escape}}"/>

----------------------------------------------------

[
Expand Down Expand Up @@ -125,6 +127,19 @@ quan="{$item/@quantity}"/>
["punctuation", "\""]
]],
["punctuation", "/>"]
]],

["tag", [
["punctuation", "<"],
["tag", ["li"]],
["attr-name", ["dep"]],
["attr-equals", "="],
["attr-value", [
["punctuation", "\""],
"{{escape}}",
["punctuation", "\""]
]],
["punctuation", "/>"]
]]
]

Expand Down

0 comments on commit 4672340

Please # to comment.