aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorandi <andi@splitbrain.org>2005-06-14 19:40:04 +0200
committerandi <andi@splitbrain.org>2005-06-14 19:40:04 +0200
commitc66822c0a36e3dbb262ce249f99d04a64ce0476f (patch)
treea25cd416640629cd1d5c94ab357dfe955fa84042
parent21777a2bc74163743daaa35c93a1364e269d46c7 (diff)
downloaddokuwiki-c66822c0a36e3dbb262ce249f99d04a64ce0476f.tar.gz
dokuwiki-c66822c0a36e3dbb262ce249f99d04a64ce0476f.zip
Spellchecker: use UTF-8 workaround only if needed
The spellchecker now tests the browsers UTF-8 compliance and only uses the entitiy encoding if needed. This hopefully fixes problems with Safari. darcs-hash:20050614174004-9977f-9c64abc8c31c47e89ac01784eae81f766bc54af0.gz
-rw-r--r--lib/exe/spellcheck.php38
-rw-r--r--lib/scripts/spellcheck.js73
2 files changed, 75 insertions, 36 deletions
diff --git a/lib/exe/spellcheck.php b/lib/exe/spellcheck.php
index 7147673b7..86e0e438e 100644
--- a/lib/exe/spellcheck.php
+++ b/lib/exe/spellcheck.php
@@ -105,12 +105,6 @@ function spell_check() {
// we need the text as array later
$data = explode("\n",$string);
-
- // keep some words from being checked (use blanks to preserve the offset)
- // FIXME doesn't work yet... however with the used html mode of aspell this isn't really needed
-/* $string = preg_replace('!<\?(code|del|file)( \+)?>!e','spellclean(\\1)',$string); */
-// $string = preg_replace('!()!e','spellclean(\\1)',$string);
-
// don't check links and medialinks for spelling errors
$string = preg_replace('/\{\{[^\|]*\|?(.*)\}\}/e','spaceslink("\\0","\\1")',$string);
$string = preg_replace('/\[\[[^\|]*\|?(.*)\]\]/e','spaceslink("\\0","\\1")',$string);
@@ -173,8 +167,10 @@ function spell_check() {
$string = '0'.join('<br />',$data);
}
- // encode multibyte chars as entities for broken Konqueror
- $string = utf8_tohtml($string);
+ if(!$_POST['utf8']){
+ // encode multibyte chars as entities for broken Konqueror
+ $string = utf8_tohtml($string);
+ }
//output
print $string;
@@ -193,8 +189,11 @@ function spell_formatword($word,$suggestions=null){
$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);
+ if(!$_POST['utf8']){
+ //konqueror's broken UTF-8 handling needs this
+ $suggestions = array_map('utf8_tohtml',$suggestions);
+ }
+
$suggestions = array_map('addslashes',$suggestions);
$sug = ",'".join("','",$suggestions)."'"; // build javascript args
@@ -226,17 +225,26 @@ function spell_resume(){
// restore quoted special chars
$text = unhtmlspecialchars($text);
- // but protect '&' (gets removed in JS later)
- $text = str_replace('&','&amp;',$text);
- // encode multibyte chars as entities for broken Konqueror
- $text = utf8_tohtml($text);
-
+ // check if UTF-8 is accepted
+ if(!$_POST['utf8']){
+ // protect '&' (gets removed in JS later)
+ $text = str_replace('&','&amp;',$text);
+ // encode multibyte chars as entities for broken Konqueror
+ $text = utf8_tohtml($text);
+ }
// output
print $text;
}
/**
+ * Just send data back as received for UTF-8 testing
+ */
+function spell_utf8test(){
+ print $_POST['data'];
+}
+
+/**
* Reverse htmlspecialchars
*
* @author <donwilson at gmail dot com>
diff --git a/lib/scripts/spellcheck.js b/lib/scripts/spellcheck.js
index 3df9a25ff..9c33f12c8 100644
--- a/lib/scripts/spellcheck.js
+++ b/lib/scripts/spellcheck.js
@@ -124,6 +124,7 @@ function spellButton(imageFile, speedTip, funcCall, accessKey) {
*/
function ajax_spell_class(){
this.inited = false;
+ this.utf8ok = 1;
this.handler = DOKU_BASE+'lib/exe/spellcheck.php';
// to hold the page objects (initialized with init())
this.textboxObj = null;
@@ -153,7 +154,7 @@ function ajax_spell_class(){
this.inited = true;
// check for AJAX availability
- var ajax = new sack();
+ var ajax = new sack(this.handler);
if(ajax.failed) return;
// get Elements
@@ -171,13 +172,11 @@ function ajax_spell_class(){
this.txtNoSug = txtNoSug;
this.txtChange= txtChange;
- // register click event
- document.onclick = this.docClick;
-
- // register focus event
- this.textboxObj.onfocus = this.setState;
-
- this.setState('start');
+ // start UTF-8 compliance test - send an UTF-8 char and see what comes back
+ ajax.AjaxFailedAlert = '';
+ ajax.encodeURIString = false;
+ ajax.onCompletion = this.initReady;
+ ajax.runAJAX('call=utf8test&data='+encodeURIComponent('ü'));
}
/**
@@ -298,6 +297,30 @@ function ajax_spell_class(){
// --- Callbacks ---
/**
+ * Callback. Called after the object was initialized and UTF-8 tested
+ * Inside the callback 'this' is the SACK object!!
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+ this.initReady = function(){
+ var data = this.response;
+
+ //test for UTF-8 compliance (will fail for konqueror)
+ if(data != 'ü'){
+ ajax_spell.utf8ok = 0;
+ }
+
+ // register click event
+ document.onclick = ajax_spell.docClick;
+
+ // register focus event
+ ajax_spell.textboxObj.onfocus = ajax_spell.setState;
+
+ // get started
+ ajax_spell.setState('start');
+ }
+
+ /**
* Callback. Called after finishing spellcheck.
* Inside the callback 'this' is the SACK object!!
*
@@ -309,11 +332,15 @@ 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);
- });
+
+ // convert numeric entities back to UTF-8 if needed
+ if(!ajax_spell.utf8ok){
+ 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;
@@ -340,15 +367,16 @@ function ajax_spell_class(){
this.stop = function(){
var data = this.response;
- // convert numeric entities back to UTF-8
- data = data.replace(/&#(\d+);/g,
+ // convert numeric entities back to UTF-8 if needed
+ if(!ajax_spell.utf8ok){
+ data = data.replace(/&#(\d+);/g,
function(whole,match1) {
return String.fromCharCode(+match1);
- });
- // now remove &amp; protection
- data = data.replace(/&amp;/g,'&');
+ });
+ // now remove &amp; protection
+ data = data.replace(/&amp;/g,'&');
+ }
- ajax_spell.setState('start');
// replace div with textbox again
ajax_spell.textboxObj.value = data;
ajax_spell.textboxObj.disabled = false;
@@ -356,6 +384,7 @@ function ajax_spell_class(){
ajax_spell.textboxObj.style.display = 'block';
ajax_spell.editbarObj.style.visibility = 'visible';
ajax_spell.showboxObj.innerHTML = '';
+ ajax_spell.setState('start');
}
// --- Callers ---
@@ -372,7 +401,8 @@ function ajax_spell_class(){
ajax.AjaxFailedAlert = '';
ajax.encodeURIString = false;
ajax.onCompletion = this.start;
- ajax.runAJAX('call=check&data='+encodeURIComponent(this.textboxObj.value));
+ ajax.runAJAX('call=check&utf8='+ajax_spell.utf8ok+
+ '&data='+encodeURIComponent(this.textboxObj.value));
}
/**
@@ -388,7 +418,8 @@ function ajax_spell_class(){
ajax.AjaxFailedAlert = '';
ajax.encodeURIString = false;
ajax.onCompletion = this.stop;
- ajax.runAJAX('call=resume&data='+encodeURIComponent(text));
+ ajax.runAJAX('call=resume&utf8='+ajax_spell.utf8ok+
+ '&data='+encodeURIComponent(text));
}
}