postcss-normalize-positions
Advanced tools
Comparing version
@@ -1,149 +0,209 @@ | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _postcss = require('postcss'); | ||
var _postcssValueParser = _interopRequireWildcard(require("postcss-value-parser")); | ||
var _postcssValueParser = require('postcss-value-parser'); | ||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } | ||
var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
var _cssnanoUtilGetArguments = require('cssnano-util-get-arguments'); | ||
const directionKeywords = ['top', 'right', 'bottom', 'left', 'center']; | ||
const center = '50%'; | ||
const horizontal = { | ||
right: '100%', | ||
left: '0' | ||
}; | ||
const verticalValue = { | ||
bottom: '100%', | ||
top: '0' | ||
}; | ||
var _cssnanoUtilGetArguments2 = _interopRequireDefault(_cssnanoUtilGetArguments); | ||
function isCommaNode(node) { | ||
return node.type === 'div' && node.value === ','; | ||
} | ||
var _has = require('has'); | ||
function isVariableFunctionNode(node) { | ||
if (node.type !== 'function') { | ||
return false; | ||
} | ||
var _has2 = _interopRequireDefault(_has); | ||
return ['var', 'env'].includes(node.value.toLowerCase()); | ||
} | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function isMathFunctionNode(node) { | ||
if (node.type !== 'function') { | ||
return false; | ||
} | ||
const directions = ['top', 'right', 'bottom', 'left', 'center']; | ||
return ['calc', 'min', 'max', 'clamp'].includes(node.value.toLowerCase()); | ||
} | ||
const center = '50%'; | ||
function isNumberNode(node) { | ||
if (node.type !== 'word') { | ||
return false; | ||
} | ||
const horizontal = { | ||
right: '100%', | ||
left: '0' | ||
}; | ||
const value = parseFloat(node.value); | ||
return !isNaN(value); | ||
} | ||
const vertical = { | ||
bottom: '100%', | ||
top: '0' | ||
}; | ||
function isDimensionNode(node) { | ||
if (node.type !== 'word') { | ||
return false; | ||
} | ||
const parsed = (0, _postcssValueParser.unit)(node.value); | ||
if (!parsed) { | ||
return false; | ||
} | ||
return parsed.unit !== ''; | ||
} | ||
function transform(value) { | ||
const parsed = (0, _postcssValueParser2.default)(value); | ||
const args = (0, _cssnanoUtilGetArguments2.default)(parsed); | ||
const relevant = []; | ||
const parsed = (0, _postcssValueParser.default)(value); | ||
const ranges = []; | ||
let rangeIndex = 0; | ||
let shouldContinue = true; | ||
parsed.nodes.forEach((node, index) => { | ||
// After comma (`,`) follows next background | ||
if (isCommaNode(node)) { | ||
rangeIndex += 1; | ||
shouldContinue = true; | ||
return; | ||
} | ||
args.forEach(arg => { | ||
relevant.push({ | ||
start: null, | ||
end: null | ||
}); | ||
if (!shouldContinue) { | ||
return; | ||
} // After separator (`/`) follows `background-size` values | ||
// Avoid them | ||
arg.forEach((part, index) => { | ||
const isPosition = ~directions.indexOf(part.value.toLowerCase()) || (0, _postcssValueParser.unit)(part.value); | ||
const len = relevant.length - 1; | ||
if (relevant[len].start === null && isPosition) { | ||
relevant[len].start = index; | ||
relevant[len].end = index; | ||
if (node.type === 'div' && node.value === '/') { | ||
shouldContinue = false; | ||
return; | ||
} | ||
return; | ||
} | ||
if (!ranges[rangeIndex]) { | ||
ranges[rangeIndex] = { | ||
start: null, | ||
end: null | ||
}; | ||
} // Do not try to be processed `var and `env` function inside background | ||
if (relevant[len].start !== null) { | ||
if (part.type === 'space') { | ||
return; | ||
} else if (isPosition) { | ||
relevant[len].end = index; | ||
return; | ||
} | ||
if (isVariableFunctionNode(node)) { | ||
shouldContinue = false; | ||
ranges[rangeIndex].start = null; | ||
ranges[rangeIndex].end = null; | ||
return; | ||
} | ||
return; | ||
} | ||
}); | ||
}); | ||
const isPositionKeyword = node.type === 'word' && directionKeywords.includes(node.value.toLowerCase()) || isDimensionNode(node) || isNumberNode(node) || isMathFunctionNode(node); | ||
relevant.forEach((range, index) => { | ||
if (range.start === null) { | ||
return; | ||
} | ||
if (ranges[rangeIndex].start === null && isPositionKeyword) { | ||
ranges[rangeIndex].start = index; | ||
ranges[rangeIndex].end = index; | ||
return; | ||
} | ||
const position = args[index].slice(range.start, range.end + 1); | ||
if (ranges[rangeIndex].start !== null) { | ||
if (node.type === 'space') { | ||
return; | ||
} else if (isPositionKeyword) { | ||
ranges[rangeIndex].end = index; | ||
return; | ||
} | ||
if (position.length > 3) { | ||
return; | ||
} | ||
return; | ||
} | ||
}); | ||
ranges.forEach(range => { | ||
if (range.start === null) { | ||
return; | ||
} | ||
const firstValue = position[0].value.toLowerCase(); | ||
const secondValue = position[2] && position[2].value ? position[2].value.toLowerCase() : null; | ||
const nodes = parsed.nodes.slice(range.start, range.end + 1); | ||
if (position.length === 1 || secondValue === 'center') { | ||
if (secondValue) { | ||
position[2].value = position[1].value = ''; | ||
} | ||
if (nodes.length > 3) { | ||
return; | ||
} | ||
const map = Object.assign({}, horizontal, { | ||
center | ||
}); | ||
const firstNode = nodes[0].value.toLowerCase(); | ||
const secondNode = nodes[2] && nodes[2].value ? nodes[2].value.toLowerCase() : null; | ||
if ((0, _has2.default)(map, firstValue)) { | ||
position[0].value = map[firstValue]; | ||
} | ||
if (nodes.length === 1 || secondNode === 'center') { | ||
if (secondNode) { | ||
nodes[2].value = nodes[1].value = ''; | ||
} | ||
return; | ||
} | ||
const map = Object.assign({}, horizontal, { | ||
center | ||
}); | ||
if (firstValue === 'center' && ~directions.indexOf(secondValue)) { | ||
position[0].value = position[1].value = ''; | ||
if (Object.prototype.hasOwnProperty.call(map, firstNode)) { | ||
nodes[0].value = map[firstNode]; | ||
} | ||
if ((0, _has2.default)(horizontal, secondValue)) { | ||
position[2].value = horizontal[secondValue]; | ||
} | ||
return; | ||
} | ||
return; | ||
} | ||
if (firstNode === 'center' && directionKeywords.includes(secondNode)) { | ||
nodes[0].value = nodes[1].value = ''; | ||
if ((0, _has2.default)(horizontal, firstValue) && (0, _has2.default)(vertical, secondValue)) { | ||
position[0].value = horizontal[firstValue]; | ||
position[2].value = vertical[secondValue]; | ||
if (Object.prototype.hasOwnProperty.call(horizontal, secondNode)) { | ||
nodes[2].value = horizontal[secondNode]; | ||
} | ||
return; | ||
} else if ((0, _has2.default)(vertical, firstValue) && (0, _has2.default)(horizontal, secondValue)) { | ||
position[0].value = horizontal[secondValue]; | ||
position[2].value = vertical[firstValue]; | ||
return; | ||
} | ||
return; | ||
} | ||
}); | ||
return parsed.toString(); | ||
if (Object.prototype.hasOwnProperty.call(horizontal, firstNode) && Object.prototype.hasOwnProperty.call(verticalValue, secondNode)) { | ||
nodes[0].value = horizontal[firstNode]; | ||
nodes[2].value = verticalValue[secondNode]; | ||
return; | ||
} else if (Object.prototype.hasOwnProperty.call(verticalValue, firstNode) && Object.prototype.hasOwnProperty.call(horizontal, secondNode)) { | ||
nodes[0].value = horizontal[secondNode]; | ||
nodes[2].value = verticalValue[firstNode]; | ||
return; | ||
} | ||
}); | ||
return parsed.toString(); | ||
} | ||
exports.default = (0, _postcss.plugin)('postcss-normalize-positions', () => { | ||
return css => { | ||
const cache = {}; | ||
function pluginCreator() { | ||
return { | ||
postcssPlugin: 'postcss-normalize-positions', | ||
css.walkDecls(/^(background(-position)?|(-webkit-)?perspective-origin)$/i, decl => { | ||
const value = decl.value; | ||
OnceExit(css) { | ||
const cache = {}; | ||
css.walkDecls(/^(background(-position)?|(-\w+-)?perspective-origin)$/i, decl => { | ||
const value = decl.value; | ||
if (cache[value]) { | ||
decl.value = cache[value]; | ||
if (!value) { | ||
return; | ||
} | ||
return; | ||
} | ||
if (cache[value]) { | ||
decl.value = cache[value]; | ||
return; | ||
} | ||
const result = transform(value); | ||
const result = transform(value); | ||
decl.value = result; | ||
cache[value] = result; | ||
}); | ||
} | ||
decl.value = result; | ||
cache[value] = result; | ||
}); | ||
}; | ||
}); | ||
module.exports = exports['default']; | ||
}; | ||
} | ||
pluginCreator.postcss = true; | ||
var _default = pluginCreator; | ||
exports.default = _default; | ||
module.exports = exports.default; |
{ | ||
"name": "postcss-normalize-positions", | ||
"version": "4.0.2", | ||
"version": "5.0.0-rc.0", | ||
"description": "Normalize keyword values for position into length values.", | ||
@@ -11,3 +11,5 @@ "main": "dist/index.js", | ||
"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" | ||
}, | ||
@@ -20,6 +22,2 @@ "keywords": [ | ||
"license": "MIT", | ||
"devDependencies": { | ||
"babel-cli": "^6.0.0", | ||
"cross-env": "^5.0.0" | ||
}, | ||
"homepage": "https://github.com/cssnano/cssnano", | ||
@@ -33,6 +31,3 @@ "author": { | ||
"dependencies": { | ||
"cssnano-util-get-arguments": "^4.0.0", | ||
"has": "^1.0.0", | ||
"postcss": "^7.0.0", | ||
"postcss-value-parser": "^3.0.0" | ||
"postcss-value-parser": "^4.1.0" | ||
}, | ||
@@ -43,4 +38,11 @@ "bugs": { | ||
"engines": { | ||
"node": ">=6.9.0" | ||
} | ||
"node": "^10 || ^12 || >=14.0" | ||
}, | ||
"devDependencies": { | ||
"postcss": "^8.2.1" | ||
}, | ||
"peerDependencies": { | ||
"postcss": "^8.2.1" | ||
}, | ||
"gitHead": "8c16e67a4d24a13ac7e09a36d4faf504196efd0f" | ||
} |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10695
58.77%2
-50%1
-50%5
25%165
55.66%1
Infinity%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated