From 2b7d281f06a3b4880b0536b54aa11affc35aecee Mon Sep 17 00:00:00 2001 From: jdlrobson Date: Fri, 24 Apr 2015 17:12:46 -0700 Subject: [PATCH] Support Mustache partials in Mustache template module * Add template partial support which matches the server-side implementation and means that we have full mustache support. https://mustache.github.io/mustache.5.html#Partials Bug: T97188 Change-Id: Ic752f52669dbffa21c4a514509c3ea1da8ac5d9c --- .../mediawiki/mediawiki.template.mustache.js | 23 +++++++++++-- tests/phpunit/data/templates/conds.mustache | 1 + tests/qunit/QUnitTestResources.php | 2 ++ .../mediawiki.template.mustache.test.js | 32 +++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/data/templates/conds.mustache create mode 100644 tests/qunit/suites/resources/mediawiki/mediawiki.template.mustache.test.js diff --git a/resources/src/mediawiki/mediawiki.template.mustache.js b/resources/src/mediawiki/mediawiki.template.mustache.js index 624986a9bb..7f62256adf 100644 --- a/resources/src/mediawiki/mediawiki.template.mustache.js +++ b/resources/src/mediawiki/mediawiki.template.mustache.js @@ -4,8 +4,27 @@ mw.template.registerCompiler( 'mustache', { compile: function ( src ) { return { - render: function ( data ) { - return $( $.parseHTML( Mustache.render( src, data ) ) ); + /** + * @ignore + * @return {string} The raw source code of the template + */ + getSource: function () { + return src; + }, + /** + * @ignore + * @param {Object} data Data to render + * @param {Object} partialTemplates Map partial names to Mustache template objects + * returned by mw.template.get() + */ + render: function ( data, partialTemplates ) { + var partials = {}; + if ( partialTemplates ) { + $.each( partialTemplates, function ( name, template ) { + partials[ name ] = template.getSource(); + } ); + } + return $( $.parseHTML( Mustache.render( src, data, partials ) ) ); } }; } diff --git a/tests/phpunit/data/templates/conds.mustache b/tests/phpunit/data/templates/conds.mustache new file mode 100644 index 0000000000..5ebd2ea3d9 --- /dev/null +++ b/tests/phpunit/data/templates/conds.mustache @@ -0,0 +1 @@ +{{#list}}oh no{{/list}}{{#foo}}none of this should render{{/foo}} \ No newline at end of file diff --git a/tests/qunit/QUnitTestResources.php b/tests/qunit/QUnitTestResources.php index 545718a66e..926e986d6b 100644 --- a/tests/qunit/QUnitTestResources.php +++ b/tests/qunit/QUnitTestResources.php @@ -71,6 +71,7 @@ return array( 'tests/qunit/suites/resources/mediawiki/mediawiki.RegExp.test.js', 'tests/qunit/suites/resources/mediawiki/mediawiki.storage.test.js', 'tests/qunit/suites/resources/mediawiki/mediawiki.template.test.js', + 'tests/qunit/suites/resources/mediawiki/mediawiki.template.mustache.test.js', 'tests/qunit/suites/resources/mediawiki/mediawiki.test.js', 'tests/qunit/suites/resources/mediawiki/mediawiki.html.test.js', 'tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js', @@ -126,6 +127,7 @@ return array( 'mediawiki.toc', 'mediawiki.Uri', 'mediawiki.user', + 'mediawiki.template.mustache', 'mediawiki.template', 'mediawiki.util', 'mediawiki.special.recentchanges', diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.template.mustache.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.template.mustache.test.js new file mode 100644 index 0000000000..38ae5e490f --- /dev/null +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.template.mustache.test.js @@ -0,0 +1,32 @@ +( function ( mw ) { + + QUnit.module( 'mediawiki.template.mustache', { + setup: function () { + // Stub register some templates + this.sandbox.stub( mw.templates, 'get' ).returns( { + 'test_greeting.mustache': '
{{foo}}{{>suffix}}
', + 'test_greeting_suffix.mustache': ' goodbye' + } ); + } + } ); + + QUnit.test( 'render', 2, function ( assert ) { + var html, htmlPartial, data, partials, + template = mw.template.get( 'stub', 'test_greeting.mustache' ), + partial = mw.template.get( 'stub', 'test_greeting_suffix.mustache' ); + + data = { + foo: 'Hello' + }; + partials = { + suffix: partial + }; + + html = template.render( data ).html(); + htmlPartial = template.render( data, partials ).html(); + + assert.strictEqual( html, 'Hello', 'Render without partial' ); + assert.strictEqual( htmlPartial, 'Hello goodbye', 'Render with partial' ); + } ); + +}( mediaWiki ) ); -- 2.20.1