Long Line Break (was: Preview Pane)

Michael Burns michael.burns at cosbit.com
Thu Sep 7 17:47:29 CEST 2006


Good morning,

On 7-Sep-06, at 7:37 AM, Robin Elfrink wrote:

> Now for Internet Explorer to break long lines, didn't even see that
> before, was too busy resizing <div>s ...

I've used the following "hack" before to force long lines to break.

In my testing, it works in Safari / Firefox / IE / Opera without hassle.

The <wbr> tag is non-standard though so any XHTML validators will  
complain ... but it's the best I could come up with that was the most  
compliant and compatible:


<span><wbr style="content: '\00200B'" /></span>


If you insert that every so often into long strings, it creates an  
optional line break that doesn't otherwise affect copy/paste or  
create any odd visual artifacts if no break is required.

(I tend to insert it every 15 characters in strings longer than 15  
characters.)


I don't have any PHP-foo do to that automagically, but here's my Perl- 
foo that does it. I can feed arbitrary html and it'll intelligently  
insert it, without clobbering html <tags> or anything... if someone  
can help convert it into PHP, simply add it to the output function  
for your emails (or anywhere you need it) and it should work. I just  
call:


my $html = '<div><p>some html <a href="/ 
somethinglonginatagthingy">here</a> with  
reallyreallyreallylongstringsofcharacters and other things.</p></div>';

print html_splitter($html);


Output:

<div><p>some html <a href="/somethinglonginatagthingy">here</a> with  
reallyreallyrea<span><wbr style="content: '\00200B'" /></ 
span>llylongstringso<span><wbr style="content: '\00200B'" /></ 
span>fcharacters and other things.</p></div>


Let me know, I'm happy to address comments / concerns / criticisms of  
the technique :-)

(note for Perl-isms: all the HTML::Entities stuff is needed to  
properly work with embedded entities without splitting in the middle;  
so that & is recognized as one character and preserved and not  
split half-way through)



---begin perl---
#
# call as: html_splitter( $string [, length] )
# returns: your string, with a
#
#            <span><wbr style="content: '\00200B'" /></span>
#
#          inserted every [length] characters (15 by default) on  
inter-tag
#          "words" (non-space characters actually) longer than [length]
#
#          the effect (so it seems with testing so far) of letting
#          browsers and email clients split real long strings (such as
#          email addresses or urls) while retaining their copy-and-paste
#          ability
#
#          Note: this code doesn't validate, as <wbr> isn't a W3C  
official entity
#
#          Note: we assume the string may contain markup, and so we  
don't process
#          and text between <> (tag-like sequences)
#
#          Note: to prevent splitting HTML entities (& for  
example), we decode
#          all string fragments into multi-byte chars, do the  
splitting (if needed),
#          then re-encode into HTML entities
#
#          Compatibility: tested thus far with Firefox, Safari, IE,  
Opera
#
sub html_splitter {

     my $string = shift;
     my $length = shift || 15;

     # find interesting bits and turn to next subroutine
     $string =~ s/(^|>)([^<]+)(<|$)/$1 . string_splitter($2, 
$length) . $3/egs;

     return $string;
}

#
# used by html_splitter() to actually break the chunks
#
sub string_splitter {

     my $string = shift;
     my $length = shift;

     # first, decode any entities
     HTML::Entities::decode_entities($string);

     # next split any long words in this bit
     $string =~ s/(\S\S{$length,})/word_splitter($1,$length)/eg;

     return $string;
}

#
# used by string_splitter() to do the work
#
sub word_splitter {

     my $string = shift;
     my $length = shift;
     my $strlen = length($string);

     my $out = HTML::Entities::encode_entities(substr($string, 0,  
$length));

     for (my $i = $length; $i < $strlen; $i += $length) {

         $out .= q|<span><wbr style="content: '\00200B'" /></span>|;
         $out .= HTML::Entities::encode_entities(substr($string, $i,  
$length));
     }

     return $out;
}
---end perl---


> Robin


-Michael

_______________________________________________________

Michael Burns
Cosbit Technologies
403-701-2672  / michael.burns at cosbit.com

GTalk: cmikeburns
AIM: cmikeburns
MSN: cmikeburns
_______________________________________________________

Box 2173, Station M  •  Calgary, Alberta, Canada  •  T2P 2M4
http://cosbit.com







More information about the Dev mailing list