Solved

Populating a textarea with a script

Posted on 2004-03-29
15
365 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Kandura  Thanks that worked.  I forgot to ask what would I use to reverse back to the orginal?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Expert Comment

by:kandura
Comment Utility
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
Comment Utility
I am not sure why the unescaping is not working.  
0
 

Author Comment

by:Andrew99
Comment Utility
so I need to unescape it when I right it to the database.  
0
 

Author Comment

by:Andrew99
Comment Utility
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
Comment Utility
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
Comment Utility
The unescape code is correct tho right?
0
 
LVL 18

Expert Comment

by:kandura
Comment Utility
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
Comment Utility
Thank you very much for you help.  :)
I will read up on html Entities.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
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…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

10 Experts available now in Live!

Get 1:1 Help Now