Skip to content

Improve readMacroParams to prevent errors #178

@YakovL

Description

@YakovL

The problem

Currently String.prototype.readMacroParams helper [1] calls .parseParams method with noNames == true. It was probably coded this way as the calculated [2] params argument passed to a macro handler is meant to contain no param names. However, this approach causes errors. Here's a number of tests that you can copy to a TW and see the results:

|<<tiddler [[a {{DDn{b}}} c]]>>|{{{<<tiddler }}}<html><code>[[a {{DDn{b}}} c</code></html>{{{]]>>}}}|
|<<tiddler name:[[a {{DDn{b}}} c]]>>|{{{<<tiddler na}}}<html><code>me:[[a {{DDn{b}}} c</code></html>{{{]]>>}}}|
|<<tiddler name:[[{{DDn{b}}} c]]>>|{{{<<tiddler na}}}<html><code>me:[[{{DDn{b}}} c</code></html>{{{]]>>}}}|
|<<tiddler name:[[a {{DDn{b}}}]]>>|{{{<<tiddler na}}}<html><code>me:[[a {{DDn{b}}}</code></html>{{{]]>>}}}|
|<<tiddler name:"a {{DDn{b}}}">>|{{{<<tiddler na}}}<html><code>me:"a {{DDn{b}}}</code></html>{{{">>}}}|

In short, the 1st and the 3d lines don't cause any error while the other three cause the

Unable to evaluate {{DDn{b}}: SyntaxError: missing ; before statement

error. What's the reason?

The thing is, when .parseParams is called with noNames == true, it "thinks" that in

name:[[a {{DDn{b}}}]]

the first argument matches unQuoted [3] name:[[a and then the next match starts from {{ resulting in dblBrace matching {{DDn{b}}.

The solution

Now, after some analysis I've come to a simple solution: as this separation was not intended, we can simply call

var p = this.parseParams("_default",null,!notAllowEval);

in String.prototype.readMacroParams (instead of doing it like in the line 172) and then, because only p[t].value is pushed to results (name is omitted), the function works nicely (as expected, backward compatible) with "correctly used" macro params, but also doesn't cause any of the mentioned errors.

I've made a plugin that overwrites String.prototype.readMacroParams with the one containing the mentioned change, and it works nicely for me. But this looks like a core problem, so my proposal is to embed this into the core.

Best regards,
Yakov.

[1] https://github.com/TiddlyWiki/tiddlywiki/blob/master/js/Strings.js#L170
[2] https://github.com/TiddlyWiki/tiddlywiki/blob/master/js/Macros.js#L20
[3] https://github.com/TiddlyWiki/tiddlywiki/blob/master/js/Strings.js#L128

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions