jenkins-bot has submitted this change and it was merged.

Change subject: Remove concept of a default compiler
......................................................................


Remove concept of a default compiler

All templates using ResourceLoaderTemplateModule must now
declare their template extension.

Change-Id: Iab1c1a9025bd3e1e3d3a2bb5625adbdb6f7fddd2
---
M includes/ResourceLoaderTemplateModule.php
M javascripts/common/templates.js
M javascripts/common/templates/handlebars.js
M javascripts/common/templates/hogan.js
M tests/ResourceLoaderTemplateModuleTest.php
M tests/javascripts/common/test_templates.js
6 files changed, 74 insertions(+), 41 deletions(-)

Approvals:
  EBernhardson: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/ResourceLoaderTemplateModule.php 
b/includes/ResourceLoaderTemplateModule.php
index 11e5cfb..b16559c 100644
--- a/includes/ResourceLoaderTemplateModule.php
+++ b/includes/ResourceLoaderTemplateModule.php
@@ -84,12 +84,7 @@
         */
        protected function getLocalTemplatePath( $name ) {
                // @FIXME: Deprecate usage of template without file extension.
-               $ext = pathinfo( $name, PATHINFO_EXTENSION);
-               if ( $ext !== '' ) {
-                       return "{$this->localTemplateBasePath}/$name";
-               } else {
-                       return "{$this->localTemplateBasePath}/$name.html";
-               }
+               return "{$this->localTemplateBasePath}/$name";
        }
 
        /**
diff --git a/javascripts/common/templates.js b/javascripts/common/templates.js
index 4bc2ae1..f89d5ef 100644
--- a/javascripts/common/templates.js
+++ b/javascripts/common/templates.js
@@ -7,13 +7,6 @@
        templates = {}, template;
        template = {
                _compilers: {},
-               _getDefaultCompiler: function() {
-                       if ( this._compilerDefault && 
this._compilers[this._compilerDefault] ) {
-                               return this._compilers[this._compilerDefault];
-                       } else {
-                               throw new Error( 'Template default compiler not 
known.' );
-                       }
-               },
                registerCompiler: function( name, obj, isDefault ) {
                        if ( obj.compile ) {
                                this._compilers[name] = obj;
@@ -25,9 +18,10 @@
                        }
                },
                /**
-                * Define template using html. Compiles newly added templates
+                * Define a template. Compiles newly added templates based on
+                * the file extension of name and the available compilers.
                 * @method
-                * @param {String} name Name of template to add
+                * @param {String} name Name of template to add including file 
extension
                 * @param {String} markup Associated markup (html)
                 */
                add: function( name, markup ) {
@@ -42,9 +36,10 @@
                                        throw new Error( 'Template compiler not 
found for: ' + ext );
                                }
                        } else {
-                               compiler = this._getDefaultCompiler();
+                               throw new Error( 'Template has no suffix. 
Unable to identify compiler.' );
                        }
-                       templates[ name ] = compiler.compile( markup, name );
+                       templates[ name ] = compiler.compile( markup );
+                       compiler.registerPartial( templateParts[0], templates[ 
name ] );
                },
                /**
                 * Retrieve defined template
@@ -64,12 +59,15 @@
                 * Wraps our template engine of choice
                 * @method
                 * @param {string} templateBody Template body.
-                * @param {string} compiler The name of a registered compiler
+                * @param {string} compilerName The name of a registered 
compiler
                 * @return {mixed} template interface
                 * accepts template data object as its argument.
                 */
-               compile: function( templateBody, compiler ) {
-                       compiler = compiler ? this._compilers[ compiler ] : 
this._getDefaultCompiler();
+               compile: function( templateBody, compilerName ) {
+                       var compiler = this._compilers[ compilerName ];
+                       if ( !compiler ) {
+                               throw new Error( 'Unknown compiler ' + 
compilerName );
+                       }
                        return compiler.compile( templateBody );
                }
        };
diff --git a/javascripts/common/templates/handlebars.js 
b/javascripts/common/templates/handlebars.js
index 0dae5b2..9c39975 100644
--- a/javascripts/common/templates/handlebars.js
+++ b/javascripts/common/templates/handlebars.js
@@ -1,16 +1,28 @@
 /**
- * Register the Handlebars compiler as a default
+ * Register the Handlebars compiler with MediaWiki.
  */
 mw.mantle.template.registerCompiler( 'handlebars', {
-       compile: function( src, name ) {
-               // Using this, we override Handlebars' partials by injecting 
our own partials within it without
-               // having to register them manually
-               name = name.replace( '.handlebars', '' );
-               Handlebars.partials[ name ] = Handlebars.compile( src );
-
+       /**
+        * Registers a partial internally in the compiler.
+        *
+        * @method
+        * @param {String} name Name of the template
+        * @param {HandleBars.Template} partial
+        */
+       registerPartial: function( name, template ) {
+               Handlebars.partials[ name ] = template.render;
+       },
+       /**
+        * Compiler source code into a template object
+        *
+        * @method
+        * @param {String} src the source of a template
+        * @return {HandleBars.Template} template object
+        */
+       compile: function( src ) {
                return {
                        /** @param {*} data */
-                       render: Handlebars.partials[ name ]
+                       render: Handlebars.compile( src )
                };
        }
 }, true );
diff --git a/javascripts/common/templates/hogan.js 
b/javascripts/common/templates/hogan.js
index 55b87ca..d464ebc 100644
--- a/javascripts/common/templates/hogan.js
+++ b/javascripts/common/templates/hogan.js
@@ -1,4 +1,24 @@
 /**
- * Register the Hogan compiler as a default
+ * Register the Hogan compiler with MediaWiki.
  */
-mw.mantle.template.registerCompiler( 'hogan', Hogan, true );
+mw.mantle.template.registerCompiler( 'hogan', {
+       /**
+        * Registers a partial internally in the compiler.
+        * Not used in Hogan compiler
+        *
+        * @method
+        * @param {String} name Name of the template
+        * @param {HandleBars.Template} partial
+        */
+       registerPartial: function( /* name, partial */ ) {},
+       /**
+        * Compiler source code into a template object
+        *
+        * @method
+        * @param {String} src the source of a template
+        * @return {Hogan.Template} template object
+        */
+       compile: function( src ) {
+               return Hogan.compile( src );
+       }
+} );
diff --git a/tests/ResourceLoaderTemplateModuleTest.php 
b/tests/ResourceLoaderTemplateModuleTest.php
index 84c65dd..5f215f3 100644
--- a/tests/ResourceLoaderTemplateModuleTest.php
+++ b/tests/ResourceLoaderTemplateModuleTest.php
@@ -23,7 +23,7 @@
 
                'templateModule' => array(
                        'templates' => array(
-                               'template', 'template2',
+                               'template.html', 'template2.html',
                        )
                ),
 
@@ -94,7 +94,11 @@
                                $this->modules[0], array(),
                        ),
                        array(
-                               $this->modules['templateModule'], array( 
'template', 'template2' ),
+                               $this->modules['templateModule'],
+                               array(
+                                       'template.html',
+                                       'template2.html',
+                               ),
                        )
                );
        }
@@ -119,8 +123,8 @@
                        ),
                        array(
                                $module,
-                               'mw.mantle.template.add("template","hello\n");' 
.
-                               
'mw.mantle.template.add("template2","goodbye\n");'
+                               
'mw.mantle.template.add("template.html","hello\n");' .
+                               
'mw.mantle.template.add("template2.html","goodbye\n");'
                        )
                );
        }
diff --git a/tests/javascripts/common/test_templates.js 
b/tests/javascripts/common/test_templates.js
index 3874ff2..e46e26c 100644
--- a/tests/javascripts/common/test_templates.js
+++ b/tests/javascripts/common/test_templates.js
@@ -1,29 +1,33 @@
-( function( M ) {
+( function( M, $ ) {
 
 QUnit.module( 'Mantle templates', {
        setup: function() {
                var abcCompiler = {
+                       registerPartial: $.noop,
                        compile: function() { return 'abc default compiler'; }
                };
-               // stub the default
-               this.sandbox.stub( M.template, '_getDefaultCompiler' ).returns( 
abcCompiler );
                // Register some template compiler languages
                M.template.registerCompiler( 'abc', abcCompiler );
                M.template.registerCompiler( 'xyz', {
+                       registerPartial: $.noop,
                        compile: function() { return 'xyz compiler'; }
                } );
 
                // register some templates
-               M.template.add( 'test_templates_foo', 'hello' );
                M.template.add( 'test_templates_foo.xyz', 'goodbye' );
                M.template.add( 'test_templates_foo.abc', 'thankyou' );
        }
 } );
 
-QUnit.test( 'Template, getCompiler - default case', 3, function( assert ) {
-       assert.strictEqual( M.template.get( 'test_templates_foo' ), 'abc 
default compiler' );
+QUnit.test( 'Template, getCompiler - default case', 4, function( assert ) {
+       assert.throws( function() {
+                       M.template.add( 'test_templates_foo', 'hello' );
+               }, 'When no prefix throw exception.' );
+       assert.throws( function() {
+                       M.template.compile( '{{foo}}', 'rainbow' );
+               }, 'Unknown compiler names throw exceptions.' );
        assert.strictEqual( M.template.get( 'test_templates_foo.xyz' ), 'xyz 
compiler' );
        assert.strictEqual( M.template.get( 'test_templates_foo.abc' ), 'abc 
default compiler' );
 } );
 
-}( mw.mantle ) );
+}( mw.mantle, jQuery ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/149360
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iab1c1a9025bd3e1e3d3a2bb5625adbdb6f7fddd2
Gerrit-PatchSet: 11
Gerrit-Project: mediawiki/extensions/Mantle
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>
Gerrit-Reviewer: Dr0ptp4kt <[email protected]>
Gerrit-Reviewer: EBernhardson <[email protected]>
Gerrit-Reviewer: GWicke <[email protected]>
Gerrit-Reviewer: JGonera <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: Spage <[email protected]>
Gerrit-Reviewer: Yurik <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to