function test_all()
    {
        $text = <<<EOL
//@ sourceMappingURL=/foo/bar/xxx.map
//# sourceMappingURL=/foo/bar/xxx.map
/*@ sourceMappingURL=/foo/bar/xxx.map */
/*# sourceMappingURL=/foo/bar/xxx.map */
bang
EOL;
        $expect = <<<EOL
//
//
/**/
/**/
bang
EOL;
        stripsourcemaps($text);
        $this->assertEquals($expect, $text);
    }
Example #2
0
/**
 * Output all needed Styles
 *
 * @author Andreas Gohr <*****@*****.**>
 */
function css_out()
{
    global $conf;
    global $lang;
    global $config_cascade;
    global $INPUT;
    if ($INPUT->str('s') == 'feed') {
        $mediatypes = array('feed');
        $type = 'feed';
    } else {
        $mediatypes = array('screen', 'all', 'print');
        $type = '';
    }
    // decide from where to get the template
    $tpl = trim(preg_replace('/[^\\w-]+/', '', $INPUT->str('t')));
    if (!$tpl) {
        $tpl = $conf['template'];
    }
    // The generated script depends on some dynamic options
    $cache = new cache('styles' . $_SERVER['HTTP_HOST'] . $_SERVER['SERVER_PORT'] . $INPUT->int('preview') . DOKU_BASE . $tpl . $type, '.css');
    // load styl.ini
    $styleini = css_styleini($tpl, $INPUT->bool('preview'));
    // cache influencers
    $tplinc = tpl_incdir($tpl);
    $cache_files = getConfigFiles('main');
    $cache_files[] = $tplinc . 'style.ini';
    $cache_files[] = DOKU_CONF . "tpl/{$tpl}/style.ini";
    $cache_files[] = __FILE__;
    if ($INPUT->bool('preview')) {
        $cache_files[] = $conf['cachedir'] . '/preview.ini';
    }
    // Array of needed files and their web locations, the latter ones
    // are needed to fix relative paths in the stylesheets
    $files = array();
    foreach ($mediatypes as $mediatype) {
        $files[$mediatype] = array();
        // load core styles
        $files[$mediatype][DOKU_INC . 'lib/styles/' . $mediatype . '.css'] = DOKU_BASE . 'lib/styles/';
        // load jQuery-UI theme
        if ($mediatype == 'screen') {
            $files[$mediatype][DOKU_INC . 'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE . 'lib/scripts/jquery/jquery-ui-theme/';
        }
        // load plugin styles
        $files[$mediatype] = array_merge($files[$mediatype], css_pluginstyles($mediatype));
        // load template styles
        if (isset($styleini['stylesheets'][$mediatype])) {
            $files[$mediatype] = array_merge($files[$mediatype], $styleini['stylesheets'][$mediatype]);
        }
        // load user styles
        if (!empty($config_cascade['userstyle'][$mediatype])) {
            foreach ($config_cascade['userstyle'][$mediatype] as $userstyle) {
                $files[$mediatype][$userstyle] = DOKU_BASE;
            }
        }
        $cache_files = array_merge($cache_files, array_keys($files[$mediatype]));
    }
    // check cache age & handle conditional request
    // This may exit if a cache can be used
    http_cached($cache->cache, $cache->useCache(array('files' => $cache_files)));
    // start output buffering
    ob_start();
    // build the stylesheet
    foreach ($mediatypes as $mediatype) {
        // print the default classes for interwiki links and file downloads
        if ($mediatype == 'screen') {
            print '@media screen {';
            css_interwiki();
            css_filetypes();
            print '}';
        }
        // load files
        $css_content = '';
        foreach ($files[$mediatype] as $file => $location) {
            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
            $css_content .= "\n/* XXXXXXXXX {$display} XXXXXXXXX */\n";
            $css_content .= css_loadfile($file, $location);
        }
        switch ($mediatype) {
            case 'screen':
                print NL . '@media screen { /* START screen styles */' . NL . $css_content . NL . '} /* /@media END screen styles */' . NL;
                break;
            case 'print':
                print NL . '@media print { /* START print styles */' . NL . $css_content . NL . '} /* /@media END print styles */' . NL;
                break;
            case 'all':
            case 'feed':
            default:
                print NL . '/* START rest styles */ ' . NL . $css_content . NL . '/* END rest styles */' . NL;
                break;
        }
    }
    // end output buffering and get contents
    $css = ob_get_contents();
    ob_end_clean();
    // strip any source maps
    stripsourcemaps($css);
    // apply style replacements
    $css = css_applystyle($css, $styleini['replacements']);
    // parse less
    $css = css_parseless($css);
    // compress whitespace and comments
    if ($conf['compress']) {
        $css = css_compress($css);
    }
    // embed small images right into the stylesheet
    if ($conf['cssdatauri']) {
        $base = preg_quote(DOKU_BASE, '#');
        $css = preg_replace_callback('#(url\\([ \'"]*)(' . $base . ')(.*?(?:\\.(png|gif)))#i', 'css_datauri', $css);
    }
    http_cached_finish($cache->cache, $css);
}
/**
 * Output all needed JavaScript
 *
 * @author Andreas Gohr <*****@*****.**>
 */
function js_out()
{
    global $conf;
    global $lang;
    global $config_cascade;
    // The generated script depends on some dynamic options
    $cache = new cache('scripts' . $_SERVER['HTTP_HOST'] . $_SERVER['SERVER_PORT'], '.js');
    $cache->_event = 'JS_CACHE_USE';
    // load minified version for some files
    $min = $conf['compress'] ? '.min' : '';
    // array of core files
    $files = array(DOKU_INC . "lib/scripts/jquery/jquery{$min}.js", DOKU_INC . 'lib/scripts/jquery/jquery.cookie.js', DOKU_INC . "lib/scripts/jquery/jquery-ui{$min}.js", DOKU_INC . "lib/scripts/jquery/jquery-migrate{$min}.js", DOKU_INC . 'inc/lang/' . $conf['lang'] . '/jquery.ui.datepicker.js', DOKU_INC . "lib/scripts/fileuploader.js", DOKU_INC . "lib/scripts/fileuploaderextended.js", DOKU_INC . 'lib/scripts/helpers.js', DOKU_INC . 'lib/scripts/delay.js', DOKU_INC . 'lib/scripts/cookie.js', DOKU_INC . 'lib/scripts/script.js', DOKU_INC . 'lib/scripts/tw-sack.js', DOKU_INC . 'lib/scripts/qsearch.js', DOKU_INC . 'lib/scripts/tree.js', DOKU_INC . 'lib/scripts/index.js', DOKU_INC . 'lib/scripts/drag.js', DOKU_INC . 'lib/scripts/textselection.js', DOKU_INC . 'lib/scripts/toolbar.js', DOKU_INC . 'lib/scripts/edit.js', DOKU_INC . 'lib/scripts/editor.js', DOKU_INC . 'lib/scripts/locktimer.js', DOKU_INC . 'lib/scripts/linkwiz.js', DOKU_INC . 'lib/scripts/media.js', DOKU_INC . 'lib/scripts/behaviour.js', DOKU_INC . 'lib/scripts/page.js', tpl_incdir() . 'script.js');
    // add possible plugin scripts and userscript
    $files = array_merge($files, js_pluginscripts());
    if (isset($config_cascade['userscript']['default'])) {
        $files[] = $config_cascade['userscript']['default'];
    }
    $cache_files = array_merge($files, getConfigFiles('main'));
    $cache_files[] = __FILE__;
    // check cache age & handle conditional request
    // This may exit if a cache can be used
    $cache_ok = $cache->useCache(array('files' => $cache_files));
    http_cached($cache->cache, $cache_ok);
    // start output buffering and build the script
    ob_start();
    $json = new JSON();
    // add some global variables
    print "var DOKU_BASE   = '" . DOKU_BASE . "';";
    print "var DOKU_TPL    = '" . tpl_basedir() . "';";
    print "var DOKU_COOKIE_PARAM = " . $json->encode(array('path' => empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'], 'secure' => $conf['securecookie'] && is_ssl())) . ";";
    // FIXME: Move those to JSINFO
    print "var DOKU_UHN    = " . (int) useHeading('navigation') . ";";
    print "var DOKU_UHC    = " . (int) useHeading('content') . ";";
    // load JS specific translations
    $lang['js']['plugins'] = js_pluginstrings();
    $templatestrings = js_templatestrings();
    if (!empty($templatestrings)) {
        $lang['js']['template'] = $templatestrings;
    }
    echo 'LANG = ' . $json->encode($lang['js']) . ";\n";
    // load toolbar
    toolbar_JSdefines('toolbar');
    // load files
    foreach ($files as $file) {
        if (!file_exists($file)) {
            continue;
        }
        $ismin = substr($file, -7) == '.min.js';
        $debugjs = $conf['allowdebug'] && strpos($file, DOKU_INC . 'lib/scripts/') !== 0;
        echo "\n\n/* XXXXXXXXXX begin of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n";
        if ($ismin) {
            echo "\n/* BEGIN NOCOMPRESS */\n";
        }
        if ($debugjs) {
            echo "\ntry {\n";
        }
        js_load($file);
        if ($debugjs) {
            echo "\n} catch (e) {\n   logError(e, '" . str_replace(DOKU_INC, '', $file) . "');\n}\n";
        }
        if ($ismin) {
            echo "\n/* END NOCOMPRESS */\n";
        }
        echo "\n\n/* XXXXXXXXXX end of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n";
    }
    // init stuff
    if ($conf['locktime'] != 0) {
        js_runonstart("dw_locktimer.init(" . ($conf['locktime'] - 60) . "," . $conf['usedraft'] . ")");
    }
    // init hotkeys - must have been done after init of toolbar
    # disabled for FS#1958    js_runonstart('initializeHotkeys()');
    // end output buffering and get contents
    $js = ob_get_contents();
    ob_end_clean();
    // strip any source maps
    stripsourcemaps($js);
    // compress whitespace and comments
    if ($conf['compress']) {
        $js = js_compress($js);
    }
    $js .= "\n";
    // https://bugzilla.mozilla.org/show_bug.cgi?id=316033
    http_cached_finish($cache->cache, $js);
}