1 QUnit
.module( 'jquery.localize', QUnit
.newMwEnvironment() );
3 QUnit
.test( 'Handle basic replacements', 3, function ( assert
) {
5 mw
.messages
.set( 'basic', 'Basic stuff' );
8 html
= '<div><span><html:msg key="basic" /></span></div>';
9 $lc
= $( html
).localize().find( 'span' );
11 assert
.strictEqual( $lc
.text(), 'Basic stuff', 'Tag: html:msg' );
13 // Attribute: title-msg
14 html
= '<div><span title-msg="basic" /></span></div>';
15 $lc
= $( html
).localize().find( 'span' );
17 assert
.strictEqual( $lc
.attr( 'title' ), 'Basic stuff', 'Attribute: title-msg' );
20 html
= '<div><span alt-msg="basic" /></span></div>';
21 $lc
= $( html
).localize().find( 'span' );
23 assert
.strictEqual( $lc
.attr( 'alt' ), 'Basic stuff', 'Attribute: alt-msg' );
26 QUnit
.test( 'Proper escaping', 2, function ( assert
) {
28 mw
.messages
.set( 'properfoo', '<proper esc="test">' );
30 // This is handled by jQuery inside $.fn.localize, just a simple sanity checked
31 // making sure it is actually using text() and attr() (or something with the same effect)
34 html
= '<div><span><html:msg key="properfoo" /></span></div>';
35 $lc
= $( html
).localize().find( 'span' );
37 assert
.strictEqual( $lc
.text(), mw
.msg( 'properfoo' ), 'Content is inserted as text, not as html.' );
40 html
= '<div><span title-msg="properfoo" /></span></div>';
41 $lc
= $( html
).localize().find( 'span' );
43 assert
.strictEqual( $lc
.attr( 'title' ), mw
.msg( 'properfoo' ), 'Attributes are not inserted raw.' );
46 QUnit
.test( 'Options', 7, function ( assert
) {
50 'foo-bar-title': 'Read more about bars',
51 'foo-bar-label': 'The Bars',
52 'foo-bazz-title': 'Read more about bazz at $1 (last modified: $2)',
53 'foo-bazz-label': 'The Bazz ($1)',
54 'foo-welcome': 'Welcome to $1! (last visit: $2)'
56 var html
, $lc
, attrs
, x
, sitename
= 'Wikipedia';
59 html
= '<div><span title-msg="lorem"><html:msg key="ipsum" /></span></div>';
60 $lc
= $( html
).localize( {
64 assert
.strictEqual( $lc
.attr( 'title' ), 'Lorem', 'Message key prefix - attr' );
65 assert
.strictEqual( $lc
.text(), 'Ipsum', 'Message key prefix - text' );
67 // Variable keys mapping
69 html
= '<div><span title-msg="title"><html:msg key="label" /></span></div>';
70 $lc
= $( html
).localize( {
72 'title': 'foo-' + x
+ '-title',
73 'label': 'foo-' + x
+ '-label'
77 assert
.strictEqual( $lc
.attr( 'title' ), 'Read more about bars', 'Variable keys mapping - attr' );
78 assert
.strictEqual( $lc
.text(), 'The Bars', 'Variable keys mapping - text' );
80 // Passing parameteters to mw.msg
81 html
= '<div><span><html:msg key="foo-welcome" /></span></div>';
82 $lc
= $( html
).localize( {
84 'foo-welcome': [sitename
, 'yesterday']
88 assert
.strictEqual( $lc
.text(), 'Welcome to Wikipedia! (last visit: yesterday)', 'Passing parameteters to mw.msg' );
90 // Combination of options prefix, params and keys
92 html
= '<div><span title-msg="title"><html:msg key="label" /></span></div>';
93 $lc
= $( html
).localize( {
96 'title': x
+ '-title',
100 'title': [sitename
, '3 minutes ago'],
101 'label': [sitename
, '3 minutes ago']
106 assert
.strictEqual( $lc
.text(), 'The Bazz (Wikipedia)', 'Combination of options prefix, params and keys - text' );
107 assert
.strictEqual( $lc
.attr( 'title' ), 'Read more about bazz at Wikipedia (last modified: 3 minutes ago)', 'Combination of options prefix, params and keys - attr' );