• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 410
  • Last Modified:

Populating a textarea with a script

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
Andrew99
Asked:
Andrew99
1 Solution
 
Andrew99Author Commented:
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
 
kanduraCommented:
You should html-escape the contents of the file. At the very minimum, replace < signs into &lt;

$content =~ s/</\&gt;/g;
0
 
Andrew99Author Commented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
kanduraCommented:
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
 
jmcgOwnerCommented:
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
 
TintinCommented:
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
 
Andrew99Author Commented:
Kandura  Thanks that worked.  I forgot to ask what would I use to reverse back to the orginal?
0
 
kanduraCommented:
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
 
Andrew99Author Commented:
I am not sure why the unescaping is not working.  
0
 
Andrew99Author Commented:
so I need to unescape it when I right it to the database.  
0
 
Andrew99Author Commented:
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
 
kanduraCommented:
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
 
Andrew99Author Commented:
The unescape code is correct tho right?
0
 
kanduraCommented:
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
 
Andrew99Author Commented:
Thank you very much for you help.  :)
I will read up on html Entities.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now