Changeset 503


Ignore:
Timestamp:
Feb 10, 2015 12:03:25 AM (9 years ago)
Author:
anonymous
Message:

Backported spellcheck fixes

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/1.1dev/lib/SpellCheck.inc.php

    r434 r503  
    2020        'highlight_end' => '</strong>',
    2121    );
    22    
     22
    2323    var $_pspell_cfg_handle;
    2424    var $_pspell_handle;
    2525    var $_use_personal_wordlist = false;
    2626    var $_errors = array();
    27    
     27
    2828    /**
    2929     * Constructor.
     
    8585        return $this->_params[$param];
    8686    }
    87    
     87
    8888    /**
    8989     * Check whether any errors have been triggered.
     
    103103        $this->_errors = array();
    104104    }
    105    
     105
    106106    /**
    107107     * Check one word.
     
    123123        }
    124124    }
    125    
     125
    126126    /**
    127127     * Suggest the correct spelling for one misspelled word.
     
    138138        return pspell_suggest($this->_pspell_handle, $word);
    139139    }
    140    
     140
    141141    /**
    142142     * Add a word to a personal list.
     
    154154            if (pspell_add_to_personal($this->_pspell_handle, $word)) {
    155155                logMsg(sprintf('Added "%s" to personal wordlist: %s', $word, $this->getParam('personal_wordlist')), LOG_DEBUG, __FILE__, __LINE__);
    156                 return true;           
     156                return true;
    157157            } else {
    158158                logMsg(sprintf('Failed adding "%s" to personal wordlist: %s', $word, $this->getParam('personal_wordlist')), LOG_ERR, __FILE__, __LINE__);
     
    161161        }
    162162    }
    163    
     163
    164164    /**
    165165     * Save personal list to file.
     
    184184        }
    185185    }
    186    
     186
    187187    /**
    188188     * Returns an array of suggested words for each misspelled word in the given text.
     
    221221        }
    222222    }
    223    
     223
    224224    /**
    225225     * Checks all words in a given string.
     
    255255        }
    256256    }
    257    
     257
    258258    /**
    259259     * Returns a given string with misspelled words highlighted.
     
    267267     */
    268268    function getStringHighlighted($string, $show_footnote=false)
    269     { 
     269    {
    270270        // Split words on punctuation except apostrophes (this regex is used in several places in this class).
    271271        // http://stackoverflow.com/questions/790596/split-a-text-into-single-words
     
    279279                    $footnote = $show_footnote ? '<sup style="color:#999;">' . ++$cnt . '</sup>' : '';
    280280                    $words[$i] = $this->getParam('highlight_start') . $word . $this->getParam('highlight_end') . $footnote;
    281                     $string = preg_replace("/\b$word\b/", $words[$i], $string);
     281                    $string = preg_replace(sprintf('/\b%s\b/', preg_quote($word, '/')), $words[$i], $string);
    282282                }
    283283            }
    284284        }
    285285        return $string;
    286         // return join(' ', $words);
    287     }
    288    
     286    }
     287
    289288    /**
    290289     * Prints the HTML for correcting all misspellings found in the text of one $_FORM element.
     
    302301        <input name="<?php echo $form_name ?>" type="hidden" value="<?php echo oTxt(getFormData($form_name)) ?>" />
    303302        <?php
    304        
     303
    305304        $form_words = $this->getStringSuggestions(getFormData($form_name));
    306305        if (is_array($form_words) && !empty($form_words)) {
     
    315314                <option value="<?php echo $original_word ?>">(<?php echo $original_word ?>)</option>
    316315                <?php
    317                
     316
    318317                foreach ($words as $suggestion) {
    319318                    ?>
     
    321320                    <?php
    322321                }
    323                
     322
    324323                ?>
    325324                </select>
     
    334333        }
    335334    }
    336    
     335
    337336    /**
    338337     * Tests if any form spelling corrections have been submitted.
     
    348347        return (false !== getFormData('spelling_suggestions', false)) || (false !== getFormData('spelling_corrections', false));
    349348    }
    350    
     349
    351350    /**
    352351     * Replace the misspelled words in the text of a specified form with the corrections.
  • trunk/lib/SpellCheck.inc.php

    r502 r503  
    44 * For details visit the project site: <http://trac.strangecode.com/codebase/>
    55 * Copyright 2001-2012 Strangecode, LLC
    6  * 
     6 *
    77 * This file is part of The Strangecode Codebase.
    88 *
     
    1111 * Free Software Foundation, either version 3 of the License, or (at your option)
    1212 * any later version.
    13  * 
     13 *
    1414 * The Strangecode Codebase is distributed in the hope that it will be useful, but
    1515 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1616 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    1717 * details.
    18  * 
     18 *
    1919 * You should have received a copy of the GNU General Public License along with
    2020 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
     
    141141    {
    142142        $app =& App::getInstance();
    143    
     143
    144144        if (array_key_exists($param, $this->_params)) {
    145145            return $this->_params[$param];
     
    267267    {
    268268        $corrections = array();
    269         // Split words on punctuation except apostrophes
     269        // Split words on punctuation except apostrophes (this regex is used in several places in this class).
    270270        // http://stackoverflow.com/questions/790596/split-a-text-into-single-words
    271         $words = preg_split("/((^\p{P}+)|(\p{P}*\s+\p{P}*)|[\p{Pd}-–—]+|(\+|(\p{P}+$))/", $string);
    272         // Remove non-word elements.
    273         $words = preg_grep('/\w+/', $words);
    274 
     271        $words = preg_split("/((?:^\p{P}+)|(?:\p{P}*\s+\p{P}*)|[\p{Pd}—–-]+|(?:\p{P}+$))/", $string, -1, PREG_SPLIT_DELIM_CAPTURE);
    275272        if (is_array($words) && !empty($words)) {
     273            // Remove non-word elements.
     274            $words = preg_grep('/\w+/', $words);
     275            $words = array_map('strip_tags', $words);
    276276            foreach ($words as $i => $word) {
    277277                if (!$this->check($word)) {
     
    303303    {
    304304        $errors = array();
    305         $words = preg_split('/([\W]+?)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
    306         // Remove non-word elements.
    307         $check_words = preg_grep('/\w+/', $words);
    308 
    309         if (is_array($check_words) && !empty($check_words)) {
    310             foreach ($check_words as $i => $word) {
     305        // Split words on punctuation except apostrophes (this regex is used in several places in this class).
     306        // http://stackoverflow.com/questions/790596/split-a-text-into-single-words
     307        $words = preg_split("/((?:^\p{P}+)|(?:\p{P}*\s+\p{P}*)|[\p{Pd}—–-]+|(?:\p{P}+$))/", $string, -1, PREG_SPLIT_DELIM_CAPTURE);
     308        if (is_array($words) && !empty($words)) {
     309            // Remove non-word elements.
     310            $words = preg_grep('/\w+/', $words);
     311            $words = array_map('strip_tags', $words);
     312            foreach ($words as $i => $word) {
    311313                if (!$this->check($word)) {
    312314                    $errors[] = $word;
     
    334336    public function getStringHighlighted($string, $show_footnote=false)
    335337    {
    336         $words = preg_split('/([\W]+?)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
    337         $check_words = preg_grep('/\w+/', $words);
     338        // Split words on punctuation except apostrophes (this regex is used in several places in this class).
     339        // http://stackoverflow.com/questions/790596/split-a-text-into-single-words
     340        $words = preg_split("/((?:^\p{P}+)|(?:\p{P}*\s+\p{P}*)|[\p{Pd}—–-]+|(?:\p{P}+$))/", $string, -1, PREG_SPLIT_DELIM_CAPTURE);
    338341        $cnt = 0;
    339         if (is_array($check_words) && !empty($check_words)) {
    340             foreach ($check_words as $i => $word) {
     342        if (is_array($words) && !empty($words)) {
     343            $words = preg_grep('/\w+/', $words);
     344            $words = array_map('strip_tags', $words);
     345            foreach ($words as $i => $word) {
    341346                if (!$this->check($word)) {
    342                     $footnote = $show_footnote ? '<sup>' . ++$cnt . '</sup>' : '';
    343                     $words[$i] = $this->getParam('highlight_start') . $word . $this->getParam('highlight_end') . $footnote;
     347                    $footnote = $show_footnote ? '<sup style="color:#999;">' . ++$cnt . '</sup>' : '';
     348                    $words[$i] = $this->getParam('highlight_start') . $word . $this->getParam('highlight_end') . $footnote;
     349                    $string = preg_replace(sprintf('/\b%s\b/', preg_quote($word, '/')), $words[$i], $string);
    344350                }
    345351            }
    346352        }
    347         return join('', $words);
     353        return $string;
    348354    }
    349355
     
    361367    {
    362368        ?>
    363         <input name="<?php echo $form_name ?>" type="hidden" value="<?php echo getFormData($form_name) ?>" />
     369        <input name="<?php echo $form_name ?>" type="hidden" value="<?php echo oTxt(getFormData($form_name)) ?>" />
    364370        <?php
    365371
     
    370376                ?>
    371377                <li>
    372                 <select name="spelling_suggestions[<?php echo $form_name ?>][<?php echo $i ?>]" onchange"document.forms[0].elements['spelling_corrections[<?php echo $form_name ?>][<?php echo $i ?>]'].value = this.value;">
     378                <label style="color:#999;"><sub style="vertical-align:text-top;"><?php echo $j++; ?></sub></label>
     379                <select name="spelling_suggestions[<?php echo $form_name ?>][<?php echo $i ?>]" onchange="document.forms[0].elements['spelling_corrections[<?php echo $form_name ?>][<?php echo $i ?>]'].value = this.value;">
    373380                <?php $original_word = array_shift($words); ?>
    374381                <option value="<?php echo $original_word ?>">(<?php echo $original_word ?>)</option>
     
    383390                ?>
    384391                </select>
    385                 <input type="text" name="spelling_corrections[<?php echo $form_name ?>][<?php echo $i ?>]" value="<?php echo $original_word ?>" class="sc-small" />
     392                <input type="text" name="spelling_corrections[<?php echo $form_name ?>][<?php echo $i ?>]" value="<?php echo oTxt($original_word) ?>" size="20">
    386393                <?php if ($this->_use_personal_wordlist) { ?>
    387394                <input name="save_to_personal_wordlist[]" type="checkbox" value="<?php echo $i ?>" /><?php echo _("Learn spelling") ?>
     
    420427    public function applyFormCorrections($form_name)
    421428    {
    422         $form_words = preg_split('/([\W]+?)/', getFormData($form_name), -1, PREG_SPLIT_DELIM_CAPTURE);
     429        // Split words on punctuation except apostrophes (this regex is used in several places in this class).
     430        // http://stackoverflow.com/questions/790596/split-a-text-into-single-words
     431        $form_words = preg_split("/((?:^\p{P}+)|(?:\p{P}*\s+\p{P}*)|[\p{Pd}—–-]+|(?:\p{P}+$))/", getFormData($form_name), -1, PREG_SPLIT_DELIM_CAPTURE);
    423432        $suggestions = getFormData('spelling_suggestions');
    424433        $corrections = getFormData('spelling_corrections');
Note: See TracChangeset for help on using the changeset viewer.