From 51fa243883459ac5f44171c7b75316466a5cf9a0 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Sat, 23 Nov 2019 18:12:11 +0100 Subject: [PATCH] Fix unintended attribute minification --- .../hbs-minifier-plugin.test.js.snap | 285 ++++++++++++++++++ hbs-minifier-plugin.js | 12 + hbs-minifier-plugin.test.js | 8 + 3 files changed, 305 insertions(+) diff --git a/__snapshots__/hbs-minifier-plugin.test.js.snap b/__snapshots__/hbs-minifier-plugin.test.js.snap index 40079ad4..3db7e698 100644 --- a/__snapshots__/hbs-minifier-plugin.test.js.snap +++ b/__snapshots__/hbs-minifier-plugin.test.js.snap @@ -824,6 +824,101 @@ exports[`HBS Minifier plugin (with @glimmer/syntax v0.35.11) minifies \`tagNames
Box 564, Disneyland
USA
" `; +exports[`HBS Minifier plugin (with @glimmer/syntax v0.35.11) skips whitespace in concatenated attributes 1`] = ` +Program { + "blockParams": Array [], + "body": Array [ + ElementNode { + "attributes": Array [ + AttrNode { + "name": "title", + "value": ConcatStatement { + "parts": Array [ + TextNode { + "chars": " foo ", + }, + MustacheStatement { + "escaped": true, + "hash": Hash { + "pairs": Array [], + }, + "params": Array [ + PathExpression { + "data": false, + "original": "this.bar", + "parts": Array [ + "bar", + ], + "this": true, + }, + StringLiteral { + "original": "BAR", + "value": "BAR", + }, + ], + "path": PathExpression { + "data": false, + "original": "if", + "parts": Array [ + "if", + ], + "this": false, + }, + }, + TextNode { + "chars": " ", + }, + ], + }, + }, + ], + "blockParams": Array [], + "children": Array [], + "comments": Array [], + "modifiers": Array [], + "selfClosing": true, + "tag": "div", + }, + ], +} +`; + +exports[`HBS Minifier plugin (with @glimmer/syntax v0.35.11) skips whitespace in concatenated attributes 2`] = ` +"
+--- +
" +`; + +exports[`HBS Minifier plugin (with @glimmer/syntax v0.35.11) skips whitespace in regular attributes 1`] = ` +Program { + "blockParams": Array [], + "body": Array [ + ElementNode { + "attributes": Array [ + AttrNode { + "name": "title", + "value": TextNode { + "chars": " foo ", + }, + }, + ], + "blockParams": Array [], + "children": Array [], + "comments": Array [], + "modifiers": Array [], + "selfClosing": true, + "tag": "div", + }, + ], +} +`; + +exports[`HBS Minifier plugin (with @glimmer/syntax v0.35.11) skips whitespace in regular attributes 2`] = ` +"
+--- +
" +`; + exports[`HBS Minifier plugin (with @glimmer/syntax v0.35.11) strips leading and trailing whitespace from ElementNode nodes 1`] = ` Program { "blockParams": Array [], @@ -1716,6 +1811,101 @@ exports[`HBS Minifier plugin (with @glimmer/syntax v0.37.1) minifies \`tagNames\
Box 564, Disneyland
USA
" `; +exports[`HBS Minifier plugin (with @glimmer/syntax v0.37.1) skips whitespace in concatenated attributes 1`] = ` +Program { + "blockParams": Array [], + "body": Array [ + ElementNode { + "attributes": Array [ + AttrNode { + "name": "title", + "value": ConcatStatement { + "parts": Array [ + TextNode { + "chars": " foo ", + }, + MustacheStatement { + "escaped": true, + "hash": Hash { + "pairs": Array [], + }, + "params": Array [ + PathExpression { + "data": false, + "original": "this.bar", + "parts": Array [ + "bar", + ], + "this": true, + }, + StringLiteral { + "original": "BAR", + "value": "BAR", + }, + ], + "path": PathExpression { + "data": false, + "original": "if", + "parts": Array [ + "if", + ], + "this": false, + }, + }, + TextNode { + "chars": " ", + }, + ], + }, + }, + ], + "blockParams": Array [], + "children": Array [], + "comments": Array [], + "modifiers": Array [], + "selfClosing": true, + "tag": "div", + }, + ], +} +`; + +exports[`HBS Minifier plugin (with @glimmer/syntax v0.37.1) skips whitespace in concatenated attributes 2`] = ` +"
+--- +
" +`; + +exports[`HBS Minifier plugin (with @glimmer/syntax v0.37.1) skips whitespace in regular attributes 1`] = ` +Program { + "blockParams": Array [], + "body": Array [ + ElementNode { + "attributes": Array [ + AttrNode { + "name": "title", + "value": TextNode { + "chars": " foo ", + }, + }, + ], + "blockParams": Array [], + "children": Array [], + "comments": Array [], + "modifiers": Array [], + "selfClosing": true, + "tag": "div", + }, + ], +} +`; + +exports[`HBS Minifier plugin (with @glimmer/syntax v0.37.1) skips whitespace in regular attributes 2`] = ` +"
+--- +
" +`; + exports[`HBS Minifier plugin (with @glimmer/syntax v0.37.1) strips leading and trailing whitespace from ElementNode nodes 1`] = ` Program { "blockParams": Array [], @@ -2608,6 +2798,101 @@ exports[`HBS Minifier plugin minifies \`tagNames\` that are not specified in .hb
Box 564, Disneyland
USA
" `; +exports[`HBS Minifier plugin skips whitespace in concatenated attributes 1`] = ` +Template { + "blockParams": Array [], + "body": Array [ + ElementNode { + "attributes": Array [ + AttrNode { + "name": "title", + "value": ConcatStatement { + "parts": Array [ + TextNode { + "chars": " foo ", + }, + MustacheStatement { + "escaped": true, + "hash": Hash { + "pairs": Array [], + }, + "params": Array [ + PathExpression { + "data": false, + "original": "this.bar", + "parts": Array [ + "bar", + ], + "this": true, + }, + StringLiteral { + "original": "BAR", + "value": "BAR", + }, + ], + "path": PathExpression { + "data": false, + "original": "if", + "parts": Array [ + "if", + ], + "this": false, + }, + }, + TextNode { + "chars": " ", + }, + ], + }, + }, + ], + "blockParams": Array [], + "children": Array [], + "comments": Array [], + "modifiers": Array [], + "selfClosing": true, + "tag": "div", + }, + ], +} +`; + +exports[`HBS Minifier plugin skips whitespace in concatenated attributes 2`] = ` +"
+--- +
" +`; + +exports[`HBS Minifier plugin skips whitespace in regular attributes 1`] = ` +Template { + "blockParams": Array [], + "body": Array [ + ElementNode { + "attributes": Array [ + AttrNode { + "name": "title", + "value": TextNode { + "chars": " foo ", + }, + }, + ], + "blockParams": Array [], + "children": Array [], + "comments": Array [], + "modifiers": Array [], + "selfClosing": true, + "tag": "div", + }, + ], +} +`; + +exports[`HBS Minifier plugin skips whitespace in regular attributes 2`] = ` +"
+--- +
" +`; + exports[`HBS Minifier plugin strips leading and trailing whitespace from ElementNode nodes 1`] = ` Template { "blockParams": Array [], diff --git a/hbs-minifier-plugin.js b/hbs-minifier-plugin.js index f2ed63bf..d1807e16 100644 --- a/hbs-minifier-plugin.js +++ b/hbs-minifier-plugin.js @@ -29,6 +29,18 @@ function createGlimmerPlugin(config) { } }, + AttrNode: { + enter(node) { + skipStack.push(node); + }, + + exit(node) { + if (skipStack[skipStack.length - 1] === node) { + skipStack.pop(); + } + }, + }, + BlockStatement: { enter(node) { if (shouldSkipBlockStatement(node, config)) { diff --git a/hbs-minifier-plugin.test.js b/hbs-minifier-plugin.test.js index 524a00a9..237b5836 100644 --- a/hbs-minifier-plugin.test.js +++ b/hbs-minifier-plugin.test.js @@ -156,6 +156,14 @@ for (let version of versions) { {{/my-component}}`); }); + it('skips whitespace in regular attributes', function() { + assert(`
`); + }); + + it('skips whitespace in concatenated attributes', function() { + assert(`
`); + }); + function assert(template, config = {}) { let ast = process(template, config); expect(ast).toMatchSnapshot();