774cbc31f3b0e86db565527647e83950703680b4
[lhc/web/wiklou.git] / tests / qunit / suites / resources / jquery / jquery.localize.js
1 module( 'jquery.localize.js' );
2
3 test( '-- Initial check', function() {
4 expect(1);
5 ok( $.fn.localize, 'jQuery.fn.localize defined' );
6 } );
7
8 test( 'Handle basic replacements', function() {
9 expect(4);
10
11 var html, $lc;
12 mw.messages.set( 'basic', 'Basic stuff' );
13
14 // Tag: html:msg
15 html = '<div><span><html:msg key="basic"/></span></div>';
16 $lc = $( html ).localize().find( 'span' );
17 console.log($lc.length);
18
19 strictEqual( $lc.text(), 'Basic stuff', 'Tag: html:msg' );
20
21 // Tag: msg (deprecated)
22 html = '<div><span><msg key="basic"/></span></div>';
23 $lc = $( html ).localize().find( 'span' );
24 $('body').append($lc);
25
26 strictEqual( $lc.text(), 'Basic stuff', 'Tag: msg' );
27
28 // Attribute: title-msg
29 html = '<div><span title-msg="basic"></span></div>';
30 $lc = $( html ).localize().find( 'span' );
31
32 strictEqual( $lc.attr( 'title' ), 'Basic stuff', 'Attribute: title-msg' );
33
34 // Attribute: alt-msg
35 html = '<div><span alt-msg="basic"></span></div>';
36 $lc = $( html ).localize().find( 'span' );
37
38 strictEqual( $lc.attr( 'alt' ), 'Basic stuff', 'Attribute: alt-msg' );
39 } );
40
41 test( 'Proper escaping', function() {
42 expect(2);
43
44 var html, $lc;
45 mw.messages.set( 'properfoo', '<proper esc="test">' );
46
47 // This is handled by jQuery inside $.fn.localize, just a simple sanity checked
48 // making sure it is actually using text() and attr() (or something with the same effect)
49
50 // Text escaping
51 html = '<div><span><html:msg key="properfoo"/></span></div>';
52 $lc = $( html ).localize().find( 'span' );
53
54 strictEqual( $lc.text(), mw.msg( 'properfoo' ), 'Content is inserted as text, not as html.' );
55
56 // Attribute escaping
57 html = '<div><span title-msg="properfoo"></span></div>';
58 $lc = $( html ).localize().find( 'span' );
59
60 strictEqual( $lc.attr( 'title' ), mw.msg( 'properfoo' ), 'Attributes are not inserted raw.' );
61 } );
62
63 test( 'Options', function() {
64 expect(7);
65
66 mw.messages.set( {
67 'foo-lorem': 'Lorem',
68 'foo-ipsum': 'Ipsum',
69 'foo-bar-title': 'Read more about bars',
70 'foo-bar-label': 'The Bars',
71 'foo-bazz-title': 'Read more about bazz at $1 (last modified: $2)',
72 'foo-bazz-label': 'The Bazz ($1)',
73 'foo-welcome': 'Welcome to $1! (last visit: $2)'
74 } );
75 var html, $lc, attrs, x, sitename = 'Wikipedia';
76
77 // Message key prefix
78 html = '<div><span title-msg="lorem"><html:msg key="ipsum" /></span></div>';
79 $lc = $( html ).localize( {
80 prefix: 'foo-'
81 } ).find( 'span' );
82
83 strictEqual( $lc.attr( 'title' ), 'Lorem', 'Message key prefix - attr' );
84 strictEqual( $lc.text(), 'Ipsum', 'Message key prefix - text' );
85
86 // Variable keys mapping
87 x = 'bar';
88 html = '<div><span title-msg="title"><html:msg key="label" /></span></div>';
89 $lc = $( html ).localize( {
90 keys: {
91 'title': 'foo-' + x + '-title',
92 'label': 'foo-' + x + '-label'
93 }
94 } ).find( 'span' );
95
96 strictEqual( $lc.attr( 'title' ), 'Read more about bars', 'Variable keys mapping - attr' );
97 strictEqual( $lc.text(), 'The Bars', 'Variable keys mapping - text' );
98
99 // Passing parameteters to mw.msg
100 html = '<div><span><html:msg key="foo-welcome" /></span></div>';
101 $lc = $( html ).localize( {
102 params: {
103 'foo-welcome': [sitename, 'yesterday']
104 }
105 } ).find( 'span' );
106
107 strictEqual( $lc.text(), 'Welcome to Wikipedia! (last visit: yesterday)', 'Passing parameteters to mw.msg' );
108
109 // Combination of options prefix, params and keys
110 x = 'bazz';
111 html = '<div><span title-msg="title"><html:msg key="label" /></span></div>';
112 $lc = $( html ).localize( {
113 prefix: 'foo-',
114 keys: {
115 'title': x + '-title',
116 'label': x + '-label'
117 },
118 params: {
119 'title': [sitename, '3 minutes ago'],
120 'label': [sitename, '3 minutes ago']
121
122 }
123 } ).find( 'span' );
124
125 strictEqual( $lc.text(), 'The Bazz (Wikipedia)', 'Combination of options prefix, params and keys - text' );
126 strictEqual( $lc.attr( 'title' ), 'Read more about bazz at Wikipedia (last modified: 3 minutes ago)', 'Combination of options prefix, params and keys - attr' );
127 } );