From 2e6d17131bc8ffabfaac735e268046ee18f81b61 Mon Sep 17 00:00:00 2001 From: Nick Downing Date: Sat, 17 Nov 2018 21:44:49 +1100 Subject: [PATCH] Add embedded ability, tokenizer starts at a specified input position just after an opening brace, and quits just before a closing brace, reporting the position --- .gitignore | 2 ++ lib/clean.js | 31 ++++++++++++++++++++++++++----- lib/tokenizer/tokenize.js | 13 +++++++++++-- package.json | 12 +++++++----- 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index c0d9f310..89397bab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /cleancss-browser.js /node_modules /npm-debug.log +/yarn.lock +/yarn-error.log diff --git a/lib/clean.js b/lib/clean.js index 8cdb4b79..d1c4d43b 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -61,24 +61,40 @@ CleanCSS.process = function (input, opts) { }); }; - CleanCSS.prototype.minify = function (input, maybeSourceMap, maybeCallback) { var options = this.options; if (options.returnPromise) { return new Promise(function (resolve, reject) { - minify(input, options, maybeSourceMap, function (errors, output) { + minify(input, -1, options, maybeSourceMap, function (errors, output) { return errors ? reject(errors) : resolve(output); }); }); } else { - return minify(input, options, maybeSourceMap, maybeCallback); + return minify(input, -1, options, maybeSourceMap, maybeCallback); } }; -function minify(input, options, maybeSourceMap, maybeCallback) { +CleanCSS.prototype.minifyEmbedded = function (input, embeddedStart, maybeSourceMap, maybeCallback) { + var options = this.options; + + if (options.returnPromise) { + return new Promise(function (resolve, reject) { + minify(input, embeddedStart, options, maybeSourceMap, function (errors, output) { + return errors ? + reject(errors) : + resolve(output); + }); + }); + } else { + return minify(input, embeddedStart, options, maybeSourceMap, maybeCallback); + } +}; + + +function minify(input, embeddedStart, options, maybeSourceMap, maybeCallback) { var sourceMap = typeof maybeSourceMap != 'function' ? maybeSourceMap : null; @@ -96,6 +112,8 @@ function minify(input, options, maybeSourceMap, maybeCallback) { cache: { specificity: {} }, + embeddedStart: embeddedStart, // Nick + embeddedEnd: -1, // Nick errors: [], inlinedStylesheets: [], inputSourceMapTracker: inputSourceMapTracker(), @@ -151,7 +169,10 @@ function optimize(tokens, context) { } function withMetadata(output, context) { - output.stats = calculateStatsFrom(output.styles, context); + if (Object.prototype.hasOwnProperty.call(output, 'styles')) // Nick + output.stats = calculateStatsFrom(output.styles, context); + output.embeddedStart = context.embeddedStart; // Nick + output.embeddedEnd = context.embeddedEnd; // Nick output.errors = context.errors; output.inlinedStylesheets = context.inlinedStylesheets; output.warnings = context.warnings; diff --git a/lib/tokenizer/tokenize.js b/lib/tokenizer/tokenize.js index 39c9e67b..f5ef63ca 100644 --- a/lib/tokenizer/tokenize.js +++ b/lib/tokenizer/tokenize.js @@ -1,3 +1,4 @@ +var assert = require('assert'); // Nick var Marker = require('./marker'); var Token = require('./token'); @@ -70,7 +71,10 @@ function tokenize(source, externalContext) { source: externalContext.source || undefined, line: 1, column: 0, - index: 0 + index: //Nick 0 + externalContext.embeddedStart == -1 ? + 0 : + externalContext.embeddedStart } }; @@ -414,7 +418,12 @@ function intoTokens(source, externalContext, internalContext, isNested) { level = levels.pop(); seekingValue = false; - } else if (character == Marker.CLOSE_CURLY_BRACKET && level == Level.BLOCK && !isNested && position.index <= source.length - 1) { + } else if (character == Marker.CLOSE_CURLY_BRACKET && level == Level.BLOCK && !isNested /*&& position.index <= source.length - 1*/) { + assert(position.index <= source.length - 1); // Nick + if (externalContext.embeddedStart != -1) { + externalContext.embeddedEnd = position.index; + break; + } // stray close brace at block level, e.g. a{color:red}color:blue}<-- externalContext.warnings.push('Unexpected \'}\' at ' + formatPosition([position.line, position.column, position.source]) + '.'); buffer.push(character); diff --git a/package.json b/package.json index 1de9bc54..4b83240c 100644 --- a/package.json +++ b/package.json @@ -1,20 +1,21 @@ { - "name": "clean-css", + "name": "@ndcode/clean-css", "version": "4.2.1", "author": "Jakub Pawlowicz (http://twitter.com/jakubpawlowicz)", - "description": "A well-tested CSS minifier", + "maintainer": "Nick Downing ", + "description": "A well-tested CSS minifier, modified to add NDCODE hooks.", "license": "MIT", "keywords": [ "css", "minifier" ], - "homepage": "https://github.com/jakubpawlowicz/clean-css", + "homepage": "https://www.ndcode.org", "repository": { "type": "git", - "url": "https://github.com/jakubpawlowicz/clean-css.git" + "url": "https://git.ndcode.org/public/clean-css.git" }, "bugs": { - "url": "https://github.com/jakubpawlowicz/clean-css/issues" + "email": "nick@ndcode.org" }, "main": "index.js", "files": [ @@ -31,6 +32,7 @@ "test": "vows" }, "dependencies": { + "assert": "^1.4.1", "source-map": "~0.6.0" }, "devDependencies": { -- 2.34.1