marcelklehr / buzzmap

draw and edit mindmaps interactively, using force-directed layouts (jQuery plugin)
marcelklehr.github.com/buzzmap
Other
22 stars 17 forks source link

Special characters issue while loading from json #1

Closed vishalpai closed 11 years ago

vishalpai commented 11 years ago

Root reason: decodeURI used while loading data from json but corresponding encodeURI not used while serializing

Simple steps to reproduce:

  1. Type in % character as part of node label.
  2. Subscribe to onchange event and save json.
  3. now load the mindmap with json obtained in step2 to see the error.

Node.prototype.serialize = function () {

var spanstartstring = ""; var spanendstring = ""; var label = $(this.el).html().substring(spanstartstring.length,$(this.el).html().length - spanendstring.length); var encodedlabel = encodeURIComponent(label); var finallabel = spanstartstring + encodedlabel + spanendstring;

var string = '{"label":"' + finallabel.replace(/'/g, '\'') + '","children":[';

........... };

marcelklehr commented 11 years ago

Oh.

marcelklehr commented 11 years ago

Any reason you introduce spanstartstring and spanendstring?

vishalpai commented 11 years ago

Umm...it was a quick fix...$(this.el).html() has the label enclosed by span....

i have a copy paste typo in there. I wonder how I managed to do that :)

var spanstartstring = ""; var spanendstring = "";

vishalpai commented 11 years ago

ok github comment screen also has a bug ;)...the spanstartstring has the span word enclosed in angular braces....and i cant type it here ;)....

due to the bug on github screen it shows that the spanstartstring is set to empty.

basically what i am doing is extracting the label between span start and end and then encoding the label and sticking in the serialized string.

marcelklehr commented 11 years ago

It's not a bug. You can type html in here.. :)

Just use this:

your code

They call it GFM.

vishalpai commented 11 years ago
var spanstartstring = "<span>";
var spanendstring = "</span>";
var label = $(this.el).html().substring(spanstartstring.length,$(this.el).html().length - spanendstring.length);
var encodedlabel = encodeURIComponent(label);
var finallabel = spanstartstring + encodedlabel + spanendstring;

var string = '{"label":"' + finallabel.replace(/'/g, '\\\'') + '","children":[';

there you go...lesson learnt is look at the preview before submit :)

first time github user and first time js code

vishalpai commented 11 years ago

okay and the single quote had to be handled separately using the replace. Not sure if its the right place to do it (cannot do it before encoding as \ would be encoded)

marcelklehr commented 11 years ago

I think I was just replacing " with \", because " would collide with JSONs string literals, but encodeURIComponent takes care of ", so we don't need this anymore, IMO

marcelklehr commented 11 years ago

Why not just encode the tags, too? They get decoded anyway, afaik

vishalpai commented 11 years ago

hmm let me check the tag thing.. double quote not needed...but single quote is.

marcelklehr commented 11 years ago

Ok, I think I fixed it. I completely ditched this en/de-codeURI stuff and adopted plain old JSON.{stringify/parse}, see my commit d0f1e1d7e955061...

Thanks for your help!

vishalpai commented 11 years ago

Thanks for fixing. I will check and get back.

In the mean time I have opened another one. Supporting cut and paste. I have provided a crude way to do it. Again serialize and de-serialize to the help.