Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/html/extract-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const htmlparser = require("htmlparser2");
const buildSyntaxResolver = require("../syntax/build-syntax-resolver");
const buildTemplateSyntax = require("../template/syntax");
const SvelteTokenizer = require("./svelte-tokenizer");
const { cssSafeSyntax } = require("../syntax/syntaxes");

function iterateCode(source, { onStyleTag, onStyleAttribute, svelte }) {
const openTag = {};
Expand Down Expand Up @@ -125,6 +126,7 @@ function extractStyles(source, opts) {
/\.(?:\w*html?|xht|xslt?|jsp|aspx?|ejs|php\d*|twig|liquid|m(?:ark)?d(?:ow)?n|mk?d)$/i.test(
opts.from
);
const svelte = opts.from && /\.svelte$/i.test(opts.from);

function onStyleTag(style) {
if (
Expand All @@ -142,7 +144,11 @@ function extractStyles(source, opts) {

function onStyleAttribute(style) {
if (/\{[\s\S]*?\}/.test(style.content)) {
style.syntax = buildTemplateSyntax(resolveSyntax());
style.syntax = buildTemplateSyntax(
resolveSyntax("css", {
defaultSyntax: svelte ? cssSafeSyntax : undefined,
})
);
style.lang = "custom-template";
} else {
style.syntax = resolveSyntax();
Expand All @@ -154,7 +160,7 @@ function extractStyles(source, opts) {
iterateCode(source, {
onStyleTag,
onStyleAttribute,
svelte: opts.from && /\.svelte$/i.test(opts.from),
svelte,
});

return styles;
Expand Down
11 changes: 6 additions & 5 deletions lib/syntax/build-syntax-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ module.exports = function buildSyntaxResolver(config) {
...Object.keys(syntaxes),
]);

return function resolve(baseLang) {
return function resolve(baseLang, baseOptions) {
let lang = baseLang || "css";
const options = baseOptions || {};

const cwd = process.cwd();
const placeholderFilePath = path.join(cwd, `__placeholder__.${lang}`);
Expand All @@ -66,7 +67,7 @@ module.exports = function buildSyntaxResolver(config) {
const syntax = syntaxes[lang] || defaultSyntaxes[lang];
if (syntax) {
if (typeof syntax === "string") {
const syntaxModule = loadFromString(syntax);
const syntaxModule = loadFromString(syntax, options);
if (syntaxModule) {
return syntaxModule;
}
Expand All @@ -88,7 +89,7 @@ module.exports = function buildSyntaxResolver(config) {
return null;
}

return cssSyntax;
return options.defaultSyntax || cssSyntax;
};
};

Expand All @@ -105,9 +106,9 @@ const standardModuleResolvers = {
"postcss-styl": () => require("postcss-styl"),
};

function loadFromString(syntax) {
function loadFromString(syntax, options) {
if (syntax === "postcss") {
return cssSyntax;
return options.defaultSyntax || cssSyntax;
}
if (syntax === "postcss-safe-parser") {
return cssSafeSyntax;
Expand Down
6 changes: 6 additions & 0 deletions test-fixtures/ast/test2.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
let bgOpacity = 0.5;
$: color = bgOpacity < 0.6 ? '#000' : '#fff';
$: styleOpacity = `--opacity: ${bgOpacity};`
</script>
<p style="color: {color}; {styleOpacity}">This is a paragraph.</p>
86 changes: 86 additions & 0 deletions test/__snapshots__/ast.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,92 @@ const buttonPointerEvents = computed(() =>
}
`;

exports[`AST tests test2.svelte ast 1`] = `
Object {
"inputs": Array [
Object {
"css": "color: {color}; {styleOpacity}",
"file": "/test2.svelte",
"hasBOM": false,
},
Object {
"css": "<script>
let bgOpacity = 0.5;
$: color = bgOpacity < 0.6 ? '#000' : '#fff';
$: styleOpacity = \`--opacity: \${bgOpacity};\`
</script>
<p style=\\"color: {color}; {styleOpacity}\\">This is a paragraph.</p>
",
"file": "/test2.svelte",
"hasBOM": false,
},
],
"nodes": Array [
Object {
"document": "$document",
"indexes": Object {},
"lastEach": 1,
"nodes": Array [
Object {
"prop": "color",
"raws": Object {
"before": "",
"between": ": ",
},
"source": Object {
"end": Object {
"column": 25,
"line": 6,
"offset": 158,
},
"inputId": 0,
"start": Object {
"column": 11,
"line": 6,
"offset": 144,
},
},
"type": "decl",
"value": "{color}",
},
],
"raws": Object {
"after": " {styleOpacity}",
"codeAfter": "\\">This is a paragraph.</p>
",
"codeBefore": "<script>
let bgOpacity = 0.5;
$: color = bgOpacity < 0.6 ? '#000' : '#fff';
$: styleOpacity = \`--opacity: \${bgOpacity};\`
</script>
<p style=\\"",
"semicolon": true,
},
"source": Object {
"end": undefined,
"inputId": 0,
"start": Object {
"column": 11,
"line": 6,
"offset": 144,
},
},
"type": "root",
},
],
"raws": Object {},
"source": Object {
"end": undefined,
"inputId": 0,
"start": Object {
"column": 1,
"line": 1,
},
},
"type": "document",
}
`;

exports[`AST tests unknown-lang.vue ast 1`] = `
Object {
"inputs": Array [
Expand Down