You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/src/parse/read/style.js

48 lines
916 B

import parse from 'css-tree/lib/parser/index.js';
import walk from 'css-tree/lib/utils/walk.js';
export default function readStyle ( parser, start, attributes ) {
const contentStart = parser.index;
const styles = parser.readUntil( /<\/style>/ );
const contentEnd = parser.index;
let ast;
try {
ast = parse( styles, {
positions: true,
offset: contentStart
});
} catch ( err ) {
if ( err.name === 'CssSyntaxError' ) {
parser.error( err.message, err.offset );
} else {
throw err;
}
}
// tidy up AST
walk.all( ast, node => {
if ( node.loc ) {
node.start = node.loc.start.offset;
node.end = node.loc.end.offset;
delete node.loc;
}
});
parser.eat( '</style>', true );
const end = parser.index;
return {
start,
end,
attributes,
children: JSON.parse( JSON.stringify( ast.children ) ),
content: {
start: contentStart,
end: contentEnd,
styles
}
};
}