1 module( 'jquery.localize', QUnit.newMwEnvironment() );
3 test( '-- Initial check', function() {
5 ok( $.fn.localize, 'jQuery.fn.localize defined' );
8 test( 'Handle basic replacements', function() {
12 mw.messages.set( 'basic', 'Basic stuff' );
15 html = '<div><span><html:msg key="basic" /></span></div>';
16 $lc = $( html ).localize().find( 'span' );
18 strictEqual( $lc.text(), 'Basic stuff', 'Tag: html:msg' );
20 // Attribute: title-msg
21 html = '<div><span title-msg="basic" /></span></div>';
22 $lc = $( html ).localize().find( 'span' );
24 strictEqual( $lc.attr( 'title' ), 'Basic stuff', 'Attribute: title-msg' );
27 html = '<div><span alt-msg="basic" /></span></div>';
28 $lc = $( html ).localize().find( 'span' );
30 strictEqual( $lc.attr( 'alt' ), 'Basic stuff', 'Attribute: alt-msg' );
33 test( 'Proper escaping', function() {
37 mw.messages.set( 'properfoo', '<proper esc="test">' );
39 // This is handled by jQuery inside $.fn.localize, just a simple sanity checked
40 // making sure it is actually using text() and attr() (or something with the same effect)
43 html = '<div><span><html:msg key="properfoo" /></span></div>';
44 $lc = $( html ).localize().find( 'span' );
46 strictEqual( $lc.text(), mw.msg( 'properfoo' ), 'Content is inserted as text, not as html.' );
49 html = '<div><span title-msg="properfoo" /></span></div>';
50 $lc = $( html ).localize().find( 'span' );
52 strictEqual( $lc.attr( 'title' ), mw.msg( 'properfoo' ), 'Attributes are not inserted raw.' );
55 test( 'Options', function() {
61 'foo-bar-title': 'Read more about bars',
62 'foo-bar-label': 'The Bars',
63 'foo-bazz-title': 'Read more about bazz at $1 (last modified: $2)',
64 'foo-bazz-label': 'The Bazz ($1)',
65 'foo-welcome': 'Welcome to $1! (last visit: $2)'
67 var html, $lc, attrs, x, sitename = 'Wikipedia';
70 html = '<div><span title-msg="lorem"><html:msg key="ipsum" /></span></div>';
71 $lc = $( html ).localize( {
75 strictEqual( $lc.attr( 'title' ), 'Lorem', 'Message key prefix - attr' );
76 strictEqual( $lc.text(), 'Ipsum', 'Message key prefix - text' );
78 // Variable keys mapping
80 html = '<div><span title-msg="title"><html:msg key="label" /></span></div>';
81 $lc = $( html ).localize( {
83 'title': 'foo-' + x + '-title',
84 'label': 'foo-' + x + '-label'
88 strictEqual( $lc.attr( 'title' ), 'Read more about bars', 'Variable keys mapping - attr' );
89 strictEqual( $lc.text(), 'The Bars', 'Variable keys mapping - text' );
91 // Passing parameteters to mw.msg
92 html = '<div><span><html:msg key="foo-welcome" /></span></div>';
93 $lc = $( html ).localize( {
95 'foo-welcome': [sitename, 'yesterday']
99 strictEqual( $lc.text(), 'Welcome to Wikipedia! (last visit: yesterday)', 'Passing parameteters to mw.msg' );
101 // Combination of options prefix, params and keys
103 html = '<div><span title-msg="title"><html:msg key="label" /></span></div>';
104 $lc = $( html ).localize( {
107 'title': x + '-title',
108 'label': x + '-label'
111 'title': [sitename, '3 minutes ago'],
112 'label': [sitename, '3 minutes ago']
117 strictEqual( $lc.text(), 'The Bazz (Wikipedia)', 'Combination of options prefix, params and keys - text' );
118 strictEqual( $lc.attr( 'title' ), 'Read more about bazz at Wikipedia (last modified: 3 minutes ago)', 'Combination of options prefix, params and keys - attr' );