1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-26 11:37:20 +00:00
TiddlyWiki5/editions/test/tiddlers/tests/test-widget.js
Jeremy Ruston 6bd4127e88
Parameterised transclusions (#6666)
* Initial commit

Everything is draft.

* Fix test execution

* Fix and test missing target handling

* Use the ubertransclude widget for the wikitext transclusion syntax

* Changed transclude widget in binary parser to ubertransclude

* Add a test for custom action widgets

* Don't worry about ordered attributes

The changes in 0bffae2108 mean that we don't need to explicitly maintain the ordered attributes

* Remove need to explicitly clear widget mapping variable when invoking overridden widget

* Use ts- prefix for system slot names

* Add a definition for the value widget just so that it doesn't cause errors

Of course, it doesn't actually need to be a JS widget, it could be a wikitext widget...

* Add support for positional parameters

* Ubertransclusion positional parameters should be based on name, not position

* Add support for shortcut syntax for positional transclusion parameters

* Importvariables should skip parameters widgets

* Refactor transclude widget before uberfying it

* Refactor ubertransclude functionality into transclude widget

* Replace ubertransclude widget with transclude widget

* Add wikitext shortcut for new-style function definitions

* Allow brackets to be omitted for function definitions with no parameters

* Add pragma rule for parameters declarations

* Remove erroneous "tag" property

* Add support for accessing function parameters as name/value pairs

* Be as permissive as possible with parameter names

Previously restricted to upper and lower case, digits and dash and underscore

* Rewrite some tests to use the shortcut syntaxes

* Mustn't allow commas in parameter names

* Fix crash when transcluding an undefined variable

Thanks @pmario

See https://github.com/Jermolene/TiddlyWiki5/pull/6666#issuecomment-1114692359

* Unquoted parameters should not eat a succeeding comma

Fixes #6672

* Remove extraneous code

* Allow the let widget to create variables starting with $

* Fix addAttributeToParseTreeNode handling of ordered attributes

* Reuse attribute objects when executing custom widgets

* Fix importing of function definitions

* Fix parameter handling

* Introduce genesis widget for dynamically creating widgets

See the "RedefineLet" test for a contrived example of usage

* Change tiddler separator used in wikitext tests

Underscore looked ambiguous; I kept typing dashes by accident

* Cache parse trees when transcluding variables

* Fix bug with empty strings ignored in $tw.utils.stringifyList/parseStringArray

I will pull this out into a separate PR. Fixing it doesn't cause problems for the core but I imagine it might cause issues for 3rd party code.

* Fixes to enable the transclude widget itself to be overridden

There are two big changes here:

Replace the previous "ts-wrapper" mechanism, which we had been using to redefine custom widgets inside their definitions to prevent recursive calls. Now we've got the genesis widget we can instead control recursion through a new "$remappable" attribute that allows the custom widget mechanism to be skipped.

We also extend the slot widget to allow a depth to be specified; it then reaches up by the indicated number of transclusion widgets to find the one from which it should retrieve the slot value.

* Fix genesis widget example

* Use enlist:raw to preserve duplicates

* Don't create variables with value undefined for missing parameters

* Fix variable retrieval bug with test harness

* Improve recursion detection

While retaining backwards compatibility

* Experimental support for custom filter operators

Just as we can define custom widgets we can also define custom parameterised filter operators

* Add visible transclusions component and demo

Very useful to see transclusions explicitly

Makes a good demo of a super-complicated widget override.

* Genesis widget should pass raw attributes onto child widget...

...so that it can more efficiently handle refreshing itself.

* Use consistent parse tree node property for params

* Extend transclude widget to work with old-style macros and use it for the macrocall shortcut syntax

* Clarify that the recent changes allow functions to be invoked with the double bracket syntax

In other words, the transclude widget distinguishes between functions and macros and handles the parameters appropriately

* Make the macrocall widget delegate to the transclude widget

* Switch to using \procedure to define new-style macros, and \function for custom filter operator functions

I now need to update the OP!

* Fix visible transclusion example

* Remove obsolete code

Left over after refactoring

* Better backwards compatibility for legacy recursion marker

Fixes the problem with tag dropdowns @btheado

* Fix stringifying/parsing string arrays containing newlines

A very old bug.

Fixes the ActionListOpsWidget problem @btheado

* Transclude: replace paramNames/paramValues with more robust JSON payload

More details at https://github.com/Jermolene/TiddlyWiki5/pull/6666#issuecomment-1123719153

* Rename internal "unknown" filter operator so that users cannot invoke it

* Detect recursion by tracking widget tree depth

The old recursion marker approach was very slow, and didn't catch test cases like editions/test/tiddlers/tests/data/transclude/Recursion.tid

* Use \widget for custom widget definitions, and remove need for angle brackets

Need to do some refactoring of all those isFunctionDefinition/isProcedureDefinition/isWidgetDefinition flags into a single property

* Rename <$value> widget to <$fill>

* Require $$ for custom widgets, and that overridden JS widgets must exist

See https://github.com/Jermolene/TiddlyWiki5/pull/6666#issuecomment-1133637763

* Fix invocation of JS macros

* Experimental update of the parse-tree preview visualisation

An experiment to try out using the new JSON operators for rendering the JSON parse tree that we get back from the wikify widget.

As usual with these experiments, this one is going to require quite a lot more work to finish up:

* The formatting is via direct styles rather than classes
* The formatting for attributes and properties is not yet completed
* The same thing needs to also be done to the widget tree preview

* Procedures and widgets inherit whitespace trim setting from their definition

* Missed off 22e7ec2381

* Require period prefix for custom filter operator functions

To ensure that custom filter operators cannot clash with future core operators.

* Allow custom functions to be invoked as attributes

* WIP

* Remove unneeded test tiddler

* Make is[variable] and variables[] operators resilient to fake widgets

* Fix importvariables to work with setvariables as well as set (they are aliases)

* Add support for $:/tags/Global

* Remove accidental commit 6cc99fcbe3

* Add utility function for parsing macro parameter definitions

* Introduce true global variables

The basic idea is that if we don't find a variable `foo` then we fallback to retrieving the value from the tiddler `$:/global/foo`, if it exists.

This allows us to replace the usual importvariables-based mechanism for global definitions, avoiding cluttering up the variable namespace with every macro.

In order to permit subprocedures to be overridden, we also introduce a mechanism for conditional definitions: preceding the word definition|procedure|function|widget with a + causes the definition only to occur if the specified variable doesn't already exist. In the next commit we'll apply this mechanism to the tabs macro

* Convert the tabs macro into a global

So far it appears to be totally backwards compatible... In practice, I think maybe this and the conversion of the other macros should go into a separate subsequent PR.

* Change to `?` for conditional definitions

* Fix tabs global so it doesn't crash when viewed directly

* Test showing how to un-override a core widget

* Cleaning up after f636349007

* Minor cleanups

* Clean up unknown filter

* Introduce function operator for calling functions

Can invoke any functions, not just those start with a period. And can pass zero parameters (in contrast when invoked as a custom filter operator there's no way to omit the first parameter).

* Use underscores for new system fields for global variable tiddlers

For consistency with `_canonical_uri`; unlike many system fields, the behaviour of these fields is baked into the core JS code.

* Refactor $parameters widget

The objective is to add a $depth attribute so that it is possible to reach up to retrieve the parameters of ancestor transclusions. However, doing so requires changing the encoding of parameter names so that it is not possible for a user parameter to clash with an attribute like $depth. So now we have to double up dollars on any attribute names seen by the parameters widget, just like with the transclude widget itself.

* Fix refreshing of global variables

Global variables access within attributes will automatically trigger a refresh if the attribute text changes, but that wasn't happening for transclusions.

* Remove support for $:/tags/Global

It is not needed now that we have true global variables

* Typo from f513b403fe

* Make slot fill data available to transclusions

Allows transcluded content to dynamically process <$fill> widgets within the calling transclusion

* Mark docs as v5.3.0

* Simplify metaparameters implementation

* Fix typo

* Adjust naming of transclusion metaparameter

* Fix up handling of slot/fill for custom widgets

Previously we were wrapping the body in an implicit `<$fill $name="ts-body">` widget

* Add format:json operator

I've been finding this useful for debugging, and it kind of goes with the JSON operators

* Docs: JSON operators and tweaks to genesis widget

* Docs: format:json

Also tweak to the behaviour of format:json if the input string is not valid JSON

* Fix #6721

* Revert "Fix #6721"

This reverts commit b216579255 which was committed to the wrong branch

* Fix new selection tracker to return relative coordinates

* Make use of type attribute consistent

* Docs: Transclude widget

* Simplify the fill widget

We can rely on the default processing in the base class

* Slot widget: be more defensive about negative depth values

* Parameters widget: Be defensive about negative depths

* Protect against excessively recursive functions

* FIx transcluding of functions

This first implementation concatenates the results of the filter (with no separator) and then wikifies the result.

The test in this commit is quite interesting...

* Tweak semantics of JSON operators to match #6932

This allows us to later bring in the optimisations without breaking backwards compatibility.

* Revert obsolete changes to boot.js

* Fix inadvertent whitespace change

* Remove tests related to obsolete changes to boot.js

Should have been part of 2f494ba152

* Revert changes to parse tree preview

This implementation requires #6666

* Add test to show that global widgets need not use the _parameters field

* Disable overriding core widgets in safe mode

* Coding style tweak

* More comments

* Fix caching of parse variables/macros/procedures

* Transcluded functions should operate on the entire store

* Refactor filter recursion detection to avoid an unneeded wrapper function

* Fix error in 25312b3e32

* WIP

* Revert "WIP"

This reverts commit 8654dfc679.

* When transcluding functions, pass an empty item list to the filter, and just return the first item

* Rejig genesis widget to be easier to use

* Parameters widget: protect against negative $depth

* Docs updates

* Docs updates

* Tweak comments

* Add custom view template body for globals, and a new sidebar tab under "more"

And also a custom view template title that greys out the $:/global/ part of the title

* Update function operator to return the input list if the function is missing

* Remove negation from function operator

This implementation was not useful.

* Tests and docs for function operator

* Docs tweaks

* Improve indentation

See https://github.com/Jermolene/TiddlyWiki5/pull/6666#discussion_r967655251

* Missing tests for parameters widget

* Fix visible transclude

* Docs update

* Docs typo

* Huge Documentation Update

Not quite finished, but definitely on the home stretch

* Slight optimisation to user defined widgets

* Remove implementation of $:/globals/

Performance with this implementation is inherently poor because of the need to perform a wiki lookup for each child widget created.

* Docs clarification

* Docs update

* Some widget.js cleanups

* Remove support for conditional definitions

It was introduced for use cases associated with the global mechanism that was dropped in e3d13696c8

* Docs updates

* Revert change to setwidget docs

* Docs update

* Docs updates

* Clarify/simplify some tests

* More docs updates

* Fix doc file locations

* Docs updates

* Revert modified date of docs that have only had minor tweaks

* Docs typo

https://github.com/Jermolene/TiddlyWiki5/pull/6666#discussion_r990811220

Thanks @btheado

* Transcluding functions: fix missing parameters passed as undefined

Thanks @btheado – see https://github.com/Jermolene/TiddlyWiki5/pull/6666#issuecomment-1276187372

* Parameter parenthesis should be mandatory in function/procedure/widget definitions

See https://github.com/Jermolene/TiddlyWiki5/pull/6666#issuecomment-1280404387

* Attempt to build this branch with CI

* Add release note etc for 5.3.0

* Temporary new release banner for v5.3.0

* New New Release Banner

* New test for undefined parameters

* Adjust modified times of docs tiddlers to make them easier to find

* Update release note

* Add parenthesis to the visible transclusion definition

Parenthesis were made mandatory in 5194b24108

Fixes #6998

* Fix macrocall refresh issue

It turns out that this.transcludeTitle is always truthy, even if we are transcluding a variable

Fixes #7001

* Filter run prefixes should use widget.makeFakeWidgetWithVariables

* Docs typo

Thanks @twMat

* Docs: clarify function operator invocation

See discussion at https://github.com/Jermolene/TiddlyWiki5/issues/6991#issuecomment-1301703599

* Docs: Update \define pragma to cover named ends

* Docs: move tiddlers to correct directory

* Add support for named end markers for procedures, functions and widgets

* Docs note about nested macro definitions

* Rename test

* Fix detection of empty transclusions

See https://talk.tiddlywiki.org/t/exploring-default-tiddler-links-hackability-in-v5-3-0/5745/25?u=jeremyruston

* New test missed off a45349cc99

* Refactor wikified function tests

* Refactor function invocation

* Introduce new widget helper function to evaluate variables.Functions are evaluated as parameterised filter strings, macros as text with textual substitution of parameters and variables, and procedures and widgets as plain text
* Refactor the function operator and unknown operator to use the new helper
* Use the new helper to evaluate variables within filter strings, thus fixing a bug whereby functions called in such a way were being returned as plain text instead of being evaluated
* Refactor the transclude widget to use the new helper
* Update tests

* Fix positional parameters in widget.evaluateVariable()

This should clear up the remaining anomalies in #7009, let me know how you get on @btheado

* Remove 5.2.8 release note

* Fix nonstandard initialisation code for fill/parameter/slot widgets

* Update modification times of doc tiddlers

So that they are at the top of the recent tab

* Update 5.3.0 release note

* Remove custom CI step for this branch

* Restore standard sitetitle
2023-04-19 11:55:25 +01:00

826 lines
39 KiB
JavaScript
Executable File

/*\
title: test-widget.js
type: application/javascript
tags: [[$:/tags/test-spec]]
Tests the wikitext rendering pipeline end-to-end. We also need tests that individually test parsers, rendertreenodes etc., but this gets us started.
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
describe("Widget module", function() {
var widget = require("$:/core/modules/widgets/widget.js");
function createWidgetNode(parseTreeNode,wiki) {
return new widget.widget(parseTreeNode,{
wiki: wiki,
document: $tw.fakeDocument
});
}
function parseText(text,wiki,options) {
var parser = wiki.parseText("text/vnd.tiddlywiki",text,options);
return parser ? {type: "widget", children: parser.tree} : undefined;
}
function renderWidgetNode(widgetNode) {
$tw.fakeDocument.setSequenceNumber(0);
var wrapper = $tw.fakeDocument.createElement("div");
widgetNode.render(wrapper,null);
// console.log(require("util").inspect(wrapper,{depth: 8}));
return wrapper;
}
function refreshWidgetNode(widgetNode,wrapper,changes) {
var changedTiddlers = {};
if(changes) {
$tw.utils.each(changes,function(title) {
changedTiddlers[title] = true;
});
}
widgetNode.refresh(changedTiddlers,wrapper,null);
// console.log(require("util").inspect(wrapper,{depth: 8}));
}
it("should deal with text nodes and HTML elements", function() {
var wiki = new $tw.Wiki();
// Test parse tree
var parseTreeNode = {type: "widget", children: [
{type: "text", text: "A text node"},
{type: "element", tag: "div", attributes: {
"class": {type: "string", value: "myClass"},
"title": {type: "string", value: "myTitle"}
}, children: [
{type: "text", text: " and the content of a DIV"},
{type: "element", tag: "div", children: [
{type: "text", text: " and an inner DIV"},
]},
{type: "text", text: " and back in the outer DIV"}
]}
]};
// Construct the widget node
var widgetNode = createWidgetNode(parseTreeNode,wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("A text node<div class=\"myClass\" title=\"myTitle\"> and the content of a DIV<div> and an inner DIV</div> and back in the outer DIV</div>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[1].sequenceNumber).toBe(2);
expect(wrapper.children[1].children[0].sequenceNumber).toBe(3);
expect(wrapper.children[1].children[1].sequenceNumber).toBe(4);
expect(wrapper.children[1].children[1].children[0].sequenceNumber).toBe(5);
expect(wrapper.children[1].children[2].sequenceNumber).toBe(6);
});
it("should deal with transclude widgets and indirect attributes", function() {
var wiki = new $tw.Wiki();
// Add a tiddler
wiki.addTiddlers([
{title: "TiddlerOne", text: "the quick brown fox"}
]);
// Test parse tree
var parseTreeNode = {type: "widget", children: [
{type: "text", text: "A text node"},
{type: "element", tag: "div", attributes: {
"class": {type: "string", value: "myClass"},
"title": {type: "indirect", textReference: "TiddlerOne"}
}, children: [
{type: "text", text: " and the content of a DIV"},
{type: "element", tag: "div", children: [
{type: "text", text: " and an inner DIV"},
]},
{type: "text", text: " and back in the outer DIV"},
{type: "transclude", attributes: {
"tiddler": {type: "string", value: "TiddlerOne"}
}}
]},
{type: "transclude", attributes: {
"tiddler": {type: "string", value: "TiddlerOne"}
}}
]};
// Construct the widget node
var widgetNode = createWidgetNode(parseTreeNode,wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("A text node<div class=\"myClass\" title=\"the quick brown fox\"> and the content of a DIV<div> and an inner DIV</div> and back in the outer DIVthe quick brown fox</div>the quick brown fox");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[1].sequenceNumber).toBe(2);
expect(wrapper.children[1].children[0].sequenceNumber).toBe(3);
expect(wrapper.children[1].children[1].sequenceNumber).toBe(4);
expect(wrapper.children[1].children[1].children[0].sequenceNumber).toBe(5);
expect(wrapper.children[1].children[2].sequenceNumber).toBe(6);
expect(wrapper.children[1].children[3].sequenceNumber).toBe(7);
expect(wrapper.children[2].sequenceNumber).toBe(8);
// Change the transcluded tiddler
wiki.addTiddler({title: "TiddlerOne", text: "jumps over the lazy dog"});
// Refresh
refreshWidgetNode(widgetNode,wrapper,["TiddlerOne"]);
// Test the refreshing
expect(wrapper.innerHTML).toBe("A text node<div class=\"myClass\" title=\"jumps over the lazy dog\"> and the content of a DIV<div> and an inner DIV</div> and back in the outer DIVjumps over the lazy dog</div>jumps over the lazy dog");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[1].sequenceNumber).toBe(2);
expect(wrapper.children[1].children[0].sequenceNumber).toBe(3);
expect(wrapper.children[1].children[1].sequenceNumber).toBe(4);
expect(wrapper.children[1].children[1].children[0].sequenceNumber).toBe(5);
expect(wrapper.children[1].children[2].sequenceNumber).toBe(6);
expect(wrapper.children[1].children[3].sequenceNumber).toBe(9);
expect(wrapper.children[2].sequenceNumber).toBe(10);
});
it("should detect recursion of the transclude macro", function() {
var wiki = new $tw.Wiki();
// Add a tiddler
wiki.addTiddlers([
{title: "TiddlerOne", text: "<$transclude tiddler='TiddlerTwo'/>"},
{title: "TiddlerTwo", text: "<$transclude tiddler='TiddlerOne'/>"}
]);
// Test parse tree
var parseTreeNode = {type: "widget", children: [
{type: "transclude", attributes: {
"tiddler": {type: "string", value: "TiddlerOne"}
}}
]};
// Construct the widget node
var widgetNode = createWidgetNode(parseTreeNode,wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<span class=\"tc-error\">Recursive transclusion error in transclude widget</span>");
});
it("should deal with SVG elements", function() {
var wiki = new $tw.Wiki();
// Construct the widget node
var text = "<svg class=\"tv-image-new-button\" viewBox=\"83 81 50 50\" width=\"22pt\" height=\"22pt\"><path d=\"M 101.25 112.5 L 101.25 127.5 C 101.25 127.5 101.25 127.5 101.25 127.5 L 101.25 127.5 C 101.25 129.156855 102.593146 130.5 104.25 130.5 L 111.75 130.5 C 113.406854 130.5 114.75 129.156854 114.75 127.5 L 114.75 112.5 L 129.75 112.5 C 131.406854 112.5 132.75 111.156854 132.75 109.5 L 132.75 102 C 132.75 100.343146 131.406854 99 129.75 99 L 114.75 99 L 114.75 84 C 114.75 82.343146 113.406854 81 111.75 81 L 104.25 81 C 104.25 81 104.25 81 104.25 81 C 102.593146 81 101.25 82.343146 101.25 84 L 101.25 99 L 86.25 99 C 86.25 99 86.25 99 86.25 99 C 84.593146 99 83.25 100.343146 83.25 102 L 83.25 109.5 C 83.25 109.5 83.25 109.5 83.25 109.5 L 83.25 109.5 C 83.25 111.156855 84.593146 112.5 86.25 112.5 Z\"/></svg>\n";
var widgetNode = createWidgetNode(parseText(text,wiki,{parseAsInline:true}),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<svg class=\"tv-image-new-button\" height=\"22pt\" viewBox=\"83 81 50 50\" width=\"22pt\"><path d=\"M 101.25 112.5 L 101.25 127.5 C 101.25 127.5 101.25 127.5 101.25 127.5 L 101.25 127.5 C 101.25 129.156855 102.593146 130.5 104.25 130.5 L 111.75 130.5 C 113.406854 130.5 114.75 129.156854 114.75 127.5 L 114.75 112.5 L 129.75 112.5 C 131.406854 112.5 132.75 111.156854 132.75 109.5 L 132.75 102 C 132.75 100.343146 131.406854 99 129.75 99 L 114.75 99 L 114.75 84 C 114.75 82.343146 113.406854 81 111.75 81 L 104.25 81 C 104.25 81 104.25 81 104.25 81 C 102.593146 81 101.25 82.343146 101.25 84 L 101.25 99 L 86.25 99 C 86.25 99 86.25 99 86.25 99 C 84.593146 99 83.25 100.343146 83.25 102 L 83.25 109.5 C 83.25 109.5 83.25 109.5 83.25 109.5 L 83.25 109.5 C 83.25 111.156855 84.593146 112.5 86.25 112.5 Z\"></path></svg>\n");
expect(wrapper.firstChild.namespaceURI).toBe("http://www.w3.org/2000/svg");
});
it("should parse and render transclusions", function() {
var wiki = new $tw.Wiki();
// Add a tiddler
wiki.addTiddlers([
{title: "TiddlerOne", text: "Jolly Old World"},
{title: "TiddlerTwo", text: "<$transclude tiddler={{TiddlerThree}}/>"},
{title: "TiddlerThree", text: "TiddlerOne"}
]);
// Construct the widget node
var text = "My <$transclude tiddler='TiddlerTwo'/> is Jolly"
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<p>My Jolly Old World is Jolly</p>");
});
it("should render the view widget", function() {
var wiki = new $tw.Wiki();
// Add a tiddler
wiki.addTiddlers([
{title: "TiddlerOne", text: "Jolly Old World"}
]);
// Construct the widget node
var text = "<$view tiddler='TiddlerOne'/>";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<p>Jolly Old World</p>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].children[0].sequenceNumber).toBe(2);
// Change the transcluded tiddler
wiki.addTiddler({title: "TiddlerOne", text: "World-wide Jelly"});
// Refresh
refreshWidgetNode(widgetNode,wrapper,["TiddlerOne"]);
// Test the refreshing
expect(wrapper.innerHTML).toBe("<p>World-wide Jelly</p>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].children[0].sequenceNumber).toBe(3);
});
it("should deal with the set widget", function() {
var wiki = new $tw.Wiki();
// Add some tiddlers
wiki.addTiddlers([
{title: "TiddlerOne", text: "Jolly Old World"},
{title: "TiddlerTwo", text: "<$transclude tiddler={{TiddlerThree}}/>"},
{title: "TiddlerThree", text: "TiddlerOne"},
{title: "TiddlerFour", text: "TiddlerTwo"}
]);
// Construct the widget node
var text = "My <$set name='currentTiddler' value={{TiddlerFour}}><$transclude tiddler={{!!title}}/></$set> is Jolly"
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<p>My Jolly Old World is Jolly</p>");
// Change the transcluded tiddler
wiki.addTiddler({title: "TiddlerFour", text: "TiddlerOne"});
// Refresh
refreshWidgetNode(widgetNode,wrapper,["TiddlerFour"]);
// Test the refreshing
expect(wrapper.innerHTML).toBe("<p>My Jolly Old World is Jolly</p>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].children[0].sequenceNumber).toBe(2);
expect(wrapper.children[0].children[1].sequenceNumber).toBe(5);
expect(wrapper.children[0].children[2].sequenceNumber).toBe(4);
});
it("should deal with the let widget", function() {
var wiki = new $tw.Wiki();
wiki.addTiddlers([
{title: "TiddlerOne", text: "lookup"},
{title: "TiddlerTwo", lookup: "value", newlookup: "value", wrong: "wrong"},
{title: "TiddlerThree", text: "wrong", value: "Happy Result", wrong: "ALL WRONG!!"}
]);
var text="\\define macro() TiddlerThree\n"+
"\\define currentTiddler() TiddlerOne\n"+
"<$let "+
"field={{!!text}} "+
"currentTiddler='TiddlerTwo' "+
"field={{{ [all[current]get<field>] }}} "+
"currentTiddler=<<macro>>>"+
"<$transclude field=<<field>>/></$let>";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
var wrapper = renderWidgetNode(widgetNode);
expect(wrapper.innerHTML).toBe("<p>Happy Result</p>");
// This is important. $Let needs to be aware enough not to let its
// own variables interfere with its ability to recognise no change.
// Doesn't matter that nothing has changed, we just need to make sure
// it recognizes that that its outward facing variables are unchanged
// EVEN IF some intermediate variables did change, there's no need to
// refresh.
wiki.addTiddler({title: "TiddlerOne", text: "newlookup"});
expect(widgetNode.refresh({})).toBe(false);
// But if we make a change that might result in different outfacing
// variables, then it should refresh
wiki.addTiddler({title: "TiddlerOne", text: "badlookup"});
expect(widgetNode.refresh({})).toBe(true);
});
it("should deal with attributes specified as macro invocations", function() {
var wiki = new $tw.Wiki();
// Construct the widget node
var text = "\\define myMacro(one:\"paramOne\",two,three:\"paramTwo\")\nMy something $one$, $two$ or other $three$\n\\end\n<div class=<<myMacro 'something' three:'thing'>>>Content</div>";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<p><div class=\"My something something, or other thing\">Content</div></p>");
});
it("should deal with built-in macros", function() {
var wiki = new $tw.Wiki();
// Add some tiddlers
wiki.addTiddlers([
{title: "TiddlerOne", text: "Jolly Old World", type: "text/vnd.tiddlywiki"}
]);
// Construct the widget node
var text = "\\define makelink(text,type)\n<a href=<<makedatauri text:\"$text$\" type:\"$type$\">>>My linky link</a>\n\\end\n\n<$macrocall $name=\"makelink\" text={{TiddlerOne}} type={{TiddlerOne!!type}}/>";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<p><a href=\"data:text/vnd.tiddlywiki,Jolly%20Old%20World\">My linky link</a></p>");
});
/* This test reproduces issue #4693. */
it("should render the entity widget", function() {
var wiki = new $tw.Wiki();
// Construct the widget node
var text = "\n\n<$entity entity='&nbsp;' />\n\n<$entity entity='&#x2713;' />\n";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe(" ✓");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].nodeType).toBe(wrapper.children[0].TEXT_NODE);
expect(wrapper.children[1].sequenceNumber).toBe(2);
expect(wrapper.children[1].nodeType).toBe(wrapper.children[1].TEXT_NODE);
});
it("should deal with the list widget", function() {
var wiki = new $tw.Wiki();
// Add some tiddlers
wiki.addTiddlers([
{title: "TiddlerOne", text: "Jolly Old World"},
{title: "TiddlerTwo", text: "Worldly Old Jelly"},
{title: "TiddlerThree", text: "Golly Gosh"},
{title: "TiddlerFour", text: "Lemon Squash"}
]);
// Construct the widget node
var text = "<$list><$view field='title'/></$list>";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<p>TiddlerFourTiddlerOneTiddlerThreeTiddlerTwo</p>");
// Add another tiddler
wiki.addTiddler({title: "TiddlerFive", text: "Jalapeno Peppers"});
// Refresh
refreshWidgetNode(widgetNode,wrapper,["TiddlerFive"]);
// Test the refreshing
expect(wrapper.innerHTML).toBe("<p>TiddlerFiveTiddlerFourTiddlerOneTiddlerThreeTiddlerTwo</p>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].children[0].sequenceNumber).toBe(6);
expect(wrapper.children[0].children[1].sequenceNumber).toBe(2);
expect(wrapper.children[0].children[2].sequenceNumber).toBe(3);
expect(wrapper.children[0].children[3].sequenceNumber).toBe(4);
expect(wrapper.children[0].children[4].sequenceNumber).toBe(5);
// Remove a tiddler
wiki.deleteTiddler("TiddlerThree");
// Refresh
refreshWidgetNode(widgetNode,wrapper,["TiddlerThree"]);
// Test the refreshing
expect(wrapper.innerHTML).toBe("<p>TiddlerFiveTiddlerFourTiddlerOneTiddlerTwo</p>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].children[0].sequenceNumber).toBe(6);
expect(wrapper.children[0].children[1].sequenceNumber).toBe(2);
expect(wrapper.children[0].children[2].sequenceNumber).toBe(3);
expect(wrapper.children[0].children[3].sequenceNumber).toBe(5);
// Add it back a tiddler
wiki.addTiddler({title: "TiddlerThree", text: "Something"});
// Refresh
refreshWidgetNode(widgetNode,wrapper,["TiddlerThree"]);
// Test the refreshing
expect(wrapper.innerHTML).toBe("<p>TiddlerFiveTiddlerFourTiddlerOneTiddlerThreeTiddlerTwo</p>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].children[0].sequenceNumber).toBe(6);
expect(wrapper.children[0].children[1].sequenceNumber).toBe(2);
expect(wrapper.children[0].children[2].sequenceNumber).toBe(3);
expect(wrapper.children[0].children[3].sequenceNumber).toBe(7);
expect(wrapper.children[0].children[4].sequenceNumber).toBe(5);
});
it("should deal with the list widget using a counter variable", function() {
var wiki = new $tw.Wiki();
// Add some tiddlers
wiki.addTiddlers([
{title: "TiddlerOne", text: "Jolly Old World"},
{title: "TiddlerTwo", text: "Worldly Old Jelly"},
{title: "TiddlerThree", text: "Golly Gosh"},
{title: "TiddlerFour", text: "Lemon Squash"}
]);
// Construct the widget node
var text = "<$list counter='index'><$view field='text'/><$text text=<<index>>/><$text text=<<index-first>>/><$text text=<<index-last>>/></$list>";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<p>Lemon Squash1yesnoJolly Old World2nonoGolly Gosh3nonoWorldly Old Jelly4noyes</p>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].children[0].sequenceNumber).toBe(2);
expect(wrapper.children[0].children[1].sequenceNumber).toBe(3);
expect(wrapper.children[0].children[2].sequenceNumber).toBe(4);
expect(wrapper.children[0].children[3].sequenceNumber).toBe(5);
expect(wrapper.children[0].children[4].sequenceNumber).toBe(6);
expect(wrapper.children[0].children[5].sequenceNumber).toBe(7);
expect(wrapper.children[0].children[6].sequenceNumber).toBe(8);
expect(wrapper.children[0].children[7].sequenceNumber).toBe(9);
expect(wrapper.children[0].children[8].sequenceNumber).toBe(10);
expect(wrapper.children[0].children[9].sequenceNumber).toBe(11);
expect(wrapper.children[0].children[10].sequenceNumber).toBe(12);
expect(wrapper.children[0].children[11].sequenceNumber).toBe(13);
expect(wrapper.children[0].children[12].sequenceNumber).toBe(14);
expect(wrapper.children[0].children[13].sequenceNumber).toBe(15);
expect(wrapper.children[0].children[14].sequenceNumber).toBe(16);
expect(wrapper.children[0].children[15].sequenceNumber).toBe(17);
// Add another tiddler
wiki.addTiddler({title: "TiddlerFive", text: "Jalapeno Peppers"});
// Refresh
refreshWidgetNode(widgetNode,wrapper,["TiddlerFive"]);
// Test the refreshing
expect(wrapper.innerHTML).toBe("<p>Jalapeno Peppers1yesnoLemon Squash2nonoJolly Old World3nonoGolly Gosh4nonoWorldly Old Jelly5noyes</p>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].children[0].sequenceNumber).toBe(18);
expect(wrapper.children[0].children[1].sequenceNumber).toBe(19);
expect(wrapper.children[0].children[2].sequenceNumber).toBe(20);
expect(wrapper.children[0].children[3].sequenceNumber).toBe(21);
expect(wrapper.children[0].children[4].sequenceNumber).toBe(22);
expect(wrapper.children[0].children[5].sequenceNumber).toBe(23);
expect(wrapper.children[0].children[6].sequenceNumber).toBe(24);
expect(wrapper.children[0].children[7].sequenceNumber).toBe(25);
expect(wrapper.children[0].children[8].sequenceNumber).toBe(26);
expect(wrapper.children[0].children[9].sequenceNumber).toBe(27);
expect(wrapper.children[0].children[10].sequenceNumber).toBe(28);
expect(wrapper.children[0].children[11].sequenceNumber).toBe(29);
expect(wrapper.children[0].children[12].sequenceNumber).toBe(30);
expect(wrapper.children[0].children[13].sequenceNumber).toBe(31);
expect(wrapper.children[0].children[14].sequenceNumber).toBe(32);
expect(wrapper.children[0].children[15].sequenceNumber).toBe(33);
expect(wrapper.children[0].children[16].sequenceNumber).toBe(34);
expect(wrapper.children[0].children[17].sequenceNumber).toBe(35);
expect(wrapper.children[0].children[18].sequenceNumber).toBe(36);
expect(wrapper.children[0].children[19].sequenceNumber).toBe(37);
// Remove a tiddler
wiki.deleteTiddler("TiddlerThree");
// Refresh
refreshWidgetNode(widgetNode,wrapper,["TiddlerThree"]);
// Test the refreshing
expect(wrapper.innerHTML).toBe("<p>Jalapeno Peppers1yesnoLemon Squash2nonoJolly Old World3nonoWorldly Old Jelly4noyes</p>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].children[0].sequenceNumber).toBe(18);
expect(wrapper.children[0].children[1].sequenceNumber).toBe(19);
expect(wrapper.children[0].children[2].sequenceNumber).toBe(20);
expect(wrapper.children[0].children[3].sequenceNumber).toBe(21);
expect(wrapper.children[0].children[4].sequenceNumber).toBe(22);
expect(wrapper.children[0].children[5].sequenceNumber).toBe(23);
expect(wrapper.children[0].children[6].sequenceNumber).toBe(24);
expect(wrapper.children[0].children[7].sequenceNumber).toBe(25);
expect(wrapper.children[0].children[8].sequenceNumber).toBe(26);
expect(wrapper.children[0].children[9].sequenceNumber).toBe(27);
expect(wrapper.children[0].children[10].sequenceNumber).toBe(28);
expect(wrapper.children[0].children[11].sequenceNumber).toBe(29);
expect(wrapper.children[0].children[12].sequenceNumber).toBe(38);
expect(wrapper.children[0].children[13].sequenceNumber).toBe(39);
expect(wrapper.children[0].children[14].sequenceNumber).toBe(40);
expect(wrapper.children[0].children[15].sequenceNumber).toBe(41);
// Add it back a tiddler
wiki.addTiddler({title: "TiddlerThree", text: "Something"});
// Refresh
refreshWidgetNode(widgetNode,wrapper,["TiddlerThree"]);
// Test the refreshing
expect(wrapper.innerHTML).toBe("<p>Jalapeno Peppers1yesnoLemon Squash2nonoJolly Old World3nonoSomething4nonoWorldly Old Jelly5noyes</p>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].children[0].sequenceNumber).toBe(18);
expect(wrapper.children[0].children[1].sequenceNumber).toBe(19);
expect(wrapper.children[0].children[2].sequenceNumber).toBe(20);
expect(wrapper.children[0].children[3].sequenceNumber).toBe(21);
expect(wrapper.children[0].children[4].sequenceNumber).toBe(22);
expect(wrapper.children[0].children[5].sequenceNumber).toBe(23);
expect(wrapper.children[0].children[6].sequenceNumber).toBe(24);
expect(wrapper.children[0].children[7].sequenceNumber).toBe(25);
expect(wrapper.children[0].children[8].sequenceNumber).toBe(26);
expect(wrapper.children[0].children[9].sequenceNumber).toBe(27);
expect(wrapper.children[0].children[10].sequenceNumber).toBe(28);
expect(wrapper.children[0].children[11].sequenceNumber).toBe(29);
expect(wrapper.children[0].children[12].sequenceNumber).toBe(42);
expect(wrapper.children[0].children[13].sequenceNumber).toBe(43);
expect(wrapper.children[0].children[14].sequenceNumber).toBe(44);
expect(wrapper.children[0].children[15].sequenceNumber).toBe(45);
//Remove last tiddler
wiki.deleteTiddler("TiddlerTwo");
//Refresh
refreshWidgetNode(widgetNode,wrapper,["TiddlerTwo"]);
//Test the refreshing
expect(wrapper.innerHTML).toBe("<p>Jalapeno Peppers1yesnoLemon Squash2nonoJolly Old World3nonoSomething4noyes</p>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].children[0].sequenceNumber).toBe(18);
expect(wrapper.children[0].children[1].sequenceNumber).toBe(19);
expect(wrapper.children[0].children[2].sequenceNumber).toBe(20);
expect(wrapper.children[0].children[3].sequenceNumber).toBe(21);
expect(wrapper.children[0].children[4].sequenceNumber).toBe(22);
expect(wrapper.children[0].children[5].sequenceNumber).toBe(23);
expect(wrapper.children[0].children[6].sequenceNumber).toBe(24);
expect(wrapper.children[0].children[7].sequenceNumber).toBe(25);
expect(wrapper.children[0].children[8].sequenceNumber).toBe(26);
expect(wrapper.children[0].children[9].sequenceNumber).toBe(27);
expect(wrapper.children[0].children[10].sequenceNumber).toBe(28);
expect(wrapper.children[0].children[11].sequenceNumber).toBe(29);
expect(wrapper.children[0].children[12].sequenceNumber).toBe(50);
expect(wrapper.children[0].children[13].sequenceNumber).toBe(51);
expect(wrapper.children[0].children[14].sequenceNumber).toBe(52);
expect(wrapper.children[0].children[15].sequenceNumber).toBe(53);
});
it("should deal with the list widget followed by other widgets", function() {
var wiki = new $tw.Wiki();
// Add some tiddlers
wiki.addTiddlers([
{title: "TiddlerOne", text: "Jolly Old World"},
{title: "TiddlerTwo", text: "Worldly Old Jelly"},
{title: "TiddlerThree", text: "Golly Gosh"},
{title: "TiddlerFour", text: "Lemon Squash"}
]);
// Construct the widget node
var text = "<$list><$view field='title'/></$list>Something";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<p>TiddlerFourTiddlerOneTiddlerThreeTiddlerTwoSomething</p>");
// Check the next siblings of each of the list elements
var listWidget = widgetNode.children[0].children[0];
// Add another tiddler
wiki.addTiddler({title: "TiddlerFive", text: "Jalapeno Peppers"});
// Refresh
refreshWidgetNode(widgetNode,wrapper,["TiddlerFive"]);
// Test the refreshing
expect(wrapper.innerHTML).toBe("<p>TiddlerFiveTiddlerFourTiddlerOneTiddlerThreeTiddlerTwoSomething</p>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].children[0].sequenceNumber).toBe(7);
expect(wrapper.children[0].children[1].sequenceNumber).toBe(2);
expect(wrapper.children[0].children[2].sequenceNumber).toBe(3);
expect(wrapper.children[0].children[3].sequenceNumber).toBe(4);
expect(wrapper.children[0].children[4].sequenceNumber).toBe(5);
// Remove a tiddler
wiki.deleteTiddler("TiddlerThree");
// Refresh
refreshWidgetNode(widgetNode,wrapper,["TiddlerThree"]);
// Test the refreshing
expect(wrapper.innerHTML).toBe("<p>TiddlerFiveTiddlerFourTiddlerOneTiddlerTwoSomething</p>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].children[0].sequenceNumber).toBe(7);
expect(wrapper.children[0].children[1].sequenceNumber).toBe(2);
expect(wrapper.children[0].children[2].sequenceNumber).toBe(3);
expect(wrapper.children[0].children[3].sequenceNumber).toBe(5);
// Add it back a tiddler
wiki.addTiddler({title: "TiddlerThree", text: "Something"});
// Refresh
refreshWidgetNode(widgetNode,wrapper,["TiddlerThree"]);
// Test the refreshing
expect(wrapper.innerHTML).toBe("<p>TiddlerFiveTiddlerFourTiddlerOneTiddlerThreeTiddlerTwoSomething</p>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].children[0].sequenceNumber).toBe(7);
expect(wrapper.children[0].children[1].sequenceNumber).toBe(2);
expect(wrapper.children[0].children[2].sequenceNumber).toBe(3);
expect(wrapper.children[0].children[3].sequenceNumber).toBe(8);
expect(wrapper.children[0].children[4].sequenceNumber).toBe(5);
// Add another a tiddler to the end of the list
wiki.addTiddler({title: "YetAnotherTiddler", text: "Something"});
// Refresh
refreshWidgetNode(widgetNode,wrapper,["YetAnotherTiddler"]);
// Test the refreshing
expect(wrapper.innerHTML).toBe("<p>TiddlerFiveTiddlerFourTiddlerOneTiddlerThreeTiddlerTwoYetAnotherTiddlerSomething</p>");
// Test the sequence numbers in the DOM
expect(wrapper.sequenceNumber).toBe(0);
expect(wrapper.children[0].sequenceNumber).toBe(1);
expect(wrapper.children[0].children[0].sequenceNumber).toBe(7);
expect(wrapper.children[0].children[1].sequenceNumber).toBe(2);
expect(wrapper.children[0].children[2].sequenceNumber).toBe(3);
expect(wrapper.children[0].children[3].sequenceNumber).toBe(8);
expect(wrapper.children[0].children[4].sequenceNumber).toBe(5);
});
it("should deal with the list widget and external templates", function() {
var wiki = new $tw.Wiki();
// Add some tiddlers
wiki.addTiddlers([
{title: "$:/myTemplate", text: "(<$view field='title'/>)"},
{title: "TiddlerOne", text: "Jolly Old World"},
{title: "TiddlerTwo", text: "Worldly Old Jelly"},
{title: "TiddlerThree", text: "Golly Gosh"},
{title: "TiddlerFour", text: "Lemon Squash"}
]);
// Construct the widget node
var text = "<$list template='$:/myTemplate'></$list>";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
//console.log(require("util").inspect(widgetNode,{depth:8,colors:true}));
// Test the rendering
expect(wrapper.innerHTML).toBe("<p>(TiddlerFour)(TiddlerOne)(TiddlerThree)(TiddlerTwo)</p>");
});
it("should deal with the list widget and empty lists", function() {
var wiki = new $tw.Wiki();
// Construct the widget node
var text = "<$list emptyMessage='nothing'><$view field='title'/></$list>";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<p>nothing</p>");
});
it("should refresh lists that become empty", function() {
var wiki = new $tw.Wiki();
// Add some tiddlers
wiki.addTiddlers([
{title: "TiddlerOne", text: "Jolly Old World"},
{title: "TiddlerTwo", text: "Worldly Old Jelly"},
{title: "TiddlerThree", text: "Golly Gosh"},
{title: "TiddlerFour", text: "Lemon Squash"}
]);
// Construct the widget node
var text = "<$list emptyMessage='nothing'><$view field='title'/></$list>";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<p>TiddlerFourTiddlerOneTiddlerThreeTiddlerTwo</p>");
// Get rid of the tiddlers
wiki.deleteTiddler("TiddlerOne");
wiki.deleteTiddler("TiddlerTwo");
wiki.deleteTiddler("TiddlerThree");
wiki.deleteTiddler("TiddlerFour");
// Refresh
refreshWidgetNode(widgetNode,wrapper,["TiddlerOne","TiddlerTwo","TiddlerThree","TiddlerFour"]);
// Test the refreshing
expect(wrapper.innerHTML).toBe("<p>nothing</p>");
});
/**This test confirms that imported set variables properly refresh
* if they use transclusion for their value. This relates to PR #4108.
*/
it("should refresh imported <$set> widgets", function() {
var wiki = new $tw.Wiki();
// Add some tiddlers
wiki.addTiddlers([
{title: "Raw", text: "Initial value"},
{title: "Macro", text: "<$set name='test' value={{Raw}}>\n\ndummy text</$set>"},
{title: "Caller", text: text}
]);
var text = "\\import Macro\n<<test>>";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<p>Initial value</p>");
wiki.addTiddler({title: "Raw", text: "New value"});
// Refresh
refreshWidgetNode(widgetNode,wrapper,["Raw"]);
expect(wrapper.innerHTML).toBe("<p>New value</p>");
});
it("should support mixed setWidgets and macros when importing", function() {
var wiki = new $tw.Wiki();
// Add some tiddlers
wiki.addTiddlers([
{title: "A", text: "\\define A() Aval"},
{title: "B", text: "<$set name='B' value='Bval'>\n\ndummy text</$set>"},
{title: "C", text: "\\define C() Cval"}
]);
var text = "\\import A B C\n<<A>> <<B>> <<C>>";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<p>Aval Bval Cval</p>");
});
it("should skip parameters widgets when importing", function() {
var wiki = new $tw.Wiki();
// Add some tiddlers
wiki.addTiddlers([
{title: "B", text: "<$parameters bee=nothing><$set name='B' value='Bval'>\n\ndummy text</$set></$parameters>"},
]);
var text = "\\import B\n<<B>>";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<p>Bval</p>");
});
it("can have more than one macroDef variable imported", function() {
var wiki = new $tw.Wiki();
wiki.addTiddlers([
{title: "ABC", text: "<$set name=A value=A>\n\n<$set name=B value=B>\n\n<$set name=C value=C>\n\ndummy text</$set></$set></$set>"},
{title: "D", text: "\\define D() D"}]);
// A and B shouldn't chew up C just cause it's a macroDef
var text = "\\import ABC D\n<<A>> <<B>> <<C>> <<D>>";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<p>A B C D</p>");
});
it("import doesn't hold onto dead variables", function() {
var wiki = new $tw.Wiki();
wiki.addTiddlers([
{title: "ABC", text: "\\define A() A\n\\define B() B\n<$set name=C value=C>\n\n</$set>"},
{title: "DE", text: "\\define D() D\n\\define E() E"}]);
var text = "\\import ABC DE\ncontent";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
renderWidgetNode(widgetNode);
var childNode = widgetNode;
while (childNode.children.length > 0) {
childNode = childNode.children[0];
}
// First make sure A and B imported
expect(childNode.getVariable("A")).toBe("A");
expect(childNode.getVariable("B")).toBe("B");
expect(childNode.getVariable("C")).toBe("C");
expect(childNode.getVariable("D")).toBe("D");
expect(childNode.getVariable("E")).toBe("E");
// Then change A and remove B
wiki.addTiddler({title: "ABC", text: "\\define A() A2\n<$set name=C value=C2>\n\n</$set>"});
wiki.addTiddler({title: "DE", text: "\\define D() D2"});
widgetNode.refresh({"ABC": {modified: true}, "DE": {modified: true}});
var childNode = widgetNode;
while (childNode.children.length > 0) {
childNode = childNode.children[0];
}
// Make sure \import recognized changes and deletions
expect(childNode.getVariable("A")).toBe("A2");
expect(childNode.getVariable("B")).toBe(undefined);
expect(childNode.getVariable("C")).toBe("C2");
expect(childNode.getVariable("D")).toBe("D2");
expect(childNode.getVariable("E")).toBe(undefined);
});
/** Special case. <$importvariables> has different handling if
* it doesn't end up importing any variables. Make sure it
* doesn't forget its childrenNodes.
*/
it("should work when import widget imports nothing", function() {
var wiki = new $tw.Wiki();
var text = "\\import [prefix[XXX]]\nDon't forget me.";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<p>Don't forget me.</p>");
});
/** Special case. \import should parse correctly, even if it's
* the only line in the tiddler. Technically doesn't cause a
* visual difference, but may affect plugins if it doesn't.
*/
it("should work when import pragma is standalone", function() {
var wiki = new $tw.Wiki();
var text = "\\import [prefix[XXX]]";
var parseTreeNode = parseText(text,wiki);
// Test the resulting parse tree node, since there is no
// rendering which may expose a problem.
expect(parseTreeNode.children[0].attributes.filter.value).toBe('[prefix[XXX]]');
});
/** This test reproduces issue #4504.
*
* The importvariable widget was creating redundant copies into
* itself of variables in widgets higher up in the tree. Normally,
* this caused no errors, but it would mess up qualify-macros.
* They would find multiple instances of the same transclusion
* variable if a transclusion occured higher up in the widget tree
* than an importvariables AND that importvariables was importing
* at least ONE variable.
*/
it("adding imported variables doesn't change qualifyers", function() {
var wiki = new $tw.Wiki();
function wikiparse(text) {
var tree = parseText(text,wiki);
var widgetNode = createWidgetNode(tree,wiki);
var wrapper = renderWidgetNode(widgetNode);
return wrapper.innerHTML;
};
wiki.addTiddlers([
{title: "body", text: "\\import A\n<<qualify this>>"},
{title: "A", text: "\\define unused() ignored"}
]);
// This transclude wraps "body", which has an
// importvariable widget in it.
var withA = wikiparse("{{body}}");
wiki.addTiddler({title: "A", text: ""});
var withoutA = wikiparse("{{body}}");
// Importing two different version of "A" shouldn't cause
// the <<qualify>> widget to spit out something different.
expect(withA).toBe(withoutA);
});
});
})();