From 1f4489847569aeaf9e7ba4c045b253a5754d6c4b Mon Sep 17 00:00:00 2001 From: eight Date: Wed, 6 Sep 2017 03:08:03 +0800 Subject: [PATCH] Add: adopt node-semver --- background/update.js | 7 ++++--- content/install-user-css.js | 9 +++++---- js/usercss.js | 26 +------------------------- manifest.json | 1 + vendor/node-semver/README.md | 1 + vendor/node-semver/semver.js | 1 + 6 files changed, 13 insertions(+), 32 deletions(-) create mode 100644 vendor/node-semver/README.md create mode 100644 vendor/node-semver/semver.js diff --git a/background/update.js b/background/update.js index 74a99bf2..fcd238e6 100644 --- a/background/update.js +++ b/background/update.js @@ -1,5 +1,6 @@ /* global getStyles, saveStyle, styleSectionsEqual, chromeLocal */ -/* global calcStyleDigest, usercss */ +/* global calcStyleDigest */ +/* global usercss semverCompare */ 'use strict'; // eslint-disable-next-line no-var @@ -100,10 +101,10 @@ var updater = { return Promise.reject(updater.ERROR_VERSION); } if (style.version) { - if (usercss.semverTest(style.version, json.version) === 0) { + if (semverCompare(style.version, json.version) === 0) { return Promise.reject(updater.SAME_VERSION); } - if (usercss.semverTest(style.version, json.version) > 0) { + if (semverCompare(style.version, json.version) > 0) { return Promise.reject(updater.ERROR_VERSION); } } diff --git a/content/install-user-css.js b/content/install-user-css.js index ccabada9..2cd17e95 100644 --- a/content/install-user-css.js +++ b/content/install-user-css.js @@ -1,4 +1,4 @@ -/* global usercss */ +/* global semverCompare */ 'use strict'; @@ -63,8 +63,8 @@ function getAppliesTo(style) { } function initInstallPage({style, dup}) { - pendingResource.then(() => { - const versionTest = dup && usercss.semverTest(style.version, dup.version); + return pendingResource.then(() => { + const versionTest = dup && semverCompare(style.version, dup.version); document.body.innerHTML = ''; // FIXME: i18n document.body.appendChild(tHTML(` @@ -113,7 +113,7 @@ function initInstallPage({style, dup}) { } function initErrorPage(err, source) { - pendingResource.then(() => { + return pendingResource.then(() => { document.body.innerHTML = ''; // FIXME: i18n document.body.appendChild(tHTML(` @@ -135,6 +135,7 @@ function initUsercssInstall() { '/js/dom.js', '/js/localization.js', '/js/usercss.js', + '/vendor/node-semver/semver.js', '/content/install-user-css.css' ] }); diff --git a/js/usercss.js b/js/usercss.js index 7ff85ad2..551fcd39 100644 --- a/js/usercss.js +++ b/js/usercss.js @@ -9,30 +9,6 @@ var usercss = (function () { 'namespace', 'noframes', 'preprocessor', 'supportURL', 'var', 'version' ]; - // FIXME: use a real semver module - function semverTest(a, b) { - a = a.split('.').map(Number); - b = b.split('.').map(Number); - - for (let i = 0; i < a.length; i++) { - if (!(i in b)) { - return 1; - } - if (a[i] < b[i]) { - return -1; - } - if (a[i] > b[i]) { - return 1; - } - } - - if (a.length < b.length) { - return -1; - } - - return 0; - } - const BUILDER = { default: { postprocess(sections, vars) { @@ -243,5 +219,5 @@ var usercss = (function () { // FIXME: validate variable formats } - return {buildMeta, buildCode, semverTest}; + return {buildMeta, buildCode}; })(); diff --git a/manifest.json b/manifest.json index 6fd8bd66..500e251b 100644 --- a/manifest.json +++ b/manifest.json @@ -27,6 +27,7 @@ "js/prefs.js", "js/script-loader.js", "background/background.js", + "vendor/node-semver/semver.js", "background/update.js" ] }, diff --git a/vendor/node-semver/README.md b/vendor/node-semver/README.md new file mode 100644 index 00000000..7810bff9 --- /dev/null +++ b/vendor/node-semver/README.md @@ -0,0 +1 @@ +See https://github.com/eight04/node-semver-bundle. \ No newline at end of file diff --git a/vendor/node-semver/semver.js b/vendor/node-semver/semver.js new file mode 100644 index 00000000..f7aec67a --- /dev/null +++ b/vendor/node-semver/semver.js @@ -0,0 +1 @@ +var semverCompare=function(){"use strict";function r(e,t){if(e instanceof r){if(e.loose===t)return e;e=e.version}else if("string"!=typeof e)throw new TypeError("Invalid Version: "+e);if(e.length>k)throw new TypeError("version is longer than "+k+" characters");if(!(this instanceof r))return new r(e,t);this.loose=t;var n=e.trim().match(t?I[L]:I[_]);if(!n)throw new TypeError("Invalid Version: "+e);if(this.raw=e,this.major=+n[1],this.minor=+n[2],this.patch=+n[3],this.major>T||this.major<0)throw new TypeError("Invalid major version");if(this.minor>T||this.minor<0)throw new TypeError("Invalid minor version");if(this.patch>T||this.patch<0)throw new TypeError("Invalid patch version");n[4]?this.prerelease=n[4].split(".").map(function(r){if(/^[0-9]+$/.test(r)){var e=+r;if(e>=0&&ee?1:0}function t(e,t,n){return new r(e,n).compare(new r(t,n))}function n(r,e,n){return t(r,e,n)>0}function i(r,e,n){return t(r,e,n)<0}function s(r,e,n){return 0===t(r,e,n)}function o(r,e,n){return 0!==t(r,e,n)}function a(r,e,n){return t(r,e,n)>=0}function h(r,e,n){return t(r,e,n)<=0}function p(r,e,t,p){var u;switch(e){case"===":"object"==typeof r&&(r=r.version),"object"==typeof t&&(t=t.version),u=r===t;break;case"!==":"object"==typeof r&&(r=r.version),"object"==typeof t&&(t=t.version),u=r!==t;break;case"":case"=":case"==":u=s(r,t,p);break;case"!=":u=o(r,t,p);break;case">":u=n(r,t,p);break;case">=":u=a(r,t,p);break;case"<":u=i(r,t,p);break;case"<=":u=h(r,t,p);break;default:throw new TypeError("Invalid operator: "+e)}return u}function u(r,e){if(r instanceof u){if(r.loose===e)return r;r=r.value}if(!(this instanceof u))return new u(r,e);this.loose=e,this.parse(r),this.semver===vr?this.value="":this.value=this.operator+this.semver.version}function c(r,e){if(r instanceof c)return r.loose===e?r:new c(r.raw,e);if(r instanceof u)return new c(r.value,e);if(!(this instanceof c))return new c(r,e);if(this.loose=e,this.raw=r,this.set=r.split(/\s*\|\|\s*/).map(function(r){return this.parseRange(r.trim())},this).filter(function(r){return r.length}),!this.set.length)throw new TypeError("Invalid SemVer Range: "+r);this.format()}function f(r,e){return r=w(r,e),r=l(r,e),r=y(r,e),r=b(r,e)}function v(r){return!r||"x"===r.toLowerCase()||"*"===r}function l(r,e){return r.trim().split(/\s+/).map(function(r){return m(r,e)}).join(" ")}function m(r,e){var t=e?I[rr]:I[Y];return r.replace(t,function(r,e,t,n,i){var s;return v(e)?s="":v(t)?s=">="+e+".0.0 <"+(+e+1)+".0.0":v(n)?s=">="+e+"."+t+".0 <"+e+"."+(+t+1)+".0":i?("-"!==i.charAt(0)&&(i="-"+i),s=">="+e+"."+t+"."+n+i+" <"+e+"."+(+t+1)+".0"):s=">="+e+"."+t+"."+n+" <"+e+"."+(+t+1)+".0",s})}function w(r,e){return r.trim().split(/\s+/).map(function(r){return g(r,e)}).join(" ")}function g(r,e){var t=e?I[ir]:I[nr];return r.replace(t,function(r,e,t,n,i){var s;return v(e)?s="":v(t)?s=">="+e+".0.0 <"+(+e+1)+".0.0":v(n)?s="0"===e?">="+e+"."+t+".0 <"+e+"."+(+t+1)+".0":">="+e+"."+t+".0 <"+(+e+1)+".0.0":i?("-"!==i.charAt(0)&&(i="-"+i),s="0"===e?"0"===t?">="+e+"."+t+"."+n+i+" <"+e+"."+t+"."+(+n+1):">="+e+"."+t+"."+n+i+" <"+e+"."+(+t+1)+".0":">="+e+"."+t+"."+n+i+" <"+(+e+1)+".0.0"):s="0"===e?"0"===t?">="+e+"."+t+"."+n+" <"+e+"."+t+"."+(+n+1):">="+e+"."+t+"."+n+" <"+e+"."+(+t+1)+".0":">="+e+"."+t+"."+n+" <"+(+e+1)+".0.0",s})}function y(r,e){return r.split(/\s+/).map(function(r){return j(r,e)}).join(" ")}function j(r,e){r=r.trim();var t=e?I[Q]:I[O];return r.replace(t,function(r,e,t,n,i,s){var o=v(t),a=o||v(n),h=a||v(i),p=h;return"="===e&&p&&(e=""),o?r=">"===e||"<"===e?"<0.0.0":"*":e&&p?(a&&(n=0),h&&(i=0),">"===e?(e=">=",a?(t=+t+1,n=0,i=0):h&&(n=+n+1,i=0)):"<="===e&&(e="<",a?t=+t+1:n=+n+1),r=e+t+"."+n+"."+i):a?r=">="+t+".0.0 <"+(+t+1)+".0.0":h&&(r=">="+t+"."+n+".0 <"+t+"."+(+n+1)+".0"),r})}function b(r,e){return r.trim().replace(I[ur],"")}function d(r,e,t,n,i,s,o,a,h,p,u,c,f){return e=v(t)?"":v(n)?">="+t+".0.0":v(i)?">="+t+"."+n+".0":">="+e,a=v(h)?"":v(p)?"<"+(+h+1)+".0.0":v(u)?"<"+h+"."+(+p+1)+".0":c?"<="+h+"."+p+"."+u+"-"+c:"<="+a,(e+" "+a).trim()}function $(r,e){for(t=0;t0){var n=r[t].semver;if(n.major===e.major&&n.minor===e.minor&&n.patch===e.patch)return!0}return!1}return!0}function E(r,e,t){try{e=new c(e,t)}catch(r){return!1}return e.test(r)}var k=256,T=Number.MAX_SAFE_INTEGER||9007199254740991,I=[],R=[],x=0,A=x++;R[A]="0|[1-9]\\d*";var S=x++;R[S]="[0-9]+";var N=x++;R[N]="\\d*[a-zA-Z-][a-zA-Z0-9-]*";var z=x++;R[z]="("+R[A]+")\\.("+R[A]+")\\.("+R[A]+")";var C=x++;R[C]="("+R[S]+")\\.("+R[S]+")\\.("+R[S]+")";var M=x++;R[M]="(?:"+R[A]+"|"+R[N]+")";var V=x++;R[V]="(?:"+R[S]+"|"+R[N]+")";var X=x++;R[X]="(?:-("+R[M]+"(?:\\."+R[M]+")*))";var Z=x++;R[Z]="(?:-?("+R[V]+"(?:\\."+R[V]+")*))";var q=x++;R[q]="[0-9A-Za-z-]+";var P=x++;R[P]="(?:\\+("+R[q]+"(?:\\."+R[q]+")*))";var _=x++,F="v?"+R[z]+R[X]+"?"+R[P]+"?";R[_]="^"+F+"$";var G="[v=\\s]*"+R[C]+R[Z]+"?"+R[P]+"?",L=x++;R[L]="^"+G+"$";var B=x++;R[B]="((?:<|>)?=?)";var D=x++;R[D]=R[S]+"|x|X|\\*";var H=x++;R[H]=R[A]+"|x|X|\\*";var J=x++;R[J]="[v=\\s]*("+R[H]+")(?:\\.("+R[H]+")(?:\\.("+R[H]+")(?:"+R[X]+")?"+R[P]+"?)?)?";var K=x++;R[K]="[v=\\s]*("+R[D]+")(?:\\.("+R[D]+")(?:\\.("+R[D]+")(?:"+R[Z]+")?"+R[P]+"?)?)?";var O=x++;R[O]="^"+R[B]+"\\s*"+R[J]+"$";var Q=x++;R[Q]="^"+R[B]+"\\s*"+R[K]+"$";var U=x++;R[U]="(?:~>?)";var W=x++;R[W]="(\\s*)"+R[U]+"\\s+",I[W]=new RegExp(R[W],"g");var Y=x++;R[Y]="^"+R[U]+R[J]+"$";var rr=x++;R[rr]="^"+R[U]+R[K]+"$";var er=x++;R[er]="(?:\\^)";var tr=x++;R[tr]="(\\s*)"+R[er]+"\\s+",I[tr]=new RegExp(R[tr],"g");var nr=x++;R[nr]="^"+R[er]+R[J]+"$";var ir=x++;R[ir]="^"+R[er]+R[K]+"$";var sr=x++;R[sr]="^"+R[B]+"\\s*("+G+")$|^$";var or=x++;R[or]="^"+R[B]+"\\s*("+F+")$|^$";var ar=x++;R[ar]="(\\s*)"+R[B]+"\\s*("+G+"|"+R[J]+")",I[ar]=new RegExp(R[ar],"g");var hr=x++;R[hr]="^\\s*("+R[J]+")\\s+-\\s+("+R[J]+")\\s*$";var pr=x++;R[pr]="^\\s*("+R[K]+")\\s+-\\s+("+R[K]+")\\s*$";var ur=x++;R[ur]="(<|>)?=?\\s*\\*";for(var cr=0;cr=0;)"number"==typeof this.prerelease[t]&&(this.prerelease[t]++,t=-2);-1===t&&this.prerelease.push(0)}e&&(this.prerelease[0]===e?isNaN(this.prerelease[1])&&(this.prerelease=[e,0]):this.prerelease=[e,0]);break;default:throw new Error("invalid increment argument: "+r)}return this.format(),this.raw=this.version,this};var fr=/^[0-9]+$/,vr={};return u.prototype.parse=function(e){var t=this.loose?I[sr]:I[or],n=e.match(t);if(!n)throw new TypeError("Invalid comparator: "+e);this.operator=n[1],"="===this.operator&&(this.operator=""),n[2]?this.semver=new r(n[2],this.loose):this.semver=vr},u.prototype.toString=function(){return this.value},u.prototype.test=function(e){return this.semver===vr||("string"==typeof e&&(e=new r(e,this.loose)),p(e,this.operator,this.semver,this.loose))},u.prototype.intersects=function(r,e){if(!(r instanceof u))throw new TypeError("a Comparator is required");var t;if(""===this.operator)return t=new c(r.value,e),E(this.value,t,e);if(""===r.operator)return t=new c(this.value,e),E(r.semver,t,e);var n=!(">="!==this.operator&&">"!==this.operator||">="!==r.operator&&">"!==r.operator),i=!("<="!==this.operator&&"<"!==this.operator||"<="!==r.operator&&"<"!==r.operator),s=this.semver.version===r.semver.version,o=!(">="!==this.operator&&"<="!==this.operator||">="!==r.operator&&"<="!==r.operator),a=p(this.semver,"<",r.semver,e)&&(">="===this.operator||">"===this.operator)&&("<="===r.operator||"<"===r.operator),h=p(this.semver,">",r.semver,e)&&("<="===this.operator||"<"===this.operator)&&(">="===r.operator||">"===r.operator);return n||i||s&&o||a||h},c.prototype.format=function(){return this.range=this.set.map(function(r){return r.join(" ").trim()}).join("||").trim(),this.range},c.prototype.toString=function(){return this.range},c.prototype.parseRange=function(r){var e=this.loose;r=r.trim();var t=e?I[pr]:I[hr];r=(r=(r=(r=(r=r.replace(t,d)).replace(I[ar],"$1$2$3")).replace(I[W],"$1~")).replace(I[tr],"$1^")).split(/\s+/).join(" ");var n=e?I[sr]:I[or],i=r.split(" ").map(function(r){return f(r,e)}).join(" ").split(/\s+/);return this.loose&&(i=i.filter(function(r){return!!r.match(n)})),i=i.map(function(r){return new u(r,e)})},c.prototype.intersects=function(r,e){if(!(r instanceof c))throw new TypeError("a Range is required");return this.set.some(function(t){return t.every(function(t){return r.set.some(function(r){return r.every(function(r){return t.intersects(r,e)})})})})},c.prototype.test=function(e){if(!e)return!1;"string"==typeof e&&(e=new r(e,this.loose));for(var t=0;t