summaryrefslogtreecommitdiff
path: root/tests/qunit/suites/resources/jquery/jquery.localize.test.js
blob: d3877e055db97aacd47381adf55567e06aaf9f70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
( function ( $, mw ) {
	QUnit.module( 'jquery.localize', QUnit.newMwEnvironment() );

	QUnit.test( 'Handle basic replacements', 4, function ( assert ) {
		var html, $lc;
		mw.messages.set( 'basic', 'Basic stuff' );

		// Tag: html:msg
		html = '<div><span><html:msg key="basic" /></span></div>';
		$lc = $( html ).localize().find( 'span' );

		assert.strictEqual( $lc.text(), 'Basic stuff', 'Tag: html:msg' );

		// Attribute: title-msg
		html = '<div><span title-msg="basic"></span></div>';
		$lc = $( html ).localize().find( 'span' );

		assert.strictEqual( $lc.attr( 'title' ), 'Basic stuff', 'Attribute: title-msg' );

		// Attribute: alt-msg
		html = '<div><span alt-msg="basic"></span></div>';
		$lc = $( html ).localize().find( 'span' );

		assert.strictEqual( $lc.attr( 'alt' ), 'Basic stuff', 'Attribute: alt-msg' );

		// Attribute: placeholder-msg
		html = '<div><input placeholder-msg="basic" /></div>';
		$lc = $( html ).localize().find( 'input' );

		assert.strictEqual( $lc.attr( 'placeholder' ), 'Basic stuff', 'Attribute: placeholder-msg' );
	} );

	QUnit.test( 'Proper escaping', 2, function ( assert ) {
		var html, $lc;
		mw.messages.set( 'properfoo', '<proper esc="test">' );

		// This is handled by jQuery inside $.fn.localize, just a simple sanity checked
		// making sure it is actually using text() and attr() (or something with the same effect)

		// Text escaping
		html = '<div><span><html:msg key="properfoo"></span></div>';
		$lc = $( html ).localize().find( 'span' );

		assert.strictEqual( $lc.text(), mw.msg( 'properfoo' ), 'Content is inserted as text, not as html.' );

		// Attribute escaping
		html = '<div><span title-msg="properfoo"></span></div>';
		$lc = $( html ).localize().find( 'span' );

		assert.strictEqual( $lc.attr( 'title' ), mw.msg( 'properfoo' ), 'Attributes are not inserted raw.' );
	} );

	QUnit.test( 'Options', 7, function ( assert ) {
		mw.messages.set( {
			'foo-lorem': 'Lorem',
			'foo-ipsum': 'Ipsum',
			'foo-bar-title': 'Read more about bars',
			'foo-bar-label': 'The Bars',
			'foo-bazz-title': 'Read more about bazz at $1 (last modified: $2)',
			'foo-bazz-label': 'The Bazz ($1)',
			'foo-welcome': 'Welcome to $1! (last visit: $2)'
		} );
		var html, $lc, x, sitename = 'Wikipedia';

		// Message key prefix
		html = '<div><span title-msg="lorem"><html:msg key="ipsum"></span></div>';
		$lc = $( html ).localize( {
			prefix: 'foo-'
		} ).find( 'span' );

		assert.strictEqual( $lc.attr( 'title' ), 'Lorem', 'Message key prefix - attr' );
		assert.strictEqual( $lc.text(), 'Ipsum', 'Message key prefix - text' );

		// Variable keys mapping
		x = 'bar';
		html = '<div><span title-msg="title"><html:msg key="label"></span></div>';
		$lc = $( html ).localize( {
			keys: {
				'title': 'foo-' + x + '-title',
				'label': 'foo-' + x + '-label'
			}
		} ).find( 'span' );

		assert.strictEqual( $lc.attr( 'title' ), 'Read more about bars', 'Variable keys mapping - attr' );
		assert.strictEqual( $lc.text(), 'The Bars', 'Variable keys mapping - text' );

		// Passing parameteters to mw.msg
		html = '<div><span><html:msg key="foo-welcome"></span></div>';
		$lc = $( html ).localize( {
			params: {
				'foo-welcome': [sitename, 'yesterday']
			}
		} ).find( 'span' );

		assert.strictEqual( $lc.text(), 'Welcome to Wikipedia! (last visit: yesterday)', 'Passing parameteters to mw.msg' );

		// Combination of options prefix, params and keys
		x = 'bazz';
		html = '<div><span title-msg="title"><html:msg key="label"></span></div>';
		$lc = $( html ).localize( {
			prefix: 'foo-',
			keys: {
				'title': x + '-title',
				'label': x + '-label'
			},
			params: {
				'title': [sitename, '3 minutes ago'],
				'label': [sitename, '3 minutes ago']

			}
		} ).find( 'span' );

		assert.strictEqual( $lc.text(), 'The Bazz (Wikipedia)', 'Combination of options prefix, params and keys - text' );
		assert.strictEqual( $lc.attr( 'title' ), 'Read more about bazz at Wikipedia (last modified: 3 minutes ago)', 'Combination of options prefix, params and keys - attr' );
	} );

	QUnit.test( 'Handle data text', 2, function ( assert ) {
		var html, $lc;
		mw.messages.set( 'option-one', 'Item 1' );
		mw.messages.set( 'option-two', 'Item 2' );
		html = '<select><option data-msg-text="option-one"></option><option data-msg-text="option-two"></option></select>';
		$lc = $( html ).localize().find( 'option' );
		assert.strictEqual( $lc.eq( 0 ).text(), mw.msg( 'option-one' ), 'data-msg-text becomes text of options' );
		assert.strictEqual( $lc.eq( 1 ).text(), mw.msg( 'option-two' ), 'data-msg-text becomes text of options' );
	} );

	QUnit.test( 'Handle data html', 2, function ( assert ) {
		var html, $lc;
		mw.messages.set( 'html', 'behold... there is a <a>link</a> here!!' );
		html = '<div><div data-msg-html="html"></div></div>';
		$lc = $( html ).localize().find( 'a' );
		assert.strictEqual( $lc.length, 1, 'link is created' );
		assert.strictEqual( $lc.text(), 'link', 'the link text got added' );
	} );
}( jQuery, mediaWiki ) );