Lucas Werkmeister (WMDE) has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/358936 )
Change subject: Add QueryTemplate._buildTemplate internal function
......................................................................
Add QueryTemplate._buildTemplate internal function
The code in the function was originally part of Ic362ea1087, though not
as a function of its own and without a dedicated test.
Change-Id: I0829138a7057c4fdd2dc796cd0d375f25488e735
---
M wikibase/queryService/ui/visualEditor/QueryTemplate.js
M wikibase/tests/queryService/ui/visualEditor/QueryTemplate.test.js
2 files changed, 37 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/wikidata/query/gui
refs/changes/36/358936/1
diff --git a/wikibase/queryService/ui/visualEditor/QueryTemplate.js
b/wikibase/queryService/ui/visualEditor/QueryTemplate.js
index 2fe402c..4469e55 100644
--- a/wikibase/queryService/ui/visualEditor/QueryTemplate.js
+++ b/wikibase/queryService/ui/visualEditor/QueryTemplate.js
@@ -61,5 +61,30 @@
return fragments;
};
+ /**
+ * Assemble the template span out of the fragments.
+ *
+ * @param {string[]} fragments The template fragments (see {@link
_getQueryTemplateFragments}).
+ * @param {Object.<string, jQuery>} variables The individual variables
are stored in this object, indexed by variable name.
+ * @return {jQuery}
+ */
+ SELF._buildTemplate = function( fragments, variables ) {
+ var template = $( '<span>' );
+
+ template.append( document.createTextNode( fragments[ 0 ] ) );
+ for ( var i = 1; i < fragments.length; i += 2 ) {
+ var variable = fragments[ i ],
+ $variable = $( '<span>' ).text( variable );
+ if ( !( variable in variables ) ) {
+ variables[variable] = [];
+ }
+ variables[variable].push( $variable );
+ template.append( $variable );
+ template.append( document.createTextNode( fragments[ i
+ 1 ] ) );
+ }
+
+ return template;
+ };
+
return SELF;
}( jQuery, wikibase ) );
diff --git a/wikibase/tests/queryService/ui/visualEditor/QueryTemplate.test.js
b/wikibase/tests/queryService/ui/visualEditor/QueryTemplate.test.js
index f3ff237..d300bef 100644
--- a/wikibase/tests/queryService/ui/visualEditor/QueryTemplate.test.js
+++ b/wikibase/tests/queryService/ui/visualEditor/QueryTemplate.test.js
@@ -48,4 +48,16 @@
"should not be possible to manipulate the fragment list
via regex characters in variables"
);
} );
+
+ QUnit.test( '_buildTemplate internal function', function( assert ) {
+ assert.expect( 4 );
+
+ var variables = {};
+ var template = QueryTemplate._buildTemplate( [ 'a ', '?b', ' c
', '?d', ' e ?f g' ], variables );
+
+ assert.equal( template.text(), 'a ?b c ?d e ?f g', 'template
text should look like template' );
+ assert.ok( '?b' in variables, 'variables should contain the
variables from the template' );
+ assert.ok( '?d' in variables, 'variables should contain the
variables from the template' );
+ assert.equal( Object.getOwnPropertyNames( variables ).length,
2, 'variables should not contain any other properties' );
+ } );
}( jQuery, QUnit, sinon, wikibase ) );
--
To view, visit https://gerrit.wikimedia.org/r/358936
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I0829138a7057c4fdd2dc796cd0d375f25488e735
Gerrit-PatchSet: 1
Gerrit-Project: wikidata/query/gui
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits