[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * @package s9e\TextFormatter 5 * @copyright Copyright (c) 2010-2019 The s9e Authors 6 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 7 */ 8 namespace s9e\TextFormatter\Configurator; 9 use ReflectionClass; 10 use s9e\TextFormatter\Configurator; 11 use s9e\TextFormatter\Configurator\Helpers\ConfigHelper; 12 use s9e\TextFormatter\Configurator\JavaScript\CallbackGenerator; 13 use s9e\TextFormatter\Configurator\JavaScript\Code; 14 use s9e\TextFormatter\Configurator\JavaScript\ConfigOptimizer; 15 use s9e\TextFormatter\Configurator\JavaScript\Dictionary; 16 use s9e\TextFormatter\Configurator\JavaScript\Encoder; 17 use s9e\TextFormatter\Configurator\JavaScript\HintGenerator; 18 use s9e\TextFormatter\Configurator\JavaScript\Minifier; 19 use s9e\TextFormatter\Configurator\JavaScript\Minifiers\Noop; 20 use s9e\TextFormatter\Configurator\JavaScript\RegexpConvertor; 21 use s9e\TextFormatter\Configurator\JavaScript\StylesheetCompressor; 22 use s9e\TextFormatter\Configurator\RendererGenerators\XSLT; 23 class JavaScript 24 { 25 protected $callbackGenerator; 26 protected $config; 27 protected $configOptimizer; 28 protected $configurator; 29 public $encoder; 30 public $exportMethods; 31 public $exports = [ 32 'disablePlugin', 33 'disableTag', 34 'enablePlugin', 35 'enableTag', 36 'getLogger', 37 'parse', 38 'preview', 39 'registeredVars', 40 'setNestingLimit', 41 'setParameter', 42 'setTagLimit' 43 ]; 44 protected $hintGenerator; 45 protected $minifier; 46 protected $stylesheetCompressor; 47 protected $xsl; 48 public function __construct(Configurator $configurator) 49 { 50 $this->exportMethods =& $this->exports; 51 $this->encoder = new Encoder; 52 $this->callbackGenerator = new CallbackGenerator; 53 $this->configOptimizer = new ConfigOptimizer($this->encoder); 54 $this->configurator = $configurator; 55 $this->hintGenerator = new HintGenerator; 56 $this->stylesheetCompressor = new StylesheetCompressor; 57 } 58 public function getMinifier() 59 { 60 if (!isset($this->minifier)) 61 $this->minifier = new Noop; 62 return $this->minifier; 63 } 64 public function getParser(array $config = \null) 65 { 66 $this->configOptimizer->reset(); 67 $xslt = new XSLT; 68 $xslt->optimizer->normalizer->remove('RemoveLivePreviewAttributes'); 69 $this->xsl = $xslt->getXSL($this->configurator->rendering); 70 $this->config = (isset($config)) ? $config : $this->configurator->asConfig(); 71 $this->config = ConfigHelper::filterConfig($this->config, 'JS'); 72 $this->config = $this->callbackGenerator->replaceCallbacks($this->config); 73 $src = $this->getHints() . $this->injectConfig($this->getSource()); 74 $src .= "if (!window['s9e']) window['s9e'] = {};\n" . $this->getExports(); 75 $src = $this->getMinifier()->get($src); 76 $src = '(function(){' . $src . '})()'; 77 return $src; 78 } 79 public function setMinifier($minifier) 80 { 81 if (\is_string($minifier)) 82 { 83 $className = __NAMESPACE__ . '\\JavaScript\\Minifiers\\' . $minifier; 84 $args = \array_slice(\func_get_args(), 1); 85 if (!empty($args)) 86 { 87 $reflection = new ReflectionClass($className); 88 $minifier = $reflection->newInstanceArgs($args); 89 } 90 else 91 $minifier = new $className; 92 } 93 $this->minifier = $minifier; 94 return $minifier; 95 } 96 protected function encode($value) 97 { 98 return $this->encoder->encode($value); 99 } 100 protected function getExports() 101 { 102 if (empty($this->exports)) 103 return ''; 104 $exports = []; 105 foreach ($this->exports as $export) 106 $exports[] = "'" . $export . "':" . $export; 107 \sort($exports); 108 return "window['s9e']['TextFormatter'] = {" . \implode(',', $exports) . '}'; 109 } 110 protected function getHints() 111 { 112 $this->hintGenerator->setConfig($this->config); 113 $this->hintGenerator->setPlugins($this->configurator->plugins); 114 $this->hintGenerator->setXSL($this->xsl); 115 return $this->hintGenerator->getHints(); 116 } 117 protected function getPluginsConfig() 118 { 119 $plugins = new Dictionary; 120 foreach ($this->config['plugins'] as $pluginName => $pluginConfig) 121 { 122 if (!isset($pluginConfig['js'])) 123 continue; 124 $js = $pluginConfig['js']; 125 unset($pluginConfig['js']); 126 unset($pluginConfig['className']); 127 if (isset($pluginConfig['quickMatch'])) 128 { 129 $valid = [ 130 '[[:ascii:]]', 131 '[\\xC0-\\xDF][\\x80-\\xBF]', 132 '[\\xE0-\\xEF][\\x80-\\xBF]{2}', 133 '[\\xF0-\\xF7][\\x80-\\xBF]{3}' 134 ]; 135 $regexp = '#(?>' . \implode('|', $valid) . ')+#'; 136 if (\preg_match($regexp, $pluginConfig['quickMatch'], $m)) 137 $pluginConfig['quickMatch'] = $m[0]; 138 else 139 unset($pluginConfig['quickMatch']); 140 } 141 $globalKeys = [ 142 'quickMatch' => 1, 143 'regexp' => 1, 144 'regexpLimit' => 1 145 ]; 146 $globalConfig = \array_intersect_key($pluginConfig, $globalKeys); 147 $localConfig = \array_diff_key($pluginConfig, $globalKeys); 148 if (isset($globalConfig['regexp']) && !($globalConfig['regexp'] instanceof Code)) 149 $globalConfig['regexp'] = new Code(RegexpConvertor::toJS($globalConfig['regexp'], \true)); 150 $globalConfig['parser'] = new Code( 151 '/** 152 * @param {!string} text 153 * @param {!Array.<Array>} matches 154 */ 155 function(text, matches) 156 { 157 /** @const */ 158 var config=' . $this->encode($localConfig) . '; 159 ' . $js . ' 160 }' 161 ); 162 $plugins[$pluginName] = $globalConfig; 163 } 164 return $plugins; 165 } 166 protected function getRegisteredVarsConfig() 167 { 168 $registeredVars = $this->config['registeredVars']; 169 unset($registeredVars['cacheDir']); 170 return new Dictionary($registeredVars); 171 } 172 protected function getRootContext() 173 { 174 return $this->config['rootContext']; 175 } 176 protected function getSource() 177 { 178 $rootDir = __DIR__ . '/..'; 179 $src = ''; 180 $logger = (\in_array('getLogger', $this->exports)) ? 'Logger.js' : 'NullLogger.js'; 181 $files = \glob($rootDir . '/Parser/AttributeFilters/*.js'); 182 $files[] = $rootDir . '/Parser/utils.js'; 183 $files[] = $rootDir . '/Parser/FilterProcessing.js'; 184 $files[] = $rootDir . '/Parser/' . $logger; 185 $files[] = $rootDir . '/Parser/Tag.js'; 186 $files[] = $rootDir . '/Parser.js'; 187 if (\in_array('preview', $this->exports, \true)) 188 { 189 $files[] = $rootDir . '/render.js'; 190 $src .= '/** @const */ var xsl=' . $this->getStylesheet() . ";\n"; 191 } 192 $src .= \implode("\n", \array_map('file_get_contents', $files)); 193 return $src; 194 } 195 protected function getStylesheet() 196 { 197 return $this->stylesheetCompressor->encode($this->xsl); 198 } 199 protected function getTagsConfig() 200 { 201 $tags = new Dictionary; 202 foreach ($this->config['tags'] as $tagName => $tagConfig) 203 { 204 if (isset($tagConfig['attributes'])) 205 $tagConfig['attributes'] = new Dictionary($tagConfig['attributes']); 206 $tags[$tagName] = $tagConfig; 207 } 208 return $tags; 209 } 210 protected function injectConfig($src) 211 { 212 $config = \array_map( 213 [$this, 'encode'], 214 $this->configOptimizer->optimize( 215 [ 216 'plugins' => $this->getPluginsConfig(), 217 'registeredVars' => $this->getRegisteredVarsConfig(), 218 'rootContext' => $this->getRootContext(), 219 'tagsConfig' => $this->getTagsConfig() 220 ] 221 ) 222 ); 223 $src = \preg_replace_callback( 224 '/(\\nvar (' . \implode('|', \array_keys($config)) . '))(;)/', 225 function ($m) use ($config) 226 { 227 return $m[1] . '=' . $config[$m[2]] . $m[3]; 228 }, 229 $src 230 ); 231 $src = $this->configOptimizer->getVarDeclarations() . $src; 232 return $src; 233 } 234 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |