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

Fix unintended attribute minification #177

Merged
merged 1 commit into from
Nov 23, 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
285 changes: 285 additions & 0 deletions __snapshots__/hbs-minifier-plugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,101 @@ exports[`HBS Minifier plugin (with @glimmer/syntax v0.35.11) minifies \`tagNames
<section> Box 564, <b> Disneyland </b> <br> <u> USA </u></section>"
`;

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`] = `
"<div title=\\" foo {{if this.bar \\"BAR\\"}} \\" />
---
<div title=\\" foo {{if this.bar \\"BAR\\"}} \\"></div>"
`;

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`] = `
"<div title=\\" foo \\" />
---
<div title=\\" foo \\"></div>"
`;

exports[`HBS Minifier plugin (with @glimmer/syntax v0.35.11) strips leading and trailing whitespace from ElementNode nodes 1`] = `
Program {
"blockParams": Array [],
Expand Down Expand Up @@ -1716,6 +1811,101 @@ exports[`HBS Minifier plugin (with @glimmer/syntax v0.37.1) minifies \`tagNames\
<section> Box 564, <b> Disneyland </b> <br> <u> USA </u></section>"
`;

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`] = `
"<div title=\\" foo {{if this.bar \\"BAR\\"}} \\" />
---
<div title=\\" foo {{if this.bar \\"BAR\\"}} \\"></div>"
`;

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`] = `
"<div title=\\" foo \\" />
---
<div title=\\" foo \\"></div>"
`;

exports[`HBS Minifier plugin (with @glimmer/syntax v0.37.1) strips leading and trailing whitespace from ElementNode nodes 1`] = `
Program {
"blockParams": Array [],
Expand Down Expand Up @@ -2608,6 +2798,101 @@ exports[`HBS Minifier plugin minifies \`tagNames\` that are not specified in .hb
<section> Box 564, <b> Disneyland </b> <br> <u> USA </u></section>"
`;

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`] = `
"<div title=\\" foo {{if this.bar \\"BAR\\"}} \\" />
---
<div title=\\" foo {{if this.bar \\"BAR\\"}} \\"></div>"
`;

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`] = `
"<div title=\\" foo \\" />
---
<div title=\\" foo \\"></div>"
`;

exports[`HBS Minifier plugin strips leading and trailing whitespace from ElementNode nodes 1`] = `
Template {
"blockParams": Array [],
Expand Down
12 changes: 12 additions & 0 deletions hbs-minifier-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
8 changes: 8 additions & 0 deletions hbs-minifier-plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ for (let version of versions) {
{{/my-component}}`);
});

it('skips whitespace in regular attributes', function() {
assert(`<div title=" foo " />`);
});

it('skips whitespace in concatenated attributes', function() {
assert(`<div title=" foo {{if this.bar "BAR"}} " />`);
});

function assert(template, config = {}) {
let ast = process(template, config);
expect(ast).toMatchSnapshot();
Expand Down