mirror of https://github.com/sveltejs/svelte
parent
03a9ce353a
commit
fd177771af
@ -1,41 +0,0 @@
|
|||||||
import { Node } from '../interfaces';
|
|
||||||
|
|
||||||
export function groupSelectors(selector: Node) {
|
|
||||||
let block = {
|
|
||||||
global: selector.children[0].type === 'PseudoClassSelector' && selector.children[0].name === 'global',
|
|
||||||
selectors: [],
|
|
||||||
combinator: null
|
|
||||||
};
|
|
||||||
|
|
||||||
const blocks = [block];
|
|
||||||
|
|
||||||
selector.children.forEach((child: Node, i: number) => {
|
|
||||||
if (child.type === 'WhiteSpace' || child.type === 'Combinator') {
|
|
||||||
const next = selector.children[i + 1];
|
|
||||||
|
|
||||||
block = {
|
|
||||||
global: next.type === 'PseudoClassSelector' && next.name === 'global',
|
|
||||||
selectors: [],
|
|
||||||
combinator: child
|
|
||||||
};
|
|
||||||
|
|
||||||
blocks.push(block);
|
|
||||||
} else {
|
|
||||||
block.selectors.push(child);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return blocks;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function walkRules(nodes: Node[], callback: (node: Node) => void) {
|
|
||||||
nodes.forEach((node: Node) => {
|
|
||||||
if (node.type === 'Rule') {
|
|
||||||
callback(node);
|
|
||||||
} else if (node.type === 'Atrule') {
|
|
||||||
if (node.name === 'media' || node.name === 'supports' || node.name === 'document') {
|
|
||||||
walkRules(node.block.children, callback);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
import { groupSelectors, walkRules } from '../../utils/css';
|
|
||||||
import { Validator } from '../index';
|
|
||||||
import { Node } from '../../interfaces';
|
|
||||||
|
|
||||||
export default function validateCss(validator: Validator, css: Node) {
|
|
||||||
walkRules(css.children, rule => {
|
|
||||||
rule.selector.children.forEach(validateSelector);
|
|
||||||
});
|
|
||||||
|
|
||||||
function validateSelector(selector: Node) {
|
|
||||||
const blocks = groupSelectors(selector);
|
|
||||||
|
|
||||||
blocks.forEach((block) => {
|
|
||||||
let i = block.selectors.length;
|
|
||||||
while (i-- > 1) {
|
|
||||||
const part = block.selectors[i];
|
|
||||||
if (part.type === 'PseudoClassSelector' && part.name === 'global') {
|
|
||||||
validator.error(`:global(...) must be the first element in a compound selector`, part.start);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let start = 0;
|
|
||||||
let end = blocks.length;
|
|
||||||
|
|
||||||
for (; start < end; start += 1) {
|
|
||||||
if (!blocks[start].global) break;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (; end > start; end -= 1) {
|
|
||||||
if (!blocks[end - 1].global) break;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = start; i < end; i += 1) {
|
|
||||||
if (blocks[i].global) {
|
|
||||||
validator.error(`:global(...) can be at the start or end of a selector sequence, but not in the middle`, blocks[i].selectors[0].start);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue