Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How do I create non-breaking space (  ) with HTML::Element?

by tphyahoo (Vicar)
on Aug 08, 2005 at 13:04 UTC ( [id://481862]=perlquestion: print w/replies, xml ) Need Help??

tphyahoo has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I am doing a large number of find and replace operations using HTML::Treebuilder and HTML::Element. One place I have run into trouble is  . This isn't an html element, yet it isn't treated quite as text either. (Pseudoelement?) For instance:
use strict; use warnings; use HTML::Element; use Test::More qw(no_plan); my $element = HTML::Element->new('td', "class"=>"zwischanspalte"); $element->push_content('&nbsp;'); my $expected = '<td class="zwischanspalte">&nbsp;</td>'; my $html = $element->as_HTML(); is($html, $expected);
output is
not ok 1 # Failed test (pseudoelement.pl at line 12) # got: '<td class="zwischanspalte">&amp;nbsp;</td> # ' # expected: '<td class="zwischanspalte">&nbsp;</td>' 1..1 # Looks like you failed 1 tests of 1.
How can I get this to work? Thanks!

Replies are listed 'Best First'.
Re: How do I create non-breaking space (&nbsp; ) with HTML::Element?
by Tanalis (Curate) on Aug 08, 2005 at 13:29 UTC
    I'd take a look at the ~literal pseudo-element in the docs .. that looks like it should be able to do what you need.

    Untested, but ..

    my $literal = HTML::Element->new('~literal', 'text' => '&nbsp;' ); my $element = HTML::Element->new('td', "class"=>"zwischanspalte"); $element->push_content($literal);
    might give you some ideas :)

    Hope that helps ...

      Thanks foxcub, that did the trick. I just had to delete whitespace to pass the test :)
      use strict; use warnings; use HTML::Element; use Test::More qw(no_plan); my $literal = HTML::Element->new('~literal', 'text' => '&nbsp;' ); my $element = HTML::Element->new('td', "class"=>"zwischanspalte"); $element->push_content($literal); my $expected = '<td class="zwischanspalte">&nbsp;</td>'; my $html = $element->as_HTML(); $html =~ s/[\r\n]//; is($html, $expected);
Re: How do I create non-breaking space (&nbsp; ) with HTML::Element?
by marnanel (Beadle) on Aug 08, 2005 at 16:47 UTC
Re: How do I create non-breaking space (&nbsp; ) with HTML::Element?
by Marel (Initiate) on Mar 15, 2012 at 22:22 UTC
    Old question, new answer:
    use strict; use warnings; use HTML::Element; use Test::More qw(no_plan); my $nbsp = chr(160); my $element = HTML::Element->new_from_lol( ['td', {"class"=>"zwischanspalte"}, $nbsp] ); my $expected = '<td class="zwischanspalte">&nbsp;</td>'; my $html = $element->as_HTML(); is($html, $expected);
    For me chr(160) is handier then using ~literal etc. as it can easily combined in strings.
Re: How do I create non-breaking space (&nbsp; ) with HTML::Element?
by gu (Beadle) on Nov 15, 2005 at 20:51 UTC
    Once, getting content from HTML::TreeBuilder , I had to find how to remove those &nbsp; spaces appearing as spaces-that-are-not-spaces :) I tried :
    my $tree = HTML::TreeBuilder->new_from_content( $m->content ) ; for ($tree->look_down( _tag => 'td', class => 'obsTempText' ) ) { my $bla = $_->as_trimmed_text ; $bla =~ s/ //g ; print $bla ; }
    But the s/ //g did nothing. As marnanel said, the answer is to use HTML character entity references :
    my $NBSP = HTML::Entities::decode_entities('&nbsp;');
    Then,
    $bla =~ s/$NBSP//g ;
    removed the non-breaking spaces.

      Thanks so much for this post referencing deleting &nbsp;. I had been trying for several days to delete these nodes.

      This was very helpful.

      Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://481862]
Approved by inman
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 19:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found