diff options
-rw-r--r-- | inc/html.php | 8 | ||||
-rw-r--r-- | inc/template.php | 10 | ||||
-rw-r--r-- | inc/utf8.php | 29 | ||||
-rw-r--r-- | lib/exe/spellcheck.php | 24 | ||||
-rw-r--r-- | lib/scripts/spellcheck.js | 16 |
5 files changed, 74 insertions, 13 deletions
diff --git a/inc/html.php b/inc/html.php index a9135e155..9ec6049ef 100644 --- a/inc/html.php +++ b/inc/html.php @@ -277,7 +277,7 @@ function html_search(){ //show progressbar print '<div align="center">'; - print '<script language="JavaScript" type="text/javascript">'; + print '<script language="javascript" type="text/javascript" charset="utf-8">'; print 'showLoadBar();'; print '</script>'; print '<br /></div>'; @@ -317,7 +317,7 @@ function html_search(){ } //hide progressbar - print '<script language="JavaScript" type="text/javascript">'; + print '<script language="javascript" type="text/javascript" charset="utf-8">'; print 'hideLoadBar();'; print '</script>'; } @@ -846,7 +846,7 @@ function html_edit($text=null,$include='edit'){ //FIXME: include needed? <tr> <td class="toolbar" colspan="2"> <?if($wr){?> - <script language="JavaScript" type="text/javascript"> + <script language="javascript" type="text/javascript" charset="utf-8"> <?/* sets changed to true when previewed */?> textChanged = <? ($pr) ? print 'true' : print 'false' ?>; @@ -907,7 +907,7 @@ function html_edit($text=null,$include='edit'){ //FIXME: include needed? <?}?> </td> <td align="right"> - <script type="text/javascript"> + <script language="javascript" type="text/javascript" charset="utf-8"> showSizeCtl(); <?if($wr){?> init_locktimer(<?=$conf['locktime']-60?>,'<?=$lang['willexpire']?>'); diff --git a/inc/template.php b/inc/template.php index 035bfbdc1..bc2bc561b 100644 --- a/inc/template.php +++ b/inc/template.php @@ -166,23 +166,23 @@ function tpl_metaheaders(){ } // include some JavaScript language strings - ptln('<script language="JavaScript" type="text/javascript">',$it); + ptln('<script language="javascript" type="text/javascript" charset="utf-8">',$it); ptln(" var alertText = '".$lang['qb_alert']."'",$it); ptln(" var notSavedYet = '".$lang['notsavedyet']."'",$it); ptln(" var DOKU_BASE = '".DOKU_BASE."'",$it); ptln('</script>',$it); // load the default JavaScript files - ptln('<script language="JavaScript" type="text/javascript" src="'. + ptln('<script language="javascript" type="text/javascript" charset="utf-8" src="'. DOKU_BASE.'lib/scripts/script.js"></script>',$it); - ptln('<script language="JavaScript" type="text/javascript" src="'. + ptln('<script language="javascript" type="text/javascript" charset="utf-8" src="'. DOKU_BASE.'lib/scripts/tw-sack.js"></script>',$it); - ptln('<script language="JavaScript" type="text/javascript" src="'. + ptln('<script language="javascript" type="text/javascript" charset="utf-8" src="'. DOKU_BASE.'lib/scripts/ajax.js"></script>',$it); // load spellchecker script if wanted if($conf['spellchecker'] && ($ACT=='edit' || $ACT=='preview')){ - ptln('<script language="JavaScript" type="text/javascript" src="'. + ptln('<script language="javascript" type="text/javascript" charset="utf-8" src="'. DOKU_BASE.'lib/scripts/spellcheck.js"></script>',$it); } diff --git a/inc/utf8.php b/inc/utf8.php index 70b16f1a5..7d0f0a266 100644 --- a/inc/utf8.php +++ b/inc/utf8.php @@ -190,7 +190,7 @@ function utf8_ltrim($str,$charlist=''){ } /** - * Unicode aware replacement for ltrim() + * Unicode aware replacement for rtrim() * * @author Andreas Gohr <andi@splitbrain.org> * @see rtrim() @@ -342,6 +342,33 @@ function utf8_strpos($haystack, $needle,$offset=0) { } /** + * Encodes UTF-8 characters to HTML entities + * + * @author <vpribish at shopping dot com> + * @link http://www.php.net/manual/en/function.utf8-decode.php + */ +function utf8_tohtml ($str) { + $ret = ''; + $max = strlen($str); + $last = 0; // keeps the index of the last regular character + for ($i=0; $i<$max; $i++) { + $c = $str{$i}; + $c1 = ord($c); + if ($c1>>5 == 6) { // 110x xxxx, 110 prefix for 2 bytes unicode + $ret .= substr($str, $last, $i-$last); // append all the regular characters we've passed + $c1 &= 31; // remove the 3 bit two bytes prefix + $c2 = ord($str{++$i}); // the next byte + $c2 &= 63; // remove the 2 bit trailing byte prefix + $c2 |= (($c1 & 3) << 6); // last 2 bits of c1 become first 2 of c2 + $c1 >>= 2; // c1 shifts 2 to the right + $ret .= '&#' . ($c1 * 100 + $c2) . ';'; // this is the fastest string concatenation + $last = $i+1; + } + } + return $ret . substr($str, $last, $i); // append the last batch of regular characters +} + +/** * This function returns any UTF-8 encoded text as a list of * Unicode values: * diff --git a/lib/exe/spellcheck.php b/lib/exe/spellcheck.php index ffa0e2886..6fa382a9c 100644 --- a/lib/exe/spellcheck.php +++ b/lib/exe/spellcheck.php @@ -104,12 +104,13 @@ function spell_check() { $line = trim($lines[$i]); if($line[0] == '@') continue; // comment if($line[0] == '*') continue; // no mistake in this word + if($line[0] == '+') continue; // root of word was found if(empty($line)){ // empty line -> new source line $lcnt--; continue; } - if(preg_match('/^& ([^ ]+) (\d+) (\d+): (.*)/',$line,$match)){ + if(preg_match('/^[&\?] ([^ ]+) (\d+) (\d+): (.*)/',$line,$match)){ // match with suggestions $word = $match[1]; $off = $match[3]-1; @@ -131,6 +132,11 @@ function spell_check() { $data[$lcnt] = utf8_substr_replace($data[$lcnt], spell_formatword($word) , $off, $len); continue; } + //still here - couldn't parse output + print '2'; + print "The spellchecker output couldn't be parsed.\n"; + print "Line $i:".$line; + return; } // the first char returns the spell info @@ -140,7 +146,10 @@ function spell_check() { $string = '0'.join('<br />',$data); } - //output + // encode multibyte chars as entities for broken Konqueror + $string = utf8_tohtml($string); + + //output print $string; } @@ -151,6 +160,11 @@ function spell_formatword($word,$suggestions=null){ //restrict to maximum of 7 elements $suggestions = array_slice($suggestions,0,7); $suggestions = array_map('htmlspecialchars',$suggestions); + + //konqueror's broken UTF-8 handling needs this + $suggestions = array_map('utf8_tohtml',$suggestions); + $suggestions = array_map('urlencode',$suggestions); + $suggestions = array_map('addslashes',$suggestions); $sug = ",'".join("','",$suggestions)."'"; //build javascript args }else{ @@ -176,6 +190,12 @@ function spell_resume(){ // restore quoted special chars $text = unhtmlspecialchars($text); + // but protect '&' (gets removed in JS later) + $text = str_replace('&','&',$text); + // encode multibyte chars as entities for broken Konqueror + $text = utf8_tohtml($text); + + // output print $text; } diff --git a/lib/scripts/spellcheck.js b/lib/scripts/spellcheck.js index b5fa35e60..03ca9c364 100644 --- a/lib/scripts/spellcheck.js +++ b/lib/scripts/spellcheck.js @@ -262,7 +262,7 @@ function ajax_spell_class(){ }else{ for(var i=1; i<args.length; i++){ text += '<a href="javascript:ajax_spell.correct('+id+',\''+ - qquote(encodeURIComponent(args[i]))+'\')">'; + qquote(args[i])+'\')">'; text += args[i]; text += '</a><br />'; } @@ -290,6 +290,11 @@ function ajax_spell_class(){ data = data.substring(1); if(error == '1'){ ajax_spell.setState('stop'); + // convert numeric entities back to UTF-8 + data = data.replace(/&#(\d+);/g, + function(whole,match1) { + return String.fromCharCode(+match1); + }); // replace textbox through div ajax_spell.showboxObj.innerHTML = data; ajax_spell.showboxObj.style.width = ajax_spell.textboxObj.style.width; @@ -315,6 +320,15 @@ function ajax_spell_class(){ */ this.stop = function(){ var data = this.response; + + // convert numeric entities back to UTF-8 + data = data.replace(/&#(\d+);/g, + function(whole,match1) { + return String.fromCharCode(+match1); + }); + // now remove & protection + data = data.replace(/&/g,'&'); + ajax_spell.setState('start'); // replace div with textbox again ajax_spell.textboxObj.value = data; |