api.js 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  1. /*eslint new-cap: ["error", {"capIsNewExceptions": ["Color"]}]*/
  2. var assert = require('assert').strict,
  3. fs = require('fs'),
  4. path = require('path'),
  5. read = fs.readFileSync,
  6. sassPath = process.env.NODESASS_COV
  7. ? require.resolve('../lib-cov')
  8. : require.resolve('../lib'),
  9. sass = require(sassPath),
  10. fixture = path.join.bind(null, __dirname, 'fixtures'),
  11. resolveFixture = path.resolve.bind(null, __dirname, 'fixtures');
  12. describe('api', function() {
  13. describe('.render(options, callback)', function() {
  14. beforeEach(function() {
  15. delete process.env.SASS_PATH;
  16. });
  17. it('should compile sass to css with file', function(done) {
  18. var expected = read(fixture('simple/expected.css'), 'utf8').trim();
  19. sass.render({
  20. file: fixture('simple/index.scss')
  21. }, function(error, result) {
  22. assert.strictEqual(result.css.toString().trim(), expected.replace(/\r\n/g, '\n'));
  23. done();
  24. });
  25. });
  26. it('should compile sass to css with outFile set to absolute url', function(done) {
  27. sass.render({
  28. file: fixture('simple/index.scss'),
  29. sourceMap: true,
  30. outFile: fixture('simple/index-test.css')
  31. }, function(error, result) {
  32. assert.strictEqual(JSON.parse(result.map).file, 'index-test.css');
  33. done();
  34. });
  35. });
  36. it('should compile sass to css with outFile set to relative url', function(done) {
  37. sass.render({
  38. file: fixture('simple/index.scss'),
  39. sourceMap: true,
  40. outFile: './index-test.css'
  41. }, function(error, result) {
  42. assert.strictEqual(JSON.parse(result.map).file, 'index-test.css');
  43. done();
  44. });
  45. });
  46. it('should compile sass to css with outFile and sourceMap set to relative url', function(done) {
  47. sass.render({
  48. file: fixture('simple/index.scss'),
  49. sourceMap: './deep/nested/index.map',
  50. outFile: './index-test.css'
  51. }, function(error, result) {
  52. assert.strictEqual(JSON.parse(result.map).file, '../../index-test.css');
  53. done();
  54. });
  55. });
  56. it('should not generate source map when not requested', function(done) {
  57. sass.render({
  58. file: fixture('simple/index.scss'),
  59. sourceMap: false
  60. }, function(error, result) {
  61. assert.strictEqual(Object.prototype.hasOwnProperty.call(result, 'map'), false, 'result has a map property');
  62. done();
  63. });
  64. });
  65. it('should not generate source map without outFile and no explicit path given', function(done) {
  66. sass.render({
  67. file: fixture('simple/index.scss'),
  68. sourceMap: true
  69. }, function(error, result) {
  70. assert.strictEqual(Object.prototype.hasOwnProperty.call(result, 'map'), false, 'result has a map property');
  71. done();
  72. });
  73. });
  74. it('should compile generate map with sourceMapRoot pass-through option', function(done) {
  75. sass.render({
  76. file: fixture('simple/index.scss'),
  77. sourceMap: './deep/nested/index.map',
  78. sourceMapRoot: 'http://test.com/',
  79. outFile: './index-test.css'
  80. }, function(error, result) {
  81. assert.strictEqual(JSON.parse(result.map).sourceRoot, 'http://test.com/');
  82. done();
  83. });
  84. });
  85. it('should compile sass to css with data', function(done) {
  86. var src = read(fixture('simple/index.scss'), 'utf8');
  87. var expected = read(fixture('simple/expected.css'), 'utf8').trim();
  88. sass.render({
  89. data: src
  90. }, function(error, result) {
  91. assert.strictEqual(result.css.toString().trim(), expected.replace(/\r\n/g, '\n'));
  92. done();
  93. });
  94. });
  95. it('should compile sass to css using indented syntax', function(done) {
  96. var src = read(fixture('indent/index.sass'), 'utf8');
  97. var expected = read(fixture('indent/expected.css'), 'utf8').trim();
  98. sass.render({
  99. data: src,
  100. indentedSyntax: true
  101. }, function(error, result) {
  102. assert.strictEqual(result.css.toString().trim(), expected.replace(/\r\n/g, '\n'));
  103. done();
  104. });
  105. });
  106. it('should NOT compile empty data string', function(done) {
  107. sass.render({
  108. data: ''
  109. }, function(error) {
  110. assert.strictEqual(error.message, 'No input specified: provide a file name or a source string to process');
  111. done();
  112. });
  113. });
  114. it('should NOT compile without any input', function(done) {
  115. sass.render({ }, function(error) {
  116. assert.strictEqual(error.message, 'No input specified: provide a file name or a source string to process');
  117. done();
  118. });
  119. });
  120. it('should returnn error status 1 for bad input', function(done) {
  121. sass.render({
  122. data: '#navbar width 80%;'
  123. }, function(error) {
  124. assert(error.message);
  125. assert.strictEqual(error.status, 1);
  126. done();
  127. });
  128. });
  129. it('should compile with include paths', function(done) {
  130. var src = read(fixture('include-path/index.scss'), 'utf8');
  131. var expected = read(fixture('include-path/expected.css'), 'utf8').trim();
  132. sass.render({
  133. data: src,
  134. includePaths: [
  135. fixture('include-path/functions'),
  136. fixture('include-path/lib')
  137. ]
  138. }, function(error, result) {
  139. assert.strictEqual(result.css.toString().trim(), expected.replace(/\r\n/g, '\n'));
  140. done();
  141. });
  142. });
  143. it('should add cwd to the front on include paths', function(done) {
  144. var src = fixture('cwd-include-path/root/index.scss');
  145. var expected = read(fixture('cwd-include-path/expected.css'), 'utf8').trim();
  146. var cwd = process.cwd();
  147. process.chdir(fixture('cwd-include-path'));
  148. sass.render({
  149. file: src,
  150. includePaths: []
  151. }, function(error, result) {
  152. assert.strictEqual(result.css.toString().trim(), expected.replace(/\r\n/g, '\n'));
  153. process.chdir(cwd);
  154. done();
  155. });
  156. });
  157. it('should check SASS_PATH in the specified order', function(done) {
  158. var src = read(fixture('sass-path/index.scss'), 'utf8');
  159. var expectedRed = read(fixture('sass-path/expected-red.css'), 'utf8').trim();
  160. var expectedOrange = read(fixture('sass-path/expected-orange.css'), 'utf8').trim();
  161. var envIncludes = [
  162. fixture('sass-path/red'),
  163. fixture('sass-path/orange')
  164. ];
  165. process.env.SASS_PATH = envIncludes.join(path.delimiter);
  166. sass.render({
  167. data: src,
  168. includePaths: []
  169. }, function(error, result) {
  170. assert.strictEqual(result.css.toString().trim(), expectedRed.replace(/\r\n/g, '\n'));
  171. });
  172. process.env.SASS_PATH = envIncludes.reverse().join(path.delimiter);
  173. sass.render({
  174. data: src,
  175. includePaths: []
  176. }, function(error, result) {
  177. assert.strictEqual(result.css.toString().trim(), expectedOrange.replace(/\r\n/g, '\n'));
  178. done();
  179. });
  180. });
  181. it('should prefer include path over SASS_PATH', function(done) {
  182. var src = read(fixture('sass-path/index.scss'), 'utf8');
  183. var expectedRed = read(fixture('sass-path/expected-red.css'), 'utf8').trim();
  184. var expectedOrange = read(fixture('sass-path/expected-orange.css'), 'utf8').trim();
  185. var envIncludes = [
  186. fixture('sass-path/red')
  187. ];
  188. process.env.SASS_PATH = envIncludes.join(path.delimiter);
  189. sass.render({
  190. data: src,
  191. includePaths: []
  192. }, function(error, result) {
  193. assert.strictEqual(result.css.toString().trim(), expectedRed.replace(/\r\n/g, '\n'));
  194. });
  195. sass.render({
  196. data: src,
  197. includePaths: [fixture('sass-path/orange')]
  198. }, function(error, result) {
  199. assert.strictEqual(result.css.toString().trim(), expectedOrange.replace(/\r\n/g, '\n'));
  200. done();
  201. });
  202. });
  203. it('should render with precision option', function(done) {
  204. var src = read(fixture('precision/index.scss'), 'utf8');
  205. var expected = read(fixture('precision/expected.css'), 'utf8').trim();
  206. sass.render({
  207. data: src,
  208. precision: 10
  209. }, function(error, result) {
  210. assert.strictEqual(result.css.toString().trim(), expected.replace(/\r\n/g, '\n'));
  211. done();
  212. });
  213. });
  214. it('should contain all included files in stats when data is passed', function(done) {
  215. var src = read(fixture('include-files/index.scss'), 'utf8');
  216. var expected = [
  217. fixture('include-files/bar.scss').replace(/\\/g, '/'),
  218. fixture('include-files/foo.scss').replace(/\\/g, '/')
  219. ];
  220. sass.render({
  221. data: src,
  222. includePaths: [fixture('include-files')]
  223. }, function(error, result) {
  224. assert.deepStrictEqual(result.stats.includedFiles, expected);
  225. done();
  226. });
  227. });
  228. it('should render with indentWidth and indentType options', function(done) {
  229. sass.render({
  230. data: 'div { color: transparent; }',
  231. indentWidth: 7,
  232. indentType: 'tab'
  233. }, function(error, result) {
  234. assert.strictEqual(result.css.toString().trim(), 'div {\n\t\t\t\t\t\t\tcolor: transparent; }');
  235. done();
  236. });
  237. });
  238. it('should render with linefeed option', function(done) {
  239. sass.render({
  240. data: 'div { color: transparent; }',
  241. linefeed: 'lfcr'
  242. }, function(error, result) {
  243. assert.strictEqual(result.css.toString().trim(), 'div {\n\r color: transparent; }');
  244. done();
  245. });
  246. });
  247. });
  248. describe('.render(importer)', function() {
  249. var src = read(fixture('include-files/index.scss'), 'utf8');
  250. it('should respect the order of chained imports when using custom importers and one file is custom imported and the other is not.', function(done) {
  251. sass.render({
  252. file: fixture('include-files/chained-imports-with-custom-importer.scss'),
  253. importer: function(url, prev, done) {
  254. // NOTE: to see that this test failure is only due to the stated
  255. // issue do each of the following and see that the tests pass.
  256. //
  257. // a) add `return sass.NULL;` as the first line in this function to
  258. // cause non-custom importers to always be used.
  259. // b) comment out the conditional below to force our custom
  260. // importer to always be used.
  261. //
  262. // You will notice that the tests pass when either all native, or
  263. // all custom importers are used, but not when a native + custom
  264. // import chain is used.
  265. if (url !== 'file-processed-by-loader') {
  266. return sass.NULL;
  267. }
  268. done({
  269. file: fixture('include-files/' + url + '.scss')
  270. });
  271. }
  272. }, function(err, data) {
  273. assert.strictEqual(err, null);
  274. assert.strictEqual(
  275. data.css.toString().trim(),
  276. 'body {\n color: "red"; }'
  277. );
  278. done();
  279. });
  280. });
  281. it('should still call the next importer with the resolved prev path when the previous importer returned both a file and contents property - issue #1219', function(done) {
  282. sass.render({
  283. data: '@import "a";',
  284. importer: function(url, prev, done) {
  285. if (url === 'a') {
  286. done({
  287. file: '/Users/me/sass/lib/a.scss',
  288. contents: '@import "b"'
  289. });
  290. } else {
  291. assert.strictEqual(prev, '/Users/me/sass/lib/a.scss');
  292. done({
  293. file: '/Users/me/sass/lib/b.scss',
  294. contents: 'div {color: yellow;}'
  295. });
  296. }
  297. }
  298. }, function() {
  299. done();
  300. });
  301. });
  302. it('should override imports with "data" as input and fires callback with file and contents', function(done) {
  303. sass.render({
  304. data: src,
  305. importer: function(url, prev, done) {
  306. done({
  307. file: '/some/other/path.scss',
  308. contents: 'div {color: yellow;}'
  309. });
  310. }
  311. }, function(error, result) {
  312. assert.strictEqual(result.css.toString().trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  313. done();
  314. });
  315. });
  316. it('should should resolve imports depth first', function (done) {
  317. var actualImportOrder = [];
  318. var expectedImportOrder = [
  319. 'a', '_common', 'vars', 'struct', 'a1', 'common', 'vars', 'struct', 'b', 'b1'
  320. ];
  321. var expected = read(fixture('depth-first/expected.css'), 'utf-8');
  322. sass.render({
  323. file: fixture('depth-first/index.scss'),
  324. importer: function (url, prev, done) {
  325. actualImportOrder.push(url);
  326. done();
  327. }
  328. }, function(error, result) {
  329. assert.strictEqual(result.css.toString().trim(), expected);
  330. assert.deepStrictEqual(actualImportOrder, expectedImportOrder);
  331. done();
  332. });
  333. });
  334. it('should override imports with "file" as input and fires callback with file and contents', function(done) {
  335. sass.render({
  336. file: fixture('include-files/index.scss'),
  337. importer: function(url, prev, done) {
  338. done({
  339. file: '/some/other/path.scss',
  340. contents: 'div {color: yellow;}'
  341. });
  342. }
  343. }, function(error, result) {
  344. assert.strictEqual(result.css.toString().trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  345. done();
  346. });
  347. });
  348. it('should override imports with "data" as input and returns file and contents', function(done) {
  349. sass.render({
  350. data: src,
  351. importer: function(url, prev) {
  352. return {
  353. file: prev + url,
  354. contents: 'div {color: yellow;}'
  355. };
  356. }
  357. }, function(error, result) {
  358. assert.strictEqual(result.css.toString().trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  359. done();
  360. });
  361. });
  362. it('should override imports with "file" as input and returns file and contents', function(done) {
  363. sass.render({
  364. file: fixture('include-files/index.scss'),
  365. importer: function(url, prev) {
  366. return {
  367. file: prev + url,
  368. contents: 'div {color: yellow;}'
  369. };
  370. }
  371. }, function(error, result) {
  372. assert.strictEqual(result.css.toString().trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  373. done();
  374. });
  375. });
  376. it('should override imports with "data" as input and fires callback with file', function(done) {
  377. sass.render({
  378. data: src,
  379. importer: function(url, /* jshint unused:false */ prev, done) {
  380. done({
  381. file: path.resolve(path.dirname(fixture('include-files/index.scss')), url + (path.extname(url) ? '' : '.scss'))
  382. });
  383. }
  384. }, function(error, result) {
  385. assert.strictEqual(result.css.toString().trim(), '/* foo.scss */\n/* bar.scss */');
  386. done();
  387. });
  388. });
  389. it('should override imports with "file" as input and fires callback with file', function(done) {
  390. sass.render({
  391. file: fixture('include-files/index.scss'),
  392. importer: function(url, prev, done) {
  393. done({
  394. file: path.resolve(path.dirname(prev), url + (path.extname(url) ? '' : '.scss'))
  395. });
  396. }
  397. }, function(error, result) {
  398. assert.strictEqual(result.css.toString().trim(), '/* foo.scss */\n/* bar.scss */');
  399. done();
  400. });
  401. });
  402. it('should override imports with "data" as input and returns file', function(done) {
  403. sass.render({
  404. data: src,
  405. importer: function(url) {
  406. return {
  407. file: path.resolve(path.dirname(fixture('include-files/index.scss')), url + (path.extname(url) ? '' : '.scss'))
  408. };
  409. }
  410. }, function(error, result) {
  411. assert.strictEqual(result.css.toString().trim(), '/* foo.scss */\n/* bar.scss */');
  412. done();
  413. });
  414. });
  415. it('should override imports with "file" as input and returns file', function(done) {
  416. sass.render({
  417. file: fixture('include-files/index.scss'),
  418. importer: function(url, prev) {
  419. return {
  420. file: path.resolve(path.dirname(prev), url + (path.extname(url) ? '' : '.scss'))
  421. };
  422. }
  423. }, function(error, result) {
  424. assert.strictEqual(result.css.toString().trim(), '/* foo.scss */\n/* bar.scss */');
  425. done();
  426. });
  427. });
  428. it('should fallback to default import behaviour if importer returns sass.NULL', function(done) {
  429. sass.render({
  430. file: fixture('include-files/index.scss'),
  431. importer: function(url, prev, done) {
  432. done(sass.NULL);
  433. }
  434. }, function(error, result) {
  435. assert.strictEqual(result.css.toString().trim(), '/* foo.scss */\n/* bar.scss */');
  436. done();
  437. });
  438. });
  439. it('should fallback to default import behaviour if importer returns null for backwards compatibility', function(done) {
  440. sass.render({
  441. file: fixture('include-files/index.scss'),
  442. importer: function(url, prev, done) {
  443. done(null);
  444. }
  445. }, function(error, result) {
  446. assert.strictEqual(result.css.toString().trim(), '/* foo.scss */\n/* bar.scss */');
  447. done();
  448. });
  449. });
  450. it('should fallback to default import behaviour if importer returns undefined for backwards compatibility', function(done) {
  451. sass.render({
  452. file: fixture('include-files/index.scss'),
  453. importer: function(url, prev, done) {
  454. done(undefined);
  455. }
  456. }, function(error, result) {
  457. assert.strictEqual(result.css.toString().trim(), '/* foo.scss */\n/* bar.scss */');
  458. done();
  459. });
  460. });
  461. it('should fallback to default import behaviour if importer returns false for backwards compatibility', function(done) {
  462. sass.render({
  463. file: fixture('include-files/index.scss'),
  464. importer: function(url, prev, done) {
  465. done(false);
  466. }
  467. }, function(error, result) {
  468. assert.strictEqual(result.css.toString().trim(), '/* foo.scss */\n/* bar.scss */');
  469. done();
  470. });
  471. });
  472. it('should override imports with "data" as input and fires callback with contents', function(done) {
  473. sass.render({
  474. data: src,
  475. importer: function(url, prev, done) {
  476. done({
  477. contents: 'div {color: yellow;}'
  478. });
  479. }
  480. }, function(error, result) {
  481. assert.strictEqual(result.css.toString().trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  482. done();
  483. });
  484. });
  485. it('should override imports with "file" as input and fires callback with contents', function(done) {
  486. sass.render({
  487. file: fixture('include-files/index.scss'),
  488. importer: function(url, prev, done) {
  489. done({
  490. contents: 'div {color: yellow;}'
  491. });
  492. }
  493. }, function(error, result) {
  494. assert.strictEqual(result.css.toString().trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  495. done();
  496. });
  497. });
  498. it('should override imports with "data" as input and returns contents', function(done) {
  499. sass.render({
  500. data: src,
  501. importer: function() {
  502. return {
  503. contents: 'div {color: yellow;}'
  504. };
  505. }
  506. }, function(error, result) {
  507. assert.strictEqual(result.css.toString().trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  508. done();
  509. });
  510. });
  511. it('should override imports with "file" as input and returns contents', function(done) {
  512. sass.render({
  513. file: fixture('include-files/index.scss'),
  514. importer: function() {
  515. return {
  516. contents: 'div {color: yellow;}'
  517. };
  518. }
  519. }, function(error, result) {
  520. assert.strictEqual(result.css.toString().trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  521. done();
  522. });
  523. });
  524. it('should accept arrays of importers and return respect the order', function(done) {
  525. sass.render({
  526. file: fixture('include-files/index.scss'),
  527. importer: [
  528. function() {
  529. return sass.NULL;
  530. },
  531. function() {
  532. return {
  533. contents: 'div {color: yellow;}'
  534. };
  535. }
  536. ]
  537. }, function(error, result) {
  538. assert.strictEqual(result.css.toString().trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  539. done();
  540. });
  541. });
  542. it('should be able to see its options in this.options', function(done) {
  543. var fxt = fixture('include-files/index.scss');
  544. sass.render({
  545. file: fxt,
  546. importer: function() {
  547. assert.strictEqual(fxt, this.options.file);
  548. return {};
  549. }
  550. }, function() {
  551. assert.strictEqual(fxt, this.options.file);
  552. done();
  553. });
  554. });
  555. it('should be able to access a persistent options object', function(done) {
  556. sass.render({
  557. data: src,
  558. importer: function() {
  559. this.state = this.state || 0;
  560. this.state++;
  561. return {
  562. contents: 'div {color: yellow;}'
  563. };
  564. }
  565. }, function() {
  566. assert.strictEqual(this.state, 2);
  567. done();
  568. });
  569. });
  570. it('should wrap importer options', function(done) {
  571. var options;
  572. options = {
  573. data: src,
  574. importer: function() {
  575. assert.notStrictEqual(this.options.importer, options.importer);
  576. return {
  577. contents: 'div {color: yellow;}'
  578. };
  579. }
  580. };
  581. sass.render(options, function() {
  582. done();
  583. });
  584. });
  585. it('should reflect user-defined error when returned as callback', function(done) {
  586. sass.render({
  587. data: src,
  588. importer: function(url, prev, done) {
  589. done(new Error('doesn\'t exist!'));
  590. }
  591. }, function(error) {
  592. assert(/doesn't exist!/.test(error.message));
  593. done();
  594. });
  595. });
  596. it('should reflect user-defined error with return', function(done) {
  597. sass.render({
  598. data: src,
  599. importer: function() {
  600. return new Error('doesn\'t exist!');
  601. }
  602. }, function(error) {
  603. assert(/doesn't exist!/.test(error.message));
  604. done();
  605. });
  606. });
  607. it('should throw exception when importer returns an invalid value', function(done) {
  608. sass.render({
  609. data: src,
  610. importer: function() {
  611. return { contents: new Buffer('i am not a string!') };
  612. }
  613. }, function(error) {
  614. assert(/returned value of `contents` must be a string/.test(error.message));
  615. done();
  616. });
  617. });
  618. });
  619. describe('.render(functions)', function() {
  620. it('should call custom defined nullary function', function(done) {
  621. sass.render({
  622. data: 'div { color: foo(); }',
  623. functions: {
  624. 'foo()': function() {
  625. return new sass.types.Number(42, 'px');
  626. }
  627. }
  628. }, function(error, result) {
  629. assert.strictEqual(result.css.toString().trim(), 'div {\n color: 42px; }');
  630. done();
  631. });
  632. });
  633. it('should call custom function with multiple args', function(done) {
  634. sass.render({
  635. data: 'div { color: foo(3, 42px); }',
  636. functions: {
  637. 'foo($a, $b)': function(factor, size) {
  638. return new sass.types.Number(factor.getValue() * size.getValue(), size.getUnit());
  639. }
  640. }
  641. }, function(error, result) {
  642. assert.strictEqual(result.css.toString().trim(), 'div {\n color: 126px; }');
  643. done();
  644. });
  645. });
  646. it('should work with custom functions that return data asynchronously', function(done) {
  647. sass.render({
  648. data: 'div { color: foo(42px); }',
  649. functions: {
  650. 'foo($a)': function(size, done) {
  651. setTimeout(function() {
  652. done(new sass.types.Number(66, 'em'));
  653. }, 50);
  654. }
  655. }
  656. }, function(error, result) {
  657. assert.strictEqual(result.css.toString().trim(), 'div {\n color: 66em; }');
  658. done();
  659. });
  660. });
  661. it('should let custom functions call setter methods on wrapped sass values (number)', function(done) {
  662. sass.render({
  663. data: 'div { width: foo(42px); height: bar(42px); }',
  664. functions: {
  665. 'foo($a)': function(size) {
  666. size.setUnit('rem');
  667. return size;
  668. },
  669. 'bar($a)': function(size) {
  670. size.setValue(size.getValue() * 2);
  671. return size;
  672. }
  673. }
  674. }, function(error, result) {
  675. assert.strictEqual(result.css.toString().trim(), 'div {\n width: 42rem;\n height: 84px; }');
  676. done();
  677. });
  678. });
  679. it('should properly convert strings when calling custom functions', function(done) {
  680. sass.render({
  681. data: 'div { color: foo("bar"); }',
  682. functions: {
  683. 'foo($a)': function(str) {
  684. str = str.getValue().replace(/['"]/g, '');
  685. return new sass.types.String('"' + str + str + '"');
  686. }
  687. }
  688. }, function(error, result) {
  689. assert.strictEqual(result.css.toString().trim(), 'div {\n color: "barbar"; }');
  690. done();
  691. });
  692. });
  693. it('should let custom functions call setter methods on wrapped sass values (string)', function(done) {
  694. sass.render({
  695. data: 'div { width: foo("bar"); }',
  696. functions: {
  697. 'foo($a)': function(str) {
  698. var unquoted = str.getValue().replace(/['"]/g, '');
  699. str.setValue('"' + unquoted + unquoted + '"');
  700. return str;
  701. }
  702. }
  703. }, function(error, result) {
  704. assert.strictEqual(result.css.toString().trim(), 'div {\n width: "barbar"; }');
  705. done();
  706. });
  707. });
  708. it('should properly convert colors when calling custom functions', function(done) {
  709. sass.render({
  710. data: 'div { color: foo(#f00); background-color: bar(); border-color: baz(); }',
  711. functions: {
  712. 'foo($a)': function(color) {
  713. assert.strictEqual(color.getR(), 255);
  714. assert.strictEqual(color.getG(), 0);
  715. assert.strictEqual(color.getB(), 0);
  716. assert.strictEqual(color.getA(), 1.0);
  717. return new sass.types.Color(255, 255, 0, 0.5);
  718. },
  719. 'bar()': function() {
  720. return new sass.types.Color(0x33ff00ff);
  721. },
  722. 'baz()': function() {
  723. return new sass.types.Color(0xffff0000);
  724. }
  725. }
  726. }, function(error, result) {
  727. assert.strictEqual(
  728. result.css.toString().trim(),
  729. 'div {\n color: rgba(255, 255, 0, 0.5);' +
  730. '\n background-color: rgba(255, 0, 255, 0.2);' +
  731. '\n border-color: red; }'
  732. );
  733. done();
  734. });
  735. });
  736. it('should properly convert boolean when calling custom functions', function(done) {
  737. sass.render({
  738. data: 'div { color: if(foo(true, false), #fff, #000);' +
  739. '\n background-color: if(foo(true, true), #fff, #000); }',
  740. functions: {
  741. 'foo($a, $b)': function(a, b) {
  742. return sass.types.Boolean(a.getValue() && b.getValue());
  743. }
  744. }
  745. }, function(error, result) {
  746. assert.strictEqual(result.css.toString().trim(), 'div {\n color: #000;\n background-color: #fff; }');
  747. done();
  748. });
  749. });
  750. it('should let custom functions call setter methods on wrapped sass values (boolean)', function(done) {
  751. sass.render({
  752. data: 'div { color: if(foo(false), #fff, #000); background-color: if(foo(true), #fff, #000); }',
  753. functions: {
  754. 'foo($a)': function(a) {
  755. return a.getValue() ? sass.types.Boolean.FALSE : sass.types.Boolean.TRUE;
  756. }
  757. }
  758. }, function(error, result) {
  759. assert.strictEqual(result.css.toString().trim(), 'div {\n color: #fff;\n background-color: #000; }');
  760. done();
  761. });
  762. });
  763. it('should properly convert lists when calling custom functions', function(done) {
  764. sass.render({
  765. data: '$test-list: (bar, #f00, 123em); @each $item in foo($test-list) { .#{$item} { color: #fff; } }',
  766. functions: {
  767. 'foo($l)': function(list) {
  768. assert.strictEqual(list.getLength(), 3);
  769. assert.ok(list.getValue(0) instanceof sass.types.String);
  770. assert.strictEqual(list.getValue(0).getValue(), 'bar');
  771. assert.ok(list.getValue(1) instanceof sass.types.Color);
  772. assert.strictEqual(list.getValue(1).getR(), 0xff);
  773. assert.strictEqual(list.getValue(1).getG(), 0);
  774. assert.strictEqual(list.getValue(1).getB(), 0);
  775. assert.ok(list.getValue(2) instanceof sass.types.Number);
  776. assert.strictEqual(list.getValue(2).getValue(), 123);
  777. assert.strictEqual(list.getValue(2).getUnit(), 'em');
  778. var out = new sass.types.List(3);
  779. out.setValue(0, new sass.types.String('foo'));
  780. out.setValue(1, new sass.types.String('bar'));
  781. out.setValue(2, new sass.types.String('baz'));
  782. return out;
  783. }
  784. }
  785. }, function(error, result) {
  786. assert.strictEqual(
  787. result.css.toString().trim(),
  788. '.foo {\n color: #fff; }\n\n.bar {\n color: #fff; }\n\n.baz {\n color: #fff; }'
  789. );
  790. done();
  791. });
  792. });
  793. it('should properly convert maps when calling custom functions', function(done) {
  794. sass.render({
  795. data: '$test-map: foo((abc: 123, #def: true)); div { color: if(map-has-key($test-map, hello), #fff, #000); }' +
  796. 'span { color: map-get($test-map, baz); }',
  797. functions: {
  798. 'foo($m)': function(map) {
  799. assert.strictEqual(map.getLength(), 2);
  800. assert.ok(map.getKey(0) instanceof sass.types.String);
  801. assert.ok(map.getKey(1) instanceof sass.types.Color);
  802. assert.ok(map.getValue(0) instanceof sass.types.Number);
  803. assert.ok(map.getValue(1) instanceof sass.types.Boolean);
  804. assert.strictEqual(map.getKey(0).getValue(), 'abc');
  805. assert.strictEqual(map.getValue(0).getValue(), 123);
  806. assert.strictEqual(map.getKey(1).getR(), 0xdd);
  807. assert.strictEqual(map.getValue(1).getValue(), true);
  808. var out = new sass.types.Map(3);
  809. out.setKey(0, new sass.types.String('hello'));
  810. out.setValue(0, new sass.types.String('world'));
  811. out.setKey(1, new sass.types.String('foo'));
  812. out.setValue(1, new sass.types.String('bar'));
  813. out.setKey(2, new sass.types.String('baz'));
  814. out.setValue(2, new sass.types.String('qux'));
  815. return out;
  816. }
  817. }
  818. }, function(error, result) {
  819. assert.strictEqual(result.css.toString().trim(), 'div {\n color: #fff; }\n\nspan {\n color: qux; }');
  820. done();
  821. });
  822. });
  823. it('should properly convert null when calling custom functions', function(done) {
  824. sass.render({
  825. data: 'div { color: if(foo("bar"), #fff, #000); } ' +
  826. 'span { color: if(foo(null), #fff, #000); }' +
  827. 'table { color: if(bar() == null, #fff, #000); }',
  828. functions: {
  829. 'foo($a)': function(a) {
  830. return sass.types.Boolean(a instanceof sass.types.Null);
  831. },
  832. 'bar()': function() {
  833. return sass.NULL;
  834. }
  835. }
  836. }, function(error, result) {
  837. assert.strictEqual(
  838. result.css.toString().trim(),
  839. 'div {\n color: #000; }\n\nspan {\n color: #fff; }\n\ntable {\n color: #fff; }'
  840. );
  841. done();
  842. });
  843. });
  844. it('should be possible to carry sass values across different renders', function(done) {
  845. var persistentMap;
  846. sass.render({
  847. data: 'div { color: foo((abc: #112233, #ddeeff: true)); }',
  848. functions: {
  849. foo: function(m) {
  850. persistentMap = m;
  851. return sass.types.Color(0, 0, 0);
  852. }
  853. }
  854. }, function() {
  855. sass.render({
  856. data: 'div { color: map-get(bar(), abc); background-color: baz(); }',
  857. functions: {
  858. bar: function() {
  859. return persistentMap;
  860. },
  861. baz: function() {
  862. return persistentMap.getKey(1);
  863. }
  864. }
  865. }, function(errror, result) {
  866. assert.strictEqual(result.css.toString().trim(), 'div {\n color: #112233;\n background-color: #ddeeff; }');
  867. done();
  868. });
  869. });
  870. });
  871. it('should let us register custom functions without signatures', function(done) {
  872. sass.render({
  873. data: 'div { color: foo(20, 22); }',
  874. functions: {
  875. foo: function(a, b) {
  876. return new sass.types.Number(a.getValue() + b.getValue(), 'em');
  877. }
  878. }
  879. }, function(error, result) {
  880. assert.strictEqual(result.css.toString().trim(), 'div {\n color: 42em; }');
  881. done();
  882. });
  883. });
  884. it('should fail when returning anything other than a sass value from a custom function', function(done) {
  885. sass.render({
  886. data: 'div { color: foo(); }',
  887. functions: {
  888. 'foo()': function() {
  889. return {};
  890. }
  891. }
  892. }, function(error) {
  893. assert.ok(/A SassValue object was expected/.test(error.message));
  894. done();
  895. });
  896. });
  897. it('should properly bubble up standard JS errors thrown by custom functions', function(done) {
  898. sass.render({
  899. data: 'div { color: foo(); }',
  900. functions: {
  901. 'foo()': function() {
  902. throw new RangeError('This is a test error');
  903. }
  904. }
  905. }, function(error) {
  906. assert.ok(/This is a test error/.test(error.message));
  907. done();
  908. });
  909. });
  910. it('should properly bubble up unknown errors thrown by custom functions', function(done) {
  911. sass.render({
  912. data: 'div { color: foo(); }',
  913. functions: {
  914. 'foo()': function() {
  915. throw {};
  916. }
  917. }
  918. }, function(error) {
  919. assert.ok(/unexpected error/.test(error.message));
  920. done();
  921. });
  922. });
  923. it('should call custom functions with correct context', function(done) {
  924. function assertExpected(result) {
  925. assert.strictEqual(result.css.toString().trim(), 'div {\n foo1: 1;\n foo2: 2; }');
  926. }
  927. var options = {
  928. data: 'div { foo1: foo(); foo2: foo(); }',
  929. functions: {
  930. // foo() is stateful and will persist an incrementing counter
  931. 'foo()': function() {
  932. assert(this);
  933. this.fooCounter = (this.fooCounter || 0) + 1;
  934. return new sass.types.Number(this.fooCounter);
  935. }
  936. }
  937. };
  938. sass.render(options, function(error, result) {
  939. assertExpected(result);
  940. done();
  941. });
  942. });
  943. describe('should properly bubble up errors from sass color constructor', function() {
  944. it('four booleans', function(done) {
  945. sass.render({
  946. data: 'div { color: foo(); }',
  947. functions: {
  948. 'foo()': function() {
  949. return new sass.types.Color(false, false, false, false);
  950. }
  951. }
  952. }, function(error) {
  953. assert.ok(/Constructor arguments should be numbers exclusively/.test(error.message));
  954. done();
  955. });
  956. });
  957. it('two arguments', function(done) {
  958. sass.render({
  959. data: 'div { color: foo(); }',
  960. functions: {
  961. 'foo()': function() {
  962. return sass.types.Color(2,3);
  963. }
  964. }
  965. }, function(error) {
  966. assert.ok(/Constructor should be invoked with either 0, 1, 3 or 4 arguments/.test(error.message));
  967. done();
  968. });
  969. });
  970. it('single string argument', function(done) {
  971. sass.render({
  972. data: 'div { color: foo(); }',
  973. functions: {
  974. 'foo()': function() {
  975. return sass.types.Color('foo');
  976. }
  977. }
  978. }, function(error) {
  979. assert.ok(/Only argument should be an integer/.test(error.message));
  980. done();
  981. });
  982. });
  983. });
  984. it('should properly bubble up errors from sass value constructors', function(done) {
  985. sass.render({
  986. data: 'div { color: foo(); }',
  987. functions: {
  988. 'foo()': function() {
  989. return sass.types.Boolean('foo');
  990. }
  991. }
  992. }, function(error) {
  993. assert.ok(/Expected one boolean argument/.test(error.message));
  994. done();
  995. });
  996. });
  997. it('should properly bubble up errors from sass value setters', function(done) {
  998. sass.render({
  999. data: 'div { color: foo(); }',
  1000. functions: {
  1001. 'foo()': function() {
  1002. var ret = new sass.types.Number(42);
  1003. ret.setUnit(123);
  1004. return ret;
  1005. }
  1006. }
  1007. }, function(error) {
  1008. assert.ok(/Supplied value should be a string/.test(error.message));
  1009. done();
  1010. });
  1011. });
  1012. it('should fail when trying to set a bare number as the List item', function(done) {
  1013. sass.render({
  1014. data: 'div { color: foo(); }',
  1015. functions: {
  1016. 'foo()': function() {
  1017. var out = new sass.types.List(1);
  1018. out.setValue(0, 2);
  1019. return out;
  1020. }
  1021. }
  1022. }, function(error) {
  1023. assert.ok(/Supplied value should be a SassValue object/.test(error.message));
  1024. done();
  1025. });
  1026. });
  1027. it('should fail when trying to set a bare Object as the List item', function(done) {
  1028. sass.render({
  1029. data: 'div { color: foo(); }',
  1030. functions: {
  1031. 'foo()': function() {
  1032. var out = new sass.types.List(1);
  1033. out.setValue(0, {});
  1034. return out;
  1035. }
  1036. }
  1037. }, function(error) {
  1038. assert.ok(/A SassValue is expected as the list item/.test(error.message));
  1039. done();
  1040. });
  1041. });
  1042. it('should fail when trying to set a bare Object as the Map key', function(done) {
  1043. sass.render({
  1044. data: 'div { color: foo(); }',
  1045. functions: {
  1046. 'foo()': function() {
  1047. var out = new sass.types.Map(1);
  1048. out.setKey(0, {});
  1049. out.setValue(0, new sass.types.String('aaa'));
  1050. return out;
  1051. }
  1052. }
  1053. }, function(error) {
  1054. assert.ok(/A SassValue is expected as a map key/.test(error.message));
  1055. done();
  1056. });
  1057. });
  1058. it('should fail when trying to set a bare Object as the Map value', function(done) {
  1059. sass.render({
  1060. data: 'div { color: foo(); }',
  1061. functions: {
  1062. 'foo()': function() {
  1063. var out = new sass.types.Map(1);
  1064. out.setKey(0, new sass.types.String('aaa'));
  1065. out.setValue(0, {});
  1066. return out;
  1067. }
  1068. }
  1069. }, function(error) {
  1070. assert.ok(/A SassValue is expected as a map value/.test(error.message));
  1071. done();
  1072. });
  1073. });
  1074. it('should always map null, true and false to the same (immutable) object', function(done) {
  1075. var counter = 0;
  1076. sass.render({
  1077. data: 'div { color: foo(bar(null)); background-color: baz("foo" == "bar"); }',
  1078. functions: {
  1079. foo: function(a) {
  1080. assert.strictEqual(a, sass.TRUE,
  1081. 'Supplied value should be the same instance as sass.TRUE'
  1082. );
  1083. assert.strictEqual(
  1084. sass.types.Boolean(true), sass.types.Boolean(true),
  1085. 'sass.types.Boolean(true) should return a singleton');
  1086. assert.strictEqual(
  1087. sass.types.Boolean(true), sass.TRUE,
  1088. 'sass.types.Boolean(true) should be the same instance as sass.TRUE');
  1089. counter++;
  1090. return sass.types.String('foo');
  1091. },
  1092. bar: function(a) {
  1093. assert.strictEqual(a, sass.NULL,
  1094. 'Supplied value should be the same instance as sass.NULL');
  1095. assert.throws(function() {
  1096. return new sass.types.Null();
  1097. }, /Cannot instantiate SassNull/);
  1098. counter++;
  1099. return sass.TRUE;
  1100. },
  1101. baz: function(a) {
  1102. assert.strictEqual(a, sass.FALSE,
  1103. 'Supplied value should be the same instance as sass.FALSE');
  1104. assert.throws(function() {
  1105. return new sass.types.Boolean(false);
  1106. }, /Cannot instantiate SassBoolean/);
  1107. assert.strictEqual(
  1108. sass.types.Boolean(false), sass.types.Boolean(false),
  1109. 'sass.types.Boolean(false) should return a singleton');
  1110. assert.strictEqual(
  1111. sass.types.Boolean(false), sass.FALSE,
  1112. 'sass.types.Boolean(false) should return singleton identical to sass.FALSE');
  1113. counter++;
  1114. return sass.types.String('baz');
  1115. }
  1116. }
  1117. }, function() {
  1118. assert.strictEqual(counter, 3);
  1119. done();
  1120. });
  1121. });
  1122. });
  1123. describe('.render({stats: {}})', function() {
  1124. var start = Date.now();
  1125. it('should provide a start timestamp', function(done) {
  1126. sass.render({
  1127. file: fixture('include-files/index.scss')
  1128. }, function(error, result) {
  1129. assert(!error);
  1130. assert.strictEqual(typeof result.stats.start, 'number');
  1131. assert(result.stats.start >= start);
  1132. done();
  1133. });
  1134. });
  1135. it('should provide an end timestamp', function(done) {
  1136. sass.render({
  1137. file: fixture('include-files/index.scss')
  1138. }, function(error, result) {
  1139. assert(!error);
  1140. assert.strictEqual(typeof result.stats.end, 'number');
  1141. assert(result.stats.end >= result.stats.start);
  1142. done();
  1143. });
  1144. });
  1145. it('should provide a duration', function(done) {
  1146. sass.render({
  1147. file: fixture('include-files/index.scss')
  1148. }, function(error, result) {
  1149. assert(!error);
  1150. assert.strictEqual(typeof result.stats.duration, 'number');
  1151. assert.strictEqual(result.stats.end - result.stats.start, result.stats.duration);
  1152. done();
  1153. });
  1154. });
  1155. it('should contain the given entry file', function(done) {
  1156. sass.render({
  1157. file: fixture('include-files/index.scss')
  1158. }, function(error, result) {
  1159. assert(!error);
  1160. assert.strictEqual(result.stats.entry, fixture('include-files/index.scss'));
  1161. done();
  1162. });
  1163. });
  1164. it('should contain an array of all included files', function(done) {
  1165. var expected = [
  1166. fixture('include-files/bar.scss').replace(/\\/g, '/'),
  1167. fixture('include-files/foo.scss').replace(/\\/g, '/'),
  1168. fixture('include-files/index.scss').replace(/\\/g, '/')
  1169. ];
  1170. sass.render({
  1171. file: fixture('include-files/index.scss')
  1172. }, function(error, result) {
  1173. assert(!error);
  1174. assert.deepStrictEqual(result.stats.includedFiles.sort(), expected.sort());
  1175. done();
  1176. });
  1177. });
  1178. it('should contain array with the entry if there are no import statements', function(done) {
  1179. var expected = fixture('simple/index.scss').replace(/\\/g, '/');
  1180. sass.render({
  1181. file: fixture('simple/index.scss')
  1182. }, function(error, result) {
  1183. assert.deepStrictEqual(result.stats.includedFiles, [expected]);
  1184. done();
  1185. });
  1186. });
  1187. it('should state `data` as entry file', function(done) {
  1188. sass.render({
  1189. data: read(fixture('simple/index.scss'), 'utf8')
  1190. }, function(error, result) {
  1191. assert.strictEqual(result.stats.entry, 'data');
  1192. done();
  1193. });
  1194. });
  1195. it('should contain an empty array as includedFiles', function(done) {
  1196. sass.render({
  1197. data: read(fixture('simple/index.scss'), 'utf8')
  1198. }, function(error, result) {
  1199. assert.deepStrictEqual(result.stats.includedFiles, []);
  1200. done();
  1201. });
  1202. });
  1203. });
  1204. describe('.renderSync(options)', function() {
  1205. it('should compile sass to css with file', function(done) {
  1206. var expected = read(fixture('simple/expected.css'), 'utf8').trim();
  1207. var result = sass.renderSync({ file: fixture('simple/index.scss') });
  1208. assert.strictEqual(result.css.toString().trim(), expected.replace(/\r\n/g, '\n'));
  1209. done();
  1210. });
  1211. it('should compile sass to css with outFile set to absolute url', function(done) {
  1212. var result = sass.renderSync({
  1213. file: fixture('simple/index.scss'),
  1214. sourceMap: true,
  1215. outFile: fixture('simple/index-test.css')
  1216. });
  1217. assert.strictEqual(JSON.parse(result.map).file, 'index-test.css');
  1218. done();
  1219. });
  1220. it('should compile sass to css with outFile set to relative url', function(done) {
  1221. var result = sass.renderSync({
  1222. file: fixture('simple/index.scss'),
  1223. sourceMap: true,
  1224. outFile: './index-test.css'
  1225. });
  1226. assert.strictEqual(JSON.parse(result.map).file, 'index-test.css');
  1227. done();
  1228. });
  1229. it('should compile sass to css with outFile and sourceMap set to relative url', function(done) {
  1230. var result = sass.renderSync({
  1231. file: fixture('simple/index.scss'),
  1232. sourceMap: './deep/nested/index.map',
  1233. outFile: './index-test.css'
  1234. });
  1235. assert.strictEqual(JSON.parse(result.map).file, '../../index-test.css');
  1236. done();
  1237. });
  1238. it('should not generate source map when not requested', function(done) {
  1239. var result = sass.renderSync({
  1240. file: fixture('simple/index.scss'),
  1241. sourceMap: false
  1242. });
  1243. assert.strictEqual(Object.prototype.hasOwnProperty.call(result, 'map'), false, 'result has a map property');
  1244. done();
  1245. });
  1246. it('should not generate source map without outFile and no explicit path given', function(done) {
  1247. var result = sass.renderSync({
  1248. file: fixture('simple/index.scss'),
  1249. sourceMap: true
  1250. });
  1251. assert.strictEqual(Object.prototype.hasOwnProperty.call(result, 'map'), false, 'result has a map property');
  1252. done();
  1253. });
  1254. it('should compile generate map with sourceMapRoot pass-through option', function(done) {
  1255. var result = sass.renderSync({
  1256. file: fixture('simple/index.scss'),
  1257. sourceMap: './deep/nested/index.map',
  1258. sourceMapRoot: 'http://test.com/',
  1259. outFile: './index-test.css'
  1260. });
  1261. assert.strictEqual(JSON.parse(result.map).sourceRoot, 'http://test.com/');
  1262. done();
  1263. });
  1264. it('should compile sass to css with data', function(done) {
  1265. var src = read(fixture('simple/index.scss'), 'utf8');
  1266. var expected = read(fixture('simple/expected.css'), 'utf8').trim();
  1267. var result = sass.renderSync({ data: src });
  1268. assert.strictEqual(result.css.toString().trim(), expected.replace(/\r\n/g, '\n'));
  1269. done();
  1270. });
  1271. it('should compile sass to css using indented syntax', function(done) {
  1272. var src = read(fixture('indent/index.sass'), 'utf8');
  1273. var expected = read(fixture('indent/expected.css'), 'utf8').trim();
  1274. var result = sass.renderSync({
  1275. data: src,
  1276. indentedSyntax: true
  1277. });
  1278. assert.strictEqual(result.css.toString().trim(), expected.replace(/\r\n/g, '\n'));
  1279. done();
  1280. });
  1281. it('should NOT compile empty data string', function(done) {
  1282. assert.throws(function() {
  1283. sass.renderSync({ data: '' });
  1284. }, /No input specified: provide a file name or a source string to process/ );
  1285. done();
  1286. });
  1287. it('should NOT compile without any input', function(done) {
  1288. assert.throws(function() {
  1289. sass.renderSync({});
  1290. }, /No input specified: provide a file name or a source string to process/);
  1291. done();
  1292. });
  1293. it('should throw error for bad input', function(done) {
  1294. assert.throws(function() {
  1295. sass.renderSync('somestring');
  1296. });
  1297. assert.throws(function() {
  1298. sass.renderSync({ data: '#navbar width 80%;' });
  1299. });
  1300. done();
  1301. });
  1302. it('should compile with include paths', function(done) {
  1303. var src = read(fixture('include-path/index.scss'), 'utf8');
  1304. var expected = read(fixture('include-path/expected.css'), 'utf8').trim();
  1305. var result = sass.renderSync({
  1306. data: src,
  1307. includePaths: [
  1308. fixture('include-path/functions'),
  1309. fixture('include-path/lib')
  1310. ]
  1311. });
  1312. assert.strictEqual(result.css.toString().trim(), expected.replace(/\r\n/g, '\n'));
  1313. done();
  1314. });
  1315. it('should add cwd to the front on include paths', function(done) {
  1316. var src = fixture('cwd-include-path/root/index.scss');
  1317. var expected = read(fixture('cwd-include-path/expected.css'), 'utf8').trim();
  1318. var cwd = process.cwd();
  1319. process.chdir(fixture('cwd-include-path'));
  1320. var result = sass.renderSync({
  1321. file: src,
  1322. includePaths: [
  1323. fixture('include-path/functions'),
  1324. fixture('include-path/lib')
  1325. ]
  1326. });
  1327. process.chdir(cwd);
  1328. assert.strictEqual(result.css.toString().trim(), expected.replace(/\r\n/g, '\n'));
  1329. done();
  1330. });
  1331. it('should check SASS_PATH in the specified order', function(done) {
  1332. var src = read(fixture('sass-path/index.scss'), 'utf8');
  1333. var expectedRed = read(fixture('sass-path/expected-red.css'), 'utf8').trim();
  1334. var expectedOrange = read(fixture('sass-path/expected-orange.css'), 'utf8').trim();
  1335. var envIncludes = [
  1336. fixture('sass-path/red'),
  1337. fixture('sass-path/orange')
  1338. ];
  1339. process.env.SASS_PATH = envIncludes.join(path.delimiter);
  1340. var result = sass.renderSync({
  1341. data: src,
  1342. includePaths: []
  1343. });
  1344. assert.strictEqual(result.css.toString().trim(), expectedRed.replace(/\r\n/g, '\n'));
  1345. process.env.SASS_PATH = envIncludes.reverse().join(path.delimiter);
  1346. result = sass.renderSync({
  1347. data: src,
  1348. includePaths: []
  1349. });
  1350. assert.strictEqual(result.css.toString().trim(), expectedOrange.replace(/\r\n/g, '\n'));
  1351. done();
  1352. });
  1353. it('should prefer include path over SASS_PATH', function(done) {
  1354. var src = read(fixture('sass-path/index.scss'), 'utf8');
  1355. var expectedRed = read(fixture('sass-path/expected-red.css'), 'utf8').trim();
  1356. var expectedOrange = read(fixture('sass-path/expected-orange.css'), 'utf8').trim();
  1357. var envIncludes = [
  1358. fixture('sass-path/red')
  1359. ];
  1360. process.env.SASS_PATH = envIncludes.join(path.delimiter);
  1361. var result = sass.renderSync({
  1362. data: src,
  1363. includePaths: []
  1364. });
  1365. assert.strictEqual(result.css.toString().trim(), expectedRed.replace(/\r\n/g, '\n'));
  1366. result = sass.renderSync({
  1367. data: src,
  1368. includePaths: [fixture('sass-path/orange')]
  1369. });
  1370. assert.strictEqual(result.css.toString().trim(), expectedOrange.replace(/\r\n/g, '\n'));
  1371. done();
  1372. });
  1373. it('should render with precision option', function(done) {
  1374. var src = read(fixture('precision/index.scss'), 'utf8');
  1375. var expected = read(fixture('precision/expected.css'), 'utf8').trim();
  1376. var result = sass.renderSync({
  1377. data: src,
  1378. precision: 10
  1379. });
  1380. assert.strictEqual(result.css.toString().trim(), expected.replace(/\r\n/g, '\n'));
  1381. done();
  1382. });
  1383. it('should contain all included files in stats when data is passed', function(done) {
  1384. var src = read(fixture('include-files/index.scss'), 'utf8');
  1385. var expected = [
  1386. fixture('include-files/bar.scss').replace(/\\/g, '/'),
  1387. fixture('include-files/foo.scss').replace(/\\/g, '/')
  1388. ];
  1389. var result = sass.renderSync({
  1390. data: src,
  1391. includePaths: [fixture('include-files')]
  1392. });
  1393. assert.deepStrictEqual(result.stats.includedFiles, expected);
  1394. done();
  1395. });
  1396. it('should render with indentWidth and indentType options', function(done) {
  1397. var result = sass.renderSync({
  1398. data: 'div { color: transparent; }',
  1399. indentWidth: 7,
  1400. indentType: 'tab'
  1401. });
  1402. assert.strictEqual(result.css.toString().trim(), 'div {\n\t\t\t\t\t\t\tcolor: transparent; }');
  1403. done();
  1404. });
  1405. it('should render with linefeed option', function(done) {
  1406. var result = sass.renderSync({
  1407. data: 'div { color: transparent; }',
  1408. linefeed: 'lfcr'
  1409. });
  1410. assert.strictEqual(result.css.toString().trim(), 'div {\n\r color: transparent; }');
  1411. done();
  1412. });
  1413. });
  1414. describe('.renderSync(importer)', function() {
  1415. var src = read(fixture('include-files/index.scss'), 'utf8');
  1416. it('should override imports with "data" as input and returns file and contents', function(done) {
  1417. var result = sass.renderSync({
  1418. data: src,
  1419. importer: function(url, prev) {
  1420. return {
  1421. file: prev + url,
  1422. contents: 'div {color: yellow;}'
  1423. };
  1424. }
  1425. });
  1426. assert.strictEqual(result.css.toString().trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  1427. done();
  1428. });
  1429. it('should override imports with "file" as input and returns file and contents', function(done) {
  1430. var result = sass.renderSync({
  1431. file: fixture('include-files/index.scss'),
  1432. importer: function(url, prev) {
  1433. return {
  1434. file: prev + url,
  1435. contents: 'div {color: yellow;}'
  1436. };
  1437. }
  1438. });
  1439. assert.strictEqual(result.css.toString().trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  1440. done();
  1441. });
  1442. it('should override imports with "data" as input and returns file', function(done) {
  1443. var result = sass.renderSync({
  1444. data: src,
  1445. importer: function(url) {
  1446. return {
  1447. file: path.resolve(path.dirname(fixture('include-files/index.scss')), url + (path.extname(url) ? '' : '.scss'))
  1448. };
  1449. }
  1450. });
  1451. assert.strictEqual(result.css.toString().trim(), '/* foo.scss */\n/* bar.scss */');
  1452. done();
  1453. });
  1454. it('should override imports with "file" as input and returns file', function(done) {
  1455. var result = sass.renderSync({
  1456. file: fixture('include-files/index.scss'),
  1457. importer: function(url, prev) {
  1458. return {
  1459. file: path.resolve(path.dirname(prev), url + (path.extname(url) ? '' : '.scss'))
  1460. };
  1461. }
  1462. });
  1463. assert.strictEqual(result.css.toString().trim(), '/* foo.scss */\n/* bar.scss */');
  1464. done();
  1465. });
  1466. it('should override imports with "data" as input and returns contents', function(done) {
  1467. var result = sass.renderSync({
  1468. data: src,
  1469. importer: function() {
  1470. return {
  1471. contents: 'div {color: yellow;}'
  1472. };
  1473. }
  1474. });
  1475. assert.strictEqual(result.css.toString().trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  1476. done();
  1477. });
  1478. it('should override imports with "file" as input and returns contents', function(done) {
  1479. var result = sass.renderSync({
  1480. file: fixture('include-files/index.scss'),
  1481. importer: function() {
  1482. return {
  1483. contents: 'div {color: yellow;}'
  1484. };
  1485. }
  1486. });
  1487. assert.strictEqual(result.css.toString().trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  1488. done();
  1489. });
  1490. it('should fallback to default import behaviour if importer returns sass.NULL', function(done) {
  1491. var result = sass.renderSync({
  1492. file: fixture('include-files/index.scss'),
  1493. importer: function() {
  1494. return sass.NULL;
  1495. }
  1496. });
  1497. assert.strictEqual(result.css.toString().trim(), '/* foo.scss */\n/* bar.scss */');
  1498. done();
  1499. });
  1500. it('should fallback to default import behaviour if importer returns null for backwards compatibility', function(done) {
  1501. var result = sass.renderSync({
  1502. file: fixture('include-files/index.scss'),
  1503. importer: function() {
  1504. return null;
  1505. }
  1506. });
  1507. assert.strictEqual(result.css.toString().trim(), '/* foo.scss */\n/* bar.scss */');
  1508. done();
  1509. });
  1510. it('should fallback to default import behaviour if importer returns undefined for backwards compatibility', function(done) {
  1511. var result = sass.renderSync({
  1512. file: fixture('include-files/index.scss'),
  1513. importer: function() {
  1514. return undefined;
  1515. }
  1516. });
  1517. assert.strictEqual(result.css.toString().trim(), '/* foo.scss */\n/* bar.scss */');
  1518. done();
  1519. });
  1520. it('should fallback to default import behaviour if importer returns false for backwards compatibility', function(done) {
  1521. var result = sass.renderSync({
  1522. file: fixture('include-files/index.scss'),
  1523. importer: function() {
  1524. return false;
  1525. }
  1526. });
  1527. assert.strictEqual(result.css.toString().trim(), '/* foo.scss */\n/* bar.scss */');
  1528. done();
  1529. });
  1530. it('should accept arrays of importers and return respect the order', function(done) {
  1531. var result = sass.renderSync({
  1532. file: fixture('include-files/index.scss'),
  1533. importer: [
  1534. function() {
  1535. return sass.NULL;
  1536. },
  1537. function() {
  1538. return {
  1539. contents: 'div {color: yellow;}'
  1540. };
  1541. }
  1542. ]
  1543. });
  1544. assert.strictEqual(result.css.toString().trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  1545. done();
  1546. });
  1547. it('should be able to see its options in this.options', function(done) {
  1548. var fxt = fixture('include-files/index.scss');
  1549. var sync = false;
  1550. sass.renderSync({
  1551. file: fixture('include-files/index.scss'),
  1552. importer: function() {
  1553. assert.strictEqual(fxt, this.options.file);
  1554. sync = true;
  1555. return {};
  1556. }
  1557. });
  1558. assert.strictEqual(sync, true);
  1559. done();
  1560. });
  1561. it('should throw user-defined error', function(done) {
  1562. assert.throws(function() {
  1563. sass.renderSync({
  1564. data: src,
  1565. importer: function() {
  1566. return new Error('doesn\'t exist!');
  1567. }
  1568. });
  1569. }, /doesn't exist!/);
  1570. done();
  1571. });
  1572. it('should throw exception when importer returns an invalid value', function(done) {
  1573. assert.throws(function() {
  1574. sass.renderSync({
  1575. data: src,
  1576. importer: function() {
  1577. return { contents: new Buffer('i am not a string!') };
  1578. }
  1579. });
  1580. }, /returned value of `contents` must be a string/);
  1581. done();
  1582. });
  1583. });
  1584. describe('.renderSync(functions)', function() {
  1585. it('should call custom function in sync mode', function(done) {
  1586. var result = sass.renderSync({
  1587. data: 'div { width: cos(0) * 50px; }',
  1588. functions: {
  1589. 'cos($a)': function(angle) {
  1590. if (!(angle instanceof sass.types.Number)) {
  1591. throw new TypeError('Unexpected type for "angle"');
  1592. }
  1593. return new sass.types.Number(Math.cos(angle.getValue()));
  1594. }
  1595. }
  1596. });
  1597. assert.strictEqual(result.css.toString().trim(), 'div {\n width: 50px; }');
  1598. done();
  1599. });
  1600. it('should return a list of selectors after calling the headings custom function', function(done) {
  1601. var result = sass.renderSync({
  1602. data: '#{headings(2,5)} { color: #08c; }',
  1603. functions: {
  1604. 'headings($from: 0, $to: 6)': function(from, to) {
  1605. var i, f = from.getValue(), t = to.getValue(),
  1606. list = new sass.types.List(t - f + 1);
  1607. for (i = f; i <= t; i++) {
  1608. list.setValue(i - f, new sass.types.String('h' + i));
  1609. }
  1610. return list;
  1611. }
  1612. }
  1613. });
  1614. assert.strictEqual(result.css.toString().trim(), 'h2, h3, h4, h5 {\n color: #08c; }');
  1615. done();
  1616. });
  1617. it('should let custom function invoke sass types constructors without the `new` keyword', function(done) {
  1618. var result = sass.renderSync({
  1619. data: 'div { color: foo(); }',
  1620. functions: {
  1621. 'foo()': function() {
  1622. return sass.types.Number(42, 'em');
  1623. }
  1624. }
  1625. });
  1626. assert.strictEqual(result.css.toString().trim(), 'div {\n color: 42em; }');
  1627. done();
  1628. });
  1629. it('should let us register custom functions without signatures', function(done) {
  1630. var result = sass.renderSync({
  1631. data: 'div { color: foo(20, 22); }',
  1632. functions: {
  1633. foo: function(a, b) {
  1634. return new sass.types.Number(a.getValue() + b.getValue(), 'em');
  1635. }
  1636. }
  1637. });
  1638. assert.strictEqual(result.css.toString().trim(), 'div {\n color: 42em; }');
  1639. done();
  1640. });
  1641. it('should fail when returning anything other than a sass value from a custom function', function(done) {
  1642. assert.throws(function() {
  1643. sass.renderSync({
  1644. data: 'div { color: foo(); }',
  1645. functions: {
  1646. 'foo()': function() {
  1647. return {};
  1648. }
  1649. }
  1650. });
  1651. }, /A SassValue object was expected/);
  1652. done();
  1653. });
  1654. it('should properly bubble up standard JS errors thrown by custom functions', function(done) {
  1655. assert.throws(function() {
  1656. sass.renderSync({
  1657. data: 'div { color: foo(); }',
  1658. functions: {
  1659. 'foo()': function() {
  1660. throw new RangeError('This is a test error');
  1661. }
  1662. }
  1663. });
  1664. }, /This is a test error/);
  1665. done();
  1666. });
  1667. it('should properly bubble up unknown errors thrown by custom functions', function(done) {
  1668. assert.throws(function() {
  1669. sass.renderSync({
  1670. data: 'div { color: foo(); }',
  1671. functions: {
  1672. 'foo()': function() {
  1673. throw {};
  1674. }
  1675. }
  1676. });
  1677. }, /unexpected error/);
  1678. done();
  1679. });
  1680. it('should properly bubble up errors from sass value getters/setters/constructors', function(done) {
  1681. assert.throws(function() {
  1682. sass.renderSync({
  1683. data: 'div { color: foo(); }',
  1684. functions: {
  1685. 'foo()': function() {
  1686. return sass.types.Boolean('foo');
  1687. }
  1688. }
  1689. });
  1690. }, /Expected one boolean argument/);
  1691. assert.throws(function() {
  1692. sass.renderSync({
  1693. data: 'div { color: foo(); }',
  1694. functions: {
  1695. 'foo()': function() {
  1696. var ret = new sass.types.Number(42);
  1697. ret.setUnit(123);
  1698. return ret;
  1699. }
  1700. }
  1701. });
  1702. }, /Supplied value should be a string/);
  1703. done();
  1704. });
  1705. it('should call custom functions with correct context', function(done) {
  1706. function assertExpected(result) {
  1707. assert.strictEqual(result.css.toString().trim(), 'div {\n foo1: 1;\n foo2: 2; }');
  1708. }
  1709. var options = {
  1710. data: 'div { foo1: foo(); foo2: foo(); }',
  1711. functions: {
  1712. // foo() is stateful and will persist an incrementing counter
  1713. 'foo()': function() {
  1714. assert(this);
  1715. this.fooCounter = (this.fooCounter || 0) + 1;
  1716. return new sass.types.Number(this.fooCounter);
  1717. }
  1718. }
  1719. };
  1720. assertExpected(sass.renderSync(options));
  1721. done();
  1722. });
  1723. });
  1724. describe('.renderSync({stats: {}})', function() {
  1725. var start = Date.now();
  1726. var result = sass.renderSync({
  1727. file: fixture('include-files/index.scss')
  1728. });
  1729. it('should provide a start timestamp', function(done) {
  1730. assert.strictEqual(typeof result.stats.start, 'number');
  1731. assert(result.stats.start >= start);
  1732. done();
  1733. });
  1734. it('should provide an end timestamp', function(done) {
  1735. assert.strictEqual(typeof result.stats.end, 'number');
  1736. assert(result.stats.end >= result.stats.start);
  1737. done();
  1738. });
  1739. it('should provide a duration', function(done) {
  1740. assert.strictEqual(typeof result.stats.duration, 'number');
  1741. assert.strictEqual(result.stats.end - result.stats.start, result.stats.duration);
  1742. done();
  1743. });
  1744. it('should contain the given entry file', function(done) {
  1745. assert.strictEqual(result.stats.entry, resolveFixture('include-files/index.scss'));
  1746. done();
  1747. });
  1748. it('should contain an array of all included files', function(done) {
  1749. var expected = [
  1750. fixture('include-files/bar.scss').replace(/\\/g, '/'),
  1751. fixture('include-files/foo.scss').replace(/\\/g, '/'),
  1752. fixture('include-files/index.scss').replace(/\\/g, '/')
  1753. ].sort();
  1754. var actual = result.stats.includedFiles.sort();
  1755. assert.strictEqual(actual[0], expected[0]);
  1756. assert.strictEqual(actual[1], expected[1]);
  1757. assert.strictEqual(actual[2], expected[2]);
  1758. done();
  1759. });
  1760. it('should contain array with the entry if there are no import statements', function(done) {
  1761. var expected = fixture('simple/index.scss').replace(/\\/g, '/');
  1762. var result = sass.renderSync({
  1763. file: fixture('simple/index.scss')
  1764. });
  1765. assert.deepStrictEqual(result.stats.includedFiles, [expected]);
  1766. done();
  1767. });
  1768. it('should state `data` as entry file', function(done) {
  1769. var result = sass.renderSync({
  1770. data: read(fixture('simple/index.scss'), 'utf8')
  1771. });
  1772. assert.strictEqual(result.stats.entry, 'data');
  1773. done();
  1774. });
  1775. it('should contain an empty array as includedFiles', function(done) {
  1776. var result = sass.renderSync({
  1777. data: read(fixture('simple/index.scss'), 'utf8')
  1778. });
  1779. assert.deepStrictEqual(result.stats.includedFiles, []);
  1780. done();
  1781. });
  1782. });
  1783. describe('.info', function() {
  1784. var package = require('../package.json'),
  1785. info = sass.info;
  1786. it('should return a correct version info', function(done) {
  1787. assert(info.indexOf(package.version) > 0);
  1788. assert(info.indexOf('(Wrapper)') > 0);
  1789. assert(info.indexOf('[JavaScript]') > 0);
  1790. assert(info.indexOf('[NA]') < 0);
  1791. assert(info.indexOf('(Sass Compiler)') > 0);
  1792. assert(info.indexOf('[C/C++]') > 0);
  1793. done();
  1794. });
  1795. });
  1796. });