react-ionicons
Version:
A React SVG ionicon component
81 lines (74 loc) • 1.98 kB
JavaScript
function selectors(parent, node) {
var result = [];
parent.selectors.forEach(function (i) {
node.selectors.forEach(function (j) {
if (j.indexOf('&') === -1) {
result.push(i + ' ' + j);
} else {
result.push(j.replace(/&/g, i));
}
});
});
return result;
}
function pickComment(comment, after) {
if (comment && comment.type === 'comment') {
return comment.moveAfter(after);
} else {
return after;
}
}
function atruleChilds(rule, atrule) {
var children = [];
atrule.each(function (child) {
if (child.type === 'comment') {
children.push(child);
}
if (child.type === 'decl') {
children.push(child);
} else if (child.type === 'rule') {
child.selectors = selectors(rule, child);
} else if (child.type === 'atrule') {
atruleChilds(rule, child);
}
});
if (children.length) {
var clone = rule.clone({ nodes: [] });
for (var i = 0; i < children.length; i++) children[i].moveTo(clone);
atrule.prepend(clone);
}
}
function processRule(rule, bubble) {
var unwrapped = false;
var after = rule;
rule.each(function (child) {
if (child.type === 'rule') {
unwrapped = true;
child.selectors = selectors(rule, child);
after = pickComment(child.prev(), after);
after = child.moveAfter(after);
} else if (child.type === 'atrule') {
if (bubble.indexOf(child.name) !== -1) {
unwrapped = true;
atruleChilds(rule, child);
after = pickComment(child.prev(), after);
after = child.moveAfter(after);
}
}
});
if (unwrapped) {
rule.raws.semicolon = true;
if (rule.nodes.length === 0) rule.remove();
}
}
var bubble = ['media', 'supports', 'document'];
const process = node => {
node.each(function (child) {
if (child.type === 'rule') {
processRule(child, bubble);
} else if (child.type === 'atrule') {
process(child);
}
});
};
export default process;