From 78a6aeb15ad85c8be4a7e39307b7d9aa0512742c Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 29 Oct 2005 20:52:22 +0200 Subject: More work on Javascript and CSS dispatchers darcs-hash:20051029185222-7ad00-c184ab3496539f3027407c7d17e8770a1849546a.gz --- _test/cases/lib/exe/js_js_compress.test.php | 68 +++++ _test/cases/lib/exe/jscss_js_compress.test.php | 68 ----- conf/dokuwiki.php | 1 + inc/init.php | 4 + inc/pluginutils.php | 2 + inc/template.php | 94 +------ inc/toolbar.php | 37 +-- lib/exe/css.php | 165 ++++++++++++ lib/exe/js.php | 331 +++++++++++++++++++++++++ lib/exe/jscss.php | 293 ---------------------- lib/scripts/script.js | 50 ++-- lib/tpl/default/detail.php | 10 - lib/tpl/default/main.php | 16 -- lib/tpl/default/media.php | 11 +- lib/tpl/default/mediaedit.php | 2 - lib/tpl/default/mediaref.php | 11 +- 16 files changed, 630 insertions(+), 533 deletions(-) create mode 100644 _test/cases/lib/exe/js_js_compress.test.php delete mode 100644 _test/cases/lib/exe/jscss_js_compress.test.php create mode 100644 lib/exe/css.php create mode 100644 lib/exe/js.php delete mode 100644 lib/exe/jscss.php diff --git a/_test/cases/lib/exe/js_js_compress.test.php b/_test/cases/lib/exe/js_js_compress.test.php new file mode 100644 index 000000000..0bfb62056 --- /dev/null +++ b/_test/cases/lib/exe/js_js_compress.test.php @@ -0,0 +1,68 @@ +assertEqual(js_compress($text), ''); + } + + function test_mlcom2(){ + $text = 'var foo=6;/* another comment */'; + $this->assertEqual(js_compress($text), 'var foo=6;'); + } + + function test_slcom1(){ + $text = '// an comment'; + $this->assertEqual(js_compress($text), ''); + } + + function test_slcom2(){ + $text = 'var foo=6;// another comment '; + $this->assertEqual(js_compress($text), 'var foo=6;'); + } + + function test_slcom3(){ + $text = 'var foo=6;// another comment / or something with // comments '; + $this->assertEqual(js_compress($text), 'var foo=6;'); + } + + function test_regex1(){ + $text = 'foo.split( /[a-Z\/]*/ );'; + $this->assertEqual(js_compress($text), 'foo.split(/[a-Z\/]*/);'); + } + + function test_dquot1(){ + $text = 'var foo="Now what \'do we//get /*here*/ ?";'; + $this->assertEqual(js_compress($text), $text); + } + + function test_squot1(){ + $text = "var foo='Now what \"do we//get /*here*/ ?';"; + $this->assertEqual(js_compress($text), $text); + } + + function test_nl1(){ + $text = "var foo=6;\nvar baz=7;"; + $this->assertEqual(js_compress($text), 'var foo=6;var baz=7;'); + } + + function test_lws1(){ + $text = " \t var foo=6;"; + $this->assertEqual(js_compress($text), 'var foo=6;'); + } + + function test_tws1(){ + $text = "var foo=6; \t "; + $this->assertEqual(js_compress($text), 'var foo=6;'); + } +} + +//Setup VIM: ex: et ts=4 enc=utf-8 : diff --git a/_test/cases/lib/exe/jscss_js_compress.test.php b/_test/cases/lib/exe/jscss_js_compress.test.php deleted file mode 100644 index 3d9a8b627..000000000 --- a/_test/cases/lib/exe/jscss_js_compress.test.php +++ /dev/null @@ -1,68 +0,0 @@ -assertEqual(js_compress($text), ''); - } - - function test_mlcom2(){ - $text = 'var foo=6;/* another comment */'; - $this->assertEqual(js_compress($text), 'var foo=6;'); - } - - function test_slcom1(){ - $text = '// an comment'; - $this->assertEqual(js_compress($text), ''); - } - - function test_slcom2(){ - $text = 'var foo=6;// another comment '; - $this->assertEqual(js_compress($text), 'var foo=6;'); - } - - function test_slcom3(){ - $text = 'var foo=6;// another comment / or something with // comments '; - $this->assertEqual(js_compress($text), 'var foo=6;'); - } - - function test_regex1(){ - $text = 'foo.split( /[a-Z\/]*/ );'; - $this->assertEqual(js_compress($text), 'foo.split(/[a-Z\/]*/);'); - } - - function test_dquot1(){ - $text = 'var foo="Now what \'do we//get /*here*/ ?";'; - $this->assertEqual(js_compress($text), $text); - } - - function test_squot1(){ - $text = "var foo='Now what \"do we//get /*here*/ ?';"; - $this->assertEqual(js_compress($text), $text); - } - - function test_nl1(){ - $text = "var foo=6;\nvar baz=7;"; - $this->assertEqual(js_compress($text), 'var foo=6;var baz=7;'); - } - - function test_lws1(){ - $text = " \t var foo=6;"; - $this->assertEqual(js_compress($text), 'var foo=6;'); - } - - function test_tws1(){ - $text = "var foo=6; \t "; - $this->assertEqual(js_compress($text), 'var foo=6;'); - } -} - -//Setup VIM: ex: et ts=4 enc=utf-8 : diff --git a/conf/dokuwiki.php b/conf/dokuwiki.php index d12fb27fc..2d89d1cde 100644 --- a/conf/dokuwiki.php +++ b/conf/dokuwiki.php @@ -78,6 +78,7 @@ $conf['im_convert'] = ''; //path to ImageMagicks convert (will be $conf['spellchecker']= 0; //enable Spellchecker (needs PHP >= 4.3.0 and aspell installed) $conf['subscribers'] = 0; //enable change notice subscription support $conf['pluginmanager'] = 0; //enable automated plugin management (requires plugin) +$conf['compress'] = 1; //Strip whitespaces and comments from Styles and JavaScript? 1|0 $conf['rss_type'] = 'rss1'; //type of RSS feed to provide, by default: // 'rss' - RSS 0.91 // 'rss1' - RSS 1.0 diff --git a/inc/init.php b/inc/init.php index 41363f63d..44154a039 100644 --- a/inc/init.php +++ b/inc/init.php @@ -42,6 +42,10 @@ if(!defined('DOKU_TPL')) define('DOKU_TPL', DOKU_BASE.'lib/tpl/'.$conf['template'].'/'); + // define real Template directory + if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC', + DOKU_INC.'lib/tpl/'.$conf['template'].'/'); + // make session rewrites XHTML compliant @ini_set('arg_separator.output', '&'); diff --git a/inc/pluginutils.php b/inc/pluginutils.php index d35b0dbc2..4c81f9abb 100644 --- a/inc/pluginutils.php +++ b/inc/pluginutils.php @@ -8,6 +8,8 @@ /** * prints needed HTML to include plugin CSS and JS files + * + * @deprecated - now handled by the style and script loader in lib/exe */ function plugin_printCSSJS(){ global $conf; diff --git a/inc/template.php b/inc/template.php index 7401c3e62..be0d2f193 100644 --- a/inc/template.php +++ b/inc/template.php @@ -176,7 +176,6 @@ function tpl_metaheaders(){ ptln('',$it); ptln('',$it); ptln('',$it); - ptln('',$it); // setup robot tags apropriate for different modes if( ($ACT=='show' || $ACT=='export_html') && !$REV){ @@ -195,97 +194,16 @@ function tpl_metaheaders(){ ptln('',$it); } -/* - - // include some JavaScript language strings #FIXME still needed? - ptln('',$it); - - // load the default JavaScript files - ptln('',$it); - ptln('',$it); - ptln('',$it); - ptln('',$it); - - - // dom tool tip library, for insitu footnotes - ptln('',$it); - ptln('',$it); - - ptln('',$it); - - // editing functions - if($ACT=='edit' || $ACT=='preview'){ - // add size control - ptln('',$it); - - if($INFO['writable']){ - // load toolbar functions - ptln('',$it); - - // load spellchecker functions if wanted - if($conf['spellchecker']){ - ptln('',$it+2); - } - - ptln('',$it); - } - } -*/ + // load stylesheets + ptln('',$it); + ptln('',$it); + // load javascript $js_edit = ($ACT=='edit' || $ACT=='preview') ? 1 : 0; $js_write = ($INFO['writable']) ? 1 : 0; - + $js_sig = ($conf['useacl'] && $_SERVER['REMOTE_USER']) ? 1 : 0; ptln('',$it); - - - // plugin stylesheets and Scripts - plugin_printCSSJS(); + DOKU_BASE.'lib/exe/js.php?edit='.$js_edit.'&write='.$js_write.'&sig='.$js_sig.'">',$it); } /** diff --git a/inc/toolbar.php b/inc/toolbar.php index aa52868d0..27e91ee47 100644 --- a/inc/toolbar.php +++ b/inc/toolbar.php @@ -164,17 +164,6 @@ function toolbar_JSdefines($varname){ ), ); - // if logged in add sig button - if($conf['useacl'] && $_SERVER['REMOTE_USER']){ - $menu[] = array( - 'type' => 'insert', - 'title' => $lang['qb_sig'], - 'icon' => 'sig.png', - 'key' => 'y', - 'insert' => toolbar_signature(), - ); - } - // use JSON to build the JavaScript array $json = new JSON(); print "var $varname = ".$json->encode($menu).";\n"; @@ -187,16 +176,36 @@ function toolbar_JSdefines($varname){ */ function toolbar_signature(){ global $conf; - global $INFO; $sig = $conf['signature']; $sig = strftime($sig); $sig = str_replace('@USER@',$_SERVER['REMOTE_USER'],$sig); - $sig = str_replace('@NAME@',$INFO['userinfo']['name'],$sig); - $sig = str_replace('@MAIL@',$INFO['userinfo']['mail'],$sig); + $sig = str_replace('@NAME@',$_SESSION[$conf[title]]['auth']['info']['name'],$sig); + $sig = str_replace('@MAIL@',$_SESSION[$conf[title]]['auth']['info']['mail'],$sig); $sig = str_replace('@DATE@',date($conf['dformat']),$sig); return $sig; } +/** + * Adds the signature button to the already prepared Javascript array + * @param string $varname Name of the JS variable to fill + * @author Andreas Gohr + */ +function toolbar_addsigbutton($varname){ + global $lang; + + $menu = array( + 'type' => 'insert', + 'title' => $lang['qb_sig'], + 'icon' => 'sig.png', + 'key' => 'y', + 'insert' => toolbar_signature(), + ); + // use JSON to build the JavaScript array + $json = new JSON(); + print $varname.'['.$varname.'.length] = '.$json->encode($menu).";\n"; + +} + //Setup VIM: ex: et ts=4 enc=utf-8 : diff --git a/lib/exe/css.php b/lib/exe/css.php new file mode 100644 index 000000000..7b6523b41 --- /dev/null +++ b/lib/exe/css.php @@ -0,0 +1,165 @@ + + */ + +if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); +define('NOSESSION',true); // we do not use a session or authentication here (better caching) +require_once(DOKU_INC.'inc/init.php'); +require_once(DOKU_INC.'inc/pageutils.php'); +require_once(DOKU_INC.'inc/io.php'); + +// Main (don't run when UNIT test) +if(!defined('SIMPLE_TEST')){ + header('Content-Type: text/css; charset=utf-8'); + css_out(); +} + + +// ---------------------- functions ------------------------------ + +/** + * Output all needed Styles + * + * @author Andreas Gohr + */ +function css_out(){ + global $conf; + global $lang; + $print = (bool) $_REQUEST['print']; //print mode? + + // The generated script depends on some dynamic options + $cache = getCacheName('styles'.$print,'.css'); + + // Array of needed files and their web locations, the latter ones + // are needed to fix relative paths in the stylesheets + $files = array(); + if($print){ + $files[DOKU_TPLINC.'print.css'] = DOKU_TPL; + // load plugin styles + $files = array_merge($files, css_pluginstyles('print')); + $files[DOKU_CONF.'userprint.css'] = ''; + }else{ + $files[DOKU_INC.'lib/styles/style.css'] = DOKU_BASE.'lib/styles/'; + //fixme spellchecker style + $files[DOKU_TPLINC.'layout.css'] = DOKU_TPL; + $files[DOKU_TPLINC.'design.css'] = DOKU_TPL; + if($lang['direction'] == 'rtl'){ + $files[DOKU_TPLINC.'rtl.css'] = DOKU_TPL; + } + // load plugin styles + $files = array_merge($files, css_pluginstyles('screen')); + $files[DOKU_CONF.'userstyle.css'] = ''; + } + + // check cache age + if(css_cacheok($cache,array_keys($files))){ + readfile($cache); + return; + } + + // start output buffering and build the stylesheet + ob_start(); + + // load files + foreach($files as $file => $location){ + print css_loadfile($file, $location); + } + + // end output buffering and get contents + $css = ob_get_contents(); + ob_end_clean(); + + // compress whitespace and comments + if($conf['compress']){ + $css = css_compress($css); + } + + // save cache file + io_saveFile($cache,$css); + + // finally send output + print $css; +} + +/** + * Checks if a CSS Cache file still is valid + * + * @author Andreas Gohr + */ +function css_cacheok($cache,$files){ + $ctime = @filemtime($cache); + if(!$ctime) return false; //There is no cache + + // some additional files to check + $files[] = DOKU_CONF.'dokuwiki.conf'; + $files[] = DOKU_CONF.'local.conf'; + $files[] = __FILE__; + + // now walk the files + foreach($files as $file){ + if(@filemtime($file) > $ctime){ + return false; + } + } + return true; +} + +/** + * Loads a given file and fixes relative URLs with the + * given location prefix + */ +function css_loadfile($file,$location=''){ + if(!@file_exists($file)) return ''; + $css = io_readFile($file); + if(!$location) return $css; + + $css = preg_replace('!(url\( *)([^/])!','\\1'.$location.'\\2',$css); + return $css; +} + +/** + * Returns a list of possible Plugin Styles (no existance check here) + * + * @author Andreas Gohr + */ +function css_pluginstyles($mode='screen'){ + $list = array(); + $plugins = plugin_list(); + foreach ($plugins as $p){ + if($mode == 'print'){ + $list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/"; + }else{ + $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/"; + $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/"; + } + } + return $list; +} + +/** + * Very simple CSS optimizer + * + * @author Andreas Gohr + */ +function css_compress($css){ + // strip whitespaces + $css = preg_replace('![\r\n\t ]+!',' ',$css); + $css = preg_replace('/ ?([:;,{}]) ?/','\\1',$css); + + // strip comments (ungreedy) + // We keep very small comments to maintain typical browser hacks + $css = preg_replace('!(/\*)(.{4,})(\*/)!U','',$css); + + // shorten colors + $css = preg_replace("/#([0-9a-fA-F]{1})\\1([0-9a-fA-F]{1})\\2([0-9a-fA-F]{1})\\3/", "#\\1\\2\\3",$css); + + return $css; +} + + +//Setup VIM: ex: et ts=4 enc=utf-8 : +?> diff --git a/lib/exe/js.php b/lib/exe/js.php new file mode 100644 index 000000000..9708dbc0d --- /dev/null +++ b/lib/exe/js.php @@ -0,0 +1,331 @@ + + */ + +if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); +define('NOSESSION',true); // we do not use a session or authentication here (better caching) +require_once(DOKU_INC.'inc/init.php'); +require_once(DOKU_INC.'inc/pageutils.php'); +require_once(DOKU_INC.'inc/io.php'); + +// Main (don't run when UNIT test) +if(!defined('SIMPLE_TEST')){ + header('Content-Type: text/javascript; charset=utf-8'); + js_out(); + js_dynamicout(); +} + + +// ---------------------- functions ------------------------------ + +/** + * Output all needed JavaScript + * + * @author Andreas Gohr + */ +function js_out(){ + global $conf; + global $lang; + $edit = (bool) $_REQUEST['edit']; // edit or preview mode? + $write = (bool) $_REQUEST['write']; // writable? + + // The generated script depends on some dynamic options + $cache = getCacheName('scripts'.$edit.$write,'.js'); + + // Array of needed files + $files = array( + DOKU_INC.'lib/scripts/events.js', + DOKU_INC.'lib/scripts/script.js', + DOKU_INC.'lib/scripts/tw-sack.js', + DOKU_INC.'lib/scripts/ajax.js', + DOKU_INC.'lib/scripts/domLib.js', + DOKU_INC.'lib/scripts/domTT.js', + ); + if($edit && $write){ + $files[] = DOKU_INC.'lib/scripts/edit.js'; + if($conf['spellchecker']){ + $files[] = DOKU_INC.'lib/scripts/spellcheck.js'; + } + } + + // get possible plugin scripts + $plugins = js_pluginscripts(); + + // check cache age here + if(js_cacheok($cache,array_merge($files,$plugins))){ + readfile($cache); + return; + } + + // start output buffering and build the script + ob_start(); + + // add some translation strings and global variables + print "var alertText = '".str_replace('\\\\n','\\n',addslashes($lang['qb_alert']))."';"; + print "var notSavedYet = '".str_replace('\\\\n','\\n',addslashes($lang['notsavedyet']))."';"; + print "var DOKU_BASE = '".DOKU_BASE."';"; + + // load files + foreach($files as $file){ + readfile($file); + } + + // init stuff + js_runonstart("ajax_qsearch.init('qsearch_in','qsearch_out')"); + js_runonstart("addEvent(document,'click',closePopups)"); + + if($edit){ + // size controls + js_runonstart("initSizeCtl('sizectl','wikitext')"); + + if($write){ + require_once(DOKU_INC.'inc/toolbar.php'); + toolbar_JSdefines('toolbar'); + js_runonstart("initToolbar('toolbar','wikitext',toolbar)"); + + // add pageleave check + js_runonstart("initChangeCheck('".js_escape($lang['notsavedyet'])."')"); + + // add lock timer + js_runonstart("init_locktimer(".($conf['locktime']-60).",'".js_escape($lang['willexpire'])."')"); + + // load spell checker + if($conf['spellchecker']){ + js_runonstart("ajax_spell.init('". + js_escape($lang['spell_start'])."','". + js_escape($lang['spell_stop'])."','". + js_escape($lang['spell_wait'])."','". + js_escape($lang['spell_noerr'])."','". + js_escape($lang['spell_nosug'])."','". + js_escape($lang['spell_change'])."')"); + } + } + } + + // load plugin scripts (suppress warnings for missing ones) + foreach($plugins as $plugin){ + @readfile($plugin); + } + + // load user script + @readfile(DOKU_CONF.'userscript.js'); + + // end output buffering and get contents + $js = ob_get_contents(); + ob_end_clean(); + + // compress whitespace and comments + if($conf['compress']){ + $js = js_compress($js); + } + + // save cache file + io_saveFile($cache,$js); + + // finally send output + print $js; +} + +/** + * Adds some dynamic JavaScript using the readonly Session + * + * @author Andreas Gohr + */ +function js_dynamicout(){ + $edit = (bool) $_REQUEST['edit']; // edit or preview mode? + $write = (bool) $_REQUEST['write']; // writable? + $sig = (bool) $_REQUEST['sig']; // show sig button? + if($edit && $write && $sig){ + require_once(DOKU_INC.'inc/toolbar.php'); + toolbar_addsigbutton('toolbar'); + } +} + +/** + * Checks if a JavaScript Cache file still is valid + * + * @author Andreas Gohr + */ +function js_cacheok($cache,$files){ + $ctime = @filemtime($cache); + if(!$ctime) return false; //There is no cache + + // some additional files to check + $files[] = DOKU_CONF.'dokuwiki.conf'; + $files[] = DOKU_CONF.'local.conf'; + $files[] = DOKU_CONF.'userscript.js'; + $files[] = __FILE__; + + // now walk the files + foreach($files as $file){ + if(@filemtime($file) > $ctime){ + return false; + } + } + return true; +} + +/** + * Returns a list of possible Plugin Scripts (no existance check here) + * + * @author Andreas Gohr + */ +function js_pluginscripts(){ + $list = array(); + $plugins = plugin_list(); + foreach ($plugins as $p){ + $list[] = DOKU_PLUGIN."$p/script.js"; + } + return $list; +} + +/** + * Escapes a String to be embedded in a JavaScript call, keeps \n + * as newline + * + * @author Andreas Gohr + */ +function js_escape($string){ + return str_replace('\\\\n','\\n',addslashes($string)); +} + +/** + * Adds the given JavaScript code to the window.onload() event + * + * @author Andreas Gohr + */ +function js_runonstart($func){ + print "addEvent(window,'load',function(){ $func; });"; +} + +/** + * Strip comments and whitespaces from given JavaScript Code + * + * This is a rewrite of Nick Galbreaths python tool jsstrip.py which is + * released under BSD license. See link for original code. + * + * @author Nick Galbreath + * @author Andreas Gohr + * @link http://modp.com/release/jsstrip/ + */ +function js_compress($s){ + $i = 0; + $line = 0; + $s .= "\n"; + $len = strlen($s); + + // items that don't need spaces next to them + $chars = '^&|!+\-*\/%=:;,{}()<>% \t\n\r'; + + ob_start(); + while($i < $len){ + $ch = $s{$i}; + + // multiline comments + if($ch == '/' && $s{$i+1} == '*'){ + $endC = strpos($s,'*/',$i+2); + if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR); + $i = $endC + 2; + continue; + } + + // singleline + if($ch == '/' && $s{$i+1} == '/'){ + $endC = strpos($s,"\n",$i+2); + if($endC === false) trigger_error('Invalid comment', E_USER_ERROR); + $i = $endC; + continue; + } + + // tricky. might be an RE + if($ch == '/'){ + // rewind, skip white space + $j = 1; + while($s{$i-$j} == ' '){ + $j = $j + 1; + } + if( ($s{$i-$j} == '=') || ($s{$i-$j} == '(') ){ + // yes, this is an re + // now move forward and find the end of it + $j = 1; + while($s{$i+$j} != '/'){ + while( ($s{$i+$j} != '\\') && ($s{$i+$j} != '/')){ + $j = $j + 1; + } + if($s{$i+$j} == '\\') $j = $j + 2; + } + echo substr($s,$i,$j+1); + $i = $i + $j + 1; + continue; + } + } + + // double quote strings + if($ch == '"'){ + $j = 1; + while( $s{$i+$j} != '"' ){ + while( ($s{$i+$j} != '\\') && ($s{$i+$j} != '"') ){ + $j = $j + 1; + } + if($s{$i+$j} == '\\') $j = $j + 2; + } + echo substr($s,$i,$j+1); + $i = $i + $j + 1; + continue; + } + + // single quote strings + if($ch == "'"){ + $j = 1; + while( $s{$i+$j} != "'" ){ + while( ($s{$i+$j} != '\\') && ($s{$i+$j} != "'") ){ + $j = $j + 1; + } + if ($s{$i+$j} == '\\') $j = $j + 2; + } + echo substr($s,$i,$j+1); + $i = $i + $j + 1; + continue; + } + + // newlines + if($ch == "\n" || $ch == "\r"){ + $i = $i+1; + continue; + } + + // leading spaces + if( ( $ch == ' ' || + $ch == "\n" || + $ch == "\t" ) && + !preg_match('/['.$chars.']/',$s{$i+1}) ){ + $i = $i+1; + continue; + } + + // trailing spaces + if( ( $ch == ' ' || + $ch == "\n" || + $ch == "\t" ) && + !preg_match('/['.$chars.']/',$s{$i-1}) ){ + $i = $i+1; + continue; + } + + // other chars + echo $ch; + $i = $i + 1; + } + + + $out = ob_get_contents(); + ob_end_clean(); + return $out; +} + +//Setup VIM: ex: et ts=4 enc=utf-8 : +?> diff --git a/lib/exe/jscss.php b/lib/exe/jscss.php deleted file mode 100644 index 33d67eece..000000000 --- a/lib/exe/jscss.php +++ /dev/null @@ -1,293 +0,0 @@ - - */ - -if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); -define('NOSESSION',true); // we do not use a session or authentication here (better caching) -require_once(DOKU_INC.'inc/init.php'); -require_once(DOKU_INC.'inc/pageutils.php'); -require_once(DOKU_INC.'inc/io.php'); - -// Main (don't run when UNIT test) -if(!defined('SIMPLE_TEST')){ - if($_REQUEST['type'] == 'css'){ - css_out(); - }else{ - header('Content-Type: text/javascript; charset=utf-8'); - js_out(); - } -} - - -// ---------------------- functions ------------------------------ - -/** - * Output all needed JavaScript - * - * @todo Add Whitespace and Comment Compression - * @author Andreas Gohr - */ -function js_out(){ - global $conf; - global $lang; - $edit = (bool) $_REQUEST['edit']; // edit or preview mode? - $write = (bool) $_REQUEST['write']; // writable? - - // The generated script depends on some dynamic options - $cache = getCacheName($conf['lang'].$edit.$write,$ext='.js'); - - // Array of needed files - $files = array( - DOKU_INC.'lib/scripts/events.js', - DOKU_INC.'lib/scripts/script.js', - DOKU_INC.'lib/scripts/tw-sack.js', - DOKU_INC.'lib/scripts/ajax.js', - DOKU_INC.'lib/scripts/domLib.js', - DOKU_INC.'lib/scripts/domTT.js', - ); - if($edit && $write){ - $files[] = DOKU_INC.'lib/scripts/edit.js'; - if($conf['spellchecker']){ - $files[] = DOKU_INC.'lib/scripts/spellcheck.js'; - } - } - - // FIXME load plugin scripts - - // check cache age here - if(js_cacheok($cache,$files)){ - readfile($cache); - return; - } - - // start output buffering and build the script - ob_start(); - - // add some translation strings and global variables - print "var alertText = '".str_replace('\\\\n','\\n',addslashes($lang['qb_alert']))."';"; - print "var notSavedYet = '".str_replace('\\\\n','\\n',addslashes($lang['notsavedyet']))."';"; - print "var DOKU_BASE = '".DOKU_BASE."';"; - - // load files - foreach($files as $file){ - readfile($file); - } - - // init stuff - js_runonstart("ajax_qsearch.init('qsearch_in','qsearch_out')"); - js_runonstart("addEvent(document,'click',closePopups)"); - - if($edit){ - // size controls - js_runonstart("initSizeCtl('sizectl','wikitext')"); - - if($write){ - require_once(DOKU_INC.'inc/toolbar.php'); - toolbar_JSdefines('toolbar'); - js_runonstart("initToolbar('toolbar','wikitext',toolbar)"); - - // add pageleave check - js_runonstart("initChangeCheck('".js_escape($lang['notsavedyet'])."')"); - - // add lock timer - js_runonstart("init_locktimer(".($conf['locktime']-60).",'".js_escape($lang['willexpire'])."')"); - - // load spell checker - if($conf['spellchecker']){ - js_runonstart("ajax_spell.init('". - js_escape($lang['spell_start'])."','". - js_escape($lang['spell_stop'])."','". - js_escape($lang['spell_wait'])."','". - js_escape($lang['spell_noerr'])."','". - js_escape($lang['spell_nosug'])."','". - js_escape($lang['spell_change'])."')"); - } - } - } - - - // load user script - if(@file_exists(DOKU_INC.'conf/userscript.js')){ - readfile(DOKU_INC.'conf/userscript.js'); - } - - // end output buffering and get contents - $js = ob_get_contents(); - ob_end_clean(); - - // compress whitespace and comments - $js = js_compress($js); - - // save cache file - io_saveFile($cache,$js); - - // finally send output - print $js; -} - -/** - * Checks if a JavaScript Cache file still is valid - * - * @author Andreas Gohr - */ -function js_cacheok($cache,$files){ - $ctime = @filemtime($cache); - if(!$ctime) return false; //There is no cache - - // some additional files to check - $files[] = DOKU_INC.'conf/dokuwiki.conf'; - $files[] = DOKU_INC.'conf/local.conf'; - $files[] = DOKU_INC.'conf/userscript.js'; - - // now walk the files - foreach($files as $file){ - if(@filemtime($file) > $ctime){ - return false; - } - } - return true; -} - -/** - * Escapes a String to be embedded in a JavaScript call, keeps \n - * as newline - * - * @author Andreas Gohr - */ -function js_escape($string){ - return str_replace('\\\\n','\\n',addslashes($string)); -} - -/** - * Adds the given JavaScript code to the window.onload() event - * - * @author Andreas Gohr - */ -function js_runonstart($func){ - print "addEvent(window,'load',function(){ $func; });"; -} - -//http://modp.com/release/jsstrip/jsstrip.py -function js_compress($s){ - $i = 0; - $line = 0; - $s .= "\n"; - $len = strlen($s); - - // items that don't need spaces next to them - $chars = '^&|!+\-*\/%=:;,{}()<>% \t\n\r'; - - ob_start(); - while($i < $len){ - $ch = $s{$i}; - - // multiline comments - if($ch == '/' && $s{$i+1} == '*'){ - $endC = strpos($s,'*/',$i+2); - if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR); - $i = $endC + 2; - continue; - } - - // singleline - if($ch == '/' && $s{$i+1} == '/'){ - $endC = strpos($s,"\n",$i+2); - if($endC === false) trigger_error('Invalid comment', E_USER_ERROR); - $i = $endC; - continue; - } - - // tricky. might be an RE - if($ch == '/'){ - // rewind, skip white space - $j = 1; - while($s{$i-$j} == ' '){ - $j = $j + 1; - } - if( ($s{$i-$j} == '=') || ($s{$i-$j} == '(') ){ - // yes, this is an re - // now move forward and find the end of it - $j = 1; - while($s{$i+$j} != '/'){ - while( ($s{$i+$j} != '\\') && ($s{$i+$j} != '/')){ - $j = $j + 1; - } - if($s{$i+$j} == '\\') $j = $j + 2; - } - echo substr($s,$i,$j+1); - $i = $i + $j + 1; - continue; - } - } - - // double quote strings - if($ch == '"'){ - $j = 1; - while( $s{$i+$j} != '"' ){ - while( ($s{$i+$j} != '\\') && ($s{$i+$j} != '"') ){ - $j = $j + 1; - } - if($s{$i+$j} == '\\') $j = $j + 2; - } - echo substr($s,$i,$j+1); - $i = $i + $j + 1; - continue; - } - - // single quote strings - if($ch == "'"){ - $j = 1; - while( $s{$i+$j} != "'" ){ - while( ($s{$i+$j} != '\\') && ($s{$i+$j} != "'") ){ - $j = $j + 1; - } - if ($s{$i+$j} == '\\') $j = $j + 2; - } - echo substr($s,$i,$j+1); - $i = $i + $j + 1; - continue; - } - - // newlines - if($ch == "\n" || $ch == "\r"){ - $i = $i+1; - continue; - } - - // leading spaces - if( ( $ch == ' ' || - $ch == "\n" || - $ch == "\t" ) && - !preg_match('/['.$chars.']/',$s{$i+1}) ){ - $i = $i+1; - continue; - } - - // trailing spaces - if( ( $ch == ' ' || - $ch == "\n" || - $ch == "\t" ) && - !preg_match('/['.$chars.']/',$s{$i-1}) ){ - $i = $i+1; - continue; - } - - // other chars - echo $ch; - $i = $i + 1; - } - - - $out = ob_get_contents(); - ob_end_clean(); - return $out; -} - -//http://csstidy.sourceforge.net/download.php - -//Setup VIM: ex: et ts=4 enc=utf-8 : -?> diff --git a/lib/scripts/script.js b/lib/scripts/script.js index 180f2dcd5..b91859265 100644 --- a/lib/scripts/script.js +++ b/lib/scripts/script.js @@ -6,8 +6,8 @@ * Some browser detection */ var clientPC = navigator.userAgent.toLowerCase(); // Get client info -var is_gecko = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1) - && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1)); +var is_gecko = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1) && + (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1)); var is_safari = ((clientPC.indexOf('AppleWebKit')!=-1) && (clientPC.indexOf('spoofer')==-1)); var is_khtml = (navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled )); if (clientPC.indexOf('opera')!=-1) { @@ -23,10 +23,11 @@ if (clientPC.indexOf('opera')!=-1) { */ function findPosX(object){ var curleft = 0; + var obj; if(typeof(object) == 'object'){ - var obj = object; + obj = object; }else{ - var obj = document.getElementById(object); + obj = document.getElementById(object); } if (obj.offsetParent){ while (obj.offsetParent){ @@ -47,10 +48,11 @@ function findPosX(object){ */ function findPosY(object){ var curtop = 0; + var obj; if(typeof(object) == 'object'){ - var obj = object; + obj = object; }else{ - var obj = document.getElementById(object); + obj = document.getElementById(object); } if (obj.offsetParent){ while (obj.offsetParent){ @@ -72,7 +74,7 @@ function findPosY(object){ function jsEscape(text){ var re=new RegExp("\\\\","g"); text=text.replace(re,"\\\\"); - var re=new RegExp("'","g"); + re=new RegExp("'","g"); text=text.replace(re,"\\'"); re=new RegExp('"',"g"); text=text.replace(re,'"'); @@ -150,8 +152,8 @@ function showTocToggle(showtxt,hidetxt) { document.writeln(''); + '' + hide + '' + + ''); } } @@ -200,12 +202,14 @@ function getCookie(name) { var begin = dc.indexOf("; " + prefix); if (begin == -1) { begin = dc.indexOf(prefix); - if (begin != 0) return null; - } else + if (begin !== 0){ return null; } + } else { begin += 2; + } var end = document.cookie.indexOf(";", begin); - if (end == -1) + if (end == -1){ end = dc.length; + } return unescape(dc.substring(begin + prefix.length, end)); } @@ -217,8 +221,9 @@ function getCookie(name) { function fixDate(date) { var base = new Date(0); var skew = base.getTime(); - if (skew > 0) + if (skew > 0){ date.setTime(date.getTime() - skew); + } } /* @@ -258,16 +263,17 @@ function fnt(id, e, evt) { } // does the footnote tooltip already exist? - var fnt = document.getElementById('insitu-fn'+id); - if (!fnt) { + var fnote = document.getElementById('insitu-fn'+id); + var footnote; + if (!fnote) { // if not create it... // locate the footnote anchor element var a = document.getElementById( "fn"+id ); - if (!a) return; + if (!a){ return; } // anchor parent is the footnote container, get its innerHTML - var footnote = new String (a.parentNode.innerHTML); + footnote = new String (a.parentNode.innerHTML); // strip the leading footnote anchors and their comma separators footnote = footnote.replace(//gi, ''); @@ -276,7 +282,7 @@ function fnt(id, e, evt) { // prefix ids on any elements with "insitu-" to ensure they remain unique footnote = footnote.replace(/\bid=\"(.*?)\"/gi,'id="insitu-$1'); } else { - var footnote = new String(fnt.innerHTML); + footnote = new String(fnt.innerHTML); } // activate the tooltip @@ -289,13 +295,13 @@ function fnt(id, e, evt) { * Add the edit window size controls */ function initSizeCtl(ctlid,edid){ - if(!document.getElementById) return; + if(!document.getElementById){ return; } var ctl = document.getElementById(ctlid); var textarea = document.getElementById(edid); var hgt = getCookie('DokuWikisizeCtl'); - if(hgt == null){ + if(hgt === null || hgt === ''){ textarea.style.height = '300px'; }else{ textarea.style.height = hgt; @@ -319,7 +325,7 @@ function sizeCtl(edid,val){ var height = parseInt(textarea.style.height.substr(0,textarea.style.height.length-2)); height += val; textarea.style.height = height+'px'; - + var now = new Date(); fixDate(now); now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000); //expire in a year @@ -330,7 +336,7 @@ function sizeCtl(edid,val){ * Handler to close all open Popups */ function closePopups(){ - if(!document.getElementById) return; + if(!document.getElementById){ return; } var divs = document.getElementsByTagName('div'); for(var i=0; i < divs.length; i++){ diff --git a/lib/tpl/default/detail.php b/lib/tpl/default/detail.php index 70865c933..5a59e0305 100644 --- a/lib/tpl/default/detail.php +++ b/lib/tpl/default/detail.php @@ -24,16 +24,6 @@ - - - - diff --git a/lib/tpl/default/main.php b/lib/tpl/default/main.php index afa0b5534..bb32def6b 100644 --- a/lib/tpl/default/main.php +++ b/lib/tpl/default/main.php @@ -23,22 +23,6 @@ - - - - - - - - - - diff --git a/lib/tpl/default/media.php b/lib/tpl/default/media.php index f6fb66750..b3738e39a 100644 --- a/lib/tpl/default/media.php +++ b/lib/tpl/default/media.php @@ -21,16 +21,7 @@ - - - - + diff --git a/lib/tpl/default/mediaedit.php b/lib/tpl/default/mediaedit.php index c7b7e2e53..92d1db8db 100644 --- a/lib/tpl/default/mediaedit.php +++ b/lib/tpl/default/mediaedit.php @@ -22,8 +22,6 @@ - - diff --git a/lib/tpl/default/mediaref.php b/lib/tpl/default/mediaref.php index 61254c9ce..47752a571 100644 --- a/lib/tpl/default/mediaref.php +++ b/lib/tpl/default/mediaref.php @@ -22,16 +22,7 @@ - - - - + -- cgit v1.2.3