Skip to content

Commit

Permalink
Merge pull request #13 from Minstel/Smart_trim_spaces
Browse files Browse the repository at this point in the history
Smart trim spaces
  • Loading branch information
jasny authored May 17, 2017
2 parents 29bd1e7 + d14134b commit 815f556
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 45 deletions.
18 changes: 17 additions & 1 deletion assets/mustache-tidy.js
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,8 @@ var endsWithTag = utils.endsWithTag;
var log = debug.log;
var regs = {
spaces: new RegExp('\\s+', 'g'),
startSpaces: new RegExp('^\\s* '),
endSpaces: new RegExp(' \\s*$'),
tagName: new RegExp('(?:[^"\']+|"[^"]+"|\'[^\']+\')', 'g'),
tag: new RegExp('\\{\\{\\s*([#^/])([^}]*)\\}\\}', 'g')
};
Expand Down Expand Up @@ -1202,7 +1204,7 @@ function mustacheTidy(html, options) {

// Perform tidy on single text DOM node
function tidyTextNode(node, level) {
node.nodeValue = node.nodeValue.trim();
node.nodeValue = smartTrim(node.nodeValue);

// Remove empty text nodes
if (!node.nodeValue.length && node.parentElement) {
Expand Down Expand Up @@ -1389,6 +1391,20 @@ function mustacheTidy(html, options) {

opened.node = closed.node = null;
}

// Trim text, leaving single start and ending spaces
function smartTrim(text) {
var hasStartSpace = !!regs.startSpaces.exec(text);
var hasEndSpace = !!regs.endSpaces.exec(text);

text = text.trim();
if (!text.length) return '';

if (hasStartSpace) text = ' ' + text;
if (hasEndSpace) text = text + ' ';

return text;
}
}


Expand Down
2 changes: 1 addition & 1 deletion assets/mustache-tidy.min.js

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var endsWithTag = utils.endsWithTag;
var log = debug.log;
var regs = {
spaces: new RegExp('\\s+', 'g'),
startSpaces: new RegExp('^\\s* '),
endSpaces: new RegExp(' \\s*$'),
tagName: new RegExp('(?:[^"\']+|"[^"]+"|\'[^\']+\')', 'g'),
tag: new RegExp('\\{\\{\\s*([#^/])([^}]*)\\}\\}', 'g')
};
Expand Down Expand Up @@ -132,7 +134,7 @@ function mustacheTidy(html, options) {

// Perform tidy on single text DOM node
function tidyTextNode(node, level) {
node.nodeValue = node.nodeValue.trim();
node.nodeValue = smartTrim(node.nodeValue);

// Remove empty text nodes
if (!node.nodeValue.length && node.parentElement) {
Expand Down Expand Up @@ -319,4 +321,18 @@ function mustacheTidy(html, options) {

opened.node = closed.node = null;
}

// Trim text, leaving single start and ending spaces
function smartTrim(text) {
var hasStartSpace = !!regs.startSpaces.exec(text);
var hasEndSpace = !!regs.endSpaces.exec(text);

text = text.trim();
if (!text.length) return '';

if (hasStartSpace) text = ' ' + text;
if (hasEndSpace) text = text + ' ';

return text;
}
}
37 changes: 34 additions & 3 deletions spec/base-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Basic html processing', function() {
`;

var result = tidy(input);
expect(result).toBe('<div>Data</div>');
expect(result).toBe('<div> Data </div>');
});

it('should not handle single opening tag', function() {
Expand All @@ -20,7 +20,7 @@ describe('Basic html processing', function() {
`;

var result = tidy(input);
expect(result).toBe('<div>{{#tag}}</div>');
expect(result).toBe('<div> {{#tag}} </div>');
});

it('should not handle single closing tag', function() {
Expand All @@ -31,7 +31,7 @@ describe('Basic html processing', function() {
`;

var result = tidy(input);
expect(result).toBe('<div>{{/tag}}</div>');
expect(result).toBe('<div> {{/tag}} </div>');
});

it('should correctly handle input edge cases', function() {
Expand Down Expand Up @@ -71,4 +71,35 @@ describe('Basic html processing', function() {
result = tidy(input);
expect(result).toBe('data');
});

it('should correctly handle &nbsp; tags', function() {
var input = `
<div>
{{ foo&nbsp; }}
<p>
<span>Data</span>
{{ &nbsp;#bar&nbsp; }}
<span>Data</span>
</p>
<span>Data</span>
{{&nbsp;/bar&nbsp;}}
</div>
`;

var result = tidy(input);
expect(result).toBe(
'<div>' +
' {{ foo&nbsp; }} ' +
'<p>' +
'<span>Data</span>' +
' {{ &nbsp;#bar&nbsp; }} ' +
'<span>Data</span>' +
'{{&nbsp;/bar&nbsp;}}' +
'</p>' +
'{{ &nbsp;#bar&nbsp; }}' +
'<span>Data</span>' +
' {{&nbsp;/bar&nbsp;}} ' +
'</div>'
);
});
});
16 changes: 8 additions & 8 deletions spec/extend-tags-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('Extending tags', function() {
'<div>' +
'<div>' +
'<div>' +
'Data{{#tag}}Data' +
' Data{{#tag}}Data ' +
'{{/tag}}' +
'</div>' +
'{{#tag}}' +
Expand All @@ -43,7 +43,7 @@ describe('Extending tags', function() {
'{{#tag}}' +
'<span>Data</span>' +
'<span>Data</span>' +
'{{/tag}}' +
' {{/tag}} ' +
'</div>'
);
});
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('Extending tags', function() {
var result = tidy(input);
expect(result).toBe(
'<div>' +
'{{#tag}}' +
' {{#tag}} ' +
'<span>Data</span>' +
'<span>Data</span>' +
'{{/tag}}' +
Expand All @@ -87,7 +87,7 @@ describe('Extending tags', function() {
'{{/tag}}' +
'<div>' +
'{{#tag}}' +
'Data{{/tag}}Data' +
' Data{{/tag}}Data ' +
'</div>' +
'</div>' +
'</div>' +
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('Extending tags', function() {
'<div>' +
'<div>' +
'<div>' +
'Data{{#tag}}Data' +
' Data{{#tag}}Data ' +
'{{/tag}}' +
'</div>' +
'{{#tag}}' +
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('Extending tags', function() {
'{{/tag}}' +
'<div>' +
'{{#tag}}' +
'Data{{/tag}}Data' +
' Data{{/tag}}Data ' +
'</div>' +
'</div>' +
'</div>' +
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('Extending tags', function() {
'<div>' +
'<div>' +
'<div>' +
'Data{{#tag}}Data' +
' Data{{#tag}}Data ' +
'{{/tag}}' +
'</div>' +
'{{#tag}}' +
Expand All @@ -227,7 +227,7 @@ describe('Extending tags', function() {
'{{/tag}}' +
'<div>' +
'{{#tag}}' +
'Data{{/tag}}Data' +
' Data{{/tag}}Data ' +
'</div>' +
'</div>' +
'</div>' +
Expand Down
8 changes: 4 additions & 4 deletions spec/fix-table-tags-spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var tidy = require('../');

describe('Extending tags', function() {
describe('Fixing table tags', function() {
it('should fix table tags, when opening tag is out of table, and closing is inside table', function() {
var input = `
{{^tag}}
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('Extending tags', function() {

var result = tidy(input);
expect(result).toBe(
'{{^tag}}' +
'{{^tag}} ' +
'<div>Data</div>' +
'<div>Data</div>' +
'{{/tag}}' +
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('Extending tags', function() {
'{{^tag}}' +
'<div>Data</div>' +
'<div>Data</div>' +
'{{/tag}}'
' {{/tag}} '
);
});

Expand Down Expand Up @@ -191,7 +191,7 @@ describe('Extending tags', function() {
'<th>Data</th>' +
'<th>' +
'<span>' +
'Data' +
' Data ' +
'{{^tag}}' +
'<em>Data</em>' +
'{{/tag}}' +
Expand Down
12 changes: 6 additions & 6 deletions spec/improve-tags-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Improving tags', function() {
var result = tidy(input);
expect(result).toBe(
'<div>' +
'{{^outer}}' +
' {{^outer}} ' +
'{{#foo}}' +
'{{^bar}}' +
'{{#tag}}' +
Expand All @@ -38,8 +38,8 @@ describe('Improving tags', function() {
'{{/tag}}' +
'{{/}}' +
'{{/foo}}' +
'{{/}}' +
'Data' +
' {{/}} ' +
' Data ' +
'</div>'
);
});
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('Improving tags', function() {
'</div>' +
'</div>' +
'{{/tag}}' +
'Data' +
' Data ' +
'</div>'
);
});
Expand Down Expand Up @@ -140,7 +140,7 @@ describe('Improving tags', function() {
'<div>' +
'<div>' +
'<div>' +
'{{^tag}}Data 1{{/tag}}{{#tag}}Data 2{{/tag}}' +
' {{^tag}}Data 1{{/tag}}{{#tag}}Data 2{{/tag}} ' +
'</div>' +
'</div>' +
'</div>'
Expand All @@ -163,7 +163,7 @@ describe('Improving tags', function() {
'<div>' +
'<div>' +
'<div>' +
'{{#tag}}Data 1{{/tag}}{{#foo}}Data 2{{/foo}}' +
' {{#tag}}Data 1{{/tag}}{{#foo}}Data 2{{/foo}} ' +
'</div>' +
'</div>' +
'</div>'
Expand Down
10 changes: 5 additions & 5 deletions spec/move-tags-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Moving tags', function() {
'</div>' +
'</div>' +
'</div>' +
'{{/tag}}'
' {{/tag}} '
);
});

Expand All @@ -41,7 +41,7 @@ describe('Moving tags', function() {

var result = tidy(input);
expect(result).toBe(
'{{#tag}}' +
' {{#tag}} ' +
'<div>' +
'<div>' +
'<div>' +
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('Moving tags', function() {
expect(result).toBe(
'<div>' +
'<div>' +
'Data{{#tag}}' +
' Data {{#tag}}' +
'<div>' +
'<p>Data</p>' +
'</div>' +
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('Moving tags', function() {
'<div>' +
'<p>Data</p>' +
'</div>' +
'{{/tag}}Data' +
'{{/tag}} Data ' +
'</div>' +
'</div>'
);
Expand Down Expand Up @@ -168,7 +168,7 @@ describe('Moving tags', function() {
'</div>' +
'</div>' +
'{{#tag}}' +
'Data' +
' Data ' +
'{{/tag}}' +
'<div>' +
'<div>' +
Expand Down
Loading

0 comments on commit 815f556

Please sign in to comment.