/*
* 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 ( mw, $ ) {
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' );
try {
utils.wrapAndJquerify( invalid );
} catch ( e ) {
assert.ok( e, '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 = $( '
' ),
$invisibleChildInVisibleChild = $( '' ),
$visibleChildInInvisibleChild = $( '' ),
$invisibleChildWithVisibleSiblings = $( '' );
utils.filterInvisible( $visibleChild );
utils.filterInvisible( $invisibleChild );
utils.filterInvisible( $invisibleChildInVisibleChild );
utils.filterInvisible( $visibleChildInInvisibleChild );
utils.filterInvisible( $invisibleChildWithVisibleSiblings );
assert.ok( $visibleChild.has( 'span' ).length, 'visible child is not filtered' );
assert.notOk( $invisibleChild.has( 'span' ).length, 'invisible child is filtered' );
assert.ok( $invisibleChildInVisibleChild.has( 'span' ).length, 'visible child is not filtered...' );
assert.notOk( $invisibleChildInVisibleChild.has( 'abbr' ).length, '... but its invisible child is' );
assert.notOk( $visibleChildInInvisibleChild.has( 'span' ).length, 'invisible child is filtered...' );
assert.notOk( $visibleChildInInvisibleChild.has( 'abbr' ).length, '...and its children too' );
assert.ok( $visibleChild.has( 'span' ).length, 'visible child is not filtered' );
assert.notOk( $invisibleChildWithVisibleSiblings.has( 'abbr' ).length, 'invisible sibling is filtered...' );
assert.ok( $invisibleChildWithVisibleSiblings.has( 'span' ).length, '...but its visible siblings are not' );
assert.ok( $invisibleChildWithVisibleSiblings.has( 'b' ).length, '...but its visible siblings are not' );
} );
QUnit.test( 'whitelistHtml()', function ( assert ) {
var utils = new mw.mmv.HtmlUtils(),
$whitelisted = $( '' ),
$nonWhitelisted = $( 'abcdefghi
' ),
$nonWhitelistedInWhitelisted = $( '' ),
$whitelistedInNonWhitelisted = $( '' ),
$siblings = $( '' );
utils.whitelistHtml( $whitelisted, 'a' );
utils.whitelistHtml( $nonWhitelisted, 'a' );
utils.whitelistHtml( $nonWhitelistedInWhitelisted, 'a' );
utils.whitelistHtml( $whitelistedInNonWhitelisted, 'a' );
utils.whitelistHtml( $siblings, 'a' );
assert.ok( $whitelisted.has( 'a' ).length, 'Whitelisted elements are kept.' );
assert.notOk( $nonWhitelisted.has( 'span' ).length, 'Non-whitelisted elements are removed.' );
assert.ok( $nonWhitelistedInWhitelisted.has( 'a' ).length, 'Whitelisted parents are kept.' );
assert.notOk( $nonWhitelistedInWhitelisted.has( 'span' ).length, 'Non-whitelisted children are removed.' );
assert.notOk( $whitelistedInNonWhitelisted.has( 'span' ).length, 'Non-whitelisted parents are removed.' );
assert.ok( $whitelistedInNonWhitelisted.has( 'a' ).length, 'Whitelisted children are kept.' );
assert.notOk( $siblings.has( 'span' ).length, 'Non-whitelisted siblings are removed.' );
assert.ok( $siblings.has( 'a' ).length, 'Whitelisted siblings are kept.' );
} );
QUnit.test( 'appendWhitespaceToBlockElements()', function ( assert ) {
var utils = new mw.mmv.HtmlUtils(),
$noBlockElement = $( 'abcdefghi
' ),
$blockElement = $( '' ),
$linebreak = $( 'abc
def
' );
utils.appendWhitespaceToBlockElements( $noBlockElement );
utils.appendWhitespaceToBlockElements( $blockElement );
utils.appendWhitespaceToBlockElements( $linebreak );
assert.ok( $noBlockElement.text().match( /abcdefghi/ ), 'Non-block elemens are not whitespaced.' );
assert.ok( $blockElement.text().match( /abc\s+def\s+ghi/ ), 'Block elemens are whitespaced.' );
assert.ok( $linebreak.text().match( /abc\s+def/ ), 'Linebreaks are whitespaced.' );
} );
QUnit.test( 'jqueryToHtml()', function ( assert ) {
var utils = new mw.mmv.HtmlUtils();
assert.strictEqual( utils.jqueryToHtml( $( '' ) ), '',
'works for single element' );
assert.strictEqual( utils.jqueryToHtml( $( 'foo' ) ), 'foo',
'works for complex element' );
assert.strictEqual( utils.jqueryToHtml( $( 'foo' ).contents() ), 'foo',
'works for text nodes' );
} );
QUnit.test( 'mergeWhitespace()', function ( assert ) {
var utils = new mw.mmv.HtmlUtils();
assert.strictEqual( utils.mergeWhitespace( ' x \n' ), 'x',
'leading/trainling whitespace is trimmed' );
assert.strictEqual( utils.mergeWhitespace( 'x \n\n \n y' ), 'x\ny',
'whitespace containing a newline is collapsed into a single newline' );
assert.strictEqual( utils.mergeWhitespace( 'x y' ), 'x y',
'multiple spaces are collapsed into a single one' );
} );
QUnit.test( 'htmlToText()', function ( assert ) {
var utils = new mw.mmv.HtmlUtils(),
html = '';
assert.strictEqual( utils.htmlToText( html ), 'Foo bar', 'works' );
} );
QUnit.test( 'htmlToTextWithLinks()', function ( assert ) {
var utils = new mw.mmv.HtmlUtils(),
html = '';
assert.strictEqual( utils.htmlToTextWithLinks( html ), 'Foo bar', 'works' );
} );
QUnit.test( 'htmlToTextWithTags()', function ( assert ) {
var utils = new mw.mmv.HtmlUtils(),
html = '';
assert.strictEqual( utils.htmlToTextWithTags( html ), 'Foooo bar', 'works' );
} );
QUnit.test( 'isJQueryOrHTMLElement()', function ( assert ) {
var utils = new mw.mmv.HtmlUtils();
assert.strictEqual( utils.isJQueryOrHTMLElement( $( '' ) ), true, 'Recognizes jQuery objects correctly' );
assert.strictEqual( utils.isJQueryOrHTMLElement( $( '' ).get( 0 ) ), true, 'Recognizes HTMLElements correctly' );
assert.strictEqual( utils.isJQueryOrHTMLElement( '' ), false, 'Doesn\'t recognize HTML string' );
} );
QUnit.test( 'makeLinkText()', function ( assert ) {
var utils = new mw.mmv.HtmlUtils();
assert.strictEqual( utils.makeLinkText( 'foo', {
href: 'http://example.com',
title: 'html'
} ), 'foo', 'works' );
} );
}( mediaWiki, jQuery ) );