Skip to content

Commit

Permalink
Merge pull request #578 from s7eph4n/map_merge
Browse files Browse the repository at this point in the history
Actually merge maps instead of just concatenating
  • Loading branch information
robocoder committed Jul 22, 2018
2 parents 3d3fd50 + 0b94ebc commit 79f6252
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/Compiler.php
Expand Up @@ -5003,7 +5003,17 @@ protected function libMapMerge($args)
$map1 = $this->assertMap($args[0]);
$map2 = $this->assertMap($args[1]);

return [Type::T_MAP, array_merge($map1[1], $map2[1]), array_merge($map1[2], $map2[2])];
foreach ($map2[1] as $i2 => $key2) {
foreach ($map1[1] as $i1 => $key1) {
if ($this->compileStringContent($this->coerceString($key1)) === $this->compileStringContent($this->coerceString($key2))) {
$map1[2][$i1] = $map2[2][$i2];
continue 2;
}
}
$map1[1][] = $map2[1][$i2];
$map1[2][] = $map2[2][$i2];
}
return $map1;
}

protected static $libKeywords = ['args'];
Expand Down
5 changes: 3 additions & 2 deletions tests/inputs/map.scss
Expand Up @@ -25,9 +25,10 @@ div {
bar: nth(nth($map, 1), 1);
}

$color: ("black" : #000000);
$color: ("black" : #000000, "grey" : #777777);
$color2: ("grey" : #888888, "white" : #ffffff);

@each $color_name, $color_value in $color {
@each $color_name, $color_value in map_merge( $color, $color2 ) {
.#{$color_name} {
background-color: $color_value !important;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/outputs/map.css
Expand Up @@ -12,6 +12,10 @@ div {
bar: color; }
.black {
background-color: #000 !important; }
.grey {
background-color: #888 !important; }
.white {
background-color: #fff !important; }

div {
a: 1;
Expand Down
10 changes: 8 additions & 2 deletions tests/outputs_numbered/map.css
Expand Up @@ -11,10 +11,16 @@ div {
div {
foo: color black;
bar: color; }
/* line 31, inputs/map.scss */
/* line 32, inputs/map.scss */
.black {
background-color: #000 !important; }
/* line 52, inputs/map.scss */
/* line 32, inputs/map.scss */
.grey {
background-color: #888 !important; }
/* line 32, inputs/map.scss */
.white {
background-color: #fff !important; }
/* line 53, inputs/map.scss */
div {
a: 1;
b: 2;
Expand Down

0 comments on commit 79f6252

Please sign in to comment.