🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

postcss-minify-font-values

Package Overview
Dependencies
Maintainers
8
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-minify-font-values - npm Package Compare versions

Comparing version

to
5.0.0-rc.0

49

CHANGELOG.md

@@ -1,34 +0,41 @@

# 4.0.0-rc.0
# Change Log
* Breaking: Drops support for Node 0.12, we now require at least Node 4.
* Breaking: Update PostCSS to 6.0.0.
* Breaking: `removeAfterKeyword` is now set to `false` by default. This is to
avoid incorrectly discarding emoji font families.
* Removed support for compressing whitespace, this is now delegated to
postcss-normalize-whitespace.
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# 1.0.5
# 5.0.0-rc.0 (2021-02-19)
* Resolves an issue where `var` would be removed from `font-family`
values (@ben-eb).
# 1.0.4
### Bug Fixes
* Ignores duplicated `monospace` definitions (@ben-eb).
* **postcss-minify-font-values:** correct minify font family with css variables ([#740](https://github.com/cssnano/cssnano/issues/740)) ([fc3eae9](https://github.com/cssnano/cssnano/commit/fc3eae9417974ad0ea38fa055668a2f52493b2ec))
# 1.0.3
* Resolves an issue where the module would remove quotes from font families
that began with numbers (@ben-eb).
### chore
# 1.0.2
* minimum require version of node is 10.13 ([#871](https://github.com/cssnano/cssnano/issues/871)) ([28bda24](https://github.com/cssnano/cssnano/commit/28bda243e32ce3ba89b3c358a5f78727b3732f11))
* Upgraded postcss-value-parser to version 3 (@TrySound).
# 1.0.1
### Features
* Add repository link to `package.json` (@TrySound).
* migarete to PostCSS 8 ([#975](https://github.com/cssnano/cssnano/issues/975)) ([40b82dc](https://github.com/cssnano/cssnano/commit/40b82dca7f53ac02cd4fe62846dec79b898ccb49))
# 1.0.0
* Initial release (@TrySound).
### BREAKING CHANGES
* minimum supported `postcss` version is `8.2.1`
* minimum require version of node is 10.13
## 4.1.3 (2018-09-25)
## 4.1.1 (2018-09-24)
### Bug Fixes
* minify uppercase `weight` values in `font` property ([#612](https://github.com/cssnano/cssnano/issues/612)) ([a520e69](https://github.com/cssnano/cssnano/commit/a520e6906e7fa17951a64769f030ed7b6f44c38a))
* **postcss-merge-longhand:** not mangle border output ([#555](https://github.com/cssnano/cssnano/issues/555)) ([9a70605](https://github.com/cssnano/cssnano/commit/9a706050b621e7795a9bf74eb7110b5c81804ffe)), closes [#553](https://github.com/cssnano/cssnano/issues/553) [#554](https://github.com/cssnano/cssnano/issues/554)

@@ -1,55 +0,84 @@

'use strict';
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.default = void 0;
var _postcss = require('postcss');
var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
var _postcss2 = _interopRequireDefault(_postcss);
var _minifyWeight = _interopRequireDefault(require("./lib/minify-weight"));
var _postcssValueParser = require('postcss-value-parser');
var _minifyFamily = _interopRequireDefault(require("./lib/minify-family"));
var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
var _minifyFont = _interopRequireDefault(require("./lib/minify-font"));
var _minifyWeight = require('./lib/minify-weight');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _minifyWeight2 = _interopRequireDefault(_minifyWeight);
function hasVariableFunction(value) {
const lowerCasedValue = value.toLowerCase();
return lowerCasedValue.includes('var(') || lowerCasedValue.includes('env(');
}
var _minifyFamily = require('./lib/minify-family');
function transform(prop, value, opts) {
let lowerCasedProp = prop.toLowerCase();
var _minifyFamily2 = _interopRequireDefault(_minifyFamily);
if (lowerCasedProp === 'font-weight' && !hasVariableFunction(value)) {
return (0, _minifyWeight.default)(value);
} else if (lowerCasedProp === 'font-family' && !hasVariableFunction(value)) {
const tree = (0, _postcssValueParser.default)(value);
tree.nodes = (0, _minifyFamily.default)(tree.nodes, opts);
return tree.toString();
} else if (lowerCasedProp === 'font') {
const tree = (0, _postcssValueParser.default)(value);
tree.nodes = (0, _minifyFont.default)(tree.nodes, opts);
return tree.toString();
}
var _minifyFont = require('./lib/minify-font');
return value;
}
var _minifyFont2 = _interopRequireDefault(_minifyFont);
function pluginCreator(opts) {
opts = Object.assign({}, {
removeAfterKeyword: false,
removeDuplicates: true,
removeQuotes: true
}, opts);
return {
postcssPlugin: 'postcss-minify-font-values',
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
prepare() {
const cache = {};
return {
OnceExit(css) {
css.walkDecls(/font/i, decl => {
const value = decl.value;
function transform(opts, decl) {
let tree;
let prop = decl.prop.toLowerCase();
if (!value) {
return;
}
if (prop === 'font-weight') {
decl.value = (0, _minifyWeight2.default)(decl.value);
} else if (prop === 'font-family') {
tree = (0, _postcssValueParser2.default)(decl.value);
tree.nodes = (0, _minifyFamily2.default)(tree.nodes, opts);
decl.value = tree.toString();
} else if (prop === 'font') {
tree = (0, _postcssValueParser2.default)(decl.value);
tree.nodes = (0, _minifyFont2.default)(tree.nodes, opts);
decl.value = tree.toString();
const prop = decl.prop;
const cacheKey = `${prop}|${value}`;
if (cache[cacheKey]) {
decl.value = cache[cacheKey];
return;
}
const newValue = transform(prop, value, opts);
decl.value = newValue;
cache[cacheKey] = newValue;
});
}
};
}
};
}
exports.default = _postcss2.default.plugin('postcss-minify-font-values', opts => {
opts = Object.assign({}, {
removeAfterKeyword: false,
removeDuplicates: true,
removeQuotes: true
}, opts);
return css => css.walkDecls(/font/i, transform.bind(null, opts));
});
module.exports = exports['default'];
pluginCreator.postcss = true;
var _default = pluginCreator;
exports.default = _default;
module.exports = exports.default;

@@ -1,13 +0,15 @@

'use strict';
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.default = {
style: ['italic', 'oblique'],
variant: ['small-caps'],
weight: ['100', '200', '300', '400', '500', '600', '700', '800', '900', 'bold', 'lighter', 'bolder'],
stretch: ['ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'],
size: ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'larger', 'smaller']
exports.default = void 0;
var _default = {
style: ['italic', 'oblique'],
variant: ['small-caps'],
weight: ['100', '200', '300', '400', '500', '600', '700', '800', '900', 'bold', 'lighter', 'bolder'],
stretch: ['ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'],
size: ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'larger', 'smaller']
};
module.exports = exports['default'];
exports.default = _default;
module.exports = exports.default;

@@ -1,77 +0,15 @@

'use strict';
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.default = _default;
exports.default = function (nodes, opts) {
let family = [];
let last = null;
let i, max;
var _postcssValueParser = require("postcss-value-parser");
nodes.forEach((node, index, arr) => {
if (node.type === 'string' || node.type === 'function') {
family.push(node);
} else if (node.type === 'word') {
if (!last) {
last = { type: 'word', value: '' };
family.push(last);
}
var _uniqs = _interopRequireDefault(require("./uniqs"));
last.value += node.value;
} else if (node.type === 'space') {
if (last && index !== arr.length - 1) {
last.value += ' ';
}
} else {
last = null;
}
});
family = family.map(node => {
if (node.type === 'string') {
const isKeyword = regexKeyword.test(node.value);
if (!opts.removeQuotes || isKeyword || /[0-9]/.test(node.value.slice(0, 1))) {
return (0, _postcssValueParser.stringify)(node);
}
let escaped = escapeIdentifierSequence(node.value);
if (escaped.length < node.value.length + 2) {
return escaped;
}
}
return (0, _postcssValueParser.stringify)(node);
});
if (opts.removeAfterKeyword) {
for (i = 0, max = family.length; i < max; i += 1) {
if (~genericFontFamilykeywords.indexOf(family[i].toLowerCase())) {
family = family.slice(0, i + 1);
break;
}
}
}
if (opts.removeDuplicates) {
family = uniqs(family);
}
return [{
type: 'word',
value: family.join()
}];
};
var _postcssValueParser = require('postcss-value-parser');
var _uniqs = require('./uniqs');
var _uniqs2 = _interopRequireDefault(_uniqs);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const uniqs = (0, _uniqs2.default)('monospace');
const uniqs = (0, _uniqs.default)('monospace');
const globalKeywords = ['inherit', 'initial', 'unset'];

@@ -81,48 +19,50 @@ const genericFontFamilykeywords = ['sans-serif', 'serif', 'fantasy', 'cursive', 'monospace', 'system-ui'];

function makeArray(value, length) {
let array = [];
while (length--) {
array[length] = value;
}
return array;
}
let array = [];
while (length--) {
array[length] = value;
}
return array;
} // eslint-disable-next-line no-useless-escape
const regexSimpleEscapeCharacters = /[ !"#$%&'()*+,.\/;<=>?@\[\\\]^`{|}~]/;
function escape(string, escapeForString) {
let counter = 0;
let character = null;
let charCode = null;
let value = null;
let output = '';
let counter = 0;
let character = null;
let charCode = null;
let value = null;
let output = '';
while (counter < string.length) {
character = string.charAt(counter++);
charCode = character.charCodeAt();
while (counter < string.length) {
character = string.charAt(counter++);
charCode = character.charCodeAt(); // \r is already tokenized away at this point
// `:` can be escaped as `\:`, but that fails in IE < 8
// \r is already tokenized away at this point
// `:` can be escaped as `\:`, but that fails in IE < 8
if (!escapeForString && /[\t\n\v\f:]/.test(character)) {
value = '\\' + charCode.toString(16) + ' ';
} else if (!escapeForString && regexSimpleEscapeCharacters.test(character)) {
value = '\\' + character;
} else {
value = character;
}
if (!escapeForString && /[\t\n\v\f:]/.test(character)) {
value = '\\' + charCode.toString(16) + ' ';
} else if (!escapeForString && regexSimpleEscapeCharacters.test(character)) {
value = '\\' + character;
} else {
value = character;
}
output += value;
output += value;
}
if (!escapeForString) {
if (/^-[-\d]/.test(output)) {
output = '\\-' + output.slice(1);
}
if (!escapeForString) {
if (/^-[-\d]/.test(output)) {
output = '\\-' + output.slice(1);
}
const firstChar = string.charAt(0);
const firstChar = string.charAt(0);
if (/\d/.test(firstChar)) {
output = '\\3' + firstChar + ' ' + output.slice(1);
}
if (/\d/.test(firstChar)) {
output = '\\3' + firstChar + ' ' + output.slice(1);
}
}
return output;
return output;
}

@@ -140,69 +80,129 @@

function escapeIdentifierSequence(string) {
let identifiers = string.split(regexWhitespace);
let index = 0;
let result = [];
let escapeResult;
let identifiers = string.split(regexWhitespace);
let index = 0;
let result = [];
let escapeResult;
while (index < identifiers.length) {
let subString = identifiers[index++];
while (index < identifiers.length) {
let subString = identifiers[index++];
if (subString === '') {
result.push(subString);
continue;
}
if (subString === '') {
result.push(subString);
continue;
}
escapeResult = escape(subString, false);
escapeResult = escape(subString, false);
if (regexIdentifierCharacter.test(subString)) {
// the font family name part consists of allowed characters exclusively
if (regexInvalidIdentifier.test(subString)) {
// the font family name part starts with two hyphens, a digit, or a
// hyphen followed by a digit
if (index === 1) {
// if this is the first item
result.push(escapeResult);
} else {
// if it’s not the first item, we can simply escape the space
// between the two identifiers to merge them into a single
// identifier rather than escaping the start characters of the
// second identifier
result[index - 2] += '\\';
result.push(escape(subString, true));
}
} else {
// the font family name part doesn’t start with two hyphens, a digit,
// or a hyphen followed by a digit
result.push(escapeResult);
}
if (regexIdentifierCharacter.test(subString)) {
// the font family name part consists of allowed characters exclusively
if (regexInvalidIdentifier.test(subString)) {
// the font family name part starts with two hyphens, a digit, or a
// hyphen followed by a digit
if (index === 1) {
// if this is the first item
result.push(escapeResult);
} else {
// the font family name part contains invalid identifier characters
result.push(escapeResult);
// if it’s not the first item, we can simply escape the space
// between the two identifiers to merge them into a single
// identifier rather than escaping the start characters of the
// second identifier
result[index - 2] += '\\';
result.push(escape(subString, true));
}
} else {
// the font family name part doesn’t start with two hyphens, a digit,
// or a hyphen followed by a digit
result.push(escapeResult);
}
} else {
// the font family name part contains invalid identifier characters
result.push(escapeResult);
}
}
result = result.join(' ').replace(regexConsecutiveSpaces, ($0, $1, $2) => {
const spaceCount = $2.length;
const escapesNeeded = Math.floor(spaceCount / 2);
const array = makeArray('\\ ', escapesNeeded);
result = result.join(' ').replace(regexConsecutiveSpaces, ($0, $1, $2) => {
const spaceCount = $2.length;
const escapesNeeded = Math.floor(spaceCount / 2);
const array = makeArray('\\ ', escapesNeeded);
if (spaceCount % 2) {
array[escapesNeeded - 1] += '\\ ';
}
if (spaceCount % 2) {
array[escapesNeeded - 1] += '\\ ';
}
return ($1 || '') + ' ' + array.join(' ');
});
return ($1 || '') + ' ' + array.join(' ');
}); // Escape trailing spaces unless they’re already part of an escape
// Escape trailing spaces unless they’re already part of an escape
if (regexTrailingSpace.test(result) && !regexTrailingEscape.test(result)) {
result = result.replace(regexTrailingSpace, '\\ ');
if (regexTrailingSpace.test(result) && !regexTrailingEscape.test(result)) {
result = result.replace(regexTrailingSpace, '\\ ');
}
if (regexSpaceAtStart.test(result)) {
result = '\\ ' + result.slice(1);
}
return result;
}
function _default(nodes, opts) {
let family = [];
let last = null;
let i, max;
nodes.forEach((node, index, arr) => {
if (node.type === 'string' || node.type === 'function') {
family.push(node);
} else if (node.type === 'word') {
if (!last) {
last = {
type: 'word',
value: ''
};
family.push(last);
}
last.value += node.value;
} else if (node.type === 'space') {
if (last && index !== arr.length - 1) {
last.value += ' ';
}
} else {
last = null;
}
});
family = family.map(node => {
if (node.type === 'string') {
const isKeyword = regexKeyword.test(node.value);
if (regexSpaceAtStart.test(result)) {
result = '\\ ' + result.slice(1);
if (!opts.removeQuotes || isKeyword || /[0-9]/.test(node.value.slice(0, 1))) {
return (0, _postcssValueParser.stringify)(node);
}
let escaped = escapeIdentifierSequence(node.value);
if (escaped.length < node.value.length + 2) {
return escaped;
}
}
return result;
return (0, _postcssValueParser.stringify)(node);
});
if (opts.removeAfterKeyword) {
for (i = 0, max = family.length; i < max; i += 1) {
if (~genericFontFamilykeywords.indexOf(family[i].toLowerCase())) {
family = family.slice(0, i + 1);
break;
}
}
}
if (opts.removeDuplicates) {
family = uniqs(family);
}
return [{
type: 'word',
value: family.join()
}];
}
;
module.exports = exports['default'];
module.exports = exports.default;

@@ -1,55 +0,60 @@

'use strict';
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.default = _default;
exports.default = function (nodes, opts) {
let i, max, node, familyStart, family;
let hasSize = false;
var _postcssValueParser = require("postcss-value-parser");
for (i = 0, max = nodes.length; i < max; i += 1) {
node = nodes[i];
if (node.type === 'word') {
if (hasSize) {
continue;
}
const value = node.value.toLowerCase();
if (value === 'normal' || ~_keywords2.default.style.indexOf(value) || ~_keywords2.default.variant.indexOf(value) || ~_keywords2.default.stretch.indexOf(value)) {
familyStart = i;
} else if (~_keywords2.default.weight.indexOf(value)) {
node.value = (0, _minifyWeight2.default)(value);
familyStart = i;
} else if (~_keywords2.default.size.indexOf(value) || (0, _postcssValueParser.unit)(value)) {
familyStart = i;
hasSize = true;
}
} else if (node.type === 'div' && node.value === '/') {
familyStart = i + 1;
break;
}
}
var _keywords = _interopRequireDefault(require("./keywords"));
familyStart += 2;
family = (0, _minifyFamily2.default)(nodes.slice(familyStart), opts);
return nodes.slice(0, familyStart).concat(family);
};
var _minifyFamily = _interopRequireDefault(require("./minify-family"));
var _postcssValueParser = require('postcss-value-parser');
var _minifyWeight = _interopRequireDefault(require("./minify-weight"));
var _keywords = require('./keywords');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _keywords2 = _interopRequireDefault(_keywords);
function _default(nodes, opts) {
let i, max, node, familyStart, family;
let hasSize = false;
var _minifyFamily = require('./minify-family');
for (i = 0, max = nodes.length; i < max; i += 1) {
node = nodes[i];
var _minifyFamily2 = _interopRequireDefault(_minifyFamily);
if (node.type === 'word') {
if (hasSize) {
continue;
}
var _minifyWeight = require('./minify-weight');
const value = node.value.toLowerCase();
var _minifyWeight2 = _interopRequireDefault(_minifyWeight);
if (value === 'normal' || value === 'inherit' || value === 'initial' || value === 'unset') {
familyStart = i;
} else if (~_keywords.default.style.indexOf(value) || (0, _postcssValueParser.unit)(value)) {
familyStart = i;
} else if (~_keywords.default.variant.indexOf(value)) {
familyStart = i;
} else if (~_keywords.default.weight.indexOf(value)) {
node.value = (0, _minifyWeight.default)(value);
familyStart = i;
} else if (~_keywords.default.stretch.indexOf(value)) {
familyStart = i;
} else if (~_keywords.default.size.indexOf(value) || (0, _postcssValueParser.unit)(value)) {
familyStart = i;
hasSize = true;
}
} else if (node.type === 'function' && nodes[i + 1] && nodes[i + 1].type === 'space') {
familyStart = i;
} else if (node.type === 'div' && node.value === '/') {
familyStart = i + 1;
break;
}
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
familyStart += 2;
family = (0, _minifyFamily.default)(nodes.slice(familyStart), opts);
return nodes.slice(0, familyStart).concat(family);
}
;
module.exports = exports['default'];
module.exports = exports.default;

@@ -1,14 +0,13 @@

'use strict';
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.default = _default;
exports.default = function (value) {
const valueInLowerCase = value.toLowerCase();
function _default(value) {
const lowerCasedValue = value.toLowerCase();
return lowerCasedValue === 'normal' ? '400' : lowerCasedValue === 'bold' ? '700' : value;
}
return valueInLowerCase === 'normal' ? '400' : valueInLowerCase === 'bold' ? '700' : value;
};
;
module.exports = exports['default'];
module.exports = exports.default;
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.default = uniqueExcept;
function uniqueExcept(exclude) {
return function unique() {
const list = Array.prototype.concat.apply([], arguments);
return list.filter((item, i) => {
if (item.toLowerCase() === exclude) {
return true;
}
return i === list.indexOf(item);
});
};
};
module.exports = exports["default"];
return function unique() {
const list = Array.prototype.concat.apply([], arguments);
return list.filter((item, i) => {
if (item.toLowerCase() === exclude) {
return true;
}
return i === list.indexOf(item);
});
};
}
module.exports = exports.default;
{
"name": "postcss-minify-font-values",
"version": "4.0.2",
"version": "5.0.0-rc.0",
"description": "Minify font declarations with PostCSS",

@@ -20,4 +20,3 @@ "main": "dist/index.js",

"dependencies": {
"postcss": "^7.0.0",
"postcss-value-parser": "^3.0.0"
"postcss-value-parser": "^4.1.0"
},

@@ -30,11 +29,16 @@ "repository": "cssnano/cssnano",

"scripts": {
"prepublish": "cross-env BABEL_ENV=publish babel src --out-dir dist --ignore /__tests__/"
"prebuild": "del-cli dist",
"build": "cross-env BABEL_ENV=publish babel src --config-file ../../babel.config.js --out-dir dist --ignore \"**/__tests__/\"",
"prepublish": "yarn build"
},
"engines": {
"node": "^10 || ^12 || >=14.0"
},
"devDependencies": {
"babel-cli": "^6.0.0",
"cross-env": "^5.0.0"
"postcss": "^8.2.1"
},
"engines": {
"node": ">=6.9.0"
}
"peerDependencies": {
"postcss": "^8.2.1"
},
"gitHead": "8c16e67a4d24a13ac7e09a36d4faf504196efd0f"
}