3 const Input = require("postcss/lib/input");
4 const Root = require("postcss/lib/root");
5 const htmlParser = require("./html-parser");
6 const mdParser = require("./markdown-parser");
7 const BlockFixer = require("./block-fixer");
9 function parser (source, opts) {
10 // Skip known style sheet files.
11 if (opts.from && /\.(?:(?:p(?:ost)?)?css|(?:wx|le|sa|sc|s)ss|styl(?:us)?)(?:\?.+)?$/i.test(opts.from)) {
15 source = source && source.toString();
16 const styleTag = htmlParser(source, opts);
17 const stylesMd = mdParser(source, opts);
18 if (!styleTag && !stylesMd) {
21 const document = new Root();
22 const documentLocalFixer = new BlockFixer(source);
24 [].concat(styleTag).concat(stylesMd).filter(Boolean).sort(function (a, b) {
25 return a.startIndex - b.startIndex;
27 const localFixer = documentLocalFixer.block(style);
28 const root = localFixer.parse(opts);
29 root.raws.beforeStart = source.slice(index, style.startIndex);
30 index = style.startIndex + style.content.length;
31 document.nodes.push(root);
33 document.raws.afterEnd = source.slice(index);
35 input: new Input(source, opts),
42 document.each = function (callback) {
43 let wasBreak, lastResult;
44 this.nodes.forEach(node => {
45 const result = node.each(callback);
46 if (result === false) {
59 document.append = function () {
60 this.last.append.apply(
67 document.prepend = function () {
68 this.first.prepend.apply(
75 document.insertBefore = function (exist, add) {
80 document.insertAfter = function (exist, add) {
88 module.exports = parser;