dealing with 'awkward' characters in the XML file

Using XMLHttpRequest() to configure entire ToolBook pages.

dealing with 'awkward' characters in the XML file

Postby John Robin Dove » Thu Jan 09, 2020 11:13 am

Hi Clifton,

I have been wrestling with a problem and have found a solution but I think there must be a better way to do this and if there is, I'm sure you'll know what it is.

When I started this project (many years ago :) ) I decided to leave the file system as it had been in the VB version i.e. including names that contain apostrophes and names that contain spaces. Surprisingly this did not cause problems in the code generated by the TB actions system. I guess there must be a few built in safety nets.

I have just transferred a function to the code in the XML file and there these things do cause problems. I am displaying a graphic in a field called "pictureBox" which must be resized and the source file path contains both a space and an apostrophe. I thought encodeURIComponent might help and I then I tried JSON.stringify but neither solved the problem. So I came up with this laborious method which actually works in this case:

<pictureBox>
<function name="myUser" event="user" params="evt,value" useTB="true">
<![CDATA[
//value[0] = width, value[1] = height, value[2] = directory, value[3] = file!!

tbfunction_pgTBObjSet("pictureBox", "width", value[0]);
tbfunction_pgTBObjSet("pictureBox", "height", value[1]);
tbfunction_pgStyleObject("pictureBox", "backgroundSize", value[0] + "px " + value[1] + "px");

//THE LINE BELOW WITH AN ESCAPED APOSTROPHE WORKS BUT IT DOESN'T USE alue[2] + [value[3]:!!
//var address = 'url("../../France1/Mimine\'s_Academy_of_Poo_Studies/teachers/BLOGGS_Bill/ENGLISH/Immagini/P-0 gardening.jpg")';!!

//value[2] + value[3] produces "../../France1/Mimine's_Academy_of_Poo_Studies/teachers/BLOGGS_Bill/ENGLISH/Immagini/P-0 gardening.jpg";!!

var address = value[2] + value[3];
address = tbfunction_pgReplace("'", String.fromCharCode(92) + "'", address)
address = "url(" + String.fromCharCode(34) + address + String.fromCharCode(34) + ")";
tbfunction_pgStyleObject("pictureBox", "background-image", address);
tbfunction_pgTBObjSet("pictureBox", "visible", true);
]]>
</function>
</pictureBox>


Can you suggest a better alternative, please?
John
John Robin Dove
 
Posts: 486
Joined: Thu Jan 23, 2014 4:35 am

Re: dealing with 'awkward' characters in the XML file

Postby Clifton » Thu Jan 09, 2020 10:26 pm

Hi John,
Using the exeJavascript tool . . .
https://pgsoftwaretools.com/powerpac/assessments/exec-js/

. . . I tried to simulate the issue you are having.

Try running this code:
var value = [];
value.push( 'Hello. This is Clifton\'s test case.\n' );
value.push( 'url("../../France1/Mimine\'s_Academy_of_Poo_Studies/teachers/BLOGGS_Bill/ENGLISH/Immagini/P-0 gardening.jpg")' );
return value[0] + value[1];

The escaped characters all retain their expected display.
They even look right in an alert() dialog, though I wouldn't always trust them to look right in an alert().

Not sure what to recommend??
It may be that your strings are getting corrupted even before the user event is received.
 
Clifton
Site Admin
 
Posts: 732
Joined: Tue Jan 14, 2014 1:04 am

Re: dealing with 'awkward' characters in the XML file

Postby John Robin Dove » Fri Jan 10, 2020 10:01 am

Thanks for your reply but I don't think I made myself clear. There is a folder on the webserver containing an apostrophe in its name. This is deliberate because I thought it conceivable that some schools may well have an apostrophe in their names. I am also using files from the PC version of my program. This version includes the use of media files which contain spaces in their names. When I started the internet version of the project I left things as they were because I wanted the two versions to be able to use the same 'exercise folders'. These folders contain the media files and a text file used by the program to recreate the exercise created by a teacher. I had no problems with this until yesterday when I found it difficult to present the data in a usable format. After some reflection I guess this is because I've never had to juggle with the "url(...)" format until now.

The file system is something like this: server/programs/country/school/teachers/subject/exercise/[media files and text file]
Each segment of the path is kept in an array. The array that I am using contains a directory with an apostrophe in its name and a media file with a space in its name. The contents of value[2] is
../../France1/Mimine's_Academy_of_Poo_Studies/teachers/BLOGGS_Bill/ENGLISH/Immagini/ and the contents of value[3] is P-0 gardening.jpg

So in the array the apostophe is not escaped. Because of the space, I need two set of quotes, single and double but because of the apostrophe (which is the same character as a single quote) I must escape the apostrophe. I used this code:

var address = value[2] + value[3];
address = tbfunction_pgReplace("'", String.fromCharCode(92) + "'", address);
address = "url(" + String.fromCharCode(34) + address + String.fromCharCode(34) + ")";
tbfunction_pgStyleObject("pictureBox", "background-image", address);
tbfunction_pgTBObjSet("pictureBox", "visible", true);


which works but seems pretty clunky to me and I wonder if there are not contexts in which it might fail. My question is: is there not a better way to do this?
John Robin Dove
 
Posts: 486
Joined: Thu Jan 23, 2014 4:35 am

Re: dealing with 'awkward' characters in the XML file

Postby Clifton » Fri Jan 10, 2020 6:28 pm

If you just need to ensure that your single apostrophe's are escaped, then just use this code:
    var s = "../../France1/Mimine's_Academy_of_Poo_Studies/teachers/BLOGGS_Bill/ENGLISH/Immagini/";
    s = s.replace( /\\?'/g, "\\'");
    return s; //only needed for exeJavascriptDirect tool
Now all single apostrophes in the string s are escaped. The above code uses a Javascript regular expression to find all single apostrophes whether escaped already or not and replace them with properly escaped code.
You can see this by running the above code in the exeJavascriptDirect tool:
https://pgsoftwaretools.com/powerpac/assessments/exec-js/

Of course, if your code is achieving the results you need, maybe just stay with that. However, ake sure to fully test apostrophes and spaces in folder names on your web server before allowing them.
Clifton
Site Admin
 
Posts: 732
Joined: Tue Jan 14, 2014 1:04 am

Re: dealing with 'awkward' characters in the XML file

Postby John Robin Dove » Sat Jan 11, 2020 5:48 am

Thanks Clifton. In an afterthought had actually added replaceAll : true in pgReplace but regex is no doubt the best tool for this kind of problem.
John Robin Dove
 
Posts: 486
Joined: Thu Jan 23, 2014 4:35 am

Re: dealing with 'awkward' characters in the XML file

Postby Clifton » Sat Jan 11, 2020 9:19 am

Please note that pgReplace() supports the full range of RegEx. It is just that to make the function work in the actions system, I had to support string replacement as the search parameter. But the function supports both. However the native RegEx support is only when the function is called from within a Javascript function or from within an XML file.
Clifton
Site Admin
 
Posts: 732
Joined: Tue Jan 14, 2014 1:04 am


Return to XML Configurations – Plugin Examples

Who is online

Users browsing this forum: No registered users and 1 guest

cron