From bf0ba2879929e20fc1ed508df15ae06efb2d9cc3 Mon Sep 17 00:00:00 2001 From: Jesse Pearson Date: Fri, 24 Apr 2020 02:46:51 -0500 Subject: [PATCH 1/2] Fix issue with common "{{ }}" template syntax in a CSS rule --- css.js | 6 +++--- css.min.js | 4 ++-- tests/cssParsing.test.js | 38 +++++++++++++++++++++++++++++++++++--- tests/data.js | 40 ++++++++++++++++++++++++++++++++++++---- 4 files changed, 76 insertions(+), 12 deletions(-) diff --git a/css.js b/css.js index f3061b3..1e564fc 100644 --- a/css.js +++ b/css.js @@ -10,15 +10,15 @@ this.cssRegex = new RegExp('([\\s\\S]*?){([\\s\\S]*?)}', 'gi'); this.cssMediaQueryRegex = '((@media [\\s\\S]*?){([\\s\\S]*?}\\s*?)})'; this.cssKeyframeRegex = '((@.*?keyframes [\\s\\S]*?){([\\s\\S]*?}\\s*?)})'; - this.combinedCSSRegex = '((\\s*?(?:\\/\\*[\\s\\S]*?\\*\\/)?\\s*?@media[\\s\\S]*?){([\\s\\S]*?)}\\s*?})|(([\\s\\S]*?){([\\s\\S]*?)})'; //to match css & media queries together + this.combinedCSSRegex = '((\\s*?(?:\\/\\*[\\s\\S]*?\\*\\/)?\\s*?@media[\\s\\S]*?){([\\s\\S]*?)(?!.*}})}\\s*?})|(([\\s\\S]*?){([\\s\\S]*?)(?![\\s\\S]?}})[\\s\\S]})'; // to match css & media queries together this.cssCommentsRegex = '(\\/\\*[\\s\\S]*?\\*\\/)'; this.cssImportStatementRegex = new RegExp('@import .*?;', 'gi'); }; /* - Strip outs css comments and returns cleaned css string + Strips out css comments and returns cleaned css string - @param css, the original css string to be stipped out of comments + @param css, the original css string to be stripped out of comments @return cleanedCSS contains no css comments */ diff --git a/css.min.js b/css.min.js index 5fece1e..67707bd 100644 --- a/css.min.js +++ b/css.min.js @@ -1,3 +1,3 @@ -/*! css.js 27-02-2018 */ +/*! jotform-css.js 24-04-2020 */ -!function(e){"use strict";var t=function(){this.cssImportStatements=[],this.cssKeyframeStatements=[],this.cssRegex=new RegExp("([\\s\\S]*?){([\\s\\S]*?)}","gi"),this.cssMediaQueryRegex="((@media [\\s\\S]*?){([\\s\\S]*?}\\s*?)})",this.cssKeyframeRegex="((@.*?keyframes [\\s\\S]*?){([\\s\\S]*?}\\s*?)})",this.combinedCSSRegex="((\\s*?(?:\\/\\*[\\s\\S]*?\\*\\/)?\\s*?@media[\\s\\S]*?){([\\s\\S]*?)}\\s*?})|(([\\s\\S]*?){([\\s\\S]*?)})",this.cssCommentsRegex="(\\/\\*[\\s\\S]*?\\*\\/)",this.cssImportStatementRegex=new RegExp("@import .*?;","gi")};t.prototype.stripComments=function(e){var t=new RegExp(this.cssCommentsRegex,"gi");return e.replace(t,"")},t.prototype.parseCSS=function(e){if(void 0===e)return[];for(var t=[];;){var s=this.cssImportStatementRegex.exec(e);if(null===s)break;this.cssImportStatements.push(s[0]),t.push({selector:"@imports",type:"imports",styles:s[0]})}e=e.replace(this.cssImportStatementRegex,"");for(var r,i=new RegExp(this.cssKeyframeRegex,"gi");null!==(r=i.exec(e));)t.push({selector:"@keyframes",type:"keyframes",styles:r[0]});e=e.replace(i,"");for(var n=new RegExp(this.combinedCSSRegex,"gi");null!==(r=n.exec(e));){var o="";o=void 0===r[2]?r[5].split("\r\n").join("\n").trim():r[2].split("\r\n").join("\n").trim();var l=new RegExp(this.cssCommentsRegex,"gi"),p=l.exec(o);if(null!==p&&(o=o.replace(l,"").trim()),-1!==(o=o.replace(/\n+/,"\n")).indexOf("@media")){var a={selector:o,type:"media",subStyles:this.parseCSS(r[3]+"\n}")};null!==p&&(a.comments=p[0]),t.push(a)}else{var c={selector:o,rules:this.parseRules(r[6])};"@font-face"===o&&(c.type="font-face"),null!==p&&(c.comments=p[0]),t.push(c)}}return t},t.prototype.parseRules=function(e){var t=[];e=(e=e.split("\r\n").join("\n")).split(";");for(var s=0;s0&&t.push({directive:"",value:r,defective:!0})}return t},t.prototype.findCorrespondingRule=function(e,t,s){void 0===s&&(s=!1);for(var r=!1,i=0;i-1;o--)if(e[o].selector===r){i=e[o];break}if(!1===i)e.push(t);else if("media"!==t.type)for(var l=0;l-1||n.selector.indexOf("keyframes")>-1||n.selector.indexOf("@import")>-1||n.selector.indexOf(".form-all")>-1||n.selector.indexOf("#stage")>-1))if("media"!==n.type){for(var o=n.selector.split(","),l=[],p=0;p Date: Tue, 28 Apr 2020 14:34:44 -0500 Subject: [PATCH 2/2] - Push up comment parsing fix --- css.js | 8 ++++++++ css.min.js | 4 ++-- tests/cssParsing.test.js | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/css.js b/css.js index 1e564fc..9880e2f 100644 --- a/css.js +++ b/css.js @@ -4,6 +4,7 @@ 'use strict'; var fi = function() { + this.skipCommentParsing = true; this.cssImportStatements = []; this.cssKeyframeStatements = []; @@ -15,6 +16,10 @@ this.cssImportStatementRegex = new RegExp('@import .*?;', 'gi'); }; + fi.prototype.setSkipCommentParsing = function (skip) { + this.skipCommentParsing = skip; + }; + /* Strips out css comments and returns cleaned css string @@ -474,6 +479,9 @@ } for (i = 0; i < cssBase.length; i++) { var tmp = cssBase[i]; + if (this.skipCommentParsing) { + tmp.comments = undefined; + } if (tmp.selector === undefined) { //temporarily omit media queries continue; } diff --git a/css.min.js b/css.min.js index 67707bd..aa94477 100644 --- a/css.min.js +++ b/css.min.js @@ -1,3 +1,3 @@ -/*! jotform-css.js 24-04-2020 */ +/*! jotform-css.js 28-04-2020 */ -!function(e){"use strict";var t=function(){this.cssImportStatements=[],this.cssKeyframeStatements=[],this.cssRegex=new RegExp("([\\s\\S]*?){([\\s\\S]*?)}","gi"),this.cssMediaQueryRegex="((@media [\\s\\S]*?){([\\s\\S]*?}\\s*?)})",this.cssKeyframeRegex="((@.*?keyframes [\\s\\S]*?){([\\s\\S]*?}\\s*?)})",this.combinedCSSRegex="((\\s*?(?:\\/\\*[\\s\\S]*?\\*\\/)?\\s*?@media[\\s\\S]*?){([\\s\\S]*?)(?!.*}})}\\s*?})|(([\\s\\S]*?){([\\s\\S]*?)(?![\\s\\S]?}})[\\s\\S]})",this.cssCommentsRegex="(\\/\\*[\\s\\S]*?\\*\\/)",this.cssImportStatementRegex=new RegExp("@import .*?;","gi")};t.prototype.stripComments=function(e){var t=new RegExp(this.cssCommentsRegex,"gi");return e.replace(t,"")},t.prototype.parseCSS=function(e){if(void 0===e)return[];for(var t=[];;){var s=this.cssImportStatementRegex.exec(e);if(null===s)break;this.cssImportStatements.push(s[0]),t.push({selector:"@imports",type:"imports",styles:s[0]})}e=e.replace(this.cssImportStatementRegex,"");for(var r,i=new RegExp(this.cssKeyframeRegex,"gi");null!==(r=i.exec(e));)t.push({selector:"@keyframes",type:"keyframes",styles:r[0]});e=e.replace(i,"");for(var n=new RegExp(this.combinedCSSRegex,"gi");null!==(r=n.exec(e));){var o="";o=void 0===r[2]?r[5].split("\r\n").join("\n").trim():r[2].split("\r\n").join("\n").trim();var l=new RegExp(this.cssCommentsRegex,"gi"),p=l.exec(o);if(null!==p&&(o=o.replace(l,"").trim()),-1!==(o=o.replace(/\n+/,"\n")).indexOf("@media")){var a={selector:o,type:"media",subStyles:this.parseCSS(r[3]+"\n}")};null!==p&&(a.comments=p[0]),t.push(a)}else{var c={selector:o,rules:this.parseRules(r[6])};"@font-face"===o&&(c.type="font-face"),null!==p&&(c.comments=p[0]),t.push(c)}}return t},t.prototype.parseRules=function(e){var t=[];e=(e=e.split("\r\n").join("\n")).split(";");for(var s=0;s