diff --git a/compiler/Compiler.js b/compiler/Compiler.js index 33cc9248c..d2292adc0 100644 --- a/compiler/Compiler.js +++ b/compiler/Compiler.js @@ -45,6 +45,12 @@ function transformTreeHelper(node, context) { } function transformTree(rootNode, context) { + + context.taglibLookup.forEachTemplateTransformer((transformer) => { + var transformFunc = transformer.getFunc(); + rootNode = transformFunc(rootNode, context) || rootNode; + }); + /* * The tree is continuously transformed until we go through an entire pass where * there were no new nodes that needed to be transformed. This loop makes sure that diff --git a/compiler/taglib-loader/Taglib/Taglib.js b/compiler/taglib-loader/Taglib/Taglib.js index 9387eb018..d3035b066 100644 --- a/compiler/taglib-loader/Taglib/Taglib.js +++ b/compiler/taglib-loader/Taglib/Taglib.js @@ -50,6 +50,7 @@ class Taglib { this.dirname = null; this.tags = {}; this.textTransformers = []; + this.transformers = []; this.attributes = {}; this.patternAttributes = []; this.inputFilesLookup = {}; @@ -91,6 +92,9 @@ class Taglib { addTextTransformer(transformer) { this.textTransformers.push(transformer); } + addTransformer(transformer) { + this.transformers.push(transformer); + } forEachTag(callback, thisObj) { forEachEntry(this.tags, function (key, tag) { callback.call(thisObj, tag); diff --git a/compiler/taglib-loader/loader-taglib.js b/compiler/taglib-loader/loader-taglib.js index ffea4dd35..2c29e4151 100644 --- a/compiler/taglib-loader/loader-taglib.js +++ b/compiler/taglib-loader/loader-taglib.js @@ -301,6 +301,33 @@ class TaglibLoader { var taglib = this.taglib; taglib.id = value; } + + transformer(value) { + // Marko allows a "text-transformer" to be registered. The provided + // text transformer will be called for any static text found in a template. + var taglib = this.taglib; + var dirname = this.dirname; + + var transformer = new Taglib.Transformer(); + + if (typeof value === 'string') { + value = { + path: value + }; + } + + propertyHandlers(value, { + path(value) { + var path = resolve(value, dirname); + transformer.path = path; + } + + }, this.dependencyChain.append('transformer').toString()); + + ok(transformer.path, '"path" is required for transformer'); + + taglib.addTransformer(transformer); + } } exports.loadTaglib = function(filePath, taglib, dependencyChain) { diff --git a/compiler/taglib-lookup/TaglibLookup.js b/compiler/taglib-lookup/TaglibLookup.js index a25867147..39479ba9a 100644 --- a/compiler/taglib-lookup/TaglibLookup.js +++ b/compiler/taglib-lookup/TaglibLookup.js @@ -143,6 +143,7 @@ class TaglibLookup { merge(this.merged, { tags: taglib.tags, + transformers: taglib.transformers, textTransformers: taglib.textTransformers, attributes: taglib.attributes, patternAttributes: taglib.patternAttributes @@ -286,6 +287,13 @@ class TaglibLookup { return attrDef; } + forEachTemplateTransformer(callback, thisObj) { + var transformers = this.merged.transformers; + if (transformers && transformers.length) { + transformers.forEach(callback, thisObj); + } + } + forEachNodeTransformer(node, callback, thisObj) { /* * Based on the type of node we have to choose how to transform it @@ -332,7 +340,7 @@ class TaglibLookup { this.merged.tags[tagName].forEachTransformer(addTransformer); } } - + if (this.merged.tags['*']) { this.merged.tags['*'].forEachTransformer(addTransformer); } diff --git a/test/autotests/render/template-transformer/expected.html b/test/autotests/render/template-transformer/expected.html new file mode 100644 index 000000000..c510c7c1a --- /dev/null +++ b/test/autotests/render/template-transformer/expected.html @@ -0,0 +1,6 @@ + +