Skip to content

Commit

Permalink
replace result.sels with didRetain(sel) hook. close #46.
Browse files Browse the repository at this point in the history
  • Loading branch information
leeoniya committed Apr 29, 2020
1 parent 515b693 commit fd9b383
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 32 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,22 +286,22 @@ let htmlB = `
// whitelist
let whitelist = new Set();

function didRetain(sel) {
whitelist.add(sel);
}

let resA = dropcss({
css,
html: htmlA,
didRetain,
});

// accumulate retained A selectors
resA.sels.forEach(sel => whitelist.add(sel));

let resB = dropcss({
css,
html: htmlB,
didRetain,
});

// accumulate retained B selectors
resB.sels.forEach(sel => whitelist.add(sel));

// final purge relying only on accumulated whitelist
let cleaned = dropcss({
html: '',
Expand Down
14 changes: 6 additions & 8 deletions dist/dropcss.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ function stripEmptyAts(css) {
return css.replace(/@[a-z-]+[^{]+\{\s*\}/gm, '');
}

function generate(tokens, kept) {
function generate(tokens, didRetain) {
var out = '', lastSelsLen = 0;

for (var i = 0; i < tokens.length; i++) {
Expand All @@ -400,7 +400,7 @@ function generate(tokens, kept) {
lastSelsLen = sels.length;

if (lastSelsLen > 0) {
sels.forEach(function (s) { return kept.add(s); });
sels.forEach(didRetain);
out += sels.join();
}
break;
Expand Down Expand Up @@ -961,14 +961,15 @@ function stripNonAssertablePseudos(sel) {
.replace(/:[a-z-]+\(\)/gm, '');
}

var drop = function (sel) { return true; };
var retTrue = function (sel) { return true; };

function dropcss(opts) {

// {nodes, tag, class, id}
var H = _export_parse_(opts.html, !opts.keepText);

var shouldDrop = opts.shouldDrop || drop;
var shouldDrop = opts.shouldDrop || retTrue;
var didRetain = opts.didRetain || retTrue;

var tokens = parse(opts.css);

Expand Down Expand Up @@ -1066,15 +1067,12 @@ function dropcss(opts) {
}
}

var kept = new Set();

var out = generate(tokens, kept);
var out = generate(tokens, didRetain);

out = postProc$1(out, shouldDrop);

return {
css: stripEmptyAts(out),
sels: kept,
};
}

Expand Down
14 changes: 6 additions & 8 deletions dist/dropcss.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ var dropcss = (function () {
return css.replace(/@[a-z-]+[^{]+\{\s*\}/gm, '');
}

function generate(tokens, kept) {
function generate(tokens, didRetain) {
var out = '', lastSelsLen = 0;

for (var i = 0; i < tokens.length; i++) {
Expand All @@ -401,7 +401,7 @@ var dropcss = (function () {
lastSelsLen = sels.length;

if (lastSelsLen > 0) {
sels.forEach(function (s) { return kept.add(s); });
sels.forEach(didRetain);
out += sels.join();
}
break;
Expand Down Expand Up @@ -962,14 +962,15 @@ var dropcss = (function () {
.replace(/:[a-z-]+\(\)/gm, '');
}

var drop = function (sel) { return true; };
var retTrue = function (sel) { return true; };

function dropcss(opts) {

// {nodes, tag, class, id}
var H = _export_parse_(opts.html, !opts.keepText);

var shouldDrop = opts.shouldDrop || drop;
var shouldDrop = opts.shouldDrop || retTrue;
var didRetain = opts.didRetain || retTrue;

var tokens = parse(opts.css);

Expand Down Expand Up @@ -1067,15 +1068,12 @@ var dropcss = (function () {
}
}

var kept = new Set();

var out = generate(tokens, kept);
var out = generate(tokens, didRetain);

out = postProc$1(out, shouldDrop);

return {
css: stripEmptyAts(out),
sels: kept,
};
}

Expand Down
2 changes: 1 addition & 1 deletion dist/dropcss.iife.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"devDependencies": {
"mocha": "^7.1.2",
"nyc": "^15.0.1",
"rollup": "^2.7.3",
"rollup": "^2.7.4",
"rollup-plugin-buble": "^0.19.8",
"rollup-plugin-cjs-es": "^1.0.0",
"rollup-plugin-terser": "^5.3.0"
Expand Down
4 changes: 2 additions & 2 deletions src/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function stripEmptyAts(css) {
return css.replace(/@[a-z-]+[^{]+\{\s*\}/gm, '');
}

function generate(tokens, kept) {
function generate(tokens, didRetain) {
let out = '', lastSelsLen = 0;

for (let i = 0; i < tokens.length; i++) {
Expand All @@ -197,7 +197,7 @@ function generate(tokens, kept) {
lastSelsLen = sels.length;

if (lastSelsLen > 0) {
sels.forEach(s => kept.add(s));
sels.forEach(didRetain);
out += sels.join();
}
break;
Expand Down
10 changes: 4 additions & 6 deletions src/dropcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function stripNonAssertablePseudos(sel) {
.replace(/:[a-z-]+\(\)/gm, '');
}

const drop = sel => true;
const retTrue = sel => true;

function dropcss(opts) {
let log, START;
Expand All @@ -34,7 +34,8 @@ function dropcss(opts) {

LOGGING && log.push([+new Date() - START, 'HTML parsed & processed']);

const shouldDrop = opts.shouldDrop || drop;
const shouldDrop = opts.shouldDrop || retTrue;
const didRetain = opts.didRetain || retTrue;

let tokens = parseCSS(opts.css);

Expand Down Expand Up @@ -138,9 +139,7 @@ function dropcss(opts) {

LOGGING && log.push([+new Date() - START, 'Context-aware second pass']);

let kept = new Set();

let out = generateCSS(tokens, kept);
let out = generateCSS(tokens, didRetain);

LOGGING && log.push([+new Date() - START, 'Generate output']);

Expand All @@ -150,7 +149,6 @@ function dropcss(opts) {

return {
css: stripEmptyAts(out),
sels: kept,
};
}

Expand Down

0 comments on commit fd9b383

Please sign in to comment.