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; }
5 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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; }
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); }
11 var Declaration = require('../declaration');
15 function track(start, end) {
16 return { start: start, end: end, span: end - start };
19 function getRows(tpl) {
20 return tpl.trim().slice(1, -1).split(/['"]\s*['"]?/g);
23 function getColumns(line) {
24 return line.trim().split(/\s+/g);
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') {
34 column: track(columnIndex + 1, columnIndex + 2),
35 row: track(rowIndex + 1, rowIndex + 2)
38 var _areas$area = areas[area],
39 column = _areas$area.column,
40 row = _areas$area.row;
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;
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;
56 var GridTemplateAreas = function (_Declaration) {
57 _inherits(GridTemplateAreas, _Declaration);
59 function GridTemplateAreas() {
60 _classCallCheck(this, GridTemplateAreas);
62 return _possibleConstructorReturn(this, _Declaration.apply(this, arguments));
65 GridTemplateAreas.prototype.getRoot = function getRoot(parent) {
66 if (parent.type === 'atrule' || !parent.parent) {
69 return this.getRoot(parent.parent);
73 * Translate grid-template-areas to separate -ms- prefixed properties
77 GridTemplateAreas.prototype.insert = function insert(decl, prefix, prefixes, result) {
78 if (prefix !== '-ms-') return _Declaration.prototype.insert.call(this, decl, prefix, prefixes);
80 var areas = parseGridAreas(decl.value);
81 var missed = Object.keys(areas);
83 this.getRoot(decl.parent).walkDecls('grid-area', function (gridArea) {
84 var value = gridArea.value;
85 var area = areas[value];
87 missed = missed.filter(function (e) {
92 gridArea.parent.walkDecls(/-ms-grid-(row|column)/, function (d) {
96 gridArea.cloneBefore({
98 value: String(area.row.start)
100 if (area.row.span > 1) {
101 gridArea.cloneBefore({
102 prop: '-ms-grid-row-span',
103 value: String(area.row.span)
106 gridArea.cloneBefore({
107 prop: '-ms-grid-column',
108 value: String(area.column.start)
110 if (area.column.span > 1) {
111 gridArea.cloneBefore({
112 prop: '-ms-grid-column-span',
113 value: String(area.column.span)
120 if (missed.length > 0) {
121 decl.warn(result, 'Can not find grid areas: ' + missed.join(', '));
127 return GridTemplateAreas;
130 Object.defineProperty(GridTemplateAreas, 'names', {
133 value: ['grid-template-areas']
137 module.exports = GridTemplateAreas;