.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / autoprefixer / lib / hacks / grid-template-areas.js
1 'use strict';
2
3 function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
4
5 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
7 function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
8
9 function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }
10
11 var Declaration = require('../declaration');
12
13 var DOTS = /^\.+$/;
14
15 function track(start, end) {
16     return { start: start, end: end, span: end - start };
17 }
18
19 function getRows(tpl) {
20     return tpl.trim().slice(1, -1).split(/['"]\s*['"]?/g);
21 }
22
23 function getColumns(line) {
24     return line.trim().split(/\s+/g);
25 }
26
27 function parseGridAreas(tpl) {
28     return getRows(tpl).reduce(function (areas, line, rowIndex) {
29         if (line.trim() === '') return areas;
30         getColumns(line).forEach(function (area, columnIndex) {
31             if (DOTS.test(area)) return;
32             if (typeof areas[area] === 'undefined') {
33                 areas[area] = {
34                     column: track(columnIndex + 1, columnIndex + 2),
35                     row: track(rowIndex + 1, rowIndex + 2)
36                 };
37             } else {
38                 var _areas$area = areas[area],
39                     column = _areas$area.column,
40                     row = _areas$area.row;
41
42
43                 column.start = Math.min(column.start, columnIndex + 1);
44                 column.end = Math.max(column.end, columnIndex + 2);
45                 column.span = column.end - column.start;
46
47                 row.start = Math.min(row.start, rowIndex + 1);
48                 row.end = Math.max(row.end, rowIndex + 2);
49                 row.span = row.end - row.start;
50             }
51         });
52         return areas;
53     }, {});
54 }
55
56 var GridTemplateAreas = function (_Declaration) {
57     _inherits(GridTemplateAreas, _Declaration);
58
59     function GridTemplateAreas() {
60         _classCallCheck(this, GridTemplateAreas);
61
62         return _possibleConstructorReturn(this, _Declaration.apply(this, arguments));
63     }
64
65     GridTemplateAreas.prototype.getRoot = function getRoot(parent) {
66         if (parent.type === 'atrule' || !parent.parent) {
67             return parent;
68         }
69         return this.getRoot(parent.parent);
70     };
71
72     /**
73      * Translate grid-template-areas to separate -ms- prefixed properties
74      */
75
76
77     GridTemplateAreas.prototype.insert = function insert(decl, prefix, prefixes, result) {
78         if (prefix !== '-ms-') return _Declaration.prototype.insert.call(this, decl, prefix, prefixes);
79
80         var areas = parseGridAreas(decl.value);
81         var missed = Object.keys(areas);
82
83         this.getRoot(decl.parent).walkDecls('grid-area', function (gridArea) {
84             var value = gridArea.value;
85             var area = areas[value];
86
87             missed = missed.filter(function (e) {
88                 return e !== value;
89             });
90
91             if (area) {
92                 gridArea.parent.walkDecls(/-ms-grid-(row|column)/, function (d) {
93                     d.remove();
94                 });
95
96                 gridArea.cloneBefore({
97                     prop: '-ms-grid-row',
98                     value: String(area.row.start)
99                 });
100                 if (area.row.span > 1) {
101                     gridArea.cloneBefore({
102                         prop: '-ms-grid-row-span',
103                         value: String(area.row.span)
104                     });
105                 }
106                 gridArea.cloneBefore({
107                     prop: '-ms-grid-column',
108                     value: String(area.column.start)
109                 });
110                 if (area.column.span > 1) {
111                     gridArea.cloneBefore({
112                         prop: '-ms-grid-column-span',
113                         value: String(area.column.span)
114                     });
115                 }
116             }
117             return undefined;
118         });
119
120         if (missed.length > 0) {
121             decl.warn(result, 'Can not find grid areas: ' + missed.join(', '));
122         }
123
124         return decl;
125     };
126
127     return GridTemplateAreas;
128 }(Declaration);
129
130 Object.defineProperty(GridTemplateAreas, 'names', {
131     enumerable: true,
132     writable: true,
133     value: ['grid-template-areas']
134 });
135
136
137 module.exports = GridTemplateAreas;