JavaScript Macro Does Not Process Escape Characters Correctly

67 views
Skip to first unread message

Mohammad

unread,
Mar 5, 2019, 12:13:03 AM3/5/19
to tiddl...@googlegroups.com
If in a browser console (FF and Chrome, press F12 and see the console) enter the below JavaScript code, it will work

a= "One"
b="Two"
s="\n"
y=a + s +b

then you get
 
a="One"
"One"
b="Two"
"Two"
s="\n"
"
"
a+s+b
"One
Two"


Note the two lines text in the output. That means the "\n" treated as a line-break or newline.

Now, do the same in Tiddlywiki.

Create a JS macro as below:
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

exports.name = "jsmac";

exports.params = [
    {name: "a"},
    {name: "b"},
    {name: "s"} 
];

/*
Run the macro
*/
exports.run = function(a, b, s) {

    return a + s + b;
}
})();


I have attached jsmac tiddler above to this post


Test:

Drag and drop the below tiddler into an empty wiki. save and reload

Then create a test tiddler with the below contents

<$macrocall $name=jsmac
a="one"
b="two"
s=" \n "
/>

What you get is:

one \t two

That means Twiddlywiki JavaScript macro does not work as expected. It treats "\n" literally! It does not interpret it as a newline

What is wrong here?



Note:
If you like to test on Tiddlywiki.com

Drag and drop the attached two tiddler to Tiddlywiki.com
Save and reload (a copy will saved on your system)

--Mohammad
macro_jsmac.tid
Test JMAC.tid

@TiddlyTweeter

unread,
Mar 5, 2019, 1:35:09 PM3/5/19
to TiddlyWiki
Mohammad

I so wish I could help you solve this problem. Its a barrier to decent "replace" in TW.

This issue may need Jeremy's input?

One thing IS clear to me. TW protects itself from eating itself. This may be part of the issue?

Best wishes
Josiah

Mohammad Rahmani

unread,
Mar 5, 2019, 1:49:59 PM3/5/19
to tiddl...@googlegroups.com
Hi Josiah,
I hope Jeremy gives his comments on this issue!
I could not pass any of escape sequence (\e, \f, \n, \r, \t) in Tiddlywiki to a JS macro!

--Mohammad


Jeremy Ruston

unread,
Mar 5, 2019, 5:13:26 PM3/5/19
to tiddl...@googlegroups.com
Hi Mohammad

I think that the problem here is likely to be the way that HTML processes whitespace: by default, all the different types of whitespace are merged together as spaces (even newlines!).

If you want to see the full string, try either putting it in a pre tag or a textarea. For example:

<pre><code>
<$macrocall $name=jsmac
a="one"
b="two"
s=" \n "
/>
</code></pre>

Best wishes

Jeremy.


On 5 Mar 2019, at 05:13, Mohammad <mohammad...@gmail.com> wrote:

If in a browser console (FF and Chrome, press F12 and see the console) enter the below JavaScript code it will work

a= "One"
b="Two"
s="\n"
y=a + s +b

then you get
 
a="One"
"One"
b="Two"
"Two"
s="\n"
"
"
a+s+b
"One
Two"


Note the two line text in the output. That means the "\n" treated as a line-break or newline.
What is wrong here?



Note:
If you like to test on Tiddlywiki.com

Drag and drop the attached two tiddler to Tiddlywiki.com
Save and reload (a copy will saved on your system)

--Mohammad

--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/bf1fa660-48b5-474f-b2f5-253b193b36a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<macro_jsmac.tid><Test JMAC.tid>

Reply all
Reply to author
Forward
0 new messages