Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifies whitespace control and adds an option #492

Merged
merged 1 commit into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion docs/tags/whitespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
`-` character by the braces. When present, all whitespace on that side will be
omitted up to the next tag, magic tag, or non-whitespace character. It also works with [can-stache.tags.unescaped].

The following ensures there is no extra between the second `<pre>`, the output of `{{-this.message-}}`,
The following ensures there is no extra whitespace between the second `<pre>`, the output of `{{-this.message-}}`,
and the `</pre>` closing tag:

```html
Expand Down Expand Up @@ -39,6 +39,52 @@

@param {can-stache/expressions/literal|can-stache/expressions/key-lookup|can-stache/expressions/call|can-stache/expressions/helper} EXPRESSION An expression whose unescaped result is inserted into the page.

@signature `{{--}}`

Whitespace may be omitted without an expression. This is useful between html tags, for example, where it may cause issues in the display of static inline elements.

The following ensures there is no extra whitespace between the inline items:

```html
<my-demo></my-demo>
<style>
.slantycolors {
display: inline-block;
transform: skewX(15deg);
padding: 10px;
color: white;
font-weight: bold;
background: linear-gradient(to top, orange, black, blue);
}
</style>
<script type="module">
import {Component} from "can";

Component.extend({
tag: "my-demo",
view: `
<span class="slantycolors">
orange
</span>
{{--}}
<span class="slantycolors">
black
</span>
{{--}}
<span class="slantycolors">
blue
</span>
<span class="slantycolors">
{{message}}
</span>`,
ViewModel: {
message: {default: "There's no {{--}} before me."}
}
});
</script>
```
@codepen

@body

## Examples
Expand Down Expand Up @@ -122,3 +168,23 @@ would render as:
{{ user.name }}
{{/ if -}}</div>
```

### Special Case

You may want to remove all whitespace between elements without output or comment.

```html
<ul>
{{--}}
<li>Inline Nav Item 1</li>
{{--}}
<li>Inline Nav Item 2</li>
{{--}}
</ul>
```

would render as:

```html
<ul><li>Inline Nav Item 1</li><li>Inline Nav Item 2</li></ul>
```
26 changes: 2 additions & 24 deletions src/mustache_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defineLazyValue(HelperOptions.prototype,"context", function(){
// ## Helpers

var mustacheLineBreakRegExp = /(?:(^|\r?\n)(\s*)(\{\{([\s\S]*)\}\}\}?)([^\S\n\r]*)($|\r?\n))|(\{\{([\s\S]*)\}\}\}?)/g,
mustacheWhitespaceRegExp = /(\s*)(\{\{\{?)(-?)([\s\S]*?)(-?)(\}\}\}?)(\s*)/g,
mustacheWhitespaceRegExp = /\s*\{\{--\}\}\s*|\s*(\{\{\{?)-|-(\}\}\}?)\s*/g,
k = function(){};
var viewInsertSymbol = canSymbol.for("can.viewInsert");

Expand Down Expand Up @@ -522,29 +522,7 @@ var core = {
* @return {String}
*/
cleanWhitespaceControl: function(template) {
return template.replace(mustacheWhitespaceRegExp, function(
whole,
spaceBefore,
bracketBefore,
controlBefore,
expression,
controlAfter,
bracketAfter,
spaceAfter,
matchIndex
) {

if (controlBefore === '-') {
spaceBefore = '';
}

if (controlAfter === '-') {
spaceAfter = '';
}

return spaceBefore + bracketBefore + expression + bracketAfter + spaceAfter;

});
return template.replace(mustacheWhitespaceRegExp, "$1$2");
},
getTemplateById: function(){}
};
Expand Down
12 changes: 12 additions & 0 deletions test/stache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5025,6 +5025,18 @@ function makeTest(name, doc, mutation) {
equal(core.cleanWhitespaceControl(
"<foo>\n\t{{-! comment -}}\n</foo>"),
"<foo>{{! comment }}</foo>");
// right only
equal(core.cleanWhitespaceControl(
"<foo>\n\t{{! comment -}}\n</foo>"),
"<foo>\n\t{{! comment }}</foo>");
// left only
equal(core.cleanWhitespaceControl(
"<foo>\n\t{{-! comment }}\n</foo>"),
"<foo>{{! comment }}\n</foo>");
// both without output
equal(core.cleanWhitespaceControl(
"<foo>\n\t{{--}}\n</foo>"),
"<foo></foo>");

var div = doc.createElement('div');
div.appendChild(stache("<foo>\n\t{{-! comment -}}\n</foo>")());
Expand Down