/*
* This file is part of the MediaWiki extension MediaViewer.
*
* MediaViewer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* MediaViewer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MediaViewer. If not, see .
*/
( function () {
/* eslint-disable no-jquery/no-parse-html-literal */
QUnit.module( 'mmv.HtmlUtils', QUnit.newMwEnvironment() );
QUnit.test( 'wrapAndJquerify() for single node', function ( assert ) {
var utils = new mw.mmv.HtmlUtils(),
$el = $( '' ),
el = $( '' ).get( 0 ),
html = '',
invalid = {};
assert.strictEqual( utils.wrapAndJquerify( $el ).html(), '', 'jQuery' );
assert.strictEqual( utils.wrapAndJquerify( el ).html(), '', 'HTMLElement' );
assert.strictEqual( utils.wrapAndJquerify( html ).html(), '', 'HTML string' );
assert.throws( function () {
utils.wrapAndJquerify( invalid );
}, 'throws exception for invalid type' );
} );
QUnit.test( 'wrapAndJquerify() for multiple nodes', function ( assert ) {
var utils = new mw.mmv.HtmlUtils(),
$el = $( '' ),
html = '';
assert.strictEqual( utils.wrapAndJquerify( $el ).html(), '', 'jQuery' );
assert.strictEqual( utils.wrapAndJquerify( html ).html(), '', 'HTML string' );
} );
QUnit.test( 'wrapAndJquerify() for text', function ( assert ) {
var utils = new mw.mmv.HtmlUtils(),
$el = $( document.createTextNode( 'foo' ) ),
html = 'foo';
assert.strictEqual( utils.wrapAndJquerify( $el ).html(), 'foo', 'jQuery' );
assert.strictEqual( utils.wrapAndJquerify( html ).html(), 'foo', 'HTML string' );
} );
QUnit.test( 'wrapAndJquerify() does not change original', function ( assert ) {
var utils = new mw.mmv.HtmlUtils(),
$el = $( '' ),
el = $( '' ).get( 0 );
utils.wrapAndJquerify( $el ).find( 'span' ).prop( 'data-x', 1 );
utils.wrapAndJquerify( el ).find( 'span' ).prop( 'data-x', 1 );
assert.strictEqual( $el.prop( 'data-x' ), undefined, 'wrapped jQuery element is not the same as original' );
assert.strictEqual( $( el ).prop( 'data-x' ), undefined, 'wrapped HTMLElement is not the same as original' );
} );
QUnit.test( 'filterInvisible()', function ( assert ) {
var utils = new mw.mmv.HtmlUtils(),
$visibleChild = $( '
' ),
$invisibleChild = $( '
' ),
$styleChild = $( '' ),
$invisibleChildInVisibleChild = $( '' ),
$visibleChildInInvisibleChild = $( '' ),
$invisibleChildWithVisibleSiblings = $( '' );
utils.filterInvisible( $visibleChild );
utils.filterInvisible( $invisibleChild );
utils.filterInvisible( $styleChild );
utils.filterInvisible( $invisibleChildInVisibleChild );
utils.filterInvisible( $visibleChildInInvisibleChild );
utils.filterInvisible( $invisibleChildWithVisibleSiblings );
assert.strictEqual( $visibleChild.has( 'span' ).length, 1, 'visible child is not filtered' );
assert.strictEqual( $invisibleChild.has( 'span' ).length, 0, 'invisible child is filtered' );
assert.strictEqual( $styleChild.has( 'style' ).length, 0, '