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' );
25 // Attribute: placeholder-msg
26 html
= '<div><input placeholder-msg="basic" /></div>';
27 $lc
= $( html
).localize().find( 'input' );
29 assert
.strictEqual( $lc
.attr( 'placeholder' ), 'Basic stuff', 'Attribute: placeholder-msg' );
32 QUnit
.test( 'Proper escaping', 2, function ( assert
) {
34 mw
.messages
.set( 'properfoo', '<proper esc="test">' );
36 // This is handled by jQuery inside $.fn.localize, just a simple sanity checked
37 // making sure it is actually using text() and attr() (or something with the same effect)
40 html
= '<div><span><html:msg key="properfoo"></span></div>';
41 $lc
= $( html
).localize().find( 'span' );
43 assert
.strictEqual( $lc
.text(), mw
.msg( 'properfoo' ), 'Content is inserted as text, not as html.' );
46 html
= '<div><span title-msg="properfoo"></span></div>';
47 $lc
= $( html
).localize().find( 'span' );
49 assert
.strictEqual( $lc
.attr( 'title' ), mw
.msg( 'properfoo' ), 'Attributes are not inserted raw.' );
52 QUnit
.test( 'Options', 7, function ( assert
) {
56 'foo-bar-title': 'Read more about bars',
57 'foo-bar-label': 'The Bars',
58 'foo-bazz-title': 'Read more about bazz at $1 (last modified: $2)',
59 'foo-bazz-label': 'The Bazz ($1)',
60 'foo-welcome': 'Welcome to $1! (last visit: $2)'
62 var html
, $lc
, attrs
, x
, sitename
= 'Wikipedia';
65 html
= '<div><span title-msg="lorem"><html:msg key="ipsum"></span></div>';
66 $lc
= $( html
).localize( {
70 assert
.strictEqual( $lc
.attr( 'title' ), 'Lorem', 'Message key prefix - attr' );
71 assert
.strictEqual( $lc
.text(), 'Ipsum', 'Message key prefix - text' );
73 // Variable keys mapping
75 html
= '<div><span title-msg="title"><html:msg key="label"></span></div>';
76 $lc
= $( html
).localize( {
78 'title': 'foo-' + x
+ '-title',
79 'label': 'foo-' + x
+ '-label'
83 assert
.strictEqual( $lc
.attr( 'title' ), 'Read more about bars', 'Variable keys mapping - attr' );
84 assert
.strictEqual( $lc
.text(), 'The Bars', 'Variable keys mapping - text' );
86 // Passing parameteters to mw.msg
87 html
= '<div><span><html:msg key="foo-welcome"></span></div>';
88 $lc
= $( html
).localize( {
90 'foo-welcome': [sitename
, 'yesterday']
94 assert
.strictEqual( $lc
.text(), 'Welcome to Wikipedia! (last visit: yesterday)', 'Passing parameteters to mw.msg' );
96 // Combination of options prefix, params and keys
98 html
= '<div><span title-msg="title"><html:msg key="label"></span></div>';
99 $lc
= $( html
).localize( {
102 'title': x
+ '-title',
103 'label': x
+ '-label'
106 'title': [sitename
, '3 minutes ago'],
107 'label': [sitename
, '3 minutes ago']
112 assert
.strictEqual( $lc
.text(), 'The Bazz (Wikipedia)', 'Combination of options prefix, params and keys - text' );
113 assert
.strictEqual( $lc
.attr( 'title' ), 'Read more about bazz at Wikipedia (last modified: 3 minutes ago)', 'Combination of options prefix, params and keys - attr' );