Skip to content

Commit

Permalink
JSX: Regexp optimisation + handle spread operator as a whole
Browse files Browse the repository at this point in the history
  • Loading branch information
Golmote committed Oct 22, 2017
1 parent 8fc1b03 commit 28de4e2
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
8 changes: 4 additions & 4 deletions components/prism-jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
var javascript = Prism.util.clone(Prism.languages.javascript);

Prism.languages.jsx = Prism.languages.extend('markup', javascript);
Prism.languages.jsx.tag.pattern= /<\/?[\w\.:-]+\s*(?:\s+(?:[\w\.:-]+(?:=(?:("|')(\\?[\s\S])*?\1|[^\s'">=]+|(\{[\s\S]*?\})))?|\{\.{3}\w+\}))*\s*\/?>/i;
Prism.languages.jsx.tag.pattern= /<\/?[\w.:-]+\s*(?:\s+(?:[\w\.:-]+(?:=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">=]+|(?:\{[^}]*\})))?|\{\.{3}\w+\}))*\s*\/?>/i;

Prism.languages.jsx.tag.inside['attr-value'].pattern = /=(?!\{)(?:('|")[\s\S]*?(\1)|[^\s>]+)/i;
Prism.languages.jsx.tag.inside['attr-value'].pattern = /=(?!\{)(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">]+)/i;

Prism.languages.insertBefore('inside', 'attr-name', {
'spread': {
pattern: /\{\.{3}\w+\}/,
inside: {
'punctuation': /\{|\}|\./,
'punctuation': /[{}]|\.{3}/,
'attr-value': /\w+/
}
}
}, Prism.languages.jsx.tag);

var jsxExpression = Prism.util.clone(Prism.languages.jsx);

delete jsxExpression.punctuation
delete jsxExpression.punctuation;

jsxExpression = Prism.languages.insertBefore('jsx', 'operator', {
'punctuation': /=(?={)|[{}[\];(),.:]/
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.

4 changes: 1 addition & 3 deletions tests/languages/jsx/tag_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ var myElement = <MyComponent someProperty={true} />;
]],
["spread", [
["punctuation", "{"],
["punctuation", "."],
["punctuation", "."],
["punctuation", "."],
["punctuation", "..."],
["attr-value", "foo"],
["punctuation", "}"]
]],
Expand Down

0 comments on commit 28de4e2

Please sign in to comment.