travel/admin/node_modules/svg-baker/lib/transformations/prefix-style-selectors.js

31 lines
777 B
JavaScript
Raw Normal View History

2024-06-24 11:28:18 +08:00
const Promise = require('bluebird');
const decodeEntities = require('he').decode;
const postcss = require('postcss');
const prefixSelectors = require('postcss-prefix-selector');
/**
* @return {Function} PostHTML plugin
*/
function prefixStyleSelectors(prefix) {
return (tree) => {
const styleNodes = [];
tree.match({ tag: 'style' }, (node) => {
styleNodes.push(node);
return node;
});
return Promise.map(styleNodes, (node) => {
const content = node.content ? decodeEntities(node.content.join('')) : '';
return postcss()
.use(prefixSelectors({ prefix }))
.process(content)
.then(prefixedStyles => node.content = prefixedStyles.css);
}).then(() => tree);
};
}
module.exports = prefixStyleSelectors;