CerealGuy

  • Posts: 343
add_js + </ul>
« on April 27th, 2014, 08:50 PM »
I'm having a comic problem while trying to add js code with an "</ul>" in it (its connected to InviteMod but broke it a bit down to make it easier to understand).

Example1:
Code: [Select]
add_js(&quot;alert(\&quot;&lt;ul&gt;\&quot;);&quot;);
This one works as it should, we have an alert with the message "<ul>"


Example2:
Code: [Select]
add_js(&quot;alert(\&quot;&lt;/ul&gt;\&quot;);&quot;);
This one works not, even if its nearly the same as in Example1.
I dont really know whats going on there but it looks like something deletes stuff after
the </ul> tag.

Code: (html output) [Select]
&lt;script&gt;
...
});alert(&quot;&lt;/ul&gt;
&lt;div id=&quot;junk&quot;&gt;
&lt;strong&gt;Templates&lt;/strong&gt; (5):
...
&lt;/script&gt;
Some more things that i discovered:
- It works when the user is not logged in
- Tested it on multiple hosts, on one it works but on the others not
- Its the same when i add it manually to $context['footer_js'] (perhaps some parse functions doing stuff with it...)

Workarounds:
This one is not really nice but works.
Code: [Select]
print &quot;&lt;script&gt;alert(\&quot;&lt;/ul&gt;\&quot;);&lt;/script&gt;&quot;;

And this one is what made me a bit confused. A simple escape of the / did it. I have no idea why, perhaps someone can explain it to me :D
Code: [Select]
add_js(&quot;alert(\&quot;&lt;\/ul&gt;\&quot;);&quot;);

EDIT: omg... looks like there are some troubles displaying this :D

Nao

  • Dadman with a boy
  • Posts: 16,061
Re: add_js + </ul>
« Reply #1, on April 27th, 2014, 11:16 PM »
Yeah, sorry about the mess with code tags, I'm currently rewriting the whole preparse_code thing to try and fix it, but it's huge.
I'm not exactly sure when this bug was introduced... And it gets worse if you start quick-editing a message with the 'script' html code inside code tags. It might actually break the page... :-/

As for your bug, seriously... I have no idea what's happening to you, either. It'd be interesting to see the actual HTML generated at the bottom of your pages, and possibly get the contents of your error log in the JavaScript console...
Re: add_js + </ul>
« Reply #2, on April 27th, 2014, 11:49 PM »
Okay, should be working now...

Code: [Select]
<script> add_js('hello! </ul>');

Code: [Select]
<script> add_js('hello! </ul>');

Still not sure about your add_js problem, of course.
Re: add_js + </ul>
« Reply #3, on April 27th, 2014, 11:57 PM »
Just one thing...

In JavaScript, a multiline string needs to be escaped at the end of each line (I.e. add a backslash and press Enter.)

Maybe that's your problem in a nutshell..?

CerealGuy

  • Posts: 343
Re: add_js + </ul>
« Reply #4, on April 28th, 2014, 12:53 AM »Last edited on April 28th, 2014, 01:00 AM
Code: [Select]
...
add_js('$( document ).ready(function() {$( ".now" ).before( "<ul><li><a href=\"index.php?action=invite\">'.$ps_string.'</a></li></ul>" );});');
Here the backslash in the end ul tag is unescaped like all other end tags. This "bug" or whatever just appears when i try to have an </ul> tag in javascript.
JS Console tells me:
SyntaxError: unterminated string literal

and the script output from wedge:
Code: [Select]


<script>
we_notifs = 0;
eves = {
1: ["change", function (e) { this.form.submit(); }],
2: ["click", function (e) { return reqWin(this); }]
};
we_pms = 0;
we_script = "http://localhost:8888/wedge_old/index.php";
we_assets = "http://localhost:8888/wedge_old/assets";
we_sessid = "5cae2da9ffc31150b2fc24628844183b";
we_sessvar = "f7abe7450";
we_groups = ["1|-1|Administrator", "2|-1|Global Moderator", "3|-1|Moderator", "9|-1|Partner", "4|0|Newbie", "5|50|Jr. Member", "6|100|Full Member", "7|250|Sr. Member", "8|500|Hero Member"];
we_lists = [];
$(".thome").mime({
16: ["lk", "cx/16", "re/0", "mo/0", "de"],
        [...]
8: ["lk", "cx/8", "re/0", "mo/0", "de"],
7: ["cx/6", "re/6", "mo/6", "de", "bl"]
}, {
lk: ["Like", "Like this post", "", "like_button", "return oThought.like(%1%)"],
                [...]
bl: ["Posterity?", "Show this thought under my name in topics, for posterity.", "", "like_button", "return oThought.personal(%1%)"]
});$( document ).ready(function() {$( ".now" ).before( "<ul><li><a href=\"index.php?action=invite\">'.$ps_string.'</a></li></ul>
       

I'm totally unsure about this, probably its not a bug just my stupidness or i understand something not ^^.
I have it in one line and tested it with other shorter codes. Even a var test = "</ul>" fails with the same kind of error.

I totally have to check if its like that in the raw response...

But otherwise its not really important, i found a workaround. Just thought that someone maybeknows the problem :D

Nao

  • Dadman with a boy
  • Posts: 16,061
Re: add_js + </ul>
« Reply #5, on April 28th, 2014, 06:47 AM »
Note that you're mixing quotes with double quotes and end up with php code in your HTML.....
Re: add_js + </ul>
« Reply #6, on April 28th, 2014, 10:47 PM »
Bump?

Anyway, this would be the thing you want:

Code: [Select]
$(function() { $('.now').before('<ul><li><a href="index.php?action=invite">' + ps_string + '</a></li></ul>\
 the rest of your string'); });
(Assuming that you store $ps_string into a JS variable... Which you don't really want to do.)

A quick attempt at putting it into an add_js call...

Code: [Select]
add_js('$(function() { $(\'.now\').before(\'<ul><li><a href="index.php?action=invite">' . $ps_string . '</a></li></ul>\
 the rest of your string\'); });');

For fun, $(), when you pass it a function, is just a jQuery shortcut to $(document).ready()...

Please tell me if it's working for you.

CerealGuy

  • Posts: 343
Re: add_js + </ul>
« Reply #7, on April 28th, 2014, 11:55 PM »Last edited on April 29th, 2014, 12:16 AM
Thanks for your suggestion but its really the same problem.
Code: [Select]
add_js('$(function() { $(\'.now\').before(\'<ul><li><a href="index.php?action=invite">' . $ps_string . '</a></li></ul>\'); });');
Throws that js error
Code: [Select]
SyntaxError: unterminated string literal index.php:555
ReferenceError: eves is not defined script-a-english-641436.js:19

while
Code: [Select]
add_js('$(function() { $(\'.now\').before(\'<ul><li><a href="index.php?action=invite">' . $ps_string . '</a></li><\/ul>\'); });');
works. But that <\/ul> still confuses me. Why is that one working and without the "escaped backslah" not? And why that "fault" only occurs with the ul tag? :D It's really weird...

EDIT: Added a small test plugin. Try to enable it and look if the message appears how it should.

 test_plugin.zip - 0.72 kB, downloaded 47 times.


Nao

  • Dadman with a boy
  • Posts: 16,061
Re: add_js + </ul>
« Reply #8, on April 29th, 2014, 12:36 AM »
I'll look into it tomorrow.
Posted: April 29th, 2014, 12:33 AM

BTW, have you tried removing the </a></li></ul> part altogether..? In most situations, jQuery will complete the string by itself... (Maybe not for </a>, though.)

live627

  • Should five per cent appear too small / Be thankful I don't take it all / 'Cause I'm the taxman, yeah I'm the taxman
  • Posts: 1,667
Re: add_js + </ul>
« Reply #9, on April 29th, 2014, 07:27 AM »
Working here as is with nothing in firebug .

 Untitled.png - 10.63 kB, 215x243, viewed 89 times.

A confident man keeps quiet.whereas a frightened man keeps talking, hiding his fear.

Nao

  • Dadman with a boy
  • Posts: 16,061
Re: add_js + </ul>
« Reply #10, on April 29th, 2014, 10:48 AM »
Did you try in chrome?

CerealGuy

  • Posts: 343
Re: add_js + </ul>
« Reply #11, on April 29th, 2014, 05:12 PM »
Tried curl + cookie, its nothing with the browser.

Nao

  • Dadman with a boy
  • Posts: 16,061