mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-28 04:19:56 +00:00
06a0d21734
Fixes #82. The old behaviour was to parse the content of a widget or html element in block mode if the opening tag was followed by a line break. The new behaviour requires two line breaks. This makes it possible to include linebreaks more liberally within wikitext, although care must still be taken to only use double line breaks when the block mode behaviour is desired. The code change here is very simple, just a single line change in html.js. Most of the other changes are to convert various single line breaks into double line breaks.
147 lines
7.6 KiB
JavaScript
147 lines
7.6 KiB
JavaScript
/*\
|
|
title: test-wikitext-parser.js
|
|
type: application/javascript
|
|
tags: [[$:/tags/test-spec]]
|
|
|
|
Tests for wikitext parser
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true, browser: true */
|
|
/*global $tw: false */
|
|
"use strict";
|
|
|
|
describe("WikiText parser tests", function() {
|
|
|
|
// Create a wiki
|
|
var wiki = new $tw.Wiki();
|
|
|
|
// Define a parsing shortcut
|
|
var parse = function(text) {
|
|
return wiki.parseText("text/vnd.tiddlywiki",text).tree;
|
|
};
|
|
|
|
it("should parse tags", function() {
|
|
expect(parse("<br>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'element', tag : 'br', isBlock : false, attributes : { }, start : 0, end : 4 } ] } ]
|
|
|
|
);
|
|
expect(parse("</br>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'text', text : '</br>' } ] } ]
|
|
|
|
);
|
|
expect(parse("<div>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'element', tag : 'div', isBlock : false, attributes : { }, children : [ ], start : 0, end : 5 } ] } ]
|
|
|
|
);
|
|
expect(parse("<div/>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'element', tag : 'div', isSelfClosing : true, isBlock : false, attributes : { }, start : 0, end : 6 } ] } ]
|
|
|
|
);
|
|
expect(parse("<div></div>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'element', tag : 'div', isBlock : false, attributes : { }, children : [ ], start : 0, end : 5 } ] } ]
|
|
|
|
);
|
|
expect(parse("<div>some text</div>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'element', tag : 'div', isBlock : false, attributes : { }, children : [ { type : 'text', text : 'some text' } ], start : 0, end : 5 } ] } ]
|
|
|
|
);
|
|
expect(parse("<div attribute>some text</div>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'element', tag : 'div', isBlock : false, attributes : { attribute : { type : 'string', value : 'true', start : 4, end : 14, name: 'attribute' } }, children : [ { type : 'text', text : 'some text' } ], start : 0, end : 15 } ] } ]
|
|
|
|
);
|
|
expect(parse("<div attribute='value'>some text</div>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'element', tag : 'div', isBlock : false, attributes : { attribute : { type : 'string', name: 'attribute', value : 'value', start: 4, end: 22 } }, children : [ { type : 'text', text : 'some text' } ], start: 0, end: 23 } ] } ]
|
|
|
|
);
|
|
expect(parse("<div attribute={{TiddlerTitle}}>some text</div>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'element', tag : 'div', isBlock : false, attributes : { attribute : { type : 'indirect', name: 'attribute', textReference : 'TiddlerTitle', start : 4, end : 31 } }, children : [ { type : 'text', text : 'some text' } ], start : 0, end : 32 } ] } ]
|
|
|
|
);
|
|
expect(parse("<$reveal state='$:/temp/search' type='nomatch' text=''>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'reveal', start : 0, attributes : { state : { start : 8, name : 'state', type : 'string', value : '$:/temp/search', end : 31 }, type : { start : 31, name : 'type', type : 'string', value : 'nomatch', end : 46 }, text : { start : 46, name : 'text', type : 'string', value : '', end : 54 } }, end : 55, isBlock : false, children : [ ] } ] } ]
|
|
|
|
);
|
|
expect(parse("<div attribute={{TiddlerTitle!!field}}>some text</div>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'element', tag : 'div', isBlock : false, attributes : { attribute : { type : 'indirect', name : 'attribute', textReference : 'TiddlerTitle!!field', start : 4, end : 38 } }, children : [ { type : 'text', text : 'some text' } ], start : 0, end : 39 } ] } ]
|
|
|
|
);
|
|
expect(parse("<div attribute={{Tiddler Title!!field}}>some text</div>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'element', tag : 'div', isBlock : false, attributes : { attribute : { type : 'indirect', name : 'attribute', textReference : 'Tiddler Title!!field', start : 4, end : 39 } }, children : [ { type : 'text', text : 'some text' } ], start : 0, end : 40 } ] } ]
|
|
|
|
);
|
|
expect(parse("<div attribute={{TiddlerTitle!!field}}>\n\nsome text</div>")).toEqual(
|
|
|
|
[ { type : 'element', start : 0, attributes : { attribute : { start : 4, name : 'attribute', type : 'indirect', textReference : 'TiddlerTitle!!field', end : 38 } }, tag : 'div', end : 39, isBlock : true, children : [ { type : 'element', tag : 'p', children : [ { type : 'text', text : 'some text' } ] } ] } ]
|
|
|
|
);
|
|
expect(parse("<div><div attribute={{TiddlerTitle!!field}}>\n\nsome text</div></div>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'element', start : 0, attributes : { }, tag : 'div', end : 5, isBlock : false, children : [ { type : 'element', start : 5, attributes : { attribute : { start : 9, name : 'attribute', type : 'indirect', textReference : 'TiddlerTitle!!field', end : 43 } }, tag : 'div', end : 44, isBlock : true, children : [ { type : 'element', tag : 'p', children : [ { type : 'text', text : 'some text' } ] } ] } ] } ] } ]
|
|
|
|
);
|
|
expect(parse("<div><div attribute={{TiddlerTitle!!field}}>\n\n!some heading</div></div>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'element', start : 0, attributes : { }, tag : 'div', end : 5, isBlock : false, children : [ { type : 'element', start : 5, attributes : { attribute : { start : 9, name : 'attribute', type : 'indirect', textReference : 'TiddlerTitle!!field', end : 43 } }, tag : 'div', end : 44, isBlock : true, children : [ { type : 'element', tag : 'h1', attributes : { class : { type : 'string', value : '' } }, children : [ { type : 'text', text : 'some heading</div></div>' } ] } ] } ] } ] } ]
|
|
|
|
);
|
|
expect(parse("<div><div attribute={{TiddlerTitle!!field}}>\n!some heading</div></div>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'element', start : 0, attributes : { }, tag : 'div', end : 5, isBlock : false, children : [ { type : 'element', start : 5, attributes : { attribute : { start : 9, name : 'attribute', type : 'indirect', textReference : 'TiddlerTitle!!field', end : 43 } }, tag : 'div', end : 44, isBlock : false, children : [ { type : 'text', text : '\n!some heading' } ] } ] } ] } ]
|
|
|
|
);
|
|
});
|
|
|
|
it("should parse macro definitions", function() {
|
|
expect(parse("\\define myMacro()\nnothing\n\\end\n")).toEqual(
|
|
|
|
[ { type : 'set', name : 'myMacro', params : [ ], text : 'nothing', attributes : { name : { type : 'string', value : 'myMacro' }, value : { type : 'string', value : 'nothing' } }, children : [ ] } ]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it("should parse macro calls", function() {
|
|
expect(parse("<<john>><<paul>><<george>><<ringo>>")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'macrocall', name : 'john', params : [ ] }, { type : 'macrocall', name : 'paul', params : [ ] }, { type : 'macrocall', name : 'george', params : [ ] }, { type : 'macrocall', name : 'ringo', params : [ ] } ] } ]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it("should parse horizontal rules", function() {
|
|
expect(parse("---Not a rule\n\n----\n\nBetween\n\n---")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'entity', entity : '—' }, { type : 'text', text : 'Not a rule' } ] }, { type : 'element', tag : 'hr' }, { type : 'element', tag : 'p', children : [ { type : 'text', text : 'Between' } ] }, { type : 'element', tag : 'hr' } ]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it("should parse hard linebreak areas", function() {
|
|
expect(parse("\"\"\"Something\nin the\nway she moves\n\"\"\"\n\n")).toEqual(
|
|
|
|
[ { type : 'element', tag : 'p', children : [ { type : 'text', text : 'Something' }, { type : 'element', tag : 'br' }, { type : 'text', text : 'in the' }, { type : 'element', tag : 'br' }, { type : 'text', text : 'way she moves' }, { type : 'element', tag : 'br' } ] } ]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})();
|