svelte-ast-print
Version:
Serialize Svelte AST nodes into stringified syntax. A.k.a parse in reverse.
215 lines • 5.63 kB
JavaScript
/**
* Printers related to CSS **selector** related AST nodes only.
* @module svelte-ast-print/css/selector
*/
import * as char from "../_internal/char.js";
import { State } from "../_internal/shared.js";
import { DoubleQuotes, RoundBrackets, SquareBrackets } from "../_internal/wrapper.js";
/**
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors/Selectors_and_combinators#combinators}
*
* @since 1.0.0
* @__NO_SIDE_EFFECTS__
*/
export function printCSSCombinator(n, opts = {}) {
const st = State.get(n, opts);
st.add(n.name);
return st.result;
}
/**
* @since 1.0.0
* @__NO_SIDE_EFFECTS__
*/
export function printCSSSimpleSelector(n, opts = {}) {
// biome-ignore format: Prettier
// prettier-ignore
switch (n.type) {
case "AttributeSelector": return printCSSAttributeSelector(n, opts);
case "ClassSelector": return printCSSClassSelector(n, opts);
case "IdSelector": return printCSSIdSelector(n, opts);
case "NestingSelector": return printCSSNestingSelector(n, opts);
case "PseudoClassSelector": return printCSSPseudoClassSelector(n, opts);
case "PseudoElementSelector": return printCSSPseudoElementSelector(n, opts);
case "TypeSelector": return printCSSTypeSelector(n, opts);
case "Nth": return printCSSNth(n, opts);
case "Percentage": return printCSSPercentage(n, opts);
}
}
/**
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors}
*
* @example syntax
* ```css
* [name<?matcher><?"value"> flags?]
* ```
*
* @since 1.0.0
* @__NO_SIDE_EFFECTS__
*/
export function printCSSAttributeSelector(n, opts = {}) {
const st = State.get(n, opts);
const brackets = new SquareBrackets("inline",
//
n.name, n.matcher, n.value && new DoubleQuotes("inline", n.value), n.flags && [char.SPACE, n.flags]);
st.add(brackets);
return st.result;
}
/**
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/Class_selectors}
*
* @example simple
* ```css
* .class_name { style properties }
* ```
*
* @example equivalent
* ```css
* [class~=class_name] { style properties }
* ```
*
* @since 1.0.0
* @__NO_SIDE_EFFECTS__
*/
export function printCSSClassSelector(n, opts = {}) {
const st = State.get(n, opts);
st.add(".", n.name);
return st.result;
}
/**
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/ID_selectors}
*
* @example pattern
* ```css
* #name
* ```
*
* @since 1.0.0
* @__NO_SIDE_EFFECTS__
*/
export function printCSSIdSelector(n, opts = {}) {
const st = State.get(n, opts);
st.add("#", n.name);
return st.result;
}
/**
* @since 1.0.0
* @__NO_SIDE_EFFECTS__
*/
export function printCSSNestingSelector(n, opts = {}) {
const st = State.get(n, opts);
st.add(n.name);
return st.result;
}
/**
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes}
*
* @since 1.0.0
* @__NO_SIDE_EFFECTS__
*/
export function printCSSPseudoClassSelector(n, opts = {}) {
const st = State.get(n, opts);
st.add(
//
char.COLON, n.name, n.args && new RoundBrackets("inline", printCSSSelectorList(n.args, opts)));
return st.result;
}
/**
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements}
*
* @since 1.0.0
*
* @__NO_SIDE_EFFECTS__
*
* > [!WARNING]
* > It doesn't support args, e.g. `::part()` or `::slotted()`
*/
export function printCSSPseudoElementSelector(n, opts = {}) {
const st = State.get(n, opts);
st.add(char.COLON.repeat(2), n.name);
return st.result;
}
/**
* @since 1.0.0
* @__NO_SIDE_EFFECTS__
*/
export function printCSSRelativeSelector(n, opts = {}) {
const st = State.get(n, opts);
if (n.combinator)
st.add(printCSSCombinator(n.combinator, opts), char.SPACE);
for (const s of n.selectors)
st.add(printCSSSimpleSelector(s, opts));
return st.result;
}
/**
* @since 1.0.0
* @__NO_SIDE_EFFECTS__
*/
export function printCSSTypeSelector(n, opts = {}) {
const st = State.get(n, opts);
st.add(n.name);
return st.result;
}
/**
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child}
*
* @example pattern
* ```css
* :nth-child(<nth> [of <complex-selector-list>]?) { }
* ```
*
* @since 1.0.0
* @__NO_SIDE_EFFECTS__
*/
export function printCSSNth(n, opts = {}) {
const st = State.get(n, opts);
st.add(char.COLON, "nth-child", new RoundBrackets("inline", n.value));
return st.result;
}
/**
* @since 1.0.0
* @__NO_SIDE_EFFECTS__
*/
export function printCSSPercentage(n, opts = {}) {
const st = State.get(n, opts);
st.add(n.value);
return st.result;
}
/**
* @since 1.0.0
* @__NO_SIDE_EFFECTS__
*/
export function printCSSSelectorList(n, opts = {}) {
const st = State.get(n, opts);
for (const [idx, ch] of n.children.entries()) {
st.add(
//
idx > 0 && char.SPACE, printCSSComplexSelector(ch, opts));
}
return st.result;
}
/**
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/Complex_selectors}
*
* @example simple
* ```css
* .class_name { style properties }
* ```
*
* @example equivalent
* ```css
* [class~=class_name] { style properties }
* ```
*
* @since 1.0.0
* @__NO_SIDE_EFFECTS__
*/
export function printCSSComplexSelector(n, opts = {}) {
const st = State.get(n, opts);
for (const [idx, c] of n.children.entries()) {
st.add(
//
printCSSRelativeSelector(c, opts), idx !== n.children.length - 1 && char.SPACE);
}
return st.result;
}
//# sourceMappingURL=selector.js.map