mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Marko v3: Added "preserveWhitespace" as a load template/compiler option
This commit is contained in:
parent
5d2b43b88e
commit
4339c8dfd1
@ -70,13 +70,19 @@ class Compiler {
|
||||
ok(this.parser, '"options.parser" is required');
|
||||
}
|
||||
|
||||
compile(src, filename) {
|
||||
compile(src, filename, options) {
|
||||
ok(typeof src === 'string', '"src" argument should be a string');
|
||||
ok(filename, '"filename" argument is required');
|
||||
ok(typeof filename === 'string', '"filename" argument should be a string');
|
||||
|
||||
var context = new CompileContext(src, filename, this.builder);
|
||||
|
||||
if (options) {
|
||||
if (options.preserveWhitespace) {
|
||||
context.setPreserveWhitespace(true);
|
||||
}
|
||||
}
|
||||
|
||||
// STAGE 1: Parse the template to produce the initial AST
|
||||
var ast = this.parser.parse(src, context);
|
||||
context.root = ast;
|
||||
|
||||
@ -76,14 +76,14 @@ function compileFile(filename, options, callback) {
|
||||
}
|
||||
|
||||
try {
|
||||
callback(null, compiler.compile(templateSrc, filename));
|
||||
callback(null, compiler.compile(templateSrc, filename, options));
|
||||
} catch(e) {
|
||||
callback(e);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
let templateSrc = fs.readFileSync(filename, {encoding: 'utf8'});
|
||||
return compiler.compile(templateSrc, filename);
|
||||
return compiler.compile(templateSrc, filename, options);
|
||||
}
|
||||
}
|
||||
|
||||
@ -105,7 +105,7 @@ function compile(src, filename, options, callback) {
|
||||
|
||||
if (callback) {
|
||||
try {
|
||||
callback(null, compiler.compile(src, filename));
|
||||
callback(null, compiler.compile(src, filename, options));
|
||||
} catch(e) {
|
||||
callback(e);
|
||||
}
|
||||
|
||||
@ -44,7 +44,7 @@ function loadSource(templatePath, compiledSrc) {
|
||||
return templateModule.exports;
|
||||
}
|
||||
|
||||
function loadFile(templatePath) {
|
||||
function loadFile(templatePath, options) {
|
||||
templatePath = nodePath.resolve(cwd, templatePath);
|
||||
var targetDir = nodePath.dirname(templatePath);
|
||||
|
||||
@ -54,7 +54,7 @@ function loadFile(templatePath) {
|
||||
return require(targetFile);
|
||||
}
|
||||
|
||||
var compiledSrc = markoCompiler.compileFile(templatePath);
|
||||
var compiledSrc = markoCompiler.compileFile(templatePath, options);
|
||||
// console.log('Compiled code for "' + templatePath + '":\n' + compiledSrc);
|
||||
|
||||
var filename = nodePath.basename(targetFile);
|
||||
@ -91,10 +91,10 @@ module.exports = function load(templatePath, templateSrc, options) {
|
||||
templateSrc = fs.readFileSync(templatePath, fsReadOptions);
|
||||
}
|
||||
|
||||
var compiledSrc = markoCompiler.compile(templateSrc, templatePath);
|
||||
var compiledSrc = markoCompiler.compile(templateSrc, templatePath, options);
|
||||
return loadSource(templatePath, compiledSrc);
|
||||
} else {
|
||||
return loadFile(templatePath);
|
||||
return loadFile(templatePath, options);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -39,6 +39,9 @@
|
||||
"ignore": true
|
||||
}
|
||||
},
|
||||
"<style>": {
|
||||
"preserve-whitespace": true
|
||||
},
|
||||
"<textarea>": {
|
||||
"preserve-whitespace": true
|
||||
},
|
||||
|
||||
8
test/fixtures/render/autotest/preserveWhitespace-load-option/expected.html
vendored
Normal file
8
test/fixtures/render/autotest/preserveWhitespace-load-option/expected.html
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
<div>
|
||||
This whitespace
|
||||
should be
|
||||
preserved.
|
||||
|
||||
preserveWhitespace is set to true
|
||||
See: test.js
|
||||
</div>
|
||||
8
test/fixtures/render/autotest/preserveWhitespace-load-option/template.marko
vendored
Normal file
8
test/fixtures/render/autotest/preserveWhitespace-load-option/template.marko
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
<div>
|
||||
This whitespace
|
||||
should be
|
||||
preserved.
|
||||
|
||||
preserveWhitespace is set to true
|
||||
See: test.js
|
||||
</div>
|
||||
4
test/fixtures/render/autotest/preserveWhitespace-load-option/test.js
vendored
Normal file
4
test/fixtures/render/autotest/preserveWhitespace-load-option/test.js
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
exports.templateData = {};
|
||||
exports.loadOptions = {
|
||||
preserveWhitespace: true
|
||||
};
|
||||
@ -18,12 +18,13 @@ describe('render', function() {
|
||||
var mainPath = path.join(dir, 'test.js');
|
||||
|
||||
var main = fs.existsSync(mainPath) ? require(mainPath) : {};
|
||||
|
||||
var loadOptions = main && main.loadOptions;
|
||||
|
||||
if (main.checkError) {
|
||||
var e;
|
||||
|
||||
try {
|
||||
marko.load(templatePath);
|
||||
marko.load(templatePath, loadOptions);
|
||||
} catch(_e) {
|
||||
e = _e;
|
||||
}
|
||||
@ -35,7 +36,7 @@ describe('render', function() {
|
||||
main.checkError(e);
|
||||
return '$PASS$';
|
||||
} else {
|
||||
var template = marko.load(templatePath);
|
||||
var template = marko.load(templatePath, loadOptions);
|
||||
var templateData = main.templateData || {};
|
||||
var html = template.renderSync(templateData);
|
||||
return html;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user