JAVASCRIPT   72

cssParser.js

Guest on 19th July 2021 03:51:39 PM

  1. var id, desktop, self = this;
  2.  
  3. function parse(e) {
  4.     var r = self,
  5.         t = (e = e || {}, e.data),
  6.         n = e.rootPath,
  7.         i = e.cssOb;
  8.     t = (t = (t = (t = (t = (t = r.applyRoot(n, t)).replace(/\/\*[\s\S]*?\*\//g, "")).replace(/<!--/g, "")).replace(/-->/g, "")).replace(/@charset.*?;/g, "")).replace(/@namespace.*?;/g, "");
  9.     var s = "spreadL > section";
  10.     e.id && (s = e.id);
  11.     var a = (i.parentClass ? i.parentClass : "#epubContainer.stylesEnabled") + " #" + s,
  12.         l = cssToJson(t),
  13.         p = "";
  14.     try {
  15.         for (var o = 0; o < l.length; o++) {
  16.             var h = g(l[o].keys);
  17.             if (p += h.joined, p += "{", l[o].children && l[o].children.length)
  18.                 for (var d = 0; d < l[o].children.length; d++) {
  19.                     var c, f = g(l[o].children[d].keys);
  20.                     if (p += f.joined, p += "{" + l[o].children[d].properties + "}", f.hasBody)(c = u(l[o].properties)) && (p += "#epubContainer{" + c + "}")
  21.                 }
  22.             if (l[o].properties && (p += l[o].properties), p += "}", h.hasBody)(c = u(l[o].properties)) && (p += "#epubContainer{" + c + "}")
  23.         }
  24.     } catch (e) {}
  25.  
  26.     function u(e) {
  27.         if (e) {
  28.             for (var r = e.split(";"), t = "", n = 0; n < r.length; n++) r[n] && -1 != r[n].trim().indexOf("background") && (t = r[n].trim().replace(/url\(.*?\)/g, "") + ";");
  29.             return t
  30.         }
  31.     }
  32.  
  33.     function g(e) {
  34.         for (var r = {}, t = 0; t < e.length; t++)
  35.             if (e[t]) {
  36.                 var n = e[t].trim();
  37.                 0 != n.indexOf("body") ? 0 != n.indexOf("html") ? 0 != n.indexOf("@media") && 0 != n.indexOf("@") && isNaN(n[0]) && "from" != n && "to" != n && (e[t] = a + " " + n) : e[t] = n.replace("html", a) : (e[t] = n.replace("body", a), 0 == e[t].indexOf("html") && (e[t] = e[t].replace("html", a)), r.hasBody = !0)
  38.             }
  39.         return r.joined = e.join(","), r
  40.     }
  41.     t = minimizeData(t = p), self.postMessage({
  42.         data: t,
  43.         id: id
  44.     })
  45. }
  46.  
  47. function minimizeData(e) {
  48.     var r = e;
  49.     return r = (r = (r = (r = (r = r.replace(/\/\*(?:(?!\*\/)[\s\S])*\*\/|[\r\n\t]+/g, "")).replace(/ {2,}/g, " ")).replace(/ ([{:}]) /g, "$1")).replace(/([;,]) /g, "$1")).replace(/ !/g, "!")
  50. }
  51.  
  52. function cssToJson(e) {
  53.     e = e.replace(/\r?\n|\r/g, " ");
  54.     var r, t = [],
  55.         n = "key",
  56.         i = [0],
  57.         s = t,
  58.         a = [s];
  59.  
  60.     function l(e) {
  61.         return e = e || 1, i[i.length - e]
  62.     }
  63.     for (var p = 0; p < e.length; p++) r = r || {}, "}" == e[p] ? ("properties" == n && (r.properties = e.substring(l(), p).trim(), r.keys && r.keys.length && (r.keys[r.keys.length - 1] = r.keys[r.keys.length - 1].trim()), s.push(r)), "key" == n && 1 < a.length && (a.pop(), s = a[a.length - 1]), r = null, n = "key", i.push(p + 1)) : "{" == e[p] ? ("properties" == n && (r.children || (r.children = []), r.keys && r.keys.length && (r.keys[r.keys.length - 1] = r.keys[r.keys.length - 1].trim()), t.push(r), s = r.children, a.push(s), (r = {
  64.         keys: []
  65.     }).keys.push(e.substring(l(), p).trim())), i.push(p + 1), n = "properties") : "key" == n && (r.keys || (r.keys = [""]), "," == e[p] ? r.keys.push("") : r.keys[r.keys.length - 1] += e[p]);
  66.     return t
  67. }
  68.  
  69. function applyRoot(n, e) {
  70.     return e.replace(/url\((.*?)\)/g, function(e, r) {
  71.         var t = "";
  72.         return 0 == r.indexOf('"') ? (r = r.substr(1, r.length - 2), t = '"') : 0 == r.indexOf("'") && (r = r.substr(1, r.length - 2), t = "'"), -1 != r.indexOf("http://") ? e : -1 != r.indexOf("https://") ? e : 0 == r.indexOf("data:") ? e : "url(" + t + (r = ph.join(n, r)) + t + ")"
  73.     })
  74. }
  75.  
  76. function isAbsoluteURLPath(e) {
  77.     return 0 == e.trim().indexOf("http://") || (0 == e.trim().indexOf("https://") || (0 == e.trim().indexOf("//") || (0 == e.trim().indexOf("data:") || void 0)))
  78. }
  79. self.addEventListener("message", function(e) {
  80.     var r = e.data.func,
  81.         t = e.data.params;
  82.     id = e.data.id, desktop = e.data.desktop, self[r].apply(self, t)
  83. }, !1);
  84. var ph = {
  85.         join: function() {
  86.             for (var e = new Array, r = 0; r < arguments.length; r++) {
  87.                 var t = ph.normalizedArray(arguments[r]);
  88.                 r < arguments.length - 1 && !isAbsoluteURLPath(t[t.length - 1]) && 0 <= t[t.length - 1].indexOf(".") && t.splice(t.length - 1, 1), e = e.concat(t)
  89.             }
  90.             var n = ph.normalize(e.join("/"));
  91.             return "/" == arguments[0].substr(0, 1) && (n = desktop ? "file:////" + n : "/" + n), n
  92.         },
  93.         normalize: function(e) {
  94.             var r = ph.normalizedArray(e).join("/");
  95.             return arguments.length && "/" == e.substr(0, 1) && (r = "file:///" + r), r
  96.         },
  97.         normalizedArray: function(e) {
  98.             for (var r = (e = e.replace(/\\/g, "/")).split("/"), t = r.length; t--;) r[t] ? ".." == r[t] && 0 < t && ".." != r[t - 1] && (r.splice(t, 1), r.splice(t - 1, 1)) : r.splice(t, 1);
  99.             return r[0].match(/http[s]?:$/g) && (r[0] += "//" + r[1], r.splice(1, 1)), "file:" == r[0] && (r[0] += "///" + r[1], r.splice(1, 1)), r
  100.         }
  101.     },
  102.     log = console.log;

Raw Paste


Login or Register to edit or fork this paste. It's free.