/* * 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 . */ const { HtmlUtils } = require( 'mmv.bootstrap' ); ( function () { /* eslint-disable no-jquery/no-parse-html-literal */ QUnit.module( 'mmv.HtmlUtils', QUnit.newMwEnvironment() ); QUnit.test( 'wrapAndJquerify() for single node', function ( assert ) { const $el = $( '' ); const el = $( '' ).get( 0 ); const html = ''; const invalid = {}; assert.strictEqual( HtmlUtils.wrapAndJquerify( $el ).html(), '', 'jQuery' ); assert.strictEqual( HtmlUtils.wrapAndJquerify( el ).html(), '', 'HTMLElement' ); assert.strictEqual( HtmlUtils.wrapAndJquerify( html ).html(), '', 'HTML string' ); assert.throws( function () { HtmlUtils.wrapAndJquerify( invalid ); }, 'throws exception for invalid type' ); } ); QUnit.test( 'wrapAndJquerify() for multiple nodes', function ( assert ) { const $el = $( '' ); const html = ''; assert.strictEqual( HtmlUtils.wrapAndJquerify( $el ).html(), '', 'jQuery' ); assert.strictEqual( HtmlUtils.wrapAndJquerify( html ).html(), '', 'HTML string' ); } ); QUnit.test( 'wrapAndJquerify() for text', function ( assert ) { const $el = $( document.createTextNode( 'foo' ) ); const html = 'foo'; assert.strictEqual( HtmlUtils.wrapAndJquerify( $el ).html(), 'foo', 'jQuery' ); assert.strictEqual( HtmlUtils.wrapAndJquerify( html ).html(), 'foo', 'HTML string' ); } ); QUnit.test( 'wrapAndJquerify() does not change original', function ( assert ) { const $el = $( '' ); const el = $( '' ).get( 0 ); HtmlUtils.wrapAndJquerify( $el ).find( 'span' ).prop( 'data-x', 1 ); HtmlUtils.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 ) { const $visibleChild = $( '
' ); const $invisibleChild = $( '
' ); const $styleChild = $( '
' ); const $invisibleChildInVisibleChild = $( '
' ); const $visibleChildInInvisibleChild = $( '
' ); const $invisibleChildWithVisibleSiblings = $( '
' ); HtmlUtils.filterInvisible( $visibleChild ); HtmlUtils.filterInvisible( $invisibleChild ); HtmlUtils.filterInvisible( $styleChild ); HtmlUtils.filterInvisible( $invisibleChildInVisibleChild ); HtmlUtils.filterInvisible( $visibleChildInInvisibleChild ); HtmlUtils.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, '