Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

postcss-svg

Package Overview
Dependencies
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-svg - npm Package Compare versions

Comparing version 2.2.0 to 2.3.0

lib/element-as-data-uri-svg.js

6

CHANGELOG.md
# Changes to PostCSS SVG
### 2.3.0 - 2017-11-02
* Update SVGO to 1.x
* Improve SVG file resolution
* Improve SVG file caching
### 2.2.0 - 2017-10-09

@@ -4,0 +10,0 @@

17

index.js

@@ -16,3 +16,3 @@ /* Tooling

module.exports = postcss.plugin('postcss-svg-fragments', rawopts => (css, result) => {
module.exports = postcss.plugin('postcss-svg-fragments', argopts => (css, result) => {
// svg promises array

@@ -24,12 +24,15 @@ const promises = [];

// additional directories to search for SVGs
dirs: rawopts && 'dirs' in rawopts ? typeof rawopts.dirs === 'string' ? [rawopts.dirs] : Array.from(rawopts.dirs) : [],
dirs: argopts && 'dirs' in argopts ? [].concat(argopts.dirs) : [],
// whether to encode as utf-8
utf8: rawopts && 'utf8' in rawopts ? Boolean(rawopts.utf8) : true,
utf8: argopts && 'utf8' in argopts ? Boolean(argopts.utf8) : true,
// whether and how to compress with svgo
svgo: rawopts && 'svgo' in rawopts ? Object(rawopts.svgo) : false
svgo: argopts && 'svgo' in argopts ? Object(argopts.svgo) : false
};
// path to the current working directory by stylesheet
const cssCWD = css.source && css.source.input && css.source.input.file ? path.dirname(css.source.input.file) : process.cwd();
const cssWD = css.source && css.source.input && css.source.input.file ? path.dirname(css.source.input.file) : process.cwd();
// cache of file content and json content promises
const cache = {};
// for each declaration in the stylesheet

@@ -40,6 +43,6 @@ css.walkDecls(decl => {

// path to the current working directory by declaration
const declCWD = decl.source && decl.source.input && decl.source.input.file ? path.dirname(decl.source.input.file) : cssCWD;
const declWD = decl.source && decl.source.input && decl.source.input.file ? path.dirname(decl.source.input.file) : cssWD;
// transpile declaration parts
transpileDecl(result, promises, decl, declCWD, opts);
transpileDecl(result, promises, decl, declWD, opts, cache);
}

@@ -46,0 +49,0 @@ });

@@ -0,1 +1,3 @@

'use strict';
/* Tooling

@@ -5,3 +7,3 @@ /* ========================================================================== */

// native tooling
const fs = require('fs');
const fs = require('fs');
const path = require('path');

@@ -12,123 +14,134 @@

/* Promise the XML tree of the closest direct or directory/module-relative svg
/* Promise the XML tree of the closest svg
/* ========================================================================== */
module.exports = (cwd, src, dirs) => {
// unique id from the current working directory and the src
const uuid = [cwd, src].join();
module.exports = (id, wds, cache) => wds.reduce(
// for each working directory
(promise, wd) => promise.catch(() => {
// set cwd as the current working directory
let cwd = wd;
// segmented paths (the src always uses slash segmentation)
const cwds = cwd.split(path.sep);
const srcs = src.split('/');
// if id starts with root
if (starts_with_root(id)) {
// set cwd as the root
cwd = '';
}
// cached promise for the closest svg
cache[uuid] = cache[uuid] || [
// direct relative directory
cwds.concat(srcs).join(path.sep)
].concat(
// closest module directories
cwds.map(
(segment, index) => [].concat(
cwds.slice(0, index + 1),
'node_modules',
srcs[0],
srcs.slice(1)
).join(path.sep)
).reverse()
).reduce(
// promise the closest module-relative file
(promise, modulePath) => promise.catch(
// promise whether the module-relative package.json can be accessed
() => access(
path.join(modulePath, 'package.json')
).then(
// if the package.json can be accessed;
// promise the json contents of the package.json
() => readJSON(
path.join(modulePath, 'package.json')
).then(
// promise whether the module-specified file can be accessed
pkg => accessFile(
path.join(
modulePath,
pkg.media || pkg.main
)
)
),
// if the package.json cannot be accessed;
// promise whether the module-relative file can be accessed
() => srcs.slice(1).length ? accessFile(
path.join(modulePath)
) : Promise.reject()
)
),
dirs.map(
dir => path.resolve(cwd, dir).split(path.sep).concat(srcs).join(path.sep)
).reduce(
// promise the first directory-relative file
(promise, dir) => promise.catch(
// promise the directory-relative file
() => accessFile(dir)
),
// promise the direct file
accessFile(
cwds.concat(srcs).join(path.sep)
)
// resolve as a file using cwd/id as file
return resolveAsFile(path.join(cwd, id), cache)
// otherwise, resolve as a directory using cwd/id as dir
.catch(() => resolve_as_directory(path.join(cwd, id), cache))
// otherwise, if id does not start with root or relative
.catch(() => !starts_with_root_or_relative(id)
// resolve as a module using cwd and id
? resolve_as_module(cwd, id, cache)
: Promise.reject()
)
).then(
// promise the contents of the file
readFile
).then(
// promise the xml tree of the contents
contents => new XmlDocument(contents)
// otherwise, reject as id not found
.catch(() => Promise.reject(`${id} not found`));
}),
Promise.reject()
).then(
// resolve xml contents
contents => new XmlDocument(contents)
);
function resolveAsFile(file, cache) {
// if file is a file, resolve the contents of file
return file_contents(file, cache)
// otherwise, if file.svg is a file, resolve the contents of file.svg
.catch(() => file_contents(`${file}.svg`, cache));
}
function resolve_as_directory(dir, cache) {
// if dir/package.json is a file, set pkg as the JSON contents of dir/package.json
return json_contents(dir, cache).then(
// if pkg contains a media field
pkg => 'media' in pkg
// resolve the contents of dir/pkg.media
? file_contents(path.join(dir, pkg.media), cache)
// otherwise, if pkg contains a main field
: 'main' in pkg
// resolve the contents of dir/pkg.main
? file_contents(path.join(dir, pkg.main), cache)
// otherwise, if dir/index.svg is a file, resolve the contents of dir/index.svg
: file_contents(path.join(dir, 'index.svg'), cache)
);
}
// return the promise for the closest file
return cache[uuid];
function resolve_as_module(cwd, id, cache) {
return node_modules_dirs(cwd).reduce(
// for each dir in module dirs using cwd:
(promise, dir) => promise.catch(
// resolve as a file using dir/id as file
() => resolveAsFile(path.join(dir, id), cache)
// otherwise, resolve as a directory using dir/id as dir
.catch(() => resolve_as_directory(path.join(dir, id), cache))
),
Promise.reject()
);
}
/* Promise Cache
/* ========================================================================== */
function node_modules_dirs(cwd) {
// set segments as cwd split by the separator
const segments = cwd.split(path.sep);
const cache = {};
// set count as the length of segments
let count = segments.length;
/* Inline Tooling
/* ========================================================================== */
// set dirs as an empty array
const dirs = [];
// promise the file path if the file can be accessed
function access(pathname) {
return new Promise(
(resolve, reject) => fs.access(
pathname,
error => error ? reject(error) : resolve(pathname)
)
);
// while count is greater than 0:
while (count > 0) {
// if segments[count] is not node_modules
if (segments[count] !== 'node_modules') {
// push a new item to dirs as the separator-joined segments[0 - count] and node_modules
dirs.push(
path.join(segments.slice(0, count).join('/') || '/', 'node_modules')
);
}
// set count as count minus 1
--count;
}
return dirs;
}
// promise the file path if the file is a file
function accessFile(pathname) {
return new Promise(
(resolvePromise, rejectPromise) => fs.lstat(
pathname,
(error, stats) => error ? rejectPromise(error) : stats.isFile() ? resolvePromise(pathname) : rejectPromise(`${pathname} is not a file`)
function file_contents(file, cache) {
// if file is a file, resolve the contents of file
cache[file] = cache[file] || new Promise(
(resolvePromise, rejectPromise) => fs.readFile(
file,
'utf8',
(error, contents) => error ? rejectPromise(error) : resolvePromise(contents)
)
);
return cache[file];
}
// promise the file contents
function readFile(pathname) {
return new Promise(
function json_contents(dir, cache) {
// path of dir/package.json
const pkg = path.join(dir, 'package.json');
// resolve the JSON contents of dir/package.json
cache[pkg] = cache[pkg] || new Promise(
(resolvePromise, rejectPromise) => fs.readFile(
pathname,
pkg,
'utf8',
(error, contents) => error ? rejectPromise(error) : resolvePromise(contents)
(error, contents) => error ? rejectPromise(error) : resolvePromise(JSON.parse(contents))
)
);
return cache[pkg];
}
// promise the json contents
function readJSON(pathname) {
return readFile(pathname).then(
contents => JSON.parse(contents)
);
function starts_with_root(id) {
return /^\//.test(id);
}
function starts_with_root_or_relative(id) {
return /^\.{0,2}\//.test(id);
}

@@ -13,3 +13,3 @@ /* Tooling

const elementById = require('./element-by-id');
const elementAsDataURI = require('./element-as-data-uri');
const elementAsDURISVG = require('./element-as-data-uri-svg');
const readClosestSVG = require('./read-closest-svg');

@@ -21,3 +21,3 @@ const transpileStyles = require('./transpile-styles');

module.exports = (result, promises, decl, cwd, opts) => {
module.exports = (result, promises, decl, declWD, opts, cache) => {
// walk each node of the declaration

@@ -30,3 +30,3 @@ const declAST = parser(decl.value).walk(node => {

// <url> split by #
// <url> split by fragment identifier symbol (#)
const urlParts = urlNode.value.split('#');

@@ -37,3 +37,3 @@

// url fragment identifier
// <url> fragment identifier
const id = urlParts.slice(1).join('#');

@@ -50,7 +50,7 @@

promises.push(
readClosestSVG(cwd, src, opts.dirs).then(document => {
readClosestSVG(src, [declWD].concat(opts.dirs), cache).then(document => {
// document cache
const ids = document.ids = document.ids || {};
// conditionally update document cache
// conditionally update the document cache
if (hasID && !ids[id]) {

@@ -60,3 +60,3 @@ ids[id] = elementById(document, id);

// get the element fragment or document
// element fragment or document
const element = hasID ? ids[id] : document;

@@ -69,7 +69,7 @@

// update the clone styles with param
// update the clone styles using the params
transpileStyles(clone, params);
// promise updated <url> and declaration
return elementAsDataURI(clone, document, opts).then(xml => {
return elementAsDURISVG(clone, document, opts).then(xml => {
// update <url>

@@ -76,0 +76,0 @@ urlNode.value = xml;

@@ -30,78 +30,80 @@ # CC0 1.0 Universal

1. Copyright and Related Rights. A Work made available under CC0 may be
protected by copyright and related or neighboring rights (“Copyright and
Related Rights”). Copyright and Related Rights include, but are not limited
to, the following:
1. the right to reproduce, adapt, distribute, perform, display,
communicate, and translate a Work;
2. moral rights retained by the original author(s) and/or performer(s);
3. publicity and privacy rights pertaining to a person’s image or likeness
depicted in a Work;
4. rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(i), below;
5. rights protecting the extraction, dissemination, use and reuse of data
in a Work;
6. database rights (such as those arising under Directive 96/9/EC of the
European Parliament and of the Council of 11 March 1996 on the legal
protection of databases, and under any national implementation thereof,
including any amended or successor version of such directive); and
7. other similar, equivalent or corresponding rights throughout the world
based on applicable law or treaty, and any national implementations
thereof.
protected by copyright and related or neighboring rights (“Copyright and
Related Rights”). Copyright and Related Rights include, but are not limited
to, the following:
1. the right to reproduce, adapt, distribute, perform, display, communicate,
and translate a Work;
2. moral rights retained by the original author(s) and/or performer(s);
3. publicity and privacy rights pertaining to a person’s image or likeness
depicted in a Work;
4. rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(i), below;
5. rights protecting the extraction, dissemination, use and reuse of data in
a Work;
6. database rights (such as those arising under Directive 96/9/EC of the
European Parliament and of the Council of 11 March 1996 on the legal
protection of databases, and under any national implementation thereof,
including any amended or successor version of such directive); and
7. other similar, equivalent or corresponding rights throughout the world
based on applicable law or treaty, and any national implementations
thereof.
2. Waiver. To the greatest extent permitted by, but not in contravention of,
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
unconditionally waives, abandons, and surrenders all of Affirmer’s Copyright
and Related Rights and associated claims and causes of action, whether now
known or unknown (including existing as well as future claims and causes of
action), in the Work (i) in all territories worldwide, (ii) for the maximum
duration provided by applicable law or treaty (including future time
extensions), (iii) in any current or future medium and for any number of
copies, and (iv) for any purpose whatsoever, including without limitation
commercial, advertising or promotional purposes (the “Waiver”). Affirmer makes
the Waiver for the benefit of each member of the public at large and to the
detriment of Affirmer’s heirs and successors, fully intending that such Waiver
shall not be subject to revocation, rescission, cancellation, termination, or
any other legal or equitable action to disrupt the quiet enjoyment of the Work
by the public as contemplated by Affirmer’s express Statement of Purpose.
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
unconditionally waives, abandons, and surrenders all of Affirmer’s Copyright
and Related Rights and associated claims and causes of action, whether now
known or unknown (including existing as well as future claims and causes of
action), in the Work (i) in all territories worldwide, (ii) for the maximum
duration provided by applicable law or treaty (including future time
extensions), (iii) in any current or future medium and for any number of
copies, and (iv) for any purpose whatsoever, including without limitation
commercial, advertising or promotional purposes (the “Waiver”). Affirmer
makes the Waiver for the benefit of each member of the public at large and
to the detriment of Affirmer’s heirs and successors, fully intending that
such Waiver shall not be subject to revocation, rescission, cancellation,
termination, or any other legal or equitable action to disrupt the quiet
enjoyment of the Work by the public as contemplated by Affirmer’s express
Statement of Purpose.
3. Public License Fallback. Should any part of the Waiver for any reason be
judged legally invalid or ineffective under applicable law, then the Waiver
shall be preserved to the maximum extent permitted taking into account
Affirmer’s express Statement of Purpose. In addition, to the extent the Waiver
is so judged Affirmer hereby grants to each affected person a royalty-free, non
transferable, non sublicensable, non exclusive, irrevocable and unconditional
license to exercise Affirmer’s Copyright and Related Rights in the Work (i) in
all territories worldwide, (ii) for the maximum duration provided by applicable
law or treaty (including future time extensions), (iii) in any current or
future medium and for any number of copies, and (iv) for any purpose
whatsoever, including without limitation commercial, advertising or promotional
purposes (the “License”). The License shall be deemed effective as of the date
CC0 was applied by Affirmer to the Work. Should any part of the License for any
reason be judged legally invalid or ineffective under applicable law, such
partial invalidity or ineffectiveness shall not invalidate the remainder of the
License, and in such case Affirmer hereby affirms that he or she will not (i)
exercise any of his or her remaining Copyright and Related Rights in the Work
or (ii) assert any associated claims and causes of action with respect to the
Work, in either case contrary to Affirmer’s express Statement of Purpose.
judged legally invalid or ineffective under applicable law, then the Waiver
shall be preserved to the maximum extent permitted taking into account
Affirmer’s express Statement of Purpose. In addition, to the extent the
Waiver is so judged Affirmer hereby grants to each affected person a
royalty-free, non transferable, non sublicensable, non exclusive,
irrevocable and unconditional license to exercise Affirmer’s Copyright and
Related Rights in the Work (i) in all territories worldwide, (ii) for the
maximum duration provided by applicable law or treaty (including future time
extensions), (iii) in any current or future medium and for any number of
copies, and (iv) for any purpose whatsoever, including without limitation
commercial, advertising or promotional purposes (the “License”). The License
shall be deemed effective as of the date CC0 was applied by Affirmer to the
Work. Should any part of the License for any reason be judged legally
invalid or ineffective under applicable law, such partial invalidity or
ineffectiveness shall not invalidate the remainder of the License, and in
such case Affirmer hereby affirms that he or she will not (i) exercise any
of his or her remaining Copyright and Related Rights in the Work or (ii)
assert any associated claims and causes of action with respect to the Work,
in either case contrary to Affirmer’s express Statement of Purpose.
4. Limitations and Disclaimers.
1. No trademark or patent rights held by Affirmer are waived, abandoned,
surrendered, licensed or otherwise affected by this document.
2. Affirmer offers the Work as-is and makes no representations or
warranties of any kind concerning the Work, express, implied, statutory
or otherwise, including without limitation warranties of title,
merchantability, fitness for a particular purpose, non infringement, or
the absence of latent or other defects, accuracy, or the present or
absence of errors, whether or not discoverable, all to the greatest
extent permissible under applicable law.
3. Affirmer disclaims responsibility for clearing rights of other persons
that may apply to the Work or any use thereof, including without
limitation any person’s Copyright and Related Rights in the Work.
Further, Affirmer disclaims responsibility for obtaining any necessary
consents, permissions or other rights required for any use of the Work.
4. Affirmer understands and acknowledges that Creative Commons is not a
party to this document and has no duty or obligation with respect to
this CC0 or use of the Work.
1. No trademark or patent rights held by Affirmer are waived, abandoned,
surrendered, licensed or otherwise affected by this document.
2. Affirmer offers the Work as-is and makes no representations or warranties
of any kind concerning the Work, express, implied, statutory or
otherwise, including without limitation warranties of title,
merchantability, fitness for a particular purpose, non infringement, or
the absence of latent or other defects, accuracy, or the present or
absence of errors, whether or not discoverable, all to the greatest
extent permissible under applicable law.
3. Affirmer disclaims responsibility for clearing rights of other persons
that may apply to the Work or any use thereof, including without
limitation any person’s Copyright and Related Rights in the Work.
Further, Affirmer disclaims responsibility for obtaining any necessary
consents, permissions or other rights required for any use of the Work.
4. Affirmer understands and acknowledges that Creative Commons is not a
party to this document and has no duty or obligation with respect to this
CC0 or use of the Work.
For more information, please see
https://creativecommons.org/publicdomain/zero/1.0/.
http://creativecommons.org/publicdomain/zero/1.0/.
{
"name": "postcss-svg",
"version": "2.2.0",
"version": "2.3.0",
"description": "Inline SVGs in CSS. Supports SVG Fragments, SVG Parameters.",

@@ -21,4 +21,5 @@ "author": "Jonathan Neal <jonathantneal@hotmail.com>",

"clean": "git clean -X -d -f",
"prepublish": "npm test",
"test": "echo 'Running tests...'; npm run test:js && npm run test:tape",
"prepublish": "npm run test",
"test": "echo 'Running tests...'; npm run test:ec && npm run test:js && npm run test:tape",
"test:ec": "echint",
"test:js": "eslint *.js --cache --ignore-pattern .gitignore",

@@ -33,11 +34,11 @@ "test:tape": "postcss-tape"

"postcss-value-parser": "3.x",
"svgo": "0.x",
"svgo": "1.x",
"xmldoc": "1.x"
},
"devDependencies": {
"echint": "4.x",
"eslint": "4.x",
"eslint-config-dev": "2.x",
"postcss-tape": "2.x",
"pre-commit": "1.x",
"test-sprite-module": "file:test/sprite-module"
"pre-commit": "1.x"
},

@@ -44,0 +45,0 @@ "eslintConfig": {

@@ -42,2 +42,5 @@ # PostCSS SVG [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]

The location of an SVG is intelligently resolved using the
[SVG Resolve Algorithm].
## Usage

@@ -168,2 +171,3 @@

[SVG Parameters]: https://tabatkins.github.io/specs/svg-params/
[SVG Resolve Algorithm]: lib/read-closest-svg.md
[svgo]: https://github.com/svg/svgo
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc