From 957430a5e1e5f8919a2411fe523fb10de0783bd5 Mon Sep 17 00:00:00 2001 From: Scimonster Date: Mon, 14 Dec 2015 18:56:26 +0200 Subject: [PATCH] Add case-changing magic words to mw.jqueryMsg Add the lc, uc, lcfirst, and ucfirst magic words. Task done as part of GCI15. Bug: T46495 Change-Id: I246b878ff4e8ef4cf49fe1859ee6c5d33d373b30 --- .../src/mediawiki/mediawiki.jqueryMsg.js | 42 +++++++++++++++++++ .../mediawiki/mediawiki.jqueryMsg.test.js | 23 ++++++++++ 2 files changed, 65 insertions(+) diff --git a/resources/src/mediawiki/mediawiki.jqueryMsg.js b/resources/src/mediawiki/mediawiki.jqueryMsg.js index 620dd5e5af..e905f69b71 100644 --- a/resources/src/mediawiki/mediawiki.jqueryMsg.js +++ b/resources/src/mediawiki/mediawiki.jqueryMsg.js @@ -1246,6 +1246,48 @@ number = nodes[ 0 ]; return this.language.convertNumber( number, isInteger ); + }, + + /** + * Lowercase text + * + * @param {Array} nodes List of nodes + * @return {string} The given text, all in lowercase + */ + lc: function ( nodes ) { + return textify( nodes[ 0 ] ).toLowerCase(); + }, + + /** + * Uppercase text + * + * @param {Array} nodes List of nodes + * @return {string} The given text, all in uppercase + */ + uc: function ( nodes ) { + return textify( nodes[ 0 ] ).toUpperCase(); + }, + + /** + * Lowercase first letter of input, leaving the rest unchanged + * + * @param {Array} nodes List of nodes + * @return {string} The given text, with the first character in lowercase + */ + lcfirst: function ( nodes ) { + var text = textify( nodes[ 0 ] ); + return text.charAt( 0 ).toLowerCase() + text.slice( 1 ); + }, + + /** + * Uppercase first letter of input, leaving the rest unchanged + * + * @param {Array} nodes List of nodes + * @return {string} The given text, with the first character in uppercase + */ + ucfirst: function ( nodes ) { + var text = textify( nodes[ 0 ] ); + return text.charAt( 0 ).toUpperCase() + text.slice( 1 ); } }; diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js index 53a714f81c..43b324edb5 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js @@ -333,6 +333,29 @@ mw.user.options.set( 'gender', originalGender ); } ); + QUnit.test( 'Case changing', 8, function ( assert ) { + mw.messages.set( 'to-lowercase', '{{lc:thIS hAS MEsSed uP CapItaliZatiON}}' ); + assert.equal( formatParse( 'to-lowercase' ), 'this has messed up capitalization', 'To lowercase' ); + + mw.messages.set( 'to-caps', '{{uc:thIS hAS MEsSed uP CapItaliZatiON}}' ); + assert.equal( formatParse( 'to-caps' ), 'THIS HAS MESSED UP CAPITALIZATION', 'To caps' ); + + mw.messages.set( 'uc-to-lcfirst', '{{lcfirst:THis hAS MEsSed uP CapItaliZatiON}}' ); + mw.messages.set( 'lc-to-lcfirst', '{{lcfirst:thIS hAS MEsSed uP CapItaliZatiON}}' ); + assert.equal( formatParse( 'uc-to-lcfirst' ), 'tHis hAS MEsSed uP CapItaliZatiON', 'Lcfirst caps' ); + assert.equal( formatParse( 'lc-to-lcfirst' ), 'thIS hAS MEsSed uP CapItaliZatiON', 'Lcfirst lowercase' ); + + mw.messages.set( 'uc-to-ucfirst', '{{ucfirst:THis hAS MEsSed uP CapItaliZatiON}}' ); + mw.messages.set( 'lc-to-ucfirst', '{{ucfirst:thIS hAS MEsSed uP CapItaliZatiON}}' ); + assert.equal( formatParse( 'uc-to-ucfirst' ), 'THis hAS MEsSed uP CapItaliZatiON', 'Ucfirst caps' ); + assert.equal( formatParse( 'lc-to-ucfirst' ), 'ThIS hAS MEsSed uP CapItaliZatiON', 'Ucfirst lowercase' ); + + mw.messages.set( 'mixed-to-sentence', '{{ucfirst:{{lc:thIS hAS MEsSed uP CapItaliZatiON}}}}' ); + assert.equal( formatParse( 'mixed-to-sentence' ), 'This has messed up capitalization', 'To sentence case' ); + mw.messages.set( 'all-caps-except-first', '{{lcfirst:{{uc:thIS hAS MEsSed uP CapItaliZatiON}}}}' ); + assert.equal( formatParse( 'all-caps-except-first' ), 'tHIS HAS MESSED UP CAPITALIZATION', 'To opposite sentence case' ); + } ); + QUnit.test( 'Grammar', 2, function ( assert ) { assert.equal( formatParse( 'grammar-msg' ), 'Przeszukaj Wiki', 'Grammar Test with sitename' ); -- 2.20.1