Skip to content

Commit

Permalink
Markup: Added support for DOM event attributes (#2702)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Mar 5, 2021
1 parent 970674c commit 8dbbbb3
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 85 deletions.
29 changes: 1 addition & 28 deletions components/prism-css.js
Expand Up @@ -49,34 +49,7 @@
var markup = Prism.languages.markup;
if (markup) {
markup.tag.addInlined('style', 'css');

Prism.languages.insertBefore('inside', 'attr-value', {
'style-attr': {
pattern: /(^|["'\s])style\s*=\s*(?:"[^"]*"|'[^']*')/i,
lookbehind: true,
inside: {
'attr-value': {
pattern: /=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+)/,
inside: {
'style': {
pattern: /(["'])[\s\S]+(?=["']$)/,
lookbehind: true,
alias: 'language-css',
inside: Prism.languages.css
},
'punctuation': [
{
pattern: /^=/,
alias: 'attr-equals'
},
/"|'/
]
}
},
'attr-name': /^style/i
}
}
}, markup.tag);
markup.tag.addAttribute('style', 'css');
}

}(Prism));
2 changes: 1 addition & 1 deletion components/prism-css.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions components/prism-javascript.js
Expand Up @@ -96,6 +96,13 @@ Prism.languages.insertBefore('javascript', 'string', {

if (Prism.languages.markup) {
Prism.languages.markup.tag.addInlined('script', 'javascript');

// add attribute support for all DOM events.
// https://developer.mozilla.org/en-US/docs/Web/Events#Standard_events
Prism.languages.markup.tag.addAttribute(
/on(?:abort|blur|change|click|composition(?:end|start|update)|dblclick|error|focus(?:in|out)?|key(?:down|up)|load|mouse(?:down|enter|leave|move|out|over|up)|reset|resize|scroll|select|slotchange|submit|unload|wheel)/.source,
'javascript'
);
}

Prism.languages.js = Prism.languages.javascript;
2 changes: 1 addition & 1 deletion components/prism-javascript.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/prism-jsx.js
Expand Up @@ -38,7 +38,7 @@ Prism.languages.insertBefore('inside', 'attr-name', {
}
}, Prism.languages.jsx.tag);

Prism.languages.insertBefore('inside', 'attr-value',{
Prism.languages.insertBefore('inside', 'special-attr',{
'script': {
// Allow for two levels of nesting
pattern: re(/=<BRACES>/.source),
Expand Down
2 changes: 1 addition & 1 deletion components/prism-jsx.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions components/prism-markup.js
Expand Up @@ -33,6 +33,7 @@ Prism.languages.markup = {
'namespace': /^[^\s>\/:]+:/
}
},
'special-attr': [],
'attr-value': {
pattern: /=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+)/,
inside: {
Expand Down Expand Up @@ -119,6 +120,49 @@ Object.defineProperty(Prism.languages.markup.tag, 'addInlined', {
Prism.languages.insertBefore('markup', 'cdata', def);
}
});
Object.defineProperty(Prism.languages.markup.tag, 'addAttribute', {
/**
* Adds an pattern to highlight languages embedded in HTML attributes.
*
* An example of an inlined language is CSS with `style` attributes.
*
* @param {string} attrName The name of the tag that contains the inlined language. This name will be treated as
* case insensitive.
* @param {string} lang The language key.
* @example
* addAttribute('style', 'css');
*/
value: function (attrName, lang) {
Prism.languages.markup.tag.inside['special-attr'].push({
pattern: RegExp(
/(^|["'\s])/.source + '(?:' + attrName + ')' + /\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))/.source,
'i'
),
lookbehind: true,
inside: {
'attr-name': /^[^\s=]+/,
'attr-value': {
pattern: /=[\s\S]+/,
inside: {
'value': {
pattern: /(=\s*(["']|(?!["'])))\S[\s\S]*(?=\2$)/,
lookbehind: true,
alias: [lang, 'language-' + lang],
inside: Prism.languages[lang]
},
'punctuation': [
{
pattern: /^=/,
alias: 'attr-equals'
},
/"|'/
]
}
}
}
});
}
});

Prism.languages.html = Prism.languages.markup;
Prism.languages.mathml = Prism.languages.markup;
Expand Down

0 comments on commit 8dbbbb3

Please sign in to comment.