Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dart Sass Module support for 3.0 + added tests
- Loading branch information
1 parent
ff78cb9
commit 7177cf0
Showing
12 changed files
with
7,407 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules/ | ||
npm-debug.log | ||
yarn-error.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.npmignore | ||
.gitignore | ||
|
||
node_modules/ | ||
npm-debug.log | ||
yarn.lock | ||
|
||
*.test.js | ||
.travis.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
language: node_js | ||
cache: yarn | ||
node_js: | ||
- stable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,46 @@ | ||
// list-separator polyfill by Hugo Giraudel (https://sass-compatibility.github.io/#list_separator_function) | ||
@function em-separator($list) { | ||
@if function-exists("list-separator") == true { | ||
@return list-separator($list); | ||
} | ||
@use "sass:list"; | ||
@use "sass:map"; | ||
@use "sass:math"; | ||
@use "sass:meta"; | ||
|
||
$test-list: (); | ||
@each $item in $list { | ||
$test-list: append($test-list, $item, space); | ||
// Dart Sass <1.33.0 compatibility | ||
@function _divide($a, $b) { | ||
@if map.has-key(meta.module-functions("math"), "div") { | ||
@return math.div($a, $b); | ||
} | ||
|
||
@return if($test-list == $list, space, comma); | ||
@return $a / $b; | ||
} | ||
|
||
@function em($values...) { | ||
$context: nth($values, length($values)); | ||
@function convert($values...) { | ||
$context: list.nth($values, list.length($values)); | ||
$result: (); | ||
$separator: em-separator($values); | ||
|
||
@for $i from 1 through length($values) - 1 { | ||
$value: nth($values, $i); | ||
@if type-of($value) == "number" and unit($value) == "px" { | ||
$result: append($result, $value / $context * 1em, $separator); | ||
} @else if type-of($value) == "list" { | ||
$result: append($result, em(append($value, $context)...), $separator); | ||
$separator: list.separator($values); | ||
|
||
@for $i from 1 through list.length($values) - 1 { | ||
$value: list.nth($values, $i); | ||
@if meta.type-of($value) == "number" and math.unit($value) == "px" { | ||
$result: list.append($result, _divide($value, $context) * 1em, $separator); | ||
} @else if meta.type-of($value) == "list" { | ||
$result: list.append($result, em(list.append($value, $context)...), $separator); | ||
} @else { | ||
$result: append($result, $value, $separator); | ||
$result: list.append($result, $value, $separator); | ||
} | ||
} | ||
|
||
@return if(length($result) == 1, nth($result, 1), $result); | ||
@return if(list.length($result) == 1, list.nth($result, 1), $result); | ||
} | ||
|
||
@mixin em($properties, $context) { | ||
@each $property in map-keys($properties) { | ||
#{$property}: em(append(map-get($properties, $property), $context)...); | ||
@mixin convert($properties, $context) { | ||
@each $property in map.keys($properties) { | ||
#{$property}: em(list.append(map.get($properties, $property), $context)...); | ||
} | ||
} | ||
|
||
@function em($values...) { | ||
@return convert($values...); | ||
} | ||
|
||
@mixin em($properties, $context) { | ||
@include convert($properties, $context); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@use "em"; | ||
|
||
@forward "em" as em-* hide em-em; | ||
|
||
@function em($values...) { | ||
@return em.convert($values...); | ||
} | ||
|
||
@mixin em($properties, $context) { | ||
@include em.convert($properties, $context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@forward "em"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
var sass = require('sass'); | ||
|
||
function render(data) { | ||
return new Promise(function(resolve, reject) { | ||
sass.render({ data, includePaths: ['./'], precision: 5 }, function(err, data) { | ||
if (err !== null) reject(err); | ||
else resolve(data.css.toString()); | ||
}); | ||
}); | ||
} | ||
|
||
async function run(input, output, config = `@use "." as em;`) { | ||
const a = await render(config.concat(input)); | ||
const b = await render(output); | ||
expect(a).toEqual(b); | ||
}; | ||
|
||
it('Simple', () => run( | ||
'.simple { font-size: em.convert(24px, 16px); }', | ||
'.simple { font-size: 1.5em; }' | ||
)); | ||
|
||
it('Multiple values', () => run( | ||
'.multiple { padding: em.convert(5px 10px, 16px); }', | ||
'.multiple { padding: 0.3125em 0.625em; }' | ||
)); | ||
|
||
it('Multiple mixed values', () => run( | ||
'.mixed { border-bottom: em.convert(1px solid black, 16px); }', | ||
'.mixed { border-bottom: 0.0625em solid black; }' | ||
)); | ||
|
||
it('Comma-separated values', () => run( | ||
'.comma { box-shadow: em.convert(0 0 2px #ccc, inset 0 0 5px #eee, 16px); }', | ||
'.comma { box-shadow: 0 0 0.125em #ccc, inset 0 0 0.3125em #eee; }' | ||
)); | ||
|
||
it('Alternate use', () => run( | ||
'.alternate { text-shadow: em.convert(1px 1px, 16px) #eee, em.convert(-1px, 16px) 0 #eee; }', | ||
'.alternate { text-shadow: 0.0625em 0.0625em #eee, -0.0625em 0 #eee; }', | ||
)); | ||
|
||
it('Multiple properties', () => run( | ||
'.multiple-properties { @include em.convert((font-size: 24px, margin: 10px 1.5em), 16px); }', | ||
'.multiple-properties { font-size: 1.5em; margin: 0.625em 1.5em; }', | ||
)); | ||
|
||
it('Changing namespace', () => run( | ||
'.changing-namespace { font-size: to.em(24px, 16px); }', | ||
'.changing-namespace { font-size: 1.5em; }', | ||
'@use "." as to;', | ||
)); | ||
|
||
it('Global namespace', () => run( | ||
'.global-namespace { font-size: em(24px, 16px); }', | ||
'.global-namespace { font-size: 1.5em; }', | ||
'@use "." as *;', | ||
)); | ||
|
||
it('Legacy import', () => run( | ||
'.legacy-import { @include em((font-size: 24px), 16px); margin: em(10px 1.5em, 16px); }', | ||
'.legacy-import { font-size: 1.5em; margin: 0.625em 1.5em; }', | ||
'@import ".";', | ||
)); | ||
|
||
it('Demo', () => run(`$base-font-size: 16px; | ||
$h1-font-size: 24px; | ||
.demo { | ||
font-size: em.convert($h1-font-size, $base-font-size); // Simple | ||
border-bottom: em.convert(1px solid black, $h1-font-size); // Shorthand | ||
box-shadow: em.convert(0 0 2px #ccc, inset 0 0 5px #eee, $h1-font-size); // Multiple values | ||
// Multiple properties | ||
@include em.convert(( | ||
margin: 20px 5%, | ||
padding: 10px | ||
), $h1-font-size); | ||
}`,`.demo { | ||
font-size: 1.5em; | ||
border-bottom: 0.0416666667em solid black; | ||
box-shadow: 0 0 0.0833333333em #ccc, inset 0 0 0.2083333333em #eee; | ||
margin: 0.8333333333em 5%; | ||
padding: 0.4166666667em; | ||
}` | ||
)); |
Oops, something went wrong.