-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setting `preserve` to `true` will preserve `calc()` in the output, so that they can be used by supporting browsers. Useful now that browsers are starting to implement vars (eg Firefox 31+) & it’s even better for debug [to see real rules](http://cl.ly/image/3W3O0E41173X). I’ve refactored the code a little bit to make it easier to use the `preserve` in the main function (& also use rework-visit which is more bullet proof) like rework-vars. This should be a major update since now calc need to be called as a function (like rework-vars for example) to be able to pass options. So this should be tagged as 0.3 (or maybe it’s time to ship 1.0 :) ?) I also quickly refactor tests to make it more understandable (plugins.js near preserve.css was confusing imo). I also add some error reporting like we have in rework-vars. Btw, maybe we can expand the use of balanced-match to rewrite other parts of the plugin. I hope it’s ok. FWI, I did this since I’m more seeing rework-vars & rework-calc as fallback than replacement. See reworkcss/rework-vars#28 reworkcss/rework-vars#29 & segmentio/myth#64
- Loading branch information
Showing
17 changed files
with
161 additions
and
109 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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 @@ | ||
|
||
body { | ||
width: 100%; | ||
} | ||
|
||
body > header { | ||
height: calc(3em * 2); | ||
font-size: calc(6em / 2); | ||
} |
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 @@ | ||
|
||
body { | ||
width: 100%; | ||
} | ||
|
||
body > header { | ||
height: 6em; | ||
height: calc(3em * 2); | ||
font-size: 3em; | ||
font-size: calc(6em / 2); | ||
} |
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 @@ | ||
div { | ||
width: calc(); | ||
} |
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 @@ | ||
div { | ||
/* missing closing ')' */ | ||
width: calc(10px - 5px; | ||
} |
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,53 @@ | ||
var calc = require('../index'), | ||
rework = require('rework'), | ||
expect = require('chai').expect, | ||
read = require('fs').readFileSync; | ||
|
||
function fixture(name){ | ||
return read('test/fixtures/' + name + '.css', 'utf8').trim(); | ||
} | ||
|
||
function compareFixtures(name, options){ | ||
return expect( | ||
rework(fixture(name + '.in')) | ||
.use(calc(options)) | ||
.toString().trim() | ||
).to.equal(fixture(name + '.out')); | ||
} | ||
|
||
describe('rework-calc', function() { | ||
it('throws an error when a calc function is empty', function () { | ||
var output = function () { | ||
return rework(fixture('substitution-empty')).use(calc()).toString(); | ||
}; | ||
expect(output).to.Throw(Error, 'rework-calc: calc() must contain a non-whitespace string'); | ||
}); | ||
|
||
it('throws an error when a variable function is malformed', function () { | ||
var output = function () { | ||
return rework(fixture('substitution-malformed')).use(calc()).toString(); | ||
}; | ||
expect(output).to.Throw(Error, 'rework-calc: missing closing ")" in the value "calc(10px - 5px"'); | ||
}); | ||
|
||
|
||
it('should calculate expressions with only one unit involved', function() { | ||
compareFixtures('calc'); | ||
}); | ||
|
||
it('should calculate expressions with percents correctly', function () { | ||
compareFixtures('calc-percent'); | ||
}); | ||
|
||
it('should use CSS3 Calc function as fallback for expressions with multiple units', function () { | ||
compareFixtures('calc-complex'); | ||
}); | ||
|
||
it('should handle vendor prefixed expressions', function () { | ||
compareFixtures('calc-prefix'); | ||
}); | ||
|
||
it('should preserves calc() when `preserve` is `true`', function() { | ||
compareFixtures('preserve', {preserve: true}); | ||
}); | ||
}); |