Skip to content

Commit

Permalink
feat: avoid two closing curlies in a sequence
Browse files Browse the repository at this point in the history
fix #16
  • Loading branch information
revelt committed Jun 6, 2021
1 parent 5093502 commit b7293b0
Show file tree
Hide file tree
Showing 8 changed files with 390 additions and 155 deletions.
2 changes: 1 addition & 1 deletion packages/html-crush/coverage/coverage-summary.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"total":{"lines":{"total":342,"covered":319,"skipped":0,"pct":93.27},"statements":{"total":348,"covered":325,"skipped":0,"pct":93.39},"functions":{"total":10,"covered":10,"skipped":0,"pct":100},"branches":{"total":616,"covered":569,"skipped":0,"pct":92.37}}}
{"total":{"lines":{"total":352,"covered":328,"skipped":0,"pct":93.18},"statements":{"total":358,"covered":334,"skipped":0,"pct":93.3},"functions":{"total":10,"covered":10,"skipped":0,"pct":100},"branches":{"total":636,"covered":589,"skipped":0,"pct":92.61}}}
15 changes: 15 additions & 0 deletions packages/html-crush/dist/html-crush.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ function crush(str, originalOpts) {
}
}
cpl++;
if (!doNothing && withinStyleTag && str[i] === "}" && str[i - 1] === "}") {
if (countCharactersPerLine >= opts.lineLengthLimit) {
finalIndexesToDelete.push(i, i, lineEnding);
} else {
stageFrom = i;
stageTo = i;
stageAdd = " ";
}
}
if (doNothing && typeof doNothing === "number" && i >= doNothing) {
doNothing = undefined;
}
Expand Down Expand Up @@ -350,6 +359,9 @@ function crush(str, originalOpts) {
countCharactersPerLine -= stringLeftRight.right(str, i) - i + 1;
}
}
if (withinStyleTag && str[i] === "}" && whitespaceStartedAt && str[whitespaceStartedAt - 1] === "}") {
whatToAdd = " ";
}
if (whatToAdd && whatToAdd.length) {
countCharactersPerLine += 1;
}
Expand Down Expand Up @@ -552,6 +564,9 @@ function crush(str, originalOpts) {
if (str[i] === "<" && leftTagName !== null) {
leftTagName = null;
}
if (withinStyleTag && str[i] === "{" && str[i + 1] === "{" && str.indexOf("}}") !== -1) {
doNothing = str.indexOf("}}") + 2;
}
}
if (finalIndexesToDelete.current()) {
var ranges = finalIndexesToDelete.current();
Expand Down
36 changes: 36 additions & 0 deletions packages/html-crush/dist/html-crush.dev.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ class Ranges {
this.opts = opts;
this.ranges = [];
}
ranges;
opts;
add(originalFrom, originalTo, addVal) {
if (originalFrom == null && originalTo == null) {
return;
Expand Down Expand Up @@ -3085,6 +3087,25 @@ function crush(str, originalOpts) {
}
// count characters-per-line
cpl++;
// catch the sequence of two closing curly braces
// ███████████████████████████████████████
// MUST BE BEFORE doNothing is toggled off because of
// @media screen{div{color:{{brandWhite}}}}
// ^
// imagine we're here
if (!doNothing &&
withinStyleTag &&
str[i] === "}" &&
str[i - 1] === "}") {
if (countCharactersPerLine >= opts.lineLengthLimit) {
finalIndexesToDelete.push(i, i, lineEnding);
}
else {
stageFrom = i;
stageTo = i;
stageAdd = " ";
}
}
// turn off doNothing if marker passed
// ███████████████████████████████████████
if (doNothing && typeof doNothing === "number" && i >= doNothing) {
Expand Down Expand Up @@ -3507,6 +3528,13 @@ function crush(str, originalOpts) {
countCharactersPerLine -= right(str, i) - i + 1;
}
}
// tend double closing curlies in sequence
if (withinStyleTag &&
str[i] === "}" &&
whitespaceStartedAt &&
str[whitespaceStartedAt - 1] === "}") {
whatToAdd = " ";
}
if (whatToAdd && whatToAdd.length) {
countCharactersPerLine += 1;
}
Expand Down Expand Up @@ -3956,6 +3984,14 @@ function crush(str, originalOpts) {
// reset it after use
leftTagName = null;
}
// catch Jinja/Nunjucks two opening curlies and jump to the closing ones if latter exists
// ███████████████████████████████████████
if (withinStyleTag &&
str[i] === "{" &&
str[i + 1] === "{" &&
str.indexOf("}}") !== -1) {
doNothing = str.indexOf("}}") + 2;
}
//
//
//
Expand Down
15 changes: 15 additions & 0 deletions packages/html-crush/dist/html-crush.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ function crush(str, originalOpts) {
}
}
cpl++;
if (!doNothing && withinStyleTag && str[i] === "}" && str[i - 1] === "}") {
if (countCharactersPerLine >= opts.lineLengthLimit) {
finalIndexesToDelete.push(i, i, lineEnding);
} else {
stageFrom = i;
stageTo = i;
stageAdd = " ";
}
}
if (doNothing && typeof doNothing === "number" && i >= doNothing) {
doNothing = undefined;
}
Expand Down Expand Up @@ -327,6 +336,9 @@ function crush(str, originalOpts) {
countCharactersPerLine -= right(str, i) - i + 1;
}
}
if (withinStyleTag && str[i] === "}" && whitespaceStartedAt && str[whitespaceStartedAt - 1] === "}") {
whatToAdd = " ";
}
if (whatToAdd && whatToAdd.length) {
countCharactersPerLine += 1;
}
Expand Down Expand Up @@ -521,6 +533,9 @@ function crush(str, originalOpts) {
if (str[i] === "<" && leftTagName !== null) {
leftTagName = null;
}
if (withinStyleTag && str[i] === "{" && str[i + 1] === "{" && str.indexOf("}}") !== -1) {
doNothing = str.indexOf("}}") + 2;
}
}
if (finalIndexesToDelete.current()) {
const ranges = finalIndexesToDelete.current();
Expand Down
4 changes: 2 additions & 2 deletions packages/html-crush/dist/html-crush.umd.js

Large diffs are not rendered by default.

Loading

0 comments on commit b7293b0

Please sign in to comment.