Skip to content

Commit

Permalink
[css-grid] Fix/row column span inheritance bug (#1147)
Browse files Browse the repository at this point in the history
* Fix bug when updated row/column span values weren't applied

* Update the test cases to show that the bug is fixed

* Update the test assertion
  • Loading branch information
bogdan0083 authored and ai committed Oct 23, 2018
1 parent 6b8a25d commit 1956607
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 30 deletions.
38 changes: 18 additions & 20 deletions lib/hacks/grid-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,21 @@ function parseGridTemplatesData (css) {
return uniq(acc)
}, [])

// update grid-row/column-span values for areas with duplicate
// area names. @see #1084 and #1146
rules.forEach(r => {
areaNames.forEach(name => {
let area = r.areas[name]
if (area && area.row.span !== areas[name].row.span) {
areas[name].row.updateSpan = true
}

if (area && area.column.span !== areas[name].column.span) {
areas[name].column.updateSpan = true
}
})
})

parsed[index].allAreas = uniq([...allAreas, ...areaNames])
parsed[index].rules.push({
hasDuplicates: !hasNoDuplicates,
Expand Down Expand Up @@ -520,7 +535,7 @@ function insertAreas (css, isDisabled) {
let cloned = gridAreaRule.clone()
cloned.removeAll()

getMSDecls(area, false, false).reverse()
getMSDecls(area, area.row.updateSpan, area.column.updateSpan).reverse()
.forEach(i => cloned.prepend(
Object.assign(i, {
raws: {
Expand All @@ -546,7 +561,7 @@ function insertAreas (css, isDisabled) {
// grid-template has duplicates and not inside media rule
// see the bottom of grid-template-areas test case for example
gridAreaRule.walkDecls(/-ms-grid-(row|column)/, d => d.remove())
getMSDecls(area, false, false).reverse()
getMSDecls(area, area.row.updateSpan, area.column.updateSpan).reverse()
.forEach(i => gridAreaRule.prepend(
Object.assign(i, {
raws: {
Expand All @@ -562,24 +577,7 @@ function insertAreas (css, isDisabled) {
let cloned = gridAreaRule.clone()
cloned.removeAll()

// check if we need to add -ms-grid-row/column-span values
// by comparing areas for rules with the same selectors
// @see #1084
let [addRowSpan, addColumnSpan] = data.rules.reduce((acc, r) => {
if (!r.areas[value]) {
return acc
}
let isEqual = selectorsEqual(r, rule)
if (isEqual && r.areas[value].row.span !== area.row.span) {
acc[0] = true
}
if (isEqual && r.areas[value].column.span !== area.column.span) {
acc[1] = true
}
return acc
}, [false, false])

getMSDecls(area, addRowSpan, addColumnSpan).reverse()
getMSDecls(area, area.row.updateSpan, area.column.updateSpan).reverse()
.forEach(i => cloned.prepend(
Object.assign(i, {
raws: {
Expand Down
12 changes: 6 additions & 6 deletions test/autoprefixer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,22 +632,22 @@ describe('hacks', () => {
.map(i => i.toString())
.filter(str => str.includes('grid-template')))
.toEqual([
'autoprefixer: <css input>:143:3: Autoprefixer cannot find ' +
'autoprefixer: <css input>:144:3: Autoprefixer cannot find ' +
'a grid-template containing the duplicate grid-area ' +
'"child" with full selector matching: .uncle',
'autoprefixer: <css input>:148:3: Autoprefixer cannot find ' +
'autoprefixer: <css input>:149:3: Autoprefixer cannot find ' +
'a grid-template containing the duplicate grid-area ' +
'"child" with full selector matching: .uncle',
'autoprefixer: <css input>:153:3: Autoprefixer cannot find ' +
'autoprefixer: <css input>:154:3: Autoprefixer cannot find ' +
'a grid-template containing the duplicate grid-area ' +
'"child" with full selector matching: .grand-parent .uncle-second',
'autoprefixer: <css input>:158:3: Autoprefixer cannot find ' +
'autoprefixer: <css input>:159:3: Autoprefixer cannot find ' +
'a grid-template containing the duplicate grid-area ' +
'"child" with full selector matching: .grand-parent .uncle-second',
'autoprefixer: <css input>:163:3: Autoprefixer cannot find ' +
'autoprefixer: <css input>:164:3: Autoprefixer cannot find ' +
'a grid-template containing the duplicate grid-area ' +
'"child" with full selector matching: .grand-parent .father.uncle',
'autoprefixer: <css input>:168:3: Autoprefixer cannot find ' +
'autoprefixer: <css input>:169:3: Autoprefixer cannot find ' +
'a grid-template containing the duplicate grid-area ' +
'"child" with full selector matching: .grand-parent.uncle .father'
])
Expand Down
3 changes: 2 additions & 1 deletion test/cases/grid-template-areas.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
.grid {
display: grid;
grid-template-areas:
"a-conflict b-conflict";
"a-conflict a-conflict"
"b-conflict b-conflict";
}

.grid.conflict {
Expand Down
11 changes: 8 additions & 3 deletions test/cases/grid-template-areas.out.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
display: -ms-grid;
display: grid;
grid-template-areas:
"a-conflict b-conflict";
"a-conflict a-conflict"
"b-conflict b-conflict";
}

.grid.conflict {
Expand All @@ -72,23 +73,27 @@
.a {
-ms-grid-row: 1;
-ms-grid-column: 1;
-ms-grid-column-span: 2;
grid-area: a-conflict;
}

.grid.conflict > .a {
-ms-grid-row: 1;
-ms-grid-column: 1;
-ms-grid-column-span: 1;
}

.b {
-ms-grid-row: 1;
-ms-grid-column: 2;
-ms-grid-row: 2;
-ms-grid-column: 1;
-ms-grid-column-span: 2;
grid-area: b-conflict;
}

.grid.conflict > .b {
-ms-grid-row: 2;
-ms-grid-column: 1;
-ms-grid-column-span: 1;
}

/****************\
Expand Down
3 changes: 3 additions & 0 deletions test/cases/grid-template.out.css
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@
}
.g-conflict-2 > .k-1 {
-ms-grid-row: 1;
-ms-grid-row-span: 1;
-ms-grid-column: 1;
-ms-grid-column-span: 1;
}
.k-2 {
-ms-grid-row: 1;
Expand All @@ -443,6 +445,7 @@
.g-conflict-2 > .k-2 {
-ms-grid-row: 1;
-ms-grid-column: 3;
-ms-grid-column-span: 1;
}
}

Expand Down

0 comments on commit 1956607

Please sign in to comment.