Changeset ca0cd05 in github


Ignore:
Timestamp:
Nov 28, 2011 3:10:44 AM (19 months ago)
Author:
alecpl <alec@…>
Branches:
master, HEAD, courier-fix, dev-browser-capabilities, pdo, release-0.8
Children:
6e0fded
Parents:
79db330
Message:
  • Fix handling HTML entities when converting HTML to text (#1488212)
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • CHANGELOG

    r79db330 rca0cd05  
    22=========================== 
    33 
     4- Fix handling HTML entities when converting HTML to text (#1488212) 
    45- Fix fit_string_to_size() renders browser and ui unresponsive (#1488207) 
    56- Fix handling of invalid characters in request (#1488124) 
  • program/js/app.js

    rd0924d4 rca0cd05  
    57665766  }; 
    57675767 
    5768   this.plain2html = function(plainText, id) 
     5768  this.plain2html = function(plain, id) 
    57695769  { 
    57705770    var lock = this.set_busy(true, 'converting'); 
    5771     $('#'+id).val(plainText ? '<pre>'+plainText+'</pre>' : ''); 
     5771 
     5772    plain = plain.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'); 
     5773    $('#'+id).val(plain ? '<pre>'+plain+'</pre>' : ''); 
     5774 
    57725775    this.set_busy(false, null, lock); 
    57735776  }; 
  • program/lib/html2text.php

    r4d7fbd5 rca0cd05  
    146146        "/\r/",                                  // Non-legal carriage return 
    147147        "/[\n\t]+/",                             // Newlines and tabs 
    148         '/[ ]{2,}/',                             // Runs of spaces, pre-handling 
    149148        '/<script[^>]*>.*?<\/script>/i',         // <script>s -- which strip_tags supposedly has problems with 
    150149        '/<style[^>]*>.*?<\/style>/i',           // <style>s -- which strip_tags supposedly has problems with 
     
    162161        '/(<tr[^>]*>|<\/tr>)/i',                 // <tr> and </tr> 
    163162        '/<td[^>]*>(.*?)<\/td>/i',               // <td> and </td> 
     163    ); 
     164 
     165    /** 
     166     *  List of pattern replacements corresponding to patterns searched. 
     167     * 
     168     *  @var array $replace 
     169     *  @access public 
     170     *  @see $search 
     171     */ 
     172    var $replace = array( 
     173        '',                                     // Non-legal carriage return 
     174        ' ',                                    // Newlines and tabs 
     175        '',                                     // <script>s -- which strip_tags supposedly has problems with 
     176        '',                                     // <style>s -- which strip_tags supposedly has problems with 
     177        "\n\n",                                 // <P> 
     178        "\n",                                   // <br> 
     179        '_\\1_',                                // <i> 
     180        '_\\1_',                                // <em> 
     181        "\n\n",                                 // <ul> and </ul> 
     182        "\n\n",                                 // <ol> and </ol> 
     183        "\t* \\1\n",                            // <li> and </li> 
     184        "\n\t* ",                               // <li> 
     185        "\n-------------------------\n",        // <hr> 
     186        "<div>\n",                              // <div> 
     187        "\n\n",                                 // <table> and </table> 
     188        "\n",                                   // <tr> and </tr> 
     189        "\t\t\\1\n",                            // <td> and </td> 
     190    ); 
     191 
     192    /** 
     193     *  List of preg* regular expression patterns to search for, 
     194     *  used in conjunction with $ent_replace. 
     195     * 
     196     *  @var array $ent_search 
     197     *  @access public 
     198     *  @see $ent_replace 
     199     */ 
     200    var $ent_search = array( 
    164201        '/&(nbsp|#160);/i',                      // Non-breaking space 
    165202        '/&(quot|rdquo|ldquo|#8220|#8221|#147|#148);/i', 
     
    177214        '/&(euro|#8364);/i',                     // Euro sign 
    178215        '/&(amp|#38);/i',                        // Ampersand: see _converter() 
    179         '/[ ]{2,}/'                              // Runs of spaces, post-handling 
     216        '/[ ]{2,}/',                             // Runs of spaces, post-handling 
    180217    ); 
    181218 
     
    183220     *  List of pattern replacements corresponding to patterns searched. 
    184221     * 
    185      *  @var array $replace 
    186      *  @access public 
    187      *  @see $search 
    188      */ 
    189     var $replace = array( 
    190         '',                                     // Non-legal carriage return 
    191         ' ',                                    // Newlines and tabs 
    192         ' ',                                    // Runs of spaces, pre-handling 
    193         '',                                     // <script>s -- which strip_tags supposedly has problems with 
    194         '',                                     // <style>s -- which strip_tags supposedly has problems with 
    195         "\n\n",                                 // <P> 
    196         "\n",                                   // <br> 
    197         '_\\1_',                                // <i> 
    198         '_\\1_',                                // <em> 
    199         "\n\n",                                 // <ul> and </ul> 
    200         "\n\n",                                 // <ol> and </ol> 
    201         "\t* \\1\n",                            // <li> and </li> 
    202         "\n\t* ",                               // <li> 
    203         "\n-------------------------\n",        // <hr> 
    204         "<div>\n",                              // <div> 
    205         "\n\n",                                 // <table> and </table> 
    206         "\n",                                   // <tr> and </tr> 
    207         "\t\t\\1\n",                            // <td> and </td> 
     222     *  @var array $ent_replace 
     223     *  @access public 
     224     *  @see $ent_search 
     225     */ 
     226    var $ent_replace = array( 
    208227        ' ',                                    // Non-breaking space 
    209228        '"',                                    // Double quotes 
     
    220239        'EUR',                                  // Euro sign. € ? 
    221240        '|+|amp|+|',                            // Ampersand: see _converter() 
    222         ' '                                     // Runs of spaces, post-handling 
     241        ' ',                                    // Runs of spaces, post-handling 
    223242    ); 
    224243 
     
    304323     */ 
    305324    var $_link_list = ''; 
    306      
     325 
    307326    /** 
    308327     *  Number of valid links detected in the text, used for plain text 
     
    315334    var $_link_count = 0; 
    316335 
    317     /**  
    318      * Boolean flag, true if a table of link URLs should be listed after the text.  
    319      *   
    320      * @var boolean $_do_links  
    321      * @access private  
    322      * @see html2text()  
     336    /** 
     337     * Boolean flag, true if a table of link URLs should be listed after the text. 
     338     * 
     339     * @var boolean $_do_links 
     340     * @access private 
     341     * @see html2text() 
    323342     */ 
    324343    var $_do_links = true; 
    325   
     344 
    326345    /** 
    327346     *  Constructor. 
     
    493512        $this->_convert_pre($text); 
    494513 
    495         // Run our defined search-and-replace 
     514        // Run our defined tags search-and-replace 
    496515        $text = preg_replace($this->search, $this->replace, $text); 
     516 
     517        // Run our defined tags search-and-replace with callback 
     518        $text = preg_replace_callback($this->callback_search, array('html2text', '_preg_callback'), $text); 
     519 
     520        // Strip any other HTML tags 
     521        $text = strip_tags($text, $this->allowed_tags); 
     522 
     523        // Run our defined entities/characters search-and-replace 
     524        $text = preg_replace($this->ent_search, $this->ent_replace, $text); 
    497525 
    498526        // Replace known html entities 
    499527        $text = html_entity_decode($text, ENT_COMPAT, 'UTF-8'); 
    500  
    501         // Run our defined search-and-replace with callback 
    502         $text = preg_replace_callback($this->callback_search, array('html2text', '_preg_callback'), $text); 
    503528 
    504529        // Remove unknown/unhandled entities (this cannot be done in search-and-replace block) 
     
    509534        $text = str_replace('|+|amp|+|', '&', $text); 
    510535 
    511         // Strip any other HTML tags 
    512         $text = strip_tags($text, $this->allowed_tags); 
    513  
    514536        // Bring down number of empty lines to 2 max 
    515537        $text = preg_replace("/\n\s+\n/", "\n\n", $text); 
     
    517539 
    518540        // remove leading empty lines (can be produced by eg. P tag on the beginning) 
    519         $text = preg_replace('/^\n+/', '', $text); 
     541        $text = ltrim($text, "\n"); 
    520542 
    521543        // Wrap the text to a readable format 
     
    545567                return $display; 
    546568 
    547             if ( substr($link, 0, 7) == 'http://' || substr($link, 0, 8) == 'https://' || 
    548             substr($link, 0, 7) == 'mailto:' 
    549         ) { 
     569            if ( preg_match('!^(https?://|mailto:)!', $link) ) { 
    550570            $this->_link_count++; 
    551571            $this->_link_list .= '[' . $this->_link_count . "] $link\n"; 
Note: See TracChangeset for help on using the changeset viewer.