Solved

Populating a textarea with a script

Posted on 2004-03-29
15
376 Views
Last Modified: 2009-07-29
I have a script that populates a textarea with what ever file you will like to read.  YOu can view html, txt, pl, cgi etc.

The only problem is when I read a HTML or scripting lang into a text area, if that file has a text area in it will not load right, the textarea in the file closes the textarea in the script.

here is an example.

Test.html.

<html>
</body>
<textarea>Hello world</textarea>
blah blah blah blha
<input type="text" name="blah" id="blah">
blahb blahb
</body>
</html>

Now when I load test.html into the script which will display test.html in the text area.  The end tag for the textarea in Test.html will close the text area in the scrip that is reading the test.html.

I know this sounds confusing.  But if anyone could help that would be great!
0
Comment
Question by:Andrew99
15 Comments
 

Author Comment

by:Andrew99
ID: 10702539
Here are the results of test.html being displayed in the textarea.

----------------------------------
<html>                             |
</body>                           |
<textarea>Hello world       |
------------------------------  |
                                       
blah blah blah blha
<input type="text" name="blah" id="blah">
blahb blahb
</body>
</html>


You see the test.html </textarea> is closing off the scripts text area.
0
 
LVL 18

Expert Comment

by:kandura
ID: 10703173
You should html-escape the contents of the file. At the very minimum, replace < signs into &lt;

$content =~ s/</\&gt;/g;
0
 

Author Comment

by:Andrew99
ID: 10703334
This is how I open my file for reading.

open(INF, $file) or die("Unabled to open Dat file");
@raw_data=<INF>;
close(INF);

How do I implement the $content =~ s/</\&gt;/g; ?
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 18

Accepted Solution

by:
kandura earned 25 total points
ID: 10703526
there are several options. I'd say, loop over the array @raw_data and modify each element:

@escaped_data = map { s/</\&lt;/g; $_ } @raw_data;

and then print out @escaped_data where you previously printed @raw_data.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10704134
It's a little unclear what you want to see in the textbox when you display HTML content. Are you looking to see the HTML rendered by the browser or as HTML source?

For a reversible transformation, something like the following might do:

my $raw_data;
{
open INF, $file or die "Unable to open $file -- $!";
local $/; # enable 'slurp' mode
$raw_data = <INF>;
close INF;
}
$raw_data =~ s/&/\&amp;/g; # first escape exising ampersands
$raw_data =~ s/</\&gt;/g; # escape possible HTML tag openings

===========================================

Now $raw_data contains a string that should display HTML source text with no danger of misinterpretation. The transform can also be reversed to get back the original text, if you needed to do that.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10709183
Following on from jmcg's suggestion.  To correct escape all HTML characters, you can do:


use CGI ':standard';

my $raw_data;
{
open INF, $file or die "Unable to open $file -- $!";
local $/; # enable 'slurp' mode
$raw_data = <INF>;
close INF;
}

$raw_data = escapeHTML($raw_data);


0
 

Author Comment

by:Andrew99
ID: 10730876
Kandura  Thanks that worked.  I forgot to ask what would I use to reverse back to the orginal?
0
 
LVL 18

Expert Comment

by:kandura
ID: 10731047
assuming you receive the new value through param('name_of_textarea'), you would do:
my $new_data = param('name_of_textarea');
$new_data =~ s/\&lt;/</g;       # unescape <
$new_data =~ s/\&amp;/\&/g;     # unescape &
0
 

Author Comment

by:Andrew99
ID: 10731425
I am not sure why the unescaping is not working.  
0
 

Author Comment

by:Andrew99
ID: 10731427
so I need to unescape it when I right it to the database.  
0
 

Author Comment

by:Andrew99
ID: 10731454
What happens is I load the file up, and save it, load it up again and it doesn't escape the html.
this is how I write the file.
sub save_file{
   $Source =~ s/\&lt;/</g;       # unescape <
   $Source =~ s/\&amp;/\&/g;     # unescape &
   open uDAT,">" , $filePath or die "Cannot Open File";
  print uDAT $Source;
  close uDAT;
}

This is how I read it.
open(INF, $filePath) or die("Unabled to open Dat file");
@raw_data=<INF>;
close(INF);

@escaped_data = map { s/</\</g; $_ } @raw_data;

Now it works once, but then when I do the exact same page again it does not escape the html???
0
 
LVL 18

Expert Comment

by:kandura
ID: 10731501
you would escape the html with these regular expressions, not s/</\</g  :

@escaped_data = map {
                s/&/\&amp;/g;   # first escape existing ampersands
                s/</\&lt;/g;       # escape possible HTML tag openings
                $_;
            }
            @raw_data;

Note that both me and jmcg had a typo in the original answers: we wanted to change < into &gt;, which translates back to > ;^)
0
 

Author Comment

by:Andrew99
ID: 10731516
The unescape code is correct tho right?
0
 
LVL 18

Expert Comment

by:kandura
ID: 10731549
yes, it is.

Note that this only (un)escapes only those two entities. It may be enough for your immediate needs, but if you find out there are other things you need to (un)escape, then I suggest you take a look at the HTML::Entities package. It handles the more general case.

Here's a snippet of code from the documentation:

use HTML::Entities;
$encoded = encode_entities($a);   #### encodes unsafe characters to html entities (eg. é -> &eacute;)
$decoded = decode_entities($a);   #### decodes html entities back to regular characters (eg. &lt; -> < )
0
 

Author Comment

by:Andrew99
ID: 10731571
Thank you very much for you help.  :)
I will read up on html Entities.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question