2 QUnit.module( 'jquery.localize', QUnit.newMwEnvironment() );
4 QUnit.test( 'Handle basic replacements', 4, function ( assert ) {
6 mw.messages.set( 'basic', 'Basic stuff' );
9 html = '<div><span><html:msg key="basic" /></span></div>';
10 $lc = $( html ).localize().find( 'span' );
12 assert.strictEqual( $lc.text(), 'Basic stuff', 'Tag: html:msg' );
14 // Attribute: title-msg
15 html = '<div><span title-msg="basic"></span></div>';
16 $lc = $( html ).localize().find( 'span' );
18 assert.strictEqual( $lc.attr( 'title' ), 'Basic stuff', 'Attribute: title-msg' );
21 html = '<div><span alt-msg="basic"></span></div>';
22 $lc = $( html ).localize().find( 'span' );
24 assert.strictEqual( $lc.attr( 'alt' ), 'Basic stuff', 'Attribute: alt-msg' );
26 // Attribute: placeholder-msg
27 html = '<div><input placeholder-msg="basic" /></div>';
28 $lc = $( html ).localize().find( 'input' );
30 assert.strictEqual( $lc.attr( 'placeholder' ), 'Basic stuff', 'Attribute: placeholder-msg' );
33 QUnit.test( 'Proper escaping', 2, function ( assert ) {
35 mw.messages.set( 'properfoo', '<proper esc="test">' );
37 // This is handled by jQuery inside $.fn.localize, just a simple sanity checked
38 // making sure it is actually using text() and attr() (or something with the same effect)
41 html = '<div><span><html:msg key="properfoo" /></span></div>';
42 $lc = $( html ).localize().find( 'span' );
44 assert.strictEqual( $lc.text(), mw.msg( 'properfoo' ), 'Content is inserted as text, not as html.' );
47 html = '<div><span title-msg="properfoo"></span></div>';
48 $lc = $( html ).localize().find( 'span' );
50 assert.strictEqual( $lc.attr( 'title' ), mw.msg( 'properfoo' ), 'Attributes are not inserted raw.' );
53 QUnit.test( 'Options', 7, function ( assert ) {
57 'foo-bar-title': 'Read more about bars',
58 'foo-bar-label': 'The Bars',
59 'foo-bazz-title': 'Read more about bazz at $1 (last modified: $2)',
60 'foo-bazz-label': 'The Bazz ($1)',
61 'foo-welcome': 'Welcome to $1! (last visit: $2)'
63 var html, $lc, x, sitename = 'Wikipedia';
66 html = '<div><span title-msg="lorem"><html:msg key="ipsum" /></span></div>';
67 $lc = $( html ).localize( {
71 assert.strictEqual( $lc.attr( 'title' ), 'Lorem', 'Message key prefix - attr' );
72 assert.strictEqual( $lc.text(), 'Ipsum', 'Message key prefix - text' );
74 // Variable keys mapping
76 html = '<div><span title-msg="title"><html:msg key="label" /></span></div>';
77 $lc = $( html ).localize( {
79 'title': 'foo-' + x + '-title',
80 'label': 'foo-' + x + '-label'
84 assert.strictEqual( $lc.attr( 'title' ), 'Read more about bars', 'Variable keys mapping - attr' );
85 assert.strictEqual( $lc.text(), 'The Bars', 'Variable keys mapping - text' );
87 // Passing parameteters to mw.msg
88 html = '<div><span><html:msg key="foo-welcome" /></span></div>';
89 $lc = $( html ).localize( {
91 'foo-welcome': [sitename, 'yesterday']
95 assert.strictEqual( $lc.text(), 'Welcome to Wikipedia! (last visit: yesterday)', 'Passing parameteters to mw.msg' );
97 // Combination of options prefix, params and keys
99 html = '<div><span title-msg="title"><html:msg key="label" /></span></div>';
100 $lc = $( html ).localize( {
103 'title': x + '-title',
104 'label': x + '-label'
107 'title': [sitename, '3 minutes ago'],
108 'label': [sitename, '3 minutes ago']
113 assert.strictEqual( $lc.text(), 'The Bazz (Wikipedia)', 'Combination of options prefix, params and keys - text' );
114 assert.strictEqual( $lc.attr( 'title' ), 'Read more about bazz at Wikipedia (last modified: 3 minutes ago)', 'Combination of options prefix, params and keys - attr' );
117 QUnit.test( 'Handle data text', 2, function ( assert ) {
119 mw.messages.set( 'option-one', 'Item 1' );
120 mw.messages.set( 'option-two', 'Item 2' );
121 html = '<select><option data-msg-text="option-one"></option><option data-msg-text="option-two"></option></select>';
122 $lc = $( html ).localize().find( 'option' );
123 assert.strictEqual( $lc.eq( 0 ).text(), mw.msg( 'option-one' ), 'data-msg-text becomes text of options' );
124 assert.strictEqual( $lc.eq( 1 ).text(), mw.msg( 'option-two' ), 'data-msg-text becomes text of options' );
127 QUnit.test( 'Handle data html', 2, function ( assert ) {
129 mw.messages.set( 'html', 'behold... there is a <a>link</a> here!!' );
130 html = '<div><div data-msg-html="html"></div></div>';
131 $lc = $( html ).localize().find( 'a' );
132 assert.strictEqual( $lc.length, 1, 'link is created' );
133 assert.strictEqual( $lc.text(), 'link', 'the link text got added' );
135 }( jQuery, mediaWiki ) );