Skip to content

Commit

Permalink
Handlebars: Added support for : and improved the variable pattern (
Browse files Browse the repository at this point in the history
…#2172)

This adds support for `:` as punctuation and removes the `.` from the list of allowed characters in the `variable` pattern as it is impossible to match because of the `punctuation` pattern.
  • Loading branch information
lifeart authored and RunDevelopment committed Jan 10, 2020
1 parent 543f04d commit ef4d29d
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 15 deletions.
4 changes: 2 additions & 2 deletions components/prism-handlebars.js
Expand Up @@ -21,8 +21,8 @@
variable: /[\s\S]+/
}
},
'punctuation': /[!"#%&'()*+,.\/;<=>@\[\\\]^`{|}~]/,
'variable': /[^!"#%&'()*+,.\/;<=>@\[\\\]^`{|}~\s]+/
'punctuation': /[!"#%&':()*+,.\/;<=>@\[\\\]^`{|}~]/,
'variable': /[^!"#%&'()*+,\/;<=>@\[\\\]^`{|}~\s]+/
};

Prism.hooks.add('before-tokenize', function(env) {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-handlebars.min.js

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

31 changes: 19 additions & 12 deletions tests/languages/handlebars/block_feature.test
@@ -1,21 +1,28 @@
{{#each comments}}{{/each}}
{{~#if isActive~}}{{~/if~}}
{{#let a as |b|}}{{/let}}

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

[
["handlebars", [
["delimiter", "{{"], ["block", "#each"], ["variable", "comments"], ["delimiter", "}}"]
]],
["handlebars", [
["delimiter", "{{"], ["block", "/each"], ["delimiter", "}}"]
]],
["handlebars", [
["delimiter", "{{"], ["punctuation", "~"], ["block", "#if"], ["variable", "isActive"], ["punctuation", "~"], ["delimiter", "}}"]
]],
["handlebars", [
["delimiter", "{{"], ["punctuation", "~"], ["block", "/if"], ["punctuation", "~"], ["delimiter", "}}"]
]]
["handlebars",[
["delimiter","{{"],["block","#each"],["variable","comments"],["delimiter","}}"]]
],
["handlebars",[
["delimiter","{{"],["block","/each"],["delimiter","}}"]]
],
["handlebars",[
["delimiter","{{"],["punctuation","~"],["block","#if"],["variable","isActive"],["punctuation","~"],["delimiter","}}"]]
],
["handlebars",[
["delimiter","{{"],["punctuation","~"],["block","/if"],["punctuation","~"],["delimiter","}}"]]
],
["handlebars",[
["delimiter","{{"],["block","#let"],["variable","a"],["variable","as"],["punctuation","|"],["variable","b"],["punctuation","|"],["delimiter","}}"]]
],
["handlebars",[
["delimiter","{{"],["block","/let"],["delimiter","}}"]]
]
]

----------------------------------------------------
Expand Down
273 changes: 273 additions & 0 deletions tests/languages/handlebars/handlebars_in_html_feature.test
@@ -0,0 +1,273 @@
{{aUserModel.name}}
<div>{{listOfUsers.firstObject.name}}</div>
{{if name "I have a name" "I have no name"}}
<span data-has-name={{if name true}}></span>
<div {{on "click" this.hello}} />
----------------------------------------------------

[
[
"handlebars",
[
[
"delimiter",
"{{"
],
[
"variable",
"aUserModel"
],
[
"punctuation",
"."
],
[
"variable",
"name"
],
[
"delimiter",
"}}"
]
]
],
[
"tag",
[
[
"tag",
[
[
"punctuation",
"<"
],
"div"
]
],
[
"punctuation",
">"
]
]
],
[
"handlebars",
[
[
"delimiter",
"{{"
],
[
"variable",
"listOfUsers"
],
[
"punctuation",
"."
],
[
"variable",
"firstObject"
],
[
"punctuation",
"."
],
[
"variable",
"name"
],
[
"delimiter",
"}}"
]
]
],
[
"tag",
[
[
"tag",
[
[
"punctuation",
"</"
],
"div"
]
],
[
"punctuation",
">"
]
]
],
[
"handlebars",
[
[
"delimiter",
"{{"
],
[
"variable",
"if"
],
[
"variable",
"name"
],
[
"string",
"\"I have a name\""
],
[
"string",
"\"I have no name\""
],
[
"delimiter",
"}}"
]
]
],
[
"tag",
[
[
"tag",
[
[
"punctuation",
"<"
],
"span"
]
],
[
"attr-name",
[
"data-has-name"
]
],
[
"attr-value",
[
[
"punctuation",
"="
],
[
"handlebars",
[
[
"delimiter",
"{{"
],
[
"variable",
"if"
],
[
"variable",
"name"
],
[
"boolean",
"true"
],
[
"delimiter",
"}}"
]
]
]
]
],
[
"punctuation",
">"
]
]
],
[
"tag",
[
[
"tag",
[
[
"punctuation",
"</"
],
"span"
]
],
[
"punctuation",
">"
]
]
],
[
"tag",
[
[
"tag",
[
[
"punctuation",
"<"
],
"div"
]
],
[
"attr-name",
[
[
"handlebars",
[
[
"delimiter",
"{{"
],
[
"variable",
"on"
],
[
"string",
"\"click\""
],
[
"variable",
"this"
],
[
"punctuation",
"."
],
[
"variable",
"hello"
],
[
"delimiter",
"}}"
]
]
]
]
],
[
"punctuation",
"/>"
]
]
]
]

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

Checks for handlebars filter in Html.
31 changes: 31 additions & 0 deletions tests/languages/handlebars/punctuation_feature.test
@@ -0,0 +1,31 @@
{{~#if isActive~}}{{~/if~}}
{{:slot-name}}
----------------------------------------------------

[
["handlebars", [
["delimiter", "{{"],
["punctuation", "~"],
["block", "#if"],
["variable", "isActive"],
["punctuation", "~"],
["delimiter", "}}"]
]],
["handlebars", [
["delimiter", "{{"],
["punctuation", "~"],
["block", "/if"],
["punctuation", "~"],
["delimiter", "}}"]
]],
["handlebars", [
["delimiter", "{{"],
["punctuation", ":"],
["variable", "slot-name"],
["delimiter", "}}"]
]]
]

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

Checks for punctuation.
21 changes: 21 additions & 0 deletions tests/languages/handlebars/variable_feature.test
@@ -0,0 +1,21 @@
{{comments}}
{{@comments}}
{{this.comments}}

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

[
["handlebars",[
["delimiter","{{"],["variable","comments"],["delimiter","}}"]]
],
["handlebars",[
["delimiter","{{"],["punctuation","@"],["variable","comments"],["delimiter","}}"]]
],
["handlebars",[
["delimiter","{{"],["variable","this"],["punctuation","."],["variable","comments"],["delimiter","}}"]]
]
]

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

Checks for variable.

0 comments on commit ef4d29d

Please sign in to comment.