|
2 | 2 | "use strict";
|
3 | 3 |
|
4 | 4 | const assert = require("assert");
|
| 5 | +const path = require("path"); |
| 6 | + |
| 7 | +const createParser = require("../lib/less/parser"); |
| 8 | +function parseContent({parserOptions, fnFileHandler, content}) { |
| 9 | + return new Promise(function(resolve, reject) { |
| 10 | + const parser = createParser(parserOptions, fnFileHandler); |
| 11 | + parser.parse(content, function(err, tree) { |
| 12 | + if (err) { |
| 13 | + reject(err); |
| 14 | + } else { |
| 15 | + resolve(tree); |
| 16 | + } |
| 17 | + }); |
| 18 | + }); |
| 19 | +} |
| 20 | +function createFileHandler(files) { |
| 21 | + return function(file, currentFileInfo, handleDataAndCallCallback, callback) { |
| 22 | + let pathname; |
| 23 | + |
| 24 | + // support absolute paths such as "/resources/my/base.less" |
| 25 | + if (path.posix.isAbsolute(file)) { |
| 26 | + pathname = path.posix.normalize(file); |
| 27 | + } else { |
| 28 | + pathname = path.posix.join(currentFileInfo.currentDirectory, file); |
| 29 | + } |
| 30 | + |
| 31 | + if (files[pathname]) { |
| 32 | + handleDataAndCallCallback(pathname, files[pathname]); |
| 33 | + } else { |
| 34 | + callback({type: "File", message: "Could not find file at path '" + pathname + "'"}); |
| 35 | + } |
| 36 | + }; |
| 37 | +} |
5 | 38 |
|
6 | 39 | // tested module
|
7 | 40 | const CSSVariablesCollectorPlugin = require("../lib/plugin/css-variables-collector");
|
@@ -78,3 +111,110 @@ describe("CSSVariablesCollectorPlugin.getResolvedUrl", function() {
|
78 | 111 | );
|
79 | 112 | });
|
80 | 113 | });
|
| 114 | + |
| 115 | +describe("CSSVariablesCollectorPlugin", function() { |
| 116 | + it("should collect variables and modify tree to produce skeleton CSS", async function() { |
| 117 | + const oCSSVariablesCollector = new CSSVariablesCollectorPlugin({ |
| 118 | + libPath: "sap/ui/foo", |
| 119 | + libName: "sap.ui.foo", |
| 120 | + prefix: "_sap_ui_foo_" |
| 121 | + }); |
| 122 | + const tree = await parseContent({ |
| 123 | + content: ` |
| 124 | + @myVar: red; |
| 125 | + .rule { |
| 126 | + color: @myVar; |
| 127 | + background-color: fade(@myVar, 50%) |
| 128 | + } |
| 129 | + `, |
| 130 | + parserOptions: { |
| 131 | + relativeUrls: true, |
| 132 | + filename: "sap/ui/foo/themes/base/library.source.less" |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + const cssSkeleton = tree.toCSS({ |
| 137 | + plugins: [oCSSVariablesCollector] |
| 138 | + }); |
| 139 | + |
| 140 | + assert.strictEqual(cssSkeleton, |
| 141 | + `.rule { |
| 142 | + color: var(--myVar); |
| 143 | + background-color: var(--_sap_ui_foo_function_fade1); |
| 144 | +} |
| 145 | +`); |
| 146 | + |
| 147 | + const cssVariablesSource = oCSSVariablesCollector.toLessVariables({}); |
| 148 | + assert.strictEqual(cssVariablesSource, |
| 149 | + `@myVar: #ff0000; |
| 150 | +@_sap_ui_foo_function_fade1: fade(@myVar, 50%); |
| 151 | +
|
| 152 | +:root { |
| 153 | +--myVar: @myVar; |
| 154 | +--_sap_ui_foo_function_fade1: @_sap_ui_foo_function_fade1; |
| 155 | +} |
| 156 | +`); |
| 157 | + |
| 158 | + const cssVariablesOnly = oCSSVariablesCollector.getCssVariablesDeclaration(); |
| 159 | + assert.strictEqual(cssVariablesOnly, |
| 160 | + `:root { |
| 161 | +--myVar: @myVar; |
| 162 | +} |
| 163 | +`); |
| 164 | + }); |
| 165 | + it("should provide proper relative URLs", async function() { |
| 166 | + const oCSSVariablesCollector = new CSSVariablesCollectorPlugin({ |
| 167 | + libPath: "sap/ui/foo", |
| 168 | + libName: "sap.ui.foo", |
| 169 | + prefix: "_sap_ui_foo_" |
| 170 | + }); |
| 171 | + const tree = await parseContent({ |
| 172 | + content: ` |
| 173 | + @import "../base/shared.less"; |
| 174 | + .rule { |
| 175 | + background-image: @myUrl; |
| 176 | + } |
| 177 | + `, |
| 178 | + parserOptions: { |
| 179 | + relativeUrls: true, |
| 180 | + filename: "sap/ui/foo/themes/sap_fiori_3/library.source.less" |
| 181 | + }, |
| 182 | + fnFileHandler: createFileHandler({ |
| 183 | + "sap/ui/foo/themes/base/shared.less": ` |
| 184 | + @myUrl: url(./fancy.png); |
| 185 | + ` |
| 186 | + }) |
| 187 | + }); |
| 188 | + |
| 189 | + const cssSkeleton = tree.toCSS({ |
| 190 | + plugins: [oCSSVariablesCollector] |
| 191 | + }); |
| 192 | + |
| 193 | + assert.strictEqual(cssSkeleton, |
| 194 | + `.rule { |
| 195 | + background-image: var(--myUrl); |
| 196 | +} |
| 197 | +`); |
| 198 | + |
| 199 | + const cssVariablesSource = oCSSVariablesCollector.toLessVariables({ |
| 200 | + myUrl: "url(../base/fancy.png)" |
| 201 | + }); |
| 202 | + assert.strictEqual(cssVariablesSource, |
| 203 | + `@myUrl: url(../base/fancy.png); |
| 204 | +@myUrl__asResolvedUrl: "ui5://sap/ui/foo/themes/base/fancy.png"; |
| 205 | +
|
| 206 | +:root { |
| 207 | +--myUrl: @myUrl; |
| 208 | +--myUrl__asResolvedUrl: @myUrl__asResolvedUrl; |
| 209 | +} |
| 210 | +`); |
| 211 | + |
| 212 | + const cssVariablesOnly = oCSSVariablesCollector.getCssVariablesDeclaration(); |
| 213 | + assert.strictEqual(cssVariablesOnly, |
| 214 | + `:root { |
| 215 | +--myUrl: @myUrl; |
| 216 | +--myUrl__asResolvedUrl: @myUrl__asResolvedUrl; |
| 217 | +} |
| 218 | +`); |
| 219 | + }); |
| 220 | +}); |
0 commit comments