Skip to content

Commit

Permalink
Add support to css plugins
Browse files Browse the repository at this point in the history
`data-uri` is now implemented as plugin
  • Loading branch information
lauriro committed Jan 12, 2025
1 parent 79ed054 commit 4627980
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions css.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ exports.CSSStyleSheet = CSSStyleSheet

var fs = require("fs")
, path = require("path")
, read = (sheet, url, enc = "utf8") => fs.readFileSync(path.resolve(sheet.min.root || "", sheet.baseURI, url).split(/[+#]/)[0], enc)
, plugins = exports.plugins = {
"data-uri": function(sheet, v) {
var { DOMParser } = require("./dom.js")
return v.replace(urlRe, function(_, q1, q2, url) {
if (q1) return _
var frag = url.split("#").pop()
, ext = url.split(/[?#]/)[0].split(".").pop()
, enc = ext === "svg" ? "utf8" : "base64"
url = read(sheet, url, enc)
if (ext === "svg") {
url = new DOMParser().parseFromString(url, "application/xml")
if (frag && (frag = url.getElementById(frag))) {
frag.removeAttribute("id")
url.documentElement.childNodes = frag.tagName === "g" ? frag.childNodes : [ frag ]
}
url = url.toString(true).replace(/#/g, "%23")
enc = ""
ext += "+xml"
}
return "url('data:image/" + ext + ";" + enc + "," + url + "')"
})
}
}
, urlRe = /(["']).*?\1|url\((['"]?)(?!\/|data:|https?:)(.*?)\2\)/g
, clearFn = (_, q, str, c) =>
q ? (q = str.indexOf("'") == -1 ? "'" : "\"", q + str.replace(q === "'" ? /\\(")/g : /\\(')/g, "$1")) + q :
Expand All @@ -19,7 +43,6 @@ var fs = require("fs")
, clear = s => s
.replace(/("|')((?:\\\1|[^\1])*?)\1|\s*(\/)\*(?:[^*]|\*(?!\/))*\*\/\s*|(?:[^"'\/]|\/(?!\*))+/g, clearFn)
.replace(/(["']).*?\1|url\(("|')([^'"()\s]+)\2\)/g, (m,q1,q2,u) => q1 ? m : "url(" + u + ")")
, read = (sheet, url, enc = "utf8") => fs.readFileSync(path.resolve(sheet.min.root || "", sheet.baseURI, url).split(/[+#]/)[0], enc)
, toRgb = {
rgb(r, g, b) {
var f = n => ((n | 0) + 256).toString(16).slice(1)
Expand Down Expand Up @@ -63,7 +86,7 @@ var fs = require("fs")
, lastIdx = {}
for (; (m = re.exec(val)); ) {
if (m[4]) {
if (min && len) style[k = style[len - 1]] = clear(transformValue(m[4].trim(), style[k]))
if (min && len && plugins[m[4] = m[4].trim()]) style[k = style[len - 1]] = clear(plugins[m[4]](sheet, style[k]))
continue
} else {
k = m[1]
Expand All @@ -78,30 +101,6 @@ var fs = require("fs")
if (!style[prop]) style[style.length++] = prop
style[prop] = style[prop === "cssFloat" ? "float" : prop.replace(/[A-Z]/g, "-$&").toLowerCase()] = clear(val)
}
function transformValue(cmd, v) {
var { DOMParser } = require("./dom.js")
if (cmd === "data-uri") {
return v.replace(urlRe, function(_, q1, q2, url) {
if (q1) return _
var frag = url.split("#").pop()
, ext = url.split(/[?#]/)[0].split(".").pop()
, enc = ext === "svg" ? "utf8" : "base64"
url = read(sheet, url, enc)
if (ext === "svg") {
url = new DOMParser().parseFromString(url, "application/xml")
if (frag && (frag = url.getElementById(frag))) {
frag.removeAttribute("id")
url.documentElement.childNodes = frag.tagName === "g" ? frag.childNodes : [ frag ]
}
url = url.toString(true).replace(/#/g, "%23")
enc = ""
ext += "+xml"
}
return "url('data:image/" + ext + ";" + enc + "," + url + "')"
})
}
return v
}
}
}
, ruleTypes = {
Expand Down

0 comments on commit 4627980

Please sign in to comment.