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

Collapse multiple leading and trailing whitespaces into a single whitespace / Does not collapse multiple   #16

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
251 changes: 245 additions & 6 deletions __snapshots__/hbs-minifier-plugin.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,244 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`11. does not collapse multiple   textNode into a single whitespace 1`] = `
Object {
"blockParams": Array [],
"body": Array [
Object {
"attributes": Array [],
"blockParams": Array [],
"children": Array [
Object {
"chars": "1",
"loc": Object {
"end": Object {
"column": 7,
"line": 1,
},
"source": null,
"start": Object {
"column": 6,
"line": 1,
},
},
"type": "TextNode",
},
],
"comments": Array [],
"loc": Object {
"end": Object {
"column": 14,
"line": 1,
},
"source": null,
"start": Object {
"column": 0,
"line": 1,
},
},
"modifiers": Array [],
"tag": "span",
"type": "ElementNode",
},
Object {
"chars": "  ",
"loc": Object {
"end": Object {
"column": 26,
"line": 1,
},
"source": null,
"start": Object {
"column": 14,
"line": 1,
},
},
"type": "TextNode",
},
Object {
"attributes": Array [],
"blockParams": Array [],
"children": Array [
Object {
"chars": "2",
"loc": Object {
"end": Object {
"column": 33,
"line": 1,
},
"source": null,
"start": Object {
"column": 32,
"line": 1,
},
},
"type": "TextNode",
},
],
"comments": Array [],
"loc": Object {
"end": Object {
"column": 40,
"line": 1,
},
"source": null,
"start": Object {
"column": 26,
"line": 1,
},
},
"modifiers": Array [],
"tag": "span",
"type": "ElementNode",
},
],
"loc": Object {
"end": Object {
"column": 40,
"line": 1,
},
"source": null,
"start": Object {
"column": 0,
"line": 1,
},
},
"type": "Program",
}
`;

exports[`11. does not collapse multiple &nbsp; textNode into a single whitespace 2`] = `"<span>1</span>  <span>2</span>"`;

exports[`12. does not collapse &nbsp; surrounding a text content into a single whitespace 1`] = `
Object {
"blockParams": Array [],
"body": Array [
Object {
"attributes": Array [],
"blockParams": Array [],
"children": Array [
Object {
"attributes": Array [],
"blockParams": Array [],
"children": Array [
Object {
"chars": "  1  ",
"loc": Object {
"end": Object {
"column": 28,
"line": 2,
},
"source": null,
"start": Object {
"column": 8,
"line": 2,
},
},
"type": "TextNode",
},
],
"comments": Array [],
"loc": Object {
"end": Object {
"column": 35,
"line": 2,
},
"source": null,
"start": Object {
"column": 2,
"line": 2,
},
},
"modifiers": Array [],
"tag": "span",
"type": "ElementNode",
},
Object {
"chars": " ",
"loc": Object {
"end": Object {
"column": 2,
"line": 3,
},
"source": null,
"start": Object {
"column": 35,
"line": 2,
},
},
"type": "TextNode",
},
Object {
"attributes": Array [],
"blockParams": Array [],
"children": Array [
Object {
"chars": " 2 ",
"loc": Object {
"end": Object {
"column": 13,
"line": 3,
},
"source": null,
"start": Object {
"column": 8,
"line": 3,
},
},
"type": "TextNode",
},
],
"comments": Array [],
"loc": Object {
"end": Object {
"column": 20,
"line": 3,
},
"source": null,
"start": Object {
"column": 2,
"line": 3,
},
},
"modifiers": Array [],
"tag": "span",
"type": "ElementNode",
},
],
"comments": Array [],
"loc": Object {
"end": Object {
"column": 6,
"line": 4,
},
"source": null,
"start": Object {
"column": 0,
"line": 1,
},
},
"modifiers": Array [],
"tag": "div",
"type": "ElementNode",
},
],
"loc": Object {
"end": Object {
"column": 6,
"line": 4,
},
"source": null,
"start": Object {
"column": 0,
"line": 1,
},
},
"type": "Program",
}
`;

exports[`12. does not collapse &nbsp; surrounding a text content into a single whitespace 2`] = `"<div><span>  1  </span> <span> 2 </span></div>"`;

exports[`collapses whitespace into single space character 1`] = `
Object {
"blockParams": Array [],
Expand Down Expand Up @@ -631,7 +870,7 @@ Object {
"blockParams": Array [],
"children": Array [
Object {
"chars": "x ",
"chars": "x ",
"loc": Object {
"end": Object {
"column": 14,
Expand Down Expand Up @@ -697,7 +936,7 @@ Object {
"type": "MustacheStatement",
},
Object {
"chars": " y ",
"chars": " y ",
"loc": Object {
"end": Object {
"column": 30,
Expand Down Expand Up @@ -744,14 +983,14 @@ Object {
}
`;
exports[`does not strip leading/trailing text from ElementNode nodes 2`] = `"<div>x {{foo}} y </div>"`;
exports[`does not strip leading/trailing text from ElementNode nodes 2`] = `"<div>x {{foo}} y </div>"`;
exports[`does not strip leading/trailing text from Program nodes 1`] = `
Object {
"blockParams": Array [],
"body": Array [
Object {
"chars": "x ",
"chars": "x ",
"loc": Object {
"end": Object {
"column": 9,
Expand Down Expand Up @@ -817,7 +1056,7 @@ Object {
"type": "MustacheStatement",
},
Object {
"chars": " y ",
"chars": " y ",
"loc": Object {
"end": Object {
"column": 25,
Expand Down Expand Up @@ -847,7 +1086,7 @@ Object {
}
`;
exports[`does not strip leading/trailing text from Program nodes 2`] = `"x {{foo}} y "`;
exports[`does not strip leading/trailing text from Program nodes 2`] = `"x {{foo}} y "`;
exports[`strips leading and trailing whitespace from ElementNode nodes 1`] = `
Object {
Expand Down
26 changes: 10 additions & 16 deletions hbs-minifier-plugin.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
'use strict';

/* eslint-env node */
const Util = require('./utils/helpers');
const stripWhiteSpace = Util.stripWhiteSpace;
const isWhitespaceTextNode = Util.isWhitespaceTextNode;
const hasLeadingOrTrailingWhiteSpace = Util.hasLeadingOrTrailingWhiteSpace;
const stripNoMinifyBlocks = Util.stripNoMinifyBlocks;

const WHITESPACE = /^\s+$/;
class HBSMinifierPlugin {

module.exports = class HBSMinifierPlugin {
static createASTPlugin() {
let preStack = [];

let visitor = {
TextNode(node) {
if (preStack.length === 0 && WHITESPACE.test(node.chars)) {
node.chars = ' ';
let chars = node.chars;
if (preStack.length === 0 && hasLeadingOrTrailingWhiteSpace(chars)) {
node.chars = stripWhiteSpace(chars);
}
},

Expand Down Expand Up @@ -92,17 +96,7 @@ module.exports = class HBSMinifierPlugin {

return ast;
}
};

function isWhitespaceTextNode(node) {
return node && node.type === 'TextNode' && WHITESPACE.test(node.chars);
}

function stripNoMinifyBlocks(nodes) {
return nodes.map(node => {
if (node.type === 'BlockStatement' && node.path.original === 'no-minify') {
return node.program.body;
}
return node;
}).reduce((a, b) => a.concat(b), []);
}
module.exports = HBSMinifierPlugin;
11 changes: 11 additions & 0 deletions hbs-minifier-plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ it('does not collapse whitespace inside of {{#no-minify}} tags in other tags', f
assert(`<div>{{#no-minify}} \n\n \n{{/no-minify}}</div>`);
});

it('11. does not collapse multiple &nbsp; textNode into a single whitespace', function() {
assert(`<span>1</span>&nbsp;&nbsp;<span>2</span>`);
});

it('12. does not collapse &nbsp; surrounding a text content into a single whitespace', function() {
assert(`<div>
<span> &nbsp;1&nbsp; </span>
<span> 2 </span>
</div>`);
});

function assert(template) {
let ast = process(template);
expect(ast).toMatchSnapshot();
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"author": "",
"files": [
"hbs-minifier-plugin.js",
"index.js"
"index.js",
"utils"
],
"directories": {
"doc": "doc",
Expand Down
Loading