Add embedded ability, tokenizer starts at a specified input position just after an...
authorNick Downing <nick@ndcode.org>
Sat, 17 Nov 2018 10:44:49 +0000 (21:44 +1100)
committerNick Downing <nick@ndcode.org>
Sun, 18 Nov 2018 12:40:49 +0000 (23:40 +1100)
.gitignore
lib/clean.js
lib/tokenizer/tokenize.js
package.json

index c0d9f31..89397ba 100644 (file)
@@ -1,3 +1,5 @@
 /cleancss-browser.js
 /node_modules
 /npm-debug.log
+/yarn.lock
+/yarn-error.log
index 8cdb4b7..d1c4d43 100644 (file)
@@ -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;
index 39c9e67..f5ef63c 100644 (file)
@@ -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);
index 1de9bc5..4b83240 100644 (file)
@@ -1,20 +1,21 @@
 {
-  "name": "clean-css",
+  "name": "@ndcode/clean-css",
   "version": "4.2.1",
   "author": "Jakub Pawlowicz <contact@jakubpawlowicz.com> (http://twitter.com/jakubpawlowicz)",
-  "description": "A well-tested CSS minifier",
+  "maintainer": "Nick Downing <nick@ndcode.org>",
+  "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": {