Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 3311b4f

Browse files
committed
Documentation for custom functions
1 parent 8fdf5c4 commit 3311b4f

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

README.md

+60
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,66 @@ The callback function is passed a results object, containing the following keys:
9292
#### importer (starting from v2)
9393
`importer` is a `Function` to be called when libsass parser encounters the import directive. If present, libsass will call node-sass and let the user change file, data or both during the compilation. This option is optional, and applies to both render and renderSync functions. Also, it can either return object of form `{file:'..', contents: '..'}` or send it back via `done({})`. Note in renderSync or render, there is no restriction imposed on using `done()` callback or `return` statement (dispite of the asnchrony difference).
9494

95+
#### functions
96+
`functions` is an `Object` that holds a collection of custom functions that may be invoked by the sass files being compiled. They may take zero or more input parameters and must return a value either synchronously (`return ...;`) or asynchronously (`done();`). Those parameters will be instances of one of the constructors contained in the `require('node-sass').types` hash. The return value must be of one of these types as well. See the list of available types below:
97+
98+
##### types.Number(value [, unit = ""])
99+
* `getValue()`/ `setValue(value)` : gets / sets the numerical portion of the number
100+
* `getUnit()` / `setUnit(unit)` : gets / sets the unit portion of the number
101+
102+
##### types.String(value)
103+
* `getValue()` / `setValue(value)` : gets / sets the enclosed string
104+
105+
##### types.Color(r, g, b [, a = 1.0]) or types.Color(argb)
106+
* `getR()` / `setR(value)` : red component (integer from `0` to `255`)
107+
* `getG()` / `setG(value)` : green component (integer from `0` to `255`)
108+
* `getB()` / `setB(value)` : blue component (integer from `0` to `255`)
109+
* `getA()` / `setA(value)` : alpha component (number from `0` to `1.0`)
110+
111+
Example:
112+
113+
```javascript
114+
var Color = require('node-sass').types.Color,
115+
c1 = new Color(255, 0, 0),
116+
c2 = new Color(0xff0088cc);
117+
```
118+
119+
##### types.Boolean(value)
120+
* `getValue()` / `setValue(value)` : gets / sets the enclosed boolean
121+
122+
##### types.List(length [, commaSeparator = true])
123+
* `getValue(index)` / `setValue(index, value)` : `value` must itself be an instance of one of the constructors in `sass.types`.
124+
* `getSeparator()` / `setSeparator(isComma)` : whether to use commas as a separator
125+
* `getLength()`
126+
127+
##### types.Map(length)
128+
* `getKey(index)` / `setKey(index, value)`
129+
* `getValue(index)` / `setValue(index, value)`
130+
* `getLength()`
131+
132+
##### types.Null()
133+
134+
##### Example
135+
136+
```javascript
137+
sass.renderSync({
138+
data: '#{headings(2,5)} { color: #08c; }',
139+
functions: {
140+
'headings($from: 0, $to: 6)': function(from, to) {
141+
var i, f = from.getValue(), t = to.getValue(),
142+
list = new sass.types.List(t - f + 1);
143+
144+
for (i = f; i <= t; i++) {
145+
list.setValue(i - f, new sass.types.String('h' + i));
146+
}
147+
148+
return list;
149+
}
150+
}
151+
});
152+
```
153+
154+
95155
#### includePaths
96156
`includePaths` is an `Array` of path `String`s to look for any `@import`ed files. It is recommended that you use this option if you are using the `data` option and have **any** `@import` directives, as otherwise [libsass] may not find your depended-on files.
97157

test/api.js

+21
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,27 @@ describe('api', function() {
593593
assert.equal(result.css.trim(), 'div {\n width: 50px; }');
594594
done();
595595
});
596+
597+
it('should return a list of selectors after calling the headings custom function', function(done) {
598+
var result = sass.renderSync({
599+
data: '#{headings(2,5)} { color: #08c; }',
600+
functions: {
601+
'headings($from: 0, $to: 6)': function(from, to) {
602+
var i, f = from.getValue(), t = to.getValue(),
603+
list = new sass.types.List(t - f + 1);
604+
605+
for (i = f; i <= t; i++) {
606+
list.setValue(i - f, new sass.types.String('h' + i));
607+
}
608+
609+
return list;
610+
}
611+
}
612+
});
613+
614+
assert.equal(result.css.trim(), 'h2, h3, h4, h5 {\n color: #08c; }');
615+
done();
616+
});
596617
});
597618

598619
describe('.renderSync(options)', function() {

0 commit comments

Comments
 (0)