.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / postcss-html / lib / block-fixer.js
1 "use strict";
2 const reNewLine = /(?:\r?\n|\r)/gm;
3 const getSyntax = require("./get-syntax");
4
5 function DocumentFixer (source) {
6         let match;
7         const lines = [];
8         reNewLine.lastIndex = 0;
9         while ((match = reNewLine.exec(source))) {
10                 lines.push(match.index);
11         }
12         this.lines = lines;
13 }
14
15 DocumentFixer.prototype = {
16         block: function (style) {
17                 return new LocalFixer(this.lines, style);
18         },
19 };
20
21 function LocalFixer (lines, style) {
22         let line = 0;
23         let column = style.startIndex;
24         lines.some((lineEndIndex, lineNumber) => {
25                 if (lineEndIndex >= style.startIndex) {
26                         line = lineNumber--;
27                         if (lineNumber in lines) {
28                                 column = style.startIndex - lines[lineNumber] - 1;
29                         }
30                         return true;
31                 }
32         });
33
34         this.line = line;
35         this.column = column;
36         this.style = style;
37 }
38
39 LocalFixer.prototype = {
40         object: function (object) {
41                 if (object) {
42                         if (object.line === 1) {
43                                 object.column += this.column;
44                         }
45                         object.line += this.line;
46                 }
47         },
48         node: function (node) {
49                 this.object(node.source.start);
50                 this.object(node.source.end);
51         },
52         root: function (root) {
53                 this.object(root.source.start);
54                 root.walk(node => {
55                         this.node(node);
56                 });
57         },
58         error: function (error) {
59                 if (error && error.name === "CssSyntaxError") {
60                         this.object(error);
61                         this.object(error.input);
62                         error.message = error.message.replace(/:\d+:\d+:/, ":" + error.line + ":" + error.column + ":");
63                 }
64                 return error;
65         },
66         parse: function (opts) {
67                 const style = this.style;
68                 const syntax = getSyntax(opts, style.lang);
69                 let root;
70                 try {
71                         root = syntax.parse(style.content, Object.assign({}, opts, {
72                                 map: false,
73                         }));
74                 } catch (error) {
75                         throw this.error(error);
76                 }
77                 this.root(root);
78                 Object.assign(root.source, {
79                         isHTMLAttribute: !!style.isHTMLAttribute,
80                         isMarkdown: !!style.isMarkdown,
81                         isHTMLTag: !!style.isHTMLTag,
82                         lang: style.lang || "css",
83                         syntax,
84                 });
85
86                 return root;
87         },
88 };
89
90 module.exports = DocumentFixer;