Skip to content

Commit

Permalink
Synced to ad2f29a
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Aug 4, 2017
1 parent 9380fd3 commit 664b3ea
Show file tree
Hide file tree
Showing 20 changed files with 126 additions and 181 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "s9e/text-formatter",
"version": "0.10.1",
"version": "0.11.0",
"type": "library",
"description": "Multi-purpose text formatting and markup library. Plugins offer support for BBCodes, Markdown, emoticons, HTML, embedding media (YouTube, etc...), enhanced typography and more.",
"homepage": "https://github.com/s9e/TextFormatter/",
Expand Down
2 changes: 1 addition & 1 deletion src/Bundles/Fatdown.php

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/Bundles/Fatdown/Renderer.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Bundles/Forum.php

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/Bundles/Forum/Renderer.php

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/Bundles/MediaPack.php

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/Bundles/MediaPack/Renderer.php

Large diffs are not rendered by default.

163 changes: 50 additions & 113 deletions src/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,41 +84,20 @@ public function enableJavaScript()
if (!isset($this->javascript))
$this->javascript = new JavaScript($this);
}
public function finalize(array $options = array())
public function finalize()
{
$return = array();
$options += array(
'addHTML5Rules' => \true,
'optimizeConfig' => \true,
'returnJS' => isset($this->javascript),
'returnParser' => \true,
'returnRenderer' => \true
);
if ($options['addHTML5Rules'])
$this->addHTML5Rules($options);
if ($options['returnRenderer'])
{
$renderer = $this->rendering->getRenderer();
if (isset($options['finalizeRenderer']))
\call_user_func($options['finalizeRenderer'], $renderer);
$return['renderer'] = $renderer;
}
if ($options['returnJS'] || $options['returnParser'])
{
$config = $this->asConfig();
if ($options['returnJS'])
$return['js'] = $this->javascript->getParser(ConfigHelper::filterConfig($config, 'JS'));
if ($options['returnParser'])
{
$config = ConfigHelper::filterConfig($config, 'PHP');
if ($options['optimizeConfig'])
ConfigHelper::optimizeArray($config);
$parser = new Parser($config);
if (isset($options['finalizeParser']))
\call_user_func($options['finalizeParser'], $parser);
$return['parser'] = $parser;
}
}
$this->plugins->finalize();
foreach ($this->tags as $tag)
$this->templateNormalizer->normalizeTag($tag);
$return['renderer'] = $this->rendering->getRenderer();
$this->addTagRules();
$config = $this->asConfig();
if (isset($this->javascript))
$return['js'] = $this->javascript->getParser(ConfigHelper::filterConfig($config, 'JS'));
$config = ConfigHelper::filterConfig($config, 'PHP');
ConfigHelper::optimizeArray($config);
$return['parser'] = new Parser($config);
return $return;
}
public function loadBundle($bundleName)
Expand All @@ -134,17 +113,6 @@ public function saveBundle($className, $filepath, array $options = array())
$file = "<?php\n\n" . $this->bundleGenerator->generate($className, $options);
return (\file_put_contents($filepath, $file) !== \false);
}
public function addHTML5Rules(array $options = array())
{
$options += array('rootRules' => $this->rootRules);
$this->plugins->finalize();
foreach ($this->tags as $tag)
$this->templateNormalizer->normalizeTag($tag);
$rules = $this->rulesGenerator->getRules($this->tags, $options);
$this->rootRules->merge($rules['root'], \false);
foreach ($rules['tags'] as $tagName => $tagRules)
$this->tags[$tagName]->rules->merge($tagRules, \false);
}
public function asConfig()
{
$this->plugins->finalize();
Expand Down Expand Up @@ -173,6 +141,13 @@ public function asConfig()
unset($config['rootRules']);
return $config;
}
protected function addTagRules()
{
$rules = $this->rulesGenerator->getRules($this->tags);
$this->rootRules->merge($rules['root'], \false);
foreach ($rules['tags'] as $tagName => $tagRules)
$this->tags[$tagName]->rules->merge($tagRules, \false);
}
}

/*
Expand All @@ -195,7 +170,7 @@ public function __construct(Configurator $configurator)
public function generate($className, array $options = array())
{
$options += array('autoInclude' => \true);
$objects = $this->configurator->finalize($options);
$objects = $this->configurator->finalize();
$parser = $objects['parser'];
$renderer = $objects['renderer'];
$namespace = '';
Expand Down Expand Up @@ -1223,18 +1198,8 @@ protected static function initMatrix(array $rules)
$tagNames = \array_keys($rules);
foreach ($rules as $tagName => $tagRules)
{
if ($tagRules['defaultDescendantRule'] === 'allow')
{
$childValue = (int) ($tagRules['defaultChildRule'] === 'allow');
$descendantValue = 1;
}
else
{
$childValue = 0;
$descendantValue = 0;
}
$matrix[$tagName]['allowedChildren'] = \array_fill_keys($tagNames, $childValue);
$matrix[$tagName]['allowedDescendants'] = \array_fill_keys($tagNames, $descendantValue);
$matrix[$tagName]['allowedChildren'] = \array_fill_keys($tagNames, 0);
$matrix[$tagName]['allowedDescendants'] = \array_fill_keys($tagNames, 0);
}
return $matrix;
}
Expand All @@ -1255,7 +1220,10 @@ protected static function unrollRules(array $rules)
foreach ($rules as $tagName => $tagRules)
{
if (!empty($tagRules['ignoreTags']))
{
$rules[$tagName]['denyChild'] = $tagNames;
$rules[$tagName]['denyDescendant'] = $tagNames;
}
if (!empty($tagRules['requireParent']))
{
$denyParents = \array_diff($tagNames, $tagRules['requireParent']);
Expand All @@ -1264,10 +1232,8 @@ protected static function unrollRules(array $rules)
}
}
self::applyTargetedRule($matrix, $rules, 'allowChild', 'allowedChildren', 1);
self::applyTargetedRule($matrix, $rules, 'allowDescendant', 'allowedChildren', 1);
self::applyTargetedRule($matrix, $rules, 'allowDescendant', 'allowedDescendants', 1);
self::applyTargetedRule($matrix, $rules, 'denyChild', 'allowedChildren', 0);
self::applyTargetedRule($matrix, $rules, 'denyDescendant', 'allowedChildren', 0);
self::applyTargetedRule($matrix, $rules, 'denyDescendant', 'allowedDescendants', 0);
return $matrix;
}
Expand Down Expand Up @@ -1782,10 +1748,6 @@ public function isEmpty()
{
return $this->isEmpty;
}
public function isIframe()
{
return ($this->rootNodes === array('iframe'));
}
public function isPassthrough()
{
return $this->isPassthrough;
Expand Down Expand Up @@ -1945,8 +1907,7 @@ protected function isFormattingSpan(DOMElement $node)
{
if ($node->nodeName !== 'span')
return \false;
if ($node->getAttribute('class') === ''
&& $node->getAttribute('style') === '')
if ($node->getAttribute('class') === '' && $node->getAttribute('style') === '')
return \false;
foreach ($node->attributes as $attrName => $attribute)
if ($attrName !== 'class' && $attrName !== 'style')
Expand Down Expand Up @@ -4780,6 +4741,7 @@ public function __construct()
$this->collection = new RulesGeneratorList;
$this->collection->append('AutoCloseIfVoid');
$this->collection->append('AutoReopenFormattingElements');
$this->collection->append('BlockElementsCloseFormattingElements');
$this->collection->append('BlockElementsFosterFormattingElements');
$this->collection->append('DisableAutoLineBreaksIfNewLinesArePreserved');
$this->collection->append('EnforceContentModels');
Expand All @@ -4789,10 +4751,9 @@ public function __construct()
$this->collection->append('IgnoreWhitespaceAroundBlockElements');
$this->collection->append('TrimFirstLineInCodeBlocks');
}
public function getRules(TagCollection $tags, array $options = array())
public function getRules(TagCollection $tags)
{
$parentHTML = (isset($options['parentHTML'])) ? $options['parentHTML'] : '<div>';
$rootInspector = $this->generateRootInspector($parentHTML);
$rootInspector = new TemplateInspector('<div><xsl:apply-templates/></div>');
$templateInspector = array();
foreach ($tags as $tagName => $tag)
{
Expand All @@ -4812,20 +4773,6 @@ public function getRules(TagCollection $tags, array $options = array())
unset($rules['root']['requireParent']);
return $rules;
}
protected function generateRootInspector($html)
{
$dom = new DOMDocument;
$dom->loadHTML($html);
$body = $dom->getElementsByTagName('body')->item(0);
$node = $body;
while ($node->firstChild)
$node = $node->firstChild;
$node->appendChild($dom->createElementNS(
'http://www.w3.org/1999/XSL/Transform',
'xsl:apply-templates'
));
return new TemplateInspector($dom->saveXML($body));
}
protected function generateRulesets(array $templateInspector, TemplateInspector $rootInspector)
{
$rules = array(
Expand Down Expand Up @@ -5690,7 +5637,7 @@ public function __unset($propName)
protected $filterChain;
protected $nestingLimit = 10;
protected $rules;
protected $tagLimit = 1000;
protected $tagLimit = 5000;
protected $template;
public function __construct(array $options = \null)
{
Expand All @@ -5716,8 +5663,6 @@ public function __construct(array $options = \null)
public function asConfig()
{
$vars = \get_object_vars($this);
unset($vars['defaultChildRule']);
unset($vars['defaultDescendantRule']);
unset($vars['template']);
if (!\count($this->attributePreprocessors))
{
Expand Down Expand Up @@ -6206,6 +6151,22 @@ public function generateBooleanRules(TemplateInspector $src)
}
}

/*
* @package s9e\TextFormatter
* @copyright Copyright (c) 2010-2017 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\TextFormatter\Configurator\RulesGenerators;
use s9e\TextFormatter\Configurator\Helpers\TemplateInspector;
use s9e\TextFormatter\Configurator\RulesGenerators\Interfaces\TargetedRulesGenerator;
class BlockElementsCloseFormattingElements implements TargetedRulesGenerator
{
public function generateTargetedRules(TemplateInspector $src, TemplateInspector $trg)
{
return ($src->isBlock() && $trg->isFormattingElement()) ? array('closeParent') : array();
}
}

/*
* @package s9e\TextFormatter
* @copyright Copyright (c) 2010-2017 The s9e Authors
Expand Down Expand Up @@ -6275,13 +6236,11 @@ public function generateBooleanRules(TemplateInspector $src)
}
public function generateTargetedRules(TemplateInspector $src, TemplateInspector $trg)
{
if ($src->isIframe())
$src = $this->span;
$rules = array();
if (!$src->allowsChild($trg))
$rules[] = 'denyChild';
if (!$src->allowsDescendant($trg))
$rules[] = 'denyDescendant';
if ($src->allowsChild($trg))
$rules[] = 'allowChild';
if ($src->allowsDescendant($trg))
$rules[] = 'allowDescendant';
return $rules;
}
}
Expand Down Expand Up @@ -7978,12 +7937,6 @@ public function __construct()
{
$this->clear();
}
public function clear()
{
parent::clear();
$this->defaultChildRule('allow');
$this->defaultDescendantRule('allow');
}
public function offsetExists($k)
{
return isset($this->items[$k]);
Expand All @@ -8005,8 +7958,6 @@ public function asConfig()
$config = $this->items;
unset($config['allowChild']);
unset($config['allowDescendant']);
unset($config['defaultChildRule']);
unset($config['defaultDescendantRule']);
unset($config['denyChild']);
unset($config['denyDescendant']);
unset($config['requireParent']);
Expand Down Expand Up @@ -8123,20 +8074,6 @@ public function createParagraphs($bool = \true)
{
return $this->addBooleanRule('createParagraphs', $bool);
}
public function defaultChildRule($rule)
{
if ($rule !== 'allow' && $rule !== 'deny')
throw new InvalidArgumentException("defaultChildRule() only accepts 'allow' or 'deny'");
$this->items['defaultChildRule'] = $rule;
return $this;
}
public function defaultDescendantRule($rule)
{
if ($rule !== 'allow' && $rule !== 'deny')
throw new InvalidArgumentException("defaultDescendantRule() only accepts 'allow' or 'deny'");
$this->items['defaultDescendantRule'] = $rule;
return $this;
}
public function denyChild($tagName)
{
return $this->addTargetedRule('denyChild', $tagName);
Expand Down
2 changes: 1 addition & 1 deletion src/Configurator/JavaScript/StylesheetCompressor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class StylesheetCompressor
'<xsl:value-of select="',
'<xsl:copy-of select="@',
'<iframe allowfullscreen="" scrolling="no"',
'overflow:hidden;position:relative;padding-bottom:',
'display:block;overflow:hidden;position:relative;padding-bottom:',
'display:inline-block;width:100%;max-width:',
' [-:\\w]++="',
'\\{[^}]++\\}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
namespace s9e\TextFormatter\Configurator\RulesGenerators;
use s9e\TextFormatter\Configurator\Helpers\TemplateInspector;
use s9e\TextFormatter\Configurator\RulesGenerators\Interfaces\TargetedRulesGenerator;
class BlockElementsCloseFormattingElements implements TargetedRulesGenerator
class AllowAll implements TargetedRulesGenerator
{
public function generateTargetedRules(TemplateInspector $src, TemplateInspector $trg)
{
return ($src->isBlock() && $trg->isFormattingElement()) ? array('closeParent') : array();
return array('allowChild', 'allowDescendant');
}
}
2 changes: 1 addition & 1 deletion src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var logger = new Logger;
/**
* @type {!number} How hard the parser should work on fixing bad markup
*/
var maxFixingCost = 1000;
var maxFixingCost = 10000;

/**
* @type {!Object} Associative array of namespace prefixes in use in document (prefixes used as key)
Expand Down
2 changes: 1 addition & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Parser
protected $currentTag;
protected $isRich;
protected $logger;
public $maxFixingCost = 1000;
public $maxFixingCost = 10000;
protected $namespaces;
protected $openTags;
protected $output;
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/Autoimage/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ var tagName = config.tagName,

matches.forEach(function(m)
{
addTagPair(tagName, m[0][1], 0, m[0][1] + m[0][0].length, 0, -1).setAttribute(attrName, m[0][0]);
addTagPair(tagName, m[0][1], 0, m[0][1] + m[0][0].length, 0, 2).setAttribute(attrName, m[0][0]);
});
2 changes: 1 addition & 1 deletion src/Plugins/Autoimage/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function parse($text, array $matches)
$tagName = $this->config['tagName'];
$attrName = $this->config['attrName'];
foreach ($matches as $m)
$this->parser->addTagPair($tagName, $m[0][1], 0, $m[0][1] + \strlen($m[0][0]), 0, -1)
$this->parser->addTagPair($tagName, $m[0][1], 0, $m[0][1] + \strlen($m[0][0]), 0, 2)
->setAttribute($attrName, $m[0][0]);
}
}
1 change: 1 addition & 0 deletions src/Plugins/Autovideo/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ protected function setUp()
$filter = $this->configurator->attributeFilters['#url'];
$tag->attributes->add($this->attrName)->filterChain->append($filter);
$tag->template = '<video src="{@' . $this->attrName . '}"/>';
$tag->rules->allowChild('URL');
}
}
2 changes: 1 addition & 1 deletion src/Plugins/ConfiguratorBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class ConfiguratorBase implements ConfigProvider
{
protected $configurator;
protected $quickMatch = \false;
protected $regexpLimit = 10000;
protected $regexpLimit = 50000;
final public function __construct(Configurator $configurator, array $overrideProps = array())
{
$this->configurator = $configurator;
Expand Down
1 change: 1 addition & 0 deletions src/Plugins/MediaEmbed/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function add($siteId, array $siteConfig = \null)
$siteConfig = $this->defaultSites->get($siteId);
$this->collection[$siteId] = $siteConfig;
$tag = new Tag;
$tag->rules->allowChild('URL');
$tag->rules->autoClose();
$tag->rules->denyChild($siteId);
$tag->rules->denyChild($this->tagName);
Expand Down
Loading

0 comments on commit 664b3ea

Please sign in to comment.