.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / postcss-html / lib / parser.js
1 "use strict";
2
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");
8
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)) {
12                 return;
13         }
14
15         source = source && source.toString();
16         const styleTag = htmlParser(source, opts);
17         const stylesMd = mdParser(source, opts);
18         if (!styleTag && !stylesMd) {
19                 return;
20         }
21         const document = new Root();
22         const documentLocalFixer = new BlockFixer(source);
23         let index = 0;
24         [].concat(styleTag).concat(stylesMd).filter(Boolean).sort(function (a, b) {
25                 return a.startIndex - b.startIndex;
26         }).forEach(style => {
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);
32         });
33         document.raws.afterEnd = source.slice(index);
34         document.source = {
35                 input: new Input(source, opts),
36                 start: {
37                         line: 1,
38                         column: 1,
39                 },
40         };
41
42         document.each = function (callback) {
43                 let wasBreak, lastResult;
44                 this.nodes.forEach(node => {
45                         const result = node.each(callback);
46                         if (result === false) {
47                                 wasBreak = true;
48                         } else {
49                                 lastResult = result;
50                         }
51                 });
52                 if (wasBreak) {
53                         return false;
54                 } else {
55                         return lastResult;
56                 }
57         };
58
59         document.append = function () {
60                 this.last.append.apply(
61                         this.last,
62                         Array.from(arguments)
63                 );
64                 return this;
65         };
66
67         document.prepend = function () {
68                 this.first.prepend.apply(
69                         this.first,
70                         Array.from(arguments)
71                 );
72                 return this;
73         };
74
75         document.insertBefore = function (exist, add) {
76                 exist.prepend(add);
77                 return this;
78         };
79
80         document.insertAfter = function (exist, add) {
81                 exist.append(add);
82                 return this;
83         };
84
85         return document;
86 }
87
88 module.exports = parser;