Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/mmv.HtmlUtils.test.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MediaViewer.
3 *
4 * MediaViewer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MediaViewer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with MediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const { HtmlUtils } = require( 'mmv' );
19
20 ( function () {
21 /* eslint-disable no-jquery/no-parse-html-literal */
22
23 QUnit.module( 'mmv.HtmlUtils', QUnit.newMwEnvironment() );
24
25 QUnit.test( 'wrapAndJquerify() for single node', ( assert ) => {
26 const $el = $( '<span>' );
27 const el = $( '<span>' ).get( 0 );
28 const html = '<span></span>';
29 const invalid = {};
30
31 assert.strictEqual( HtmlUtils.wrapAndJquerify( $el ).html(), '<span></span>', 'jQuery' );
32 assert.strictEqual( HtmlUtils.wrapAndJquerify( el ).html(), '<span></span>', 'HTMLElement' );
33 assert.strictEqual( HtmlUtils.wrapAndJquerify( html ).html(), '<span></span>', 'HTML string' );
34
35 assert.throws( () => {
36 HtmlUtils.wrapAndJquerify( invalid );
37 }, 'throws exception for invalid type' );
38 } );
39
40 QUnit.test( 'wrapAndJquerify() for multiple nodes', ( assert ) => {
41 const $el = $( '<span></span><span></span>' );
42 const html = '<span></span><span></span>';
43
44 assert.strictEqual( HtmlUtils.wrapAndJquerify( $el ).html(), '<span></span><span></span>', 'jQuery' );
45 assert.strictEqual( HtmlUtils.wrapAndJquerify( html ).html(), '<span></span><span></span>', 'HTML string' );
46 } );
47
48 QUnit.test( 'wrapAndJquerify() for text', ( assert ) => {
49 const $el = $( document.createTextNode( 'foo' ) );
50 const html = 'foo';
51
52 assert.strictEqual( HtmlUtils.wrapAndJquerify( $el ).html(), 'foo', 'jQuery' );
53 assert.strictEqual( HtmlUtils.wrapAndJquerify( html ).html(), 'foo', 'HTML string' );
54 } );
55
56 QUnit.test( 'wrapAndJquerify() does not change original', ( assert ) => {
57 const $el = $( '<span>' );
58 const el = $( '<span>' ).get( 0 );
59
60 HtmlUtils.wrapAndJquerify( $el ).find( 'span' ).data( 'x', 1 );
61 HtmlUtils.wrapAndJquerify( el ).find( 'span' ).data( 'x', 1 );
62 assert.strictEqual( $el.data( 'x' ), undefined, 'wrapped jQuery element is not the same as original' );
63 assert.strictEqual( $( el ).data( 'x' ), undefined, 'wrapped HTMLElement is not the same as original' );
64 } );
65
66 QUnit.test( 'filterInvisible()', ( assert ) => {
67 const $visibleChild = $( '<div><span></span></div>' );
68 const $invisibleChild = $( '<div><span style="display: none"></span></div>' );
69 const $styleChild = $( '<div><style></style></div>' );
70 const $invisibleChildInVisibleChild = $( '<div><span><abbr style="display: none"></abbr></span></div>' );
71 const $visibleChildInInvisibleChild = $( '<div><span style="display: none"><abbr></abbr></span></div>' );
72 const $invisibleChildWithVisibleSiblings = $( '<div><span></span><abbr style="display: none"></abbr><b></b></div>' );
73
74 HtmlUtils.filterInvisible( $visibleChild );
75 HtmlUtils.filterInvisible( $invisibleChild );
76 HtmlUtils.filterInvisible( $styleChild );
77 HtmlUtils.filterInvisible( $invisibleChildInVisibleChild );
78 HtmlUtils.filterInvisible( $visibleChildInInvisibleChild );
79 HtmlUtils.filterInvisible( $invisibleChildWithVisibleSiblings );
80
81 assert.strictEqual( $visibleChild.has( 'span' ).length, 1, 'visible child is not filtered' );
82 assert.strictEqual( $invisibleChild.has( 'span' ).length, 0, 'invisible child is filtered' );
83 assert.strictEqual( $styleChild.has( 'style' ).length, 0, '<style> child is filtered' );
84 assert.strictEqual( $invisibleChildInVisibleChild.has( 'span' ).length, 1, 'visible child is not filtered...' );
85 assert.strictEqual( $invisibleChildInVisibleChild.has( 'abbr' ).length, 0, '... but its invisible child is' );
86 assert.strictEqual( $visibleChildInInvisibleChild.has( 'span' ).length, 0, 'invisible child is filtered...' );
87 assert.strictEqual( $visibleChildInInvisibleChild.has( 'abbr' ).length, 0, '...and its children too' );
88 assert.strictEqual( $visibleChild.has( 'span' ).length, 1, 'visible child is not filtered' );
89 assert.strictEqual( $invisibleChildWithVisibleSiblings.has( 'abbr' ).length, 0, 'invisible sibling is filtered...' );
90 assert.strictEqual( $invisibleChildWithVisibleSiblings.has( 'span' ).length, 1, '...but its visible siblings are not' );
91 assert.strictEqual( $invisibleChildWithVisibleSiblings.has( 'b' ).length, 1, '...but its visible siblings are not' );
92 } );
93
94 QUnit.test( 'allowlistHtml()', ( assert ) => {
95 const $allowlisted = $( '<div>abc<a>def</a>ghi</div>' );
96 const $nonAllowlisted = $( '<div>abc<span>def</span>ghi</div>' );
97 const $nonAllowlistedInAllowlisted = $( '<div>abc<a>d<span>e</span>f</a>ghi</div>' );
98 const $allowlistedInNonAllowlisted = $( '<div>abc<span>d<a>e</a>f</span>ghi</div>' );
99 const $siblings = $( '<div>ab<span>c</span>d<a>e</a>f<span>g</span>hi</div>' );
100
101 HtmlUtils.allowlistHtml( $allowlisted, 'a' );
102 HtmlUtils.allowlistHtml( $nonAllowlisted, 'a' );
103 HtmlUtils.allowlistHtml( $nonAllowlistedInAllowlisted, 'a' );
104 HtmlUtils.allowlistHtml( $allowlistedInNonAllowlisted, 'a' );
105 HtmlUtils.allowlistHtml( $siblings, 'a' );
106
107 assert.strictEqual( $allowlisted.has( 'a' ).length, 1, 'Allowlisted elements are kept.' );
108 assert.strictEqual( $nonAllowlisted.has( 'span' ).length, 0, 'Non-allowlisted elements are removed.' );
109 assert.strictEqual( $nonAllowlistedInAllowlisted.has( 'a' ).length, 1, 'Allowlisted parents are kept.' );
110 assert.strictEqual( $nonAllowlistedInAllowlisted.has( 'span' ).length, 0, 'Non-allowlisted children are removed.' );
111 assert.strictEqual( $allowlistedInNonAllowlisted.has( 'span' ).length, 0, 'Non-allowlisted parents are removed.' );
112 assert.strictEqual( $allowlistedInNonAllowlisted.has( 'a' ).length, 1, 'Allowlisted children are kept.' );
113 assert.strictEqual( $siblings.has( 'span' ).length, 0, 'Non-allowlisted siblings are removed.' );
114 assert.strictEqual( $siblings.has( 'a' ).length, 1, 'Allowlisted siblings are kept.' );
115 } );
116
117 QUnit.test( 'appendWhitespaceToBlockElements()', ( assert ) => {
118 const $noBlockElement = $( '<div>abc<i>def</i>ghi</div>' );
119 const $blockElement = $( '<div>abc<p>def</p>ghi</div>' );
120 const $linebreak = $( '<div>abc<br>def</div>' );
121
122 HtmlUtils.appendWhitespaceToBlockElements( $noBlockElement );
123 HtmlUtils.appendWhitespaceToBlockElements( $blockElement );
124 HtmlUtils.appendWhitespaceToBlockElements( $linebreak );
125
126 assert.true( /abcdefghi/.test( $noBlockElement.text() ), 'Non-block elemens are not whitespaced.' );
127 assert.true( /abc\s+def\s+ghi/.test( $blockElement.text() ), 'Block elemens are whitespaced.' );
128 assert.true( /abc\s+def/.test( $linebreak.text() ), 'Linebreaks are whitespaced.' );
129 } );
130
131 QUnit.test( 'jqueryToHtml()', ( assert ) => {
132
133 assert.strictEqual( HtmlUtils.jqueryToHtml( $( '<a>' ) ), '<a></a>',
134 'works for single element' );
135 assert.strictEqual( HtmlUtils.jqueryToHtml( $( '<b><a>foo</a></b>' ) ), '<b><a>foo</a></b>',
136 'works for complex element' );
137 assert.strictEqual( HtmlUtils.jqueryToHtml( $( '<a>foo</a>' ).contents() ), 'foo',
138 'works for text nodes' );
139 } );
140
141 QUnit.test( 'mergeWhitespace()', ( assert ) => {
142 assert.strictEqual( HtmlUtils.mergeWhitespace( ' x \n' ), 'x',
143 'leading/trainling whitespace is trimmed' );
144 assert.strictEqual( HtmlUtils.mergeWhitespace( 'x \n\n \n y' ), 'x\ny',
145 'whitespace containing a newline is collapsed into a single newline' );
146 assert.strictEqual( HtmlUtils.mergeWhitespace( 'x y' ), 'x y',
147 'multiple spaces are collapsed into a single one' );
148 } );
149
150 QUnit.test( 'htmlToText()', ( assert ) => {
151 const html = '<table><tr><td>Foo</td><td><a>bar</a></td><td style="display: none">baz</td></tr></table>';
152
153 assert.strictEqual( HtmlUtils.htmlToText( html ), 'Foo bar', 'works' );
154 } );
155
156 QUnit.test( 'htmlToTextWithLinks()', ( assert ) => {
157 const html = '<table><tr><td><b>F</b>o<i>o</i></td><td><a>bar</a></td><td style="display: none">baz</td></tr></table>';
158
159 assert.strictEqual( HtmlUtils.htmlToTextWithLinks( html ), 'Foo <a>bar</a>', 'works' );
160 } );
161
162 QUnit.test( 'htmlToTextWithTags()', ( assert ) => {
163 const html = '<table><tr><td><b>F</b>o<i>o</i><sub>o</sub><sup>o</sup></td><td><a>bar</a></td><td style="display: none">baz</td></tr></table>';
164
165 assert.strictEqual( HtmlUtils.htmlToTextWithTags( html ), '<b>F</b>o<i>o</i><sub>o</sub><sup>o</sup> <a>bar</a>', 'works' );
166 } );
167
168 QUnit.test( 'isJQueryOrHTMLElement()', ( assert ) => {
169 assert.strictEqual( HtmlUtils.isJQueryOrHTMLElement( $( '<span>' ) ), true, 'Recognizes jQuery objects correctly' );
170 assert.strictEqual( HtmlUtils.isJQueryOrHTMLElement( $( '<span>' ).get( 0 ) ), true, 'Recognizes HTMLElements correctly' );
171 assert.strictEqual( HtmlUtils.isJQueryOrHTMLElement( '<span></span>' ), false, 'Doesn\'t recognize HTML string' );
172 } );
173
174 QUnit.test( 'makeLinkText()', ( assert ) => {
175 assert.strictEqual( HtmlUtils.makeLinkText( 'foo', {
176 href: 'http://example.com',
177 title: 'h<b>t</b><i>m</i>l'
178 } ), '<a href="http://example.com" title="html">foo</a>', 'works' );
179 } );
180
181 /* eslint-enable no-jquery/no-parse-html-literal */
182 }() );