Benutzer:Schnark/js/syntaxhighlight.js/test.js

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
//<nowiki>
/*global mediaWiki, QUnit*/
(function ($, mw, base) {
"use strict";
QUnit.module('syntaxhighlight');

var Parser = base.Parser,
	wikiSyntax = base.wikiSyntax,
	jsSyntax = base.jsSyntax,
	cssSyntax = base.cssSyntax,
	luaSyntax = base.luaSyntax;

function normalize (input) {
	var normalized = [], i;
	for (i = 0; i < input.length; i++) {
		if (normalized.length && normalized[normalized.length - 1][1] === input[i][1]) {
			normalized[normalized.length - 1][0] += input[i][0];
		} else {
			normalized.push([input[i][0], input[i][1]]);
		}
	}
	return normalized;
}

function parserTest (parser, input, syntax, comment, assert) {
	assert.deepEqual(normalize(parser.parse(input)), syntax, comment);
}

function parserTestParen (parser, input, pos, syntax, comment, assert) {
	assert.deepEqual(normalize(parser.parseWithParen(input, pos)), syntax, comment);
}

QUnit.test('wikiSyntax', function (assert) {
	assert.expect(30);
	var parser = new Parser(wikiSyntax),
		simple = '== Überschrift ==\n* Liste mit [[Link]].',
		simpleParsed = [
			['== Überschrift ==\n', 'heading'],
			['*', 'listAndIndent'],
			[' Liste mit ', ''],
			['[[Link]]', 'wikilink'],
			['.\n', '']
		], simpleParsedParen1 = [
			['== Überschrift ==\n', 'heading'],
			['*', 'listAndIndent'],
			[' Liste mit ', ''],
			['[', 'matching-paren'],
			['[Link]', 'wikilink'],
			[']', 'matching-paren'],
			['.\n', '']
		], simpleParsedParen2 = [
			['== Überschrift ==\n', 'heading'],
			['*', 'listAndIndent'],
			[' Liste mit ', ''],
			['[', 'wikilink'],
			['[', 'matching-paren'],
			['Link', 'wikilink'],
			[']', 'matching-paren'],
			[']', 'wikilink'],
			['.\n', '']
		], braces = '{()[()]}', bracesParsed1 = [
			['{', 'matching-paren'],
			['()[()]', ''],
			['}', 'matching-paren'],
			['\n', '']
		], bracesParsed2 = [
			['{', ''],
			['()', 'matching-paren'],
			['[()]}\n', '']
		], bracesParsed3 = [
			['{()', ''],
			['[', 'matching-paren'],
			['()', ''],
			[']', 'matching-paren'],
			['}\n', '']
		], bracesParsed4 = [
			['{()[', ''],
			['()', 'matching-paren'],
			[']}\n', '']
		];

	parserTest(parser, '', [['\n', '']], 'Leerstring', assert);
	parserTest(parser, simple, simpleParsed, 'einfacher Text', assert);
	parserTestParen(parser, simple, 29, simpleParsed, 'einfacher Text - Klammerhervorhebung (1)', assert);
	parserTestParen(parser, simple, 30, simpleParsedParen1, 'einfacher Text - Klammerhervorhebung (2)', assert);
	parserTestParen(parser, simple, 31, simpleParsedParen1, 'einfacher Text - Klammerhervorhebung (3)', assert);
	parserTestParen(parser, simple, 32, simpleParsedParen2, 'einfacher Text - Klammerhervorhebung (4)', assert);
	parserTestParen(parser, simple, 33, simpleParsed, 'einfacher Text - Klammerhervorhebung (5)', assert);
	parserTestParen(parser, simple, 36, simpleParsedParen2, 'einfacher Text - Klammerhervorhebung (6)', assert);
	parserTestParen(parser, simple, 37, simpleParsedParen2, 'einfacher Text - Klammerhervorhebung (7)', assert);
	parserTestParen(parser, simple, 38, simpleParsedParen1, 'einfacher Text - Klammerhervorhebung (8)', assert);
	parserTestParen(parser, simple, 39, simpleParsed, 'einfacher Text - Klammerhervorhebung (9)', assert);
	parserTest(parser,
		'<noinclude>{{tausendfach verwendet}}</noinclude>' +
			'<includeonly>[[Englische Sprache|englisch]]{{#if:{{{1|}}}|&#32;{{lang|en|{{{1}}}}}}}</includeonly>' +
			'<noinclude>\n' +
		'{{lang/Doku-S|Sprache=englisch|ParamTyp1=trademark}}\n\n' +
		'[[Kategorie:Vorlage:Fremdsprachenunterstützung|EnS]]\n' +
		'</noinclude>', [
		['<noinclude>', '<noinclude>'],
		['{{tausendfach verwendet}}', 'template'],
		['</noinclude>', '<noinclude>'],
		['<includeonly>', '<includeonly>'],
		['[[Englische Sprache|englisch]]', 'wikilink'],
		['{{#if:', 'template'],
		['{{{1|}}}', 'parameter'],
		['|', 'template'],
		['&#32;', 'entity'],
		['{{lang|en|', 'template'],
		['{{{1}}}', 'parameter'],
		['}}}}', 'template'],
		['</includeonly>', '<includeonly>'],
		['<noinclude>\n', '<noinclude>'],
		['{{lang/Doku-S|Sprache=englisch|ParamTyp1=trademark}}', 'template'],
		['\n\n', '<noinclude>'],
		['[[Kategorie:Vorlage:Fremdsprachenunterstützung|EnS]]', 'wikilink'],
		['\n</noinclude>', '<noinclude>'],
		['\n', '']
	], 'Vorlage:enS', assert);
	parserTest(parser, 'http://www.example.org, //example.org/relative, https://www.example.org?foo=bar?, ' +
		'https://de.wikipedia.org/wiki/Foo_(Begriffsklärung), (https://de.wikipedia.org/wiki/Foo)', [
		['http://www.example.org', 'externalLink'],
		[', //example.org/relative, ', ''],
		['https://www.example.org?foo=bar', 'externalLink'],
		['?, ', ''],
		['https://de.wikipedia.org/wiki/Foo_(Begriffsklärung)', 'externalLink'],
		[', (', ''],
		['https://de.wikipedia.org/wiki/Foo', 'externalLink'],
		[')\n', '']
	], 'Freie externe Links', assert);
	parserTest(parser, '[http://www.example.org], [sic!], [//example.org/relative], ' +
		'[https://www.example.org?foo=bar?], [mailto:post@example.org Mail]', [
		['[http://www.example.org]', 'externalLink'],
		[', [sic!], ', ''],
		['[//example.org/relative]', 'externalLink'],
		[', ', ''],
		['[https://www.example.org?foo=bar?]', 'externalLink'],
		[', ', ''],
		['[mailto:post@example.org Mail]', 'externalLink'],
		['\n', '']
	], 'Externe Links', assert);
	parserTest(parser,
		'{{Infobox|\n' +
		' a = a |\n' +
		' b = b\n' +
		'}}\n' +
		' pre\n' +
		' pre', [
		['{{Infobox|\n a = a |\n b = b\n}}', 'template'],
		['\n', ''],
		[' pre\n pre\n', 'pre']
	], 'Führende Leerzeichen', assert);
	parserTest(parser,
		'<onlyinclude><includeonly><div class="BoxenVerschmelzen">\n' +
		'{{#switch:{{padleft:|1|{{{1|}}}}}|<|[={{{1}}}|#default={{#if:{{{1|}}}|{{{{{1}}}}}}}' +
			'{{#if:{{{2|}}}|{{{{{2}}}}}|<div style="display:none;">[[Vorlage:NaviBlock/Wartung/nur ein Element| ]]</div>}}' +
			'{{#if:{{{3|}}}|{{{{{3}}}}}}}...{{#if:{{{21|}}}|<div class="error">Zu viele Navigationsleisten</div>}}}}\n' +
		'</div></includeonly></onlyinclude>{{Dokumentation}}', [
		['<onlyinclude>', '<onlyinclude>'],
		['<includeonly>', '<includeonly>'],
		['<div class="BoxenVerschmelzen">\n', '<div>'],
		['{{#switch:{{padleft:|1|', 'template'],
		['{{{1|}}}', 'parameter'],
		['}}|<|[=', 'template'],
		['{{{1}}}', 'parameter'],
		['|#default={{#if:', 'template'],
		['{{{1|}}}', 'parameter'],
		['|{{', 'template'],
		['{{{1}}}', 'parameter'],
		['}}}}{{#if:', 'template'],
		['{{{2|}}}', 'parameter'],
		['|{{', 'template'],
		['{{{2}}}', 'parameter'],
		['}}|', 'template'],
		['<div style="display:none;">', '<div>'],
		['[[Vorlage:NaviBlock/Wartung/nur ein Element| ]]', 'wikilink'],
		['</div>', '<div>'],
		['}}{{#if:', 'template'],
		['{{{3|}}}', 'parameter'],
		['|{{', 'template'],
		['{{{3}}}', 'parameter'],
		['}}}}...{{#if:', 'template'],
		['{{{21|}}}', 'parameter'],
		['|', 'template'],
		['<div class="error">Zu viele Navigationsleisten</div>', '<div>'],
		['}}}}', 'template'],
		['\n</div>', '<div>'],
		['</includeonly>', '<includeonly>'],
		['</onlyinclude>', '<onlyinclude>'],
		['{{Dokumentation}}', 'template'],
		['\n', '']
	], 'Vorlage:NaviBlock', assert);
	parserTest(parser, '{{{{BASEPAGENAME}}/Sub}}, {{{!}}...{{!}}}', [
		['{{{{BASEPAGENAME}}/Sub}}', 'template'],
		[', {', ''],
		['{{!}}', 'template'],
		['...', ''],
		['{{!}}', 'template'],
		['}\n', '']
	], 'eklige Klammern', assert);
	parserTest(parser,
		'{| class="wikitable"\n' +
		'|+ Tabelle\n' +
		'! Eins\n' +
		'! Zwei !! Gleich?\n' +
		'|-\n' +
		'| a\n' +
		'| b || {{#ifeq:a|b||Nein!!}}\n' +
		'|}', [
		['{| class="wikitable"\n', 'table'],
		['|+', 'table-syntax'],
		[' Tabelle\n', 'table'],
		['!', 'table-syntax'],
		[' Eins\n', 'table'],
		['!', 'table-syntax'],
		[' Zwei ', 'table'],
		['!!', 'table-syntax'],
		[' Gleich?\n', 'table'],
		['|-', 'table-syntax'],
		['\n', 'table'],
		['|', 'table-syntax'],
		[' a\n', 'table'],
		['|', 'table-syntax'],
		[' b ', 'table'],
		['||', 'table-syntax'],
		[' ', 'table'],
		['{{#ifeq:a|b||Nein!!}}', 'template'],
		['\n|}', 'table'],
		['\n', '']
	], 'Tabelle', assert);
	parserTest(parser,
		'{|\n' +
		'!Tabelle\n' +
		'|}\n' +
		':{|\n' +
		'!Tabelle\n' +
		'|}\n' +
		' {|\n' +
		'!Tabelle\n' +
		' |}\n' +
		'keine {| !Tabelle |}', [
		['{|\n', 'table'],
		['!', 'table-syntax'],
		['Tabelle\n|}', 'table'],
		['\n', ''],
		[':{|\n', 'table'],
		['!', 'table-syntax'],
		['Tabelle\n|}', 'table'],
		['\n', ''],
		[' {|\n', 'table'],
		['!', 'table-syntax'],
		['Tabelle\n |}', 'table'],
		['\nkeine {| !Tabelle |}\n', '']
	], 'Tabellen und keine Tabellen', assert);
	parserTest(parser,
		'plain\n' +
		'plain\'\'italic\'\'plain\n' +
		'plain\'\'italic\'\'plain\'\'italic\'\'plain\n' +
		'plain\'\'\'bold\'\'\'plain\n' +
		'plain\'\'\'bold\'\'\'plain\'\'\'bold\'\'\'plain\n' +
		'plain\'\'italic\'\'plain\'\'\'bold\'\'\'plain\n' +
		'plain\'\'\'bold\'\'\'plain\'\'italic\'\'plain\n' +
		'plain\'\'italic\'\'\'bold-italic\'\'\'italic\'\'plain\n' +
		'plain\'\'\'bold\'\'bold-italic\'\'bold\'\'\'plain\n' +
		'plain\'\'\'\'\'bold-italic\'\'\'italic\'\'plain\n' +
		'plain\'\'\'\'\'bold-italic\'\'bold\'\'\'plain\n' +
		'plain\'\'italic\'\'\'bold-italic\'\'\'\'\'plain\n' +
		'plain\'\'\'bold\'\'bold-italic\'\'\'\'\'plain\n' +
		'plain l\'\'\'italic\'\'plain\n' +
		'plain l\'\'\'\'bold\'\'\' plain\n' +
		'\'\'\'\'\'\'bold-italic\'\'\'\'\'\'', [
		['plain\nplain', ''],
		['\'\'italic\'\'', 'italic'],
		['plain\nplain', ''],
		['\'\'italic\'\'', 'italic'],
		['plain', ''],
		['\'\'italic\'\'', 'italic'],
		['plain\nplain', ''],
		['\'\'\'bold\'\'\'', 'bold'],
		['plain\nplain', ''],
		['\'\'\'bold\'\'\'', 'bold'],
		['plain', ''],
		['\'\'\'bold\'\'\'', 'bold'],
		['plain\nplain', ''],
		['\'\'italic\'\'', 'italic'],
		['plain', ''],
		['\'\'\'bold\'\'\'', 'bold'],
		['plain\nplain', ''],
		['\'\'\'bold\'\'\'', 'bold'],
		['plain', ''],
		['\'\'italic\'\'', 'italic'],
		['plain\nplain', ''],
		['\'\'italic', 'italic'],
		['\'\'\'bold-italic\'\'\'', 'bold'],
		['italic\'\'', 'italic'],
		['plain\nplain', ''],
		['\'\'\'bold', 'bold'],
		['\'\'bold-italic\'\'', 'italic'],
		['bold\'\'\'', 'bold'],
		['plain\nplain', ''],
		['\'\'', 'italic'],
		['\'\'\'bold-italic\'\'\'', 'bold'],
		['italic\'\'', 'italic'],
		['plain\nplain', ''],
		['\'\'\'', 'bold'],
		['\'\'bold-italic\'\'', 'italic'],
		['bold\'\'\'', 'bold'],
		['plain\nplain', ''],
		['\'\'italic', 'italic'],
		['\'\'\'bold-italic\'\'\'', 'bold'],
		['\'\'', 'italic'],
		['plain\nplain', ''],
		['\'\'\'bold', 'bold'],
		['\'\'bold-italic\'\'', 'italic'],
		['\'\'\'', 'bold'],
		['plain\nplain l\'', ''],
		['\'\'italic\'\'', 'italic'],
		['plain\nplain l\'', ''],
		['\'\'\'bold\'\'\'', 'bold'],
		[' plain\n\'', ''],
		['\'\'', 'italic'],
		['\'\'\'bold-italic\'\'\'\'', 'bold'],
		['\'\'', 'italic'],
		['\n', '']
	], 'Fett und kursiv', assert);
	parserTest(parser,
		'http://example.org/foo=}}\n' +
		'[http://example.org/foo=}}]\n' +
		'{{Link|url=http://example.org/foo=}}}}\n' +
		'{{Link|url=[http://example.org/foo=}}]}}', [
		['http://example.org/foo=}}', 'externalLink'],
		['\n', ''],
		['[http://example.org/foo=}}]', 'externalLink'],
		['\n', ''],
		['{{Link|url=', 'template'],
		['http://example.org/foo=', 'externalLink'],
		['}}', 'template'],
		['}}\n', ''],
		['{{Link|url=', 'template'],
		['[http://example.org/foo=', 'externalLink'],
		['}}', 'template'],
		[']}}\n', '']
	], 'Geschweifte Klammern in Weblinks', assert);
	parserTestParen(parser, braces, 0, bracesParsed1, 'Verschiedene Klammern (1)', assert);
	parserTestParen(parser, braces, 1, bracesParsed1, 'Verschiedene Klammern (2)', assert);
	parserTestParen(parser, braces, 2, bracesParsed2, 'Verschiedene Klammern (3)', assert);
	parserTestParen(parser, braces, 3, bracesParsed2, 'Verschiedene Klammern (4)', assert);
	parserTestParen(parser, braces, 4, bracesParsed3, 'Verschiedene Klammern (5)', assert);
	parserTestParen(parser, braces, 5, bracesParsed4, 'Verschiedene Klammern (6)', assert);
	parserTestParen(parser, braces, 6, bracesParsed4, 'Verschiedene Klammern (7)', assert);
	parserTestParen(parser, braces, 7, bracesParsed3, 'Verschiedene Klammern (8)', assert);
	parserTestParen(parser, braces, 8, bracesParsed1, 'Verschiedene Klammern (9)', assert);
});

QUnit.test('jsSyntax', function (assert) {
	assert.expect(4);
	var parser = new Parser(jsSyntax), beaversLesson = '1 + 2 === ((3 + 7 + 10) * (1000 - 8)) / (900 + 90 + 2) - 17';
	parserTest(parser,
		'var foo = "bar".replace(/r/, function (param) { //bar -> baz\n' +
		'\treturn /*"r"*/ "z";\n' +
		'});', [
		['var', 'reserved'],
		[' foo = ', ''],
		['"bar"', 'string'],
		['.', ''],
		['replace', 'common'],
		['(', ''],
		['/r/', 'regexp'],
		[', ', ''],
		['function', 'reserved'],
		[' (param) { ', ''],
		['//bar -> baz', 'comment'],
		['\n\t', ''],
		['return', 'reserved'],
		[' ', ''],
		['/*"r"*/', 'comment'],
		[' ', ''],
		['"z"', 'string'],
		[';\n});\n', '']
	], 'einfacher Code', assert);
	parserTestParen(parser, beaversLesson, 10, [
		['1 + 2 === ', ''],
		['(', 'matching-paren'],
		['(3 + 7 + 10) * (1000 - 8)', ''],
		[')', 'matching-paren'],
		[' / (900 + 90 + 2) - 17\n', '']
	], 'Verschachtelte Klammern (1)', assert);
	parserTestParen(parser, beaversLesson, 12, [
		['1 + 2 === (', ''],
		['(', 'matching-paren'],
		['3 + 7 + 10', ''],
		[')', 'matching-paren'],
		[' * (1000 - 8)) / (900 + 90 + 2) - 17\n', '']
	], 'Verschachtelte Klammern (2)', assert);
	parserTestParen(parser, beaversLesson, 53, [
		['1 + 2 === ((3 + 7 + 10) * (1000 - 8)) / ', ''],
		['(', 'matching-paren'],
		['900 + 90 + 2', ''],
		[')', 'matching-paren'],
		[' - 17\n', '']
	], 'Verschachtelte Klammern (3)', assert);
});

QUnit.test('cssSyntax', function (assert) {
	assert.expect(1);
	var parser = new Parser(cssSyntax);
	parserTest(parser,
		'#foo .bar[baz$="baz"], foo:lang(en) {\n' +
		'\twidth: 100px;\n' +
		'\tvoice-family: "\\"}\\""; /*Tantek-Hack*/\n' +
		'\tvoice-family:inherit !important;\n' +
		'\twidth: 80px;' +
		'\n}', [
		['#foo', 'id'],
		[' ', ''],
		['.bar', 'class'],
		['[baz$=', 'attr'],
		['"baz"', 'string'],
		[']', 'attr'],
		[', foo', ''],
		[':lang(en)', 'pseudo'],
		[' ', ''],
		['{\n\twidth: ', 'decleration'],
		['100px', 'value'],
		[';\n\tvoice-family: ', 'decleration'],
		['"\\"}\\""', 'string'],
		['; ', 'decleration'],
		['/*Tantek-Hack*/', 'comment'],
		['\n\tvoice-family:', 'decleration'],
		['inherit ', 'value'],
		['!important', 'important'],
		[';\n\twidth: ', 'decleration'],
		['80px', 'value'],
		[';\n}', 'decleration'],
		['\n', '']
	], 'Tantek-Hack mit komischem Selektor', assert);
});

QUnit.test('luaSyntax', function (assert) {
	assert.expect(1);
	var parser = new Parser(luaSyntax);
	parserTest(parser,
		'--[[\n' +
		'Hallo Welt!\n' +
		'Code von Module:Hello\n' +
		']]\n' +
		'local p = {}\n' +
		'function p.hello(frame)\n' +
		'\tlocal name = frame.args[1]\n' +
		'\tif not name then\n' +
		'\t\tname = \'Welt\'\n' +
		'\tend\n' +
		'\treturn \'Hallo, \' .. name .. \'! Dies ist Lua!\'\n' +
		'end\n' +
		'return p', [
		['--[[\nHallo Welt!\nCode von Module:Hello\n]]', 'comment'],
		['\n', ''],
		['local', 'reserved'],
		[' p = ', ''],
		['{}', 'array'],
		['\n', ''],
		['function', 'reserved'],
		[' p.hello(frame)\n\t', ''],
		['local', 'reserved'],
		[' name = frame.args', ''],
		['[1]', 'array'],
		['\n\t', ''],
		['if', 'reserved'],
		[' ', ''],
		['not', 'reserved'],
		[' name ', ''],
		['then', 'reserved'],
		['\n\t\tname = ', ''],
		['\'Welt\'', 'string'],
		['\n\t', ''],
		['end', 'reserved'],
		['\n\t', ''],
		['return', 'reserved'],
		[' ', ''],
		['\'Hallo, \'', 'string'],
		[' .. name .. ', ''],
		['\'! Dies ist Lua!\'', 'string'],
		['\n', ''],
		['end', 'reserved'],
		['\n', ''],
		['return', 'reserved'],
		[' p\n', '']
	], 'einfacher Code (Modul:Hello)', assert);
});

})(jQuery, mediaWiki, mediaWiki.libs.qunit);
//</nowiki>