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

Fixes issue #345 - Whitespace preservation should apply to all nested text #347

Merged
merged 4 commits into from
Aug 3, 2016
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
10 changes: 10 additions & 0 deletions compiler/CodeGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,15 @@ class Generator {
beforeAfterEvent = new GeneratorEvent(node, this);
}

var isWhitespacePreserved = node.isPreserveWhitespace();

if (beforeAfterEvent) {
beforeAfterEvent.isBefore = true;
beforeAfterEvent.node.emit('beforeGenerateCode', beforeAfterEvent);

if (isWhitespacePreserved) {
this.context.beginPreserveWhitespace();
}
}

if (node.getCodeGenerator) {
Expand Down Expand Up @@ -268,6 +274,10 @@ class Generator {
if (beforeAfterEvent) {
beforeAfterEvent.isBefore = false;
beforeAfterEvent.node.emit('afterGenerateCode', beforeAfterEvent);

if (isWhitespacePreserved) {
this.context.endPreserveWhitespace();
}
}

this._currentNode = oldCurrentNode;
Expand Down
14 changes: 13 additions & 1 deletion compiler/CompileContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var macros = require('./util/macros');
var extend = require('raptor-util/extend');
var Walker = require('./Walker');

const FLAG_PRESERVE_WHITESPACE = 'PRESERVE_WHITESPACE';

const deresolveOptions = {
shouldRemoveExt(ext) {
return ext === '.js' || ext === '.json' || ext === '.es6';
Expand Down Expand Up @@ -392,8 +394,18 @@ class CompileContext {
this._preserveWhitespace = preserveWhitespace;
}

beginPreserveWhitespace() {
this.pushFlag(FLAG_PRESERVE_WHITESPACE);
}

endPreserveWhitespace() {
this.popFlag(FLAG_PRESERVE_WHITESPACE);
}

isPreserveWhitespace() {
return this._preserveWhitespace === true;
if (this.isFlagSet(FLAG_PRESERVE_WHITESPACE) || this._preserveWhitespace === true) {
return true;
}
}

setPreserveComments(preserveComments) {
Expand Down
4 changes: 4 additions & 0 deletions docs/language-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,8 @@ __Option 2)__ Disable whitespace removal using the `marko-preserve-whitespace` a
</div>
```

NOTE: When whitespace preservation is enabled, whitespace will be preserved for text at any level below the tag that the `marko-preserve-whitespace` attribute is attached to.

__Option 3)__ Disable _all_ whitespace removal by changing a compiler option

```javascript
Expand All @@ -800,6 +802,8 @@ Adding the `"preserve-whitespace": true` property to a tag definition will resul
}
```

NOTE: When whitespace preservation is enabled, whitespace will be preserved for text at any level below the tag.

# Helpers

Since Marko template files compile into CommonJS modules, any Node.js module can be "imported" into a template for use as a helper module. For example, given the following helper module:
Expand Down
4 changes: 3 additions & 1 deletion test/autotests/render/bodyText/expected.html
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<script><DIV>FOO</DIV></script>
<script>
<DIV>FOO</DIV>
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
This
whitespace
should be
preserved
<div>
This
whitespace
should also be
preserved
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<marko-compiler-options preserve-whitespace/>

<div>
This
whitespace
should be
preserved
<div>
This
whitespace
should also be
preserved
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.templateData = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
This
whitespace
should be
preserved
<div>
This
whitespace
should also be
preserved
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div marko-preserve-whitespace>
This
whitespace
should be
preserved
<div>
This
whitespace
should also be
preserved
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.templateData = {
"name": "World"
};
3 changes: 3 additions & 0 deletions test/autotests/render/whitespace-pre-code/expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<pre><code>// Hello World

console.log('Hello World!');</code></pre>
3 changes: 3 additions & 0 deletions test/autotests/render/whitespace-pre-code/template.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<pre><code>// Hello World

console.log('Hello World!');</code></pre>
3 changes: 3 additions & 0 deletions test/autotests/render/whitespace-pre-code/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.templateData = {
"name": "World"
};