Solved

Populating a textarea with a script

Posted on 2004-03-29
15
371 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now