Icontem

File: test/core/urls.js

Recommend this page to a friend!
  Classes of Sebastian Schlapkohl  >  jQuery Annex  >  test/core/urls.js  >  Download  
File: test/core/urls.js
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: jQuery Annex
General purpose functions missing in jQuery
Author: By
Last change: fixes for urlParam and urlAnchor // new multilineTextEllipsis implementation

reworked urlParam methods and urlAnchor methods to be more robust and consistent // added new tests for those methods // reworked multilineTextEllipsis to implement more precise algorithm with linear runtime not growing anymore based on length difference between text and available screen space
Date: 2 months ago
Size: 5,052 bytes
 

Contents

Class file image Download
import test from 'ava';



$versions.forEach($ => {
	let jqueryVersionString = ' @ jQuery '+$().jquery;



	test('$.urlParameter'+jqueryVersionString, (assert) => {
		let url1 = 'foobar.com/foo',
			url2 = 'https://www.foo.bar',
			url3 = '//foo.bar/foo/bar/',
			qString = '?boofar&foo=123&bar=%C3%BC%C3%A4%C3%B6%C3%9C%C3%84%C3%96%C3%9F%2B-%3C%3E%3F%26%3D&boo=boo&boo=oob&far[]=raf&far[]=%C3%BC%C3%A4%C3%B6%C3%9C%C3%84%C3%96%C3%9F%2B-%3C%3E%3F%26%3D';
		
		for(let url of [url1, url2, url3]){
			assert.true($.urlParameter(url+qString, 'boofar'));
			assert.is($.urlParameter(url+qString, 'foo'), '123');
			assert.is($.urlParameter(url+qString, 'bar'), '+-<>?&=');
			assert.deepEqual($.urlParameter(url+qString, 'boo'), ['boo', 'oob']);
			assert.deepEqual($.urlParameter(url+qString, 'far[]'), ['raf', '+-<>?&=']);
			assert.deepEqual($.urlParameter(url+qString), {
				'boofar' : true,
				'foo' : '123',
				'bar' : '+-<>?&=',
				'boo' : ['boo', 'oob'],
				'far[]' : ['raf', '+-<>?&=']
			});
			assert.deepEqual($.urlParameters(url+qString), {
				'boofar' : true,
				'foo' : '123',
				'bar' : '+-<>?&=',
				'boo' : ['boo', 'oob'],
				'far[]' : ['raf', '+-<>?&=']
			});
			assert.is($.urlParameter(url+qString, 'test'), null);
			assert.true($.urlParameter('?test', 'test'));
			assert.is($.urlParameter('?test', 'testo'), null);
			assert.is($.urlParameter('test', 'testo'), null);
			assert.is($.urlParameter(url, 'test'), null);
			assert.deepEqual($.urlParameter(url), {});
		}
	});



	test('$.urlParameters'+jqueryVersionString, (assert) => {
		assert.pass('included in $.urlParameter');
	});



	test('$.fn.urlParameter'+jqueryVersionString, (assert) => {
		let paramString = '?boofar&foo=123&bar=%C3%BC%C3%A4%C3%B6%C3%9C%C3%84%C3%96%C3%9F%2B-%3C%3E%3F%26%3D&boo=boo&boo=oob&far[]=raf&far[]=%C3%BC%C3%A4%C3%B6%C3%9C%C3%84%C3%96%C3%9F%2B-%3C%3E%3F%26%3D',
			$dummyWindow = $('<window></window>'),
			$foo = $('<img src="https://www.ifschleife.de/img/foo.png'+paramString+'" alt=""/>'),
			$bar = $('<a href="http://test.ifschleife.de/bar/'+paramString+'#test" target="_blank">bar</a>'),
			$boofar = $('<iframe src="http://test.ifschleife.de/bar/#test"></iframe>');

		$dummyWindow.get(0).location = {
			href : 'https://ifschleife.de/foobar',
			search : paramString
		};
		
		for( let $element of [$dummyWindow, $foo, $bar] ){
			assert.true($element.urlParameter('boofar'));
			assert.is($element.urlParameter('foo'), '123');
			assert.is($element.urlParameter('bar'), '+-<>?&=');
			assert.deepEqual($element.urlParameter('boo'), ['boo', 'oob']);
			assert.deepEqual($element.urlParameter('far[]'), ['raf', '+-<>?&=']);
			assert.deepEqual($element.urlParameter(), {
				'boofar' : true,
				'foo' : '123',
				'bar' : '+-<>?&=',
				'boo' : ['boo', 'oob'],
				'far[]' : ['raf', '+-<>?&=']
			});
			assert.deepEqual($element.urlParameters(), {
				'boofar' : true,
				'foo' : '123',
				'bar' : '+-<>?&=',
				'boo' : ['boo', 'oob'],
				'far[]' : ['raf', '+-<>?&=']
			});
			assert.is($element.urlParameter('test'), null);
		}
		assert.is($boofar.urlParameter('test'), null);
		assert.deepEqual($boofar.urlParameter(), {});
	});



	test('$.fn.urlParameters'+jqueryVersionString, (assert) => {
		assert.pass('included in $.fn.urlParameter');
	});



	test('$.urlAnchor'+jqueryVersionString, (assert) => {
		let url1 = 'foobar.com/foo',
			url2 = 'https://www.foo.bar',
			url3 = '//foo.bar/foo/bar/',
			anchors = [
				{
					urlAnchor : '#foo',
					anchor : '#foo',
					rawAnchor : 'foo'
				},
				{
					urlAnchor : '#bar=foo',
					anchor : '#bar=foo',
					rawAnchor : 'bar=foo'
				},
				{
					urlAnchor : '#%C3%BC%C3%A4%C3%B6%C3%9C%C3%84%C3%96%C3%9F%2B-%3C%3E%3F%26%3D',
					anchor : '#+-<>?&=',
					rawAnchor : '+-<>?&='
				},
				{
					urlAnchor : '',
					anchor : null,
					rawAnchor : null
				}
			];
		
		for(let url of [url1, url2, url3]){
			for(let anchor of anchors){
				assert.is($.urlAnchor(url+anchor.urlAnchor), anchor.anchor);
				assert.is($.urlAnchor(url+anchor.urlAnchor, true), anchor.rawAnchor);
			}
		}
	});



	test('$.fn.urlAnchor'+jqueryVersionString, (assert) => {
		let anchor = '#%C3%BC%C3%A4%C3%B6%C3%9C%C3%84%C3%96%C3%9F%2B-%3C%3E%3F%26%3D',
			$dummyWindow = $('<window></window>'),
			$foo = $('<img src="https://www.ifschleife.de/img/foo.png'+anchor+'" alt=""/>'),
			$bar = $('<a href="http://test.ifschleife.de/bar/'+anchor+'" target="_blank">bar</a>'),
			$boofar = $('<iframe src="http://test.ifschleife.de/bar/?query=1"></iframe>');

		$dummyWindow.get(0).location = {
			href : 'https://ifschleife.de/foobar',
			hash : anchor
		};
		
		for( let $element of [$dummyWindow, $foo, $bar] ){
			assert.is($element.urlAnchor(), '#+-<>?&=');
			assert.is($element.urlAnchor(true), '+-<>?&=');
		}
		assert.is($boofar.urlAnchor(), null);
	});
});