2013-11-09 01:40:00 +04:00
|
|
|
<?php
|
2018-04-17 16:44:38 +03:00
|
|
|
|
|
|
|
namespace Erusev\Parsedown;
|
2013-07-11 00:22:16 +04:00
|
|
|
|
2018-12-05 17:07:55 +03:00
|
|
|
use Erusev\Parsedown\Parsing\Context;
|
2018-12-05 13:30:49 +03:00
|
|
|
use Erusev\Parsedown\Parsing\Line;
|
2018-12-05 17:07:55 +03:00
|
|
|
use Erusev\Parsedown\Parsing\Lines;
|
2018-12-05 13:30:49 +03:00
|
|
|
|
2013-11-09 01:40:00 +04:00
|
|
|
#
|
|
|
|
#
|
2013-07-11 00:22:16 +04:00
|
|
|
# Parsedown
|
|
|
|
# http://parsedown.org
|
2013-11-09 01:40:00 +04:00
|
|
|
#
|
|
|
|
# (c) Emanuil Rusev
|
2013-07-11 00:22:16 +04:00
|
|
|
# http://erusev.com
|
2013-11-09 01:40:00 +04:00
|
|
|
#
|
2014-01-24 03:28:03 +04:00
|
|
|
# For the full license information, view the LICENSE file that was distributed
|
|
|
|
# with this source code.
|
2013-11-09 01:40:00 +04:00
|
|
|
#
|
|
|
|
#
|
2013-07-11 00:22:16 +04:00
|
|
|
|
|
|
|
class Parsedown
|
|
|
|
{
|
2014-04-17 11:59:35 +04:00
|
|
|
# ~
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-04-17 16:44:38 +03:00
|
|
|
const version = '2.0.0-dev';
|
2015-01-19 18:11:13 +03:00
|
|
|
|
|
|
|
# ~
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
public function text($text)
|
2018-04-06 20:10:41 +03:00
|
|
|
{
|
|
|
|
$Elements = $this->textElements($text);
|
|
|
|
|
|
|
|
# convert to markup
|
|
|
|
$markup = $this->elements($Elements);
|
|
|
|
|
|
|
|
# trim line breaks
|
2018-12-04 19:24:25 +03:00
|
|
|
$markup = \trim($markup, "\n");
|
2018-04-06 20:10:41 +03:00
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function textElements($text)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-21 03:54:23 +04:00
|
|
|
# iterate through lines to identify blocks
|
2018-12-05 17:07:55 +03:00
|
|
|
return $this->linesElements(Lines::fromTextLines($text, 0));
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
#
|
2014-04-17 11:59:35 +04:00
|
|
|
# Setters
|
|
|
|
#
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
public function setBreaksEnabled($breaksEnabled)
|
2014-02-21 03:54:23 +04:00
|
|
|
{
|
2014-04-17 11:59:35 +04:00
|
|
|
$this->breaksEnabled = $breaksEnabled;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2015-01-18 20:38:57 +03:00
|
|
|
protected $breaksEnabled;
|
2014-09-22 03:36:42 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
public function setMarkupEscaped($markupEscaped)
|
2014-09-20 15:52:05 +04:00
|
|
|
{
|
2014-09-22 03:36:42 +04:00
|
|
|
$this->markupEscaped = $markupEscaped;
|
2014-09-20 15:52:05 +04:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-01-18 20:38:57 +03:00
|
|
|
protected $markupEscaped;
|
2015-01-08 17:13:55 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
public function setUrlsLinked($urlsLinked)
|
2015-01-08 17:13:55 +03:00
|
|
|
{
|
|
|
|
$this->urlsLinked = $urlsLinked;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-01-18 20:38:57 +03:00
|
|
|
protected $urlsLinked = true;
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
public function setSafeMode($safeMode)
|
2015-01-21 05:50:36 +03:00
|
|
|
{
|
2017-05-09 21:22:58 +03:00
|
|
|
$this->safeMode = (bool) $safeMode;
|
2015-01-21 05:50:36 +03:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-05-09 21:22:58 +03:00
|
|
|
protected $safeMode;
|
2015-01-21 05:50:36 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
public function setStrictMode($strictMode)
|
2018-03-29 19:44:47 +03:00
|
|
|
{
|
2018-04-02 19:06:31 +03:00
|
|
|
$this->strictMode = (bool) $strictMode;
|
2018-03-29 19:44:47 +03:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-04-02 19:06:31 +03:00
|
|
|
protected $strictMode;
|
2018-03-29 19:44:47 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
protected $safeLinksWhitelist = [
|
2015-01-25 21:47:32 +03:00
|
|
|
'http://',
|
|
|
|
'https://',
|
|
|
|
'ftp://',
|
2017-05-01 05:24:40 +03:00
|
|
|
'ftps://',
|
|
|
|
'mailto:',
|
2018-10-03 02:38:21 +03:00
|
|
|
'tel:',
|
2017-05-02 21:48:08 +03:00
|
|
|
'data:image/png;base64,',
|
|
|
|
'data:image/gif;base64,',
|
2017-05-02 21:55:03 +03:00
|
|
|
'data:image/jpeg;base64,',
|
2017-05-05 23:32:27 +03:00
|
|
|
'irc:',
|
|
|
|
'ircs:',
|
|
|
|
'git:',
|
|
|
|
'ssh:',
|
|
|
|
'news:',
|
|
|
|
'steam:',
|
2018-12-04 19:24:25 +03:00
|
|
|
];
|
2015-01-25 21:47:32 +03:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2014-05-05 15:39:40 +04:00
|
|
|
# Lines
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
protected $BlockTypes = [
|
|
|
|
'#' => ['Header'],
|
|
|
|
'*' => ['Rule', 'List'],
|
|
|
|
'+' => ['List'],
|
|
|
|
'-' => ['SetextHeader', 'Table', 'Rule', 'List'],
|
|
|
|
'0' => ['List'],
|
|
|
|
'1' => ['List'],
|
|
|
|
'2' => ['List'],
|
|
|
|
'3' => ['List'],
|
|
|
|
'4' => ['List'],
|
|
|
|
'5' => ['List'],
|
|
|
|
'6' => ['List'],
|
|
|
|
'7' => ['List'],
|
|
|
|
'8' => ['List'],
|
|
|
|
'9' => ['List'],
|
|
|
|
':' => ['Table'],
|
|
|
|
'<' => ['Comment', 'Markup'],
|
|
|
|
'=' => ['SetextHeader'],
|
|
|
|
'>' => ['Quote'],
|
|
|
|
'[' => ['Reference'],
|
|
|
|
'_' => ['Rule'],
|
|
|
|
'`' => ['FencedCode'],
|
|
|
|
'|' => ['Table'],
|
|
|
|
'~' => ['FencedCode'],
|
|
|
|
];
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
# ~
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
protected $unmarkedBlockTypes = [
|
2015-01-05 00:34:19 +03:00
|
|
|
'Code',
|
2018-12-04 19:24:25 +03:00
|
|
|
];
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
#
|
|
|
|
# Blocks
|
|
|
|
#
|
|
|
|
|
2018-12-05 17:07:55 +03:00
|
|
|
protected function lines(Lines $Lines)
|
2018-03-21 05:18:34 +03:00
|
|
|
{
|
2018-12-05 17:07:55 +03:00
|
|
|
return $this->elements($this->linesElements($Lines));
|
2018-03-21 05:18:34 +03:00
|
|
|
}
|
|
|
|
|
2018-12-05 13:30:49 +03:00
|
|
|
/** @param int $indentOffset */
|
2018-12-05 17:07:55 +03:00
|
|
|
protected function linesElements(Lines $Lines, array $nonNestables = [], $indentOffset = 0)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
$Elements = [];
|
2014-04-17 11:59:35 +04:00
|
|
|
$CurrentBlock = null;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-12-05 17:07:55 +03:00
|
|
|
foreach ($Lines->contexts() as $Context) {
|
|
|
|
if (isset($CurrentBlock) && $Context->previousEmptyLines() > 0) {
|
|
|
|
$CurrentBlock['interrupted'] = $Context->previousEmptyLines();
|
2014-04-28 03:27:05 +04:00
|
|
|
}
|
|
|
|
|
2018-12-05 17:07:55 +03:00
|
|
|
$Line = $Context->line();
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($CurrentBlock['continuable'])) {
|
2018-04-13 00:16:25 +03:00
|
|
|
$methodName = 'block' . $CurrentBlock['type'] . 'Continue';
|
2018-12-05 17:07:55 +03:00
|
|
|
$Block = $this->$methodName($Context, $CurrentBlock);
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($Block)) {
|
2014-04-17 11:59:35 +04:00
|
|
|
$CurrentBlock = $Block;
|
2014-01-31 05:03:52 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
continue;
|
2018-12-04 19:24:25 +03:00
|
|
|
} else {
|
|
|
|
if ($this->isBlockCompletable($CurrentBlock['type'])) {
|
2018-04-13 00:16:25 +03:00
|
|
|
$methodName = 'block' . $CurrentBlock['type'] . 'Complete';
|
|
|
|
$CurrentBlock = $this->$methodName($CurrentBlock);
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
# ~
|
2014-01-31 04:19:18 +04:00
|
|
|
|
2018-12-05 13:30:49 +03:00
|
|
|
$marker = $Line->text()[0];
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-27 02:54:52 +04:00
|
|
|
# ~
|
|
|
|
|
|
|
|
$blockTypes = $this->unmarkedBlockTypes;
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($this->BlockTypes[$marker])) {
|
|
|
|
foreach ($this->BlockTypes[$marker] as $blockType) {
|
2016-10-13 20:25:43 +03:00
|
|
|
$blockTypes []= $blockType;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
|
|
|
}
|
2014-01-31 04:19:18 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
|
|
|
# ~
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
foreach ($blockTypes as $blockType) {
|
2018-12-05 17:07:55 +03:00
|
|
|
$Block = $this->{"block$blockType"}($Context, $CurrentBlock);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($Block)) {
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block['type'] = $blockType;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (! isset($Block['identified'])) {
|
|
|
|
if (isset($CurrentBlock)) {
|
2018-04-12 23:25:50 +03:00
|
|
|
$Elements[] = $this->extractElement($CurrentBlock);
|
2018-04-08 22:29:09 +03:00
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block['identified'] = true;
|
2014-02-21 03:54:23 +04:00
|
|
|
}
|
2013-11-10 00:23:56 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if ($this->isBlockContinuable($blockType)) {
|
2015-07-02 01:01:14 +03:00
|
|
|
$Block['continuable'] = true;
|
2014-02-21 03:54:23 +04:00
|
|
|
}
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$CurrentBlock = $Block;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
# ~
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($CurrentBlock) and $CurrentBlock['type'] === 'Paragraph') {
|
2018-12-05 17:07:55 +03:00
|
|
|
$Block = $this->paragraphContinue($Context, $CurrentBlock);
|
2018-04-09 17:12:17 +03:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($Block)) {
|
2018-04-09 17:12:17 +03:00
|
|
|
$CurrentBlock = $Block;
|
2018-12-04 19:24:25 +03:00
|
|
|
} else {
|
|
|
|
if (isset($CurrentBlock)) {
|
2018-04-12 23:25:50 +03:00
|
|
|
$Elements[] = $this->extractElement($CurrentBlock);
|
2018-04-08 22:29:09 +03:00
|
|
|
}
|
2014-04-17 11:59:35 +04:00
|
|
|
|
2018-12-05 17:07:55 +03:00
|
|
|
$CurrentBlock = $this->paragraph($Context);
|
2014-05-11 23:31:02 +04:00
|
|
|
|
2014-05-12 01:34:47 +04:00
|
|
|
$CurrentBlock['identified'] = true;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-25 00:52:42 +04:00
|
|
|
# ~
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($CurrentBlock['continuable']) and $this->isBlockCompletable($CurrentBlock['type'])) {
|
2018-04-13 00:16:25 +03:00
|
|
|
$methodName = 'block' . $CurrentBlock['type'] . 'Complete';
|
|
|
|
$CurrentBlock = $this->$methodName($CurrentBlock);
|
2014-04-25 00:52:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($CurrentBlock)) {
|
2018-04-12 23:25:50 +03:00
|
|
|
$Elements[] = $this->extractElement($CurrentBlock);
|
2015-01-11 15:35:09 +03:00
|
|
|
}
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
# ~
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2018-03-21 05:18:34 +03:00
|
|
|
return $Elements;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2018-04-12 23:25:50 +03:00
|
|
|
protected function extractElement(array $Component)
|
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
if (! isset($Component['element'])) {
|
|
|
|
if (isset($Component['markup'])) {
|
|
|
|
$Component['element'] = ['rawHtml' => $Component['markup']];
|
|
|
|
} elseif (isset($Component['hidden'])) {
|
|
|
|
$Component['element'] = [];
|
2018-05-08 23:54:30 +03:00
|
|
|
}
|
2018-04-12 23:25:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $Component['element'];
|
|
|
|
}
|
|
|
|
|
2015-12-19 06:45:14 +03:00
|
|
|
protected function isBlockContinuable($Type)
|
2015-12-17 20:46:44 +03:00
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
return \method_exists($this, 'block' . $Type . 'Continue');
|
2015-12-17 20:46:44 +03:00
|
|
|
}
|
|
|
|
|
2015-12-19 06:45:14 +03:00
|
|
|
protected function isBlockCompletable($Type)
|
2015-12-17 20:46:44 +03:00
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
return \method_exists($this, 'block' . $Type . 'Complete');
|
2015-12-17 20:46:44 +03:00
|
|
|
}
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2015-01-11 03:16:36 +03:00
|
|
|
# Inline Elements
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
protected $InlineTypes = [
|
|
|
|
'!' => ['Image'],
|
|
|
|
'&' => ['SpecialCharacter'],
|
|
|
|
'*' => ['Emphasis'],
|
|
|
|
':' => ['Url'],
|
|
|
|
'<' => ['UrlTag', 'EmailTag', 'Markup'],
|
|
|
|
'[' => ['Link'],
|
|
|
|
'_' => ['Emphasis'],
|
|
|
|
'`' => ['Code'],
|
|
|
|
'~' => ['Strikethrough'],
|
|
|
|
'\\' => ['EscapeSequence'],
|
|
|
|
];
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
# ~
|
|
|
|
|
2018-03-19 01:44:07 +03:00
|
|
|
protected $inlineMarkerList = '!*_&[:<`~\\';
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
#
|
|
|
|
# ~
|
|
|
|
#
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
public function line($text, $nonNestables = [])
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2018-03-21 05:18:34 +03:00
|
|
|
return $this->elements($this->lineElements($text, $nonNestables));
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
protected function lineElements($text, $nonNestables = [])
|
2018-03-21 05:18:34 +03:00
|
|
|
{
|
2018-10-16 20:41:42 +03:00
|
|
|
# standardize line breaks
|
2018-12-04 19:24:25 +03:00
|
|
|
$text = \str_replace(["\r\n", "\r"], "\n", $text);
|
2018-10-16 20:41:42 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$Elements = [];
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$nonNestables = (
|
|
|
|
empty($nonNestables)
|
|
|
|
? []
|
|
|
|
: \array_combine($nonNestables, $nonNestables)
|
2018-04-13 00:22:53 +03:00
|
|
|
);
|
2018-04-08 22:38:21 +03:00
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
# $excerpt is based on the first occurrence of a marker
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
while ($excerpt = \strpbrk($text, $this->inlineMarkerList)) {
|
2014-05-12 17:18:00 +04:00
|
|
|
$marker = $excerpt[0];
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$markerPosition = \strlen($text) - \strlen($excerpt);
|
2015-01-19 18:05:10 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$Excerpt = ['text' => $excerpt, 'context' => $text];
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
foreach ($this->InlineTypes[$marker] as $inlineType) {
|
2016-10-02 20:26:13 +03:00
|
|
|
# check to see if the current inline type is nestable in the current context
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($nonNestables[$inlineType])) {
|
2016-10-04 17:27:11 +03:00
|
|
|
continue;
|
2016-10-01 17:56:14 +03:00
|
|
|
}
|
|
|
|
|
2018-04-08 23:24:45 +03:00
|
|
|
$Inline = $this->{"inline$inlineType"}($Excerpt);
|
2014-01-20 11:26:25 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (! isset($Inline)) {
|
2014-04-28 03:10:18 +04:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-20 11:26:25 +04:00
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
# makes sure that the inline belongs to "our" marker
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($Inline['position']) and $Inline['position'] > $markerPosition) {
|
2015-01-19 18:05:10 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
# sets a default inline position
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (! isset($Inline['position'])) {
|
2015-01-19 18:05:10 +03:00
|
|
|
$Inline['position'] = $markerPosition;
|
|
|
|
}
|
2013-12-24 18:05:13 +04:00
|
|
|
|
2016-10-04 17:27:11 +03:00
|
|
|
# cause the new element to 'inherit' our non nestables
|
2016-10-02 20:26:13 +03:00
|
|
|
|
2018-04-13 00:16:25 +03:00
|
|
|
|
|
|
|
$Inline['element']['nonNestables'] = isset($Inline['element']['nonNestables'])
|
2018-12-04 19:24:25 +03:00
|
|
|
? \array_merge($Inline['element']['nonNestables'], $nonNestables)
|
2018-04-13 00:16:25 +03:00
|
|
|
: $nonNestables
|
|
|
|
;
|
2016-10-02 19:37:08 +03:00
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
# the text that comes before the inline
|
2018-12-04 19:24:25 +03:00
|
|
|
$unmarkedText = \substr($text, 0, $Inline['position']);
|
2015-01-19 18:05:10 +03:00
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
# compile the unmarked text
|
2018-03-21 05:18:34 +03:00
|
|
|
$InlineText = $this->inlineText($unmarkedText);
|
|
|
|
$Elements[] = $InlineText['element'];
|
2014-01-21 00:19:23 +04:00
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
# compile the inline
|
2018-04-12 23:25:50 +03:00
|
|
|
$Elements[] = $this->extractElement($Inline);
|
2014-01-21 00:19:23 +04:00
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
# remove the examined text
|
2018-12-04 19:24:25 +03:00
|
|
|
$text = \substr($text, $Inline['position'] + $Inline['extent']);
|
2013-12-24 18:05:13 +04:00
|
|
|
|
2014-04-28 03:10:18 +04:00
|
|
|
continue 2;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2013-12-24 18:05:13 +04:00
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
# the marker does not belong to an inline
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$unmarkedText = \substr($text, 0, $markerPosition + 1);
|
2015-06-25 01:05:05 +03:00
|
|
|
|
2018-03-21 05:18:34 +03:00
|
|
|
$InlineText = $this->inlineText($unmarkedText);
|
|
|
|
$Elements[] = $InlineText['element'];
|
2013-07-11 00:22:16 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$text = \substr($text, $markerPosition + 1);
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2018-03-21 05:18:34 +03:00
|
|
|
$InlineText = $this->inlineText($text);
|
|
|
|
$Elements[] = $InlineText['element'];
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
foreach ($Elements as &$Element) {
|
|
|
|
if (! isset($Element['autobreak'])) {
|
2018-04-08 22:37:36 +03:00
|
|
|
$Element['autobreak'] = false;
|
|
|
|
}
|
2018-04-08 19:33:01 +03:00
|
|
|
}
|
2018-03-21 05:18:34 +03:00
|
|
|
|
|
|
|
return $Elements;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
protected function inlineUrl($Excerpt)
|
2015-01-16 04:18:07 +03:00
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
if ($this->urlsLinked !== true or ! isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/') {
|
2015-01-19 18:05:10 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (\strpos($Excerpt['context'], 'http') !== false
|
|
|
|
and \preg_match('/\bhttps?+:[\/]{2}[^\s<]+\b\/*+/ui', $Excerpt['context'], $matches, \PREG_OFFSET_CAPTURE)
|
2018-04-09 04:32:23 +03:00
|
|
|
) {
|
2017-05-01 05:24:40 +03:00
|
|
|
$url = $matches[0][0];
|
2015-01-26 20:49:17 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$Inline = [
|
|
|
|
'extent' => \strlen($matches[0][0]),
|
2015-01-19 18:05:10 +03:00
|
|
|
'position' => $matches[0][1],
|
2018-12-04 19:24:25 +03:00
|
|
|
'element' => [
|
2015-01-19 18:05:10 +03:00
|
|
|
'name' => 'a',
|
2015-01-26 20:49:17 +03:00
|
|
|
'text' => $url,
|
2018-12-04 19:24:25 +03:00
|
|
|
'attributes' => [
|
2015-01-26 20:49:17 +03:00
|
|
|
'href' => $url,
|
2018-12-04 19:24:25 +03:00
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
2015-01-19 18:05:10 +03:00
|
|
|
|
|
|
|
return $Inline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function inlineUrlTag($Excerpt)
|
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
if (\strpos($Excerpt['text'], '>') !== false and \preg_match('/^<(\w++:\/{2}[^ >]++)>/i', $Excerpt['text'], $matches)) {
|
2017-05-01 05:24:40 +03:00
|
|
|
$url = $matches[1];
|
2015-01-16 04:18:07 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
return [
|
|
|
|
'extent' => \strlen($matches[0]),
|
|
|
|
'element' => [
|
2015-01-16 04:18:07 +03:00
|
|
|
'name' => 'a',
|
|
|
|
'text' => $url,
|
2018-12-04 19:24:25 +03:00
|
|
|
'attributes' => [
|
2015-01-16 04:18:07 +03:00
|
|
|
'href' => $url,
|
2018-12-04 19:24:25 +03:00
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
2015-01-16 04:18:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 16:19:43 +03:00
|
|
|
# ~
|
2015-01-04 19:37:24 +03:00
|
|
|
|
2015-01-08 16:19:43 +03:00
|
|
|
protected function unmarkedText($text)
|
|
|
|
{
|
2018-03-19 02:06:26 +03:00
|
|
|
$Inline = $this->inlineText($text);
|
|
|
|
return $this->element($Inline['element']);
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2015-01-16 04:18:07 +03:00
|
|
|
# Handlers
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2013-11-21 15:39:00 +04:00
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
protected function handle(array $Element)
|
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($Element['handler'])) {
|
|
|
|
if (!isset($Element['nonNestables'])) {
|
|
|
|
$Element['nonNestables'] = [];
|
2018-03-21 05:32:01 +03:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (\is_string($Element['handler'])) {
|
2018-03-21 20:31:40 +03:00
|
|
|
$function = $Element['handler'];
|
|
|
|
$argument = $Element['text'];
|
2018-04-12 23:10:09 +03:00
|
|
|
unset($Element['text']);
|
2018-03-21 20:31:40 +03:00
|
|
|
$destination = 'rawHtml';
|
2018-12-04 19:24:25 +03:00
|
|
|
} else {
|
2018-03-21 20:31:40 +03:00
|
|
|
$function = $Element['handler']['function'];
|
|
|
|
$argument = $Element['handler']['argument'];
|
|
|
|
$destination = $Element['handler']['destination'];
|
|
|
|
}
|
2018-03-21 05:32:01 +03:00
|
|
|
|
2018-03-21 19:02:57 +03:00
|
|
|
$Element[$destination] = $this->{$function}($argument, $Element['nonNestables']);
|
2018-03-31 14:07:53 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if ($destination === 'handler') {
|
2018-03-31 14:07:53 +03:00
|
|
|
$Element = $this->handle($Element);
|
|
|
|
}
|
2018-03-21 05:32:01 +03:00
|
|
|
|
2018-04-08 19:38:09 +03:00
|
|
|
unset($Element['handler']);
|
|
|
|
}
|
2018-03-21 05:32:01 +03:00
|
|
|
|
|
|
|
return $Element;
|
|
|
|
}
|
|
|
|
|
2018-03-28 22:59:56 +03:00
|
|
|
protected function handleElementRecursive(array $Element)
|
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
return $this->elementApplyRecursive([$this, 'handle'], $Element);
|
2018-04-01 18:55:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function handleElementsRecursive(array $Elements)
|
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
return $this->elementsApplyRecursive([$this, 'handle'], $Elements);
|
2018-04-01 18:55:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function elementApplyRecursive($closure, array $Element)
|
2018-04-08 19:39:24 +03:00
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
$Element = \call_user_func($closure, $Element);
|
2018-04-08 19:39:24 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($Element['elements'])) {
|
2018-04-08 19:39:24 +03:00
|
|
|
$Element['elements'] = $this->elementsApplyRecursive($closure, $Element['elements']);
|
2018-12-04 19:24:25 +03:00
|
|
|
} elseif (isset($Element['element'])) {
|
2018-04-08 19:39:24 +03:00
|
|
|
$Element['element'] = $this->elementApplyRecursive($closure, $Element['element']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $Element;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function elementApplyRecursiveDepthFirst($closure, array $Element)
|
2018-04-01 18:55:10 +03:00
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($Element['elements'])) {
|
2018-04-08 20:40:50 +03:00
|
|
|
$Element['elements'] = $this->elementsApplyRecursiveDepthFirst($closure, $Element['elements']);
|
2018-12-04 19:24:25 +03:00
|
|
|
} elseif (isset($Element['element'])) {
|
2018-04-08 20:40:50 +03:00
|
|
|
$Element['element'] = $this->elementsApplyRecursiveDepthFirst($closure, $Element['element']);
|
2018-03-28 22:59:56 +03:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$Element = \call_user_func($closure, $Element);
|
2018-04-06 21:52:25 +03:00
|
|
|
|
2018-03-28 22:59:56 +03:00
|
|
|
return $Element;
|
|
|
|
}
|
|
|
|
|
2018-04-01 18:55:10 +03:00
|
|
|
protected function elementsApplyRecursive($closure, array $Elements)
|
2018-03-28 22:59:56 +03:00
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
foreach ($Elements as &$Element) {
|
2018-04-08 19:49:36 +03:00
|
|
|
$Element = $this->elementApplyRecursive($closure, $Element);
|
2018-04-06 22:55:27 +03:00
|
|
|
}
|
|
|
|
|
2018-04-08 19:49:36 +03:00
|
|
|
return $Elements;
|
2018-03-28 22:59:56 +03:00
|
|
|
}
|
|
|
|
|
2018-04-08 19:39:24 +03:00
|
|
|
protected function elementsApplyRecursiveDepthFirst($closure, array $Elements)
|
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
foreach ($Elements as &$Element) {
|
2018-04-08 19:49:36 +03:00
|
|
|
$Element = $this->elementApplyRecursiveDepthFirst($closure, $Element);
|
2018-04-08 19:39:24 +03:00
|
|
|
}
|
|
|
|
|
2018-04-08 19:49:36 +03:00
|
|
|
return $Elements;
|
2018-04-08 19:39:24 +03:00
|
|
|
}
|
|
|
|
|
2015-01-16 04:18:07 +03:00
|
|
|
protected function element(array $Element)
|
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
if ($this->safeMode) {
|
2017-05-09 21:31:36 +03:00
|
|
|
$Element = $this->sanitiseElement($Element);
|
|
|
|
}
|
2017-05-01 05:24:40 +03:00
|
|
|
|
2018-03-28 22:59:56 +03:00
|
|
|
# identity map if element has no handler
|
2018-03-21 05:32:01 +03:00
|
|
|
$Element = $this->handle($Element);
|
|
|
|
|
2017-06-13 22:28:32 +03:00
|
|
|
$hasName = isset($Element['name']);
|
2015-01-16 04:18:07 +03:00
|
|
|
|
2017-06-13 22:28:32 +03:00
|
|
|
$markup = '';
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if ($hasName) {
|
2018-04-14 17:27:06 +03:00
|
|
|
$markup .= '<' . $Element['name'];
|
2017-06-13 22:28:32 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($Element['attributes'])) {
|
|
|
|
foreach ($Element['attributes'] as $name => $value) {
|
|
|
|
if ($value === null) {
|
2017-06-13 22:28:32 +03:00
|
|
|
continue;
|
|
|
|
}
|
2015-01-16 04:18:07 +03:00
|
|
|
|
2018-04-08 23:24:45 +03:00
|
|
|
$markup .= " $name=\"".self::escape($value).'"';
|
2017-06-13 22:28:32 +03:00
|
|
|
}
|
2015-01-16 04:18:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 22:46:03 +03:00
|
|
|
$permitRawHtml = false;
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($Element['text'])) {
|
2018-03-15 13:42:29 +03:00
|
|
|
$text = $Element['text'];
|
|
|
|
}
|
|
|
|
// very strongly consider an alternative if you're writing an
|
|
|
|
// extension
|
2018-12-04 19:24:25 +03:00
|
|
|
elseif (isset($Element['rawHtml'])) {
|
2018-03-15 22:46:03 +03:00
|
|
|
$text = $Element['rawHtml'];
|
|
|
|
|
2018-03-18 22:42:14 +03:00
|
|
|
$allowRawHtmlInSafeMode = isset($Element['allowRawHtmlInSafeMode']) && $Element['allowRawHtmlInSafeMode'];
|
|
|
|
$permitRawHtml = !$this->safeMode || $allowRawHtmlInSafeMode;
|
2018-03-15 13:42:29 +03:00
|
|
|
}
|
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
$hasContent = isset($text) || isset($Element['element']) || isset($Element['elements']);
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if ($hasContent) {
|
2017-06-13 22:28:32 +03:00
|
|
|
$markup .= $hasName ? '>' : '';
|
2015-01-16 04:18:07 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($Element['elements'])) {
|
2018-03-21 05:32:01 +03:00
|
|
|
$markup .= $this->elements($Element['elements']);
|
2018-12-04 19:24:25 +03:00
|
|
|
} elseif (isset($Element['element'])) {
|
2018-03-21 05:32:01 +03:00
|
|
|
$markup .= $this->element($Element['element']);
|
2018-12-04 19:24:25 +03:00
|
|
|
} else {
|
|
|
|
if (!$permitRawHtml) {
|
2018-03-21 05:32:01 +03:00
|
|
|
$markup .= self::escape($text, true);
|
2018-12-04 19:24:25 +03:00
|
|
|
} else {
|
2018-03-21 05:32:01 +03:00
|
|
|
$markup .= $text;
|
|
|
|
}
|
2015-01-16 04:18:07 +03:00
|
|
|
}
|
|
|
|
|
2018-04-14 17:27:06 +03:00
|
|
|
$markup .= $hasName ? '</' . $Element['name'] . '>' : '';
|
2018-12-04 19:24:25 +03:00
|
|
|
} elseif ($hasName) {
|
2015-01-16 04:18:07 +03:00
|
|
|
$markup .= ' />';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function elements(array $Elements)
|
|
|
|
{
|
|
|
|
$markup = '';
|
|
|
|
|
2018-03-19 01:36:30 +03:00
|
|
|
$autoBreak = true;
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
foreach ($Elements as $Element) {
|
|
|
|
if (empty($Element)) {
|
2018-04-08 22:27:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$autoBreakNext = (
|
|
|
|
isset($Element['autobreak'])
|
2018-04-08 22:37:36 +03:00
|
|
|
? $Element['autobreak'] : isset($Element['name'])
|
2018-03-21 05:18:34 +03:00
|
|
|
);
|
2018-03-19 01:36:30 +03:00
|
|
|
// (autobreak === false) covers both sides of an element
|
2018-03-21 05:18:34 +03:00
|
|
|
$autoBreak = !$autoBreak ? $autoBreak : $autoBreakNext;
|
2018-03-19 01:36:30 +03:00
|
|
|
|
|
|
|
$markup .= ($autoBreak ? "\n" : '') . $this->element($Element);
|
2018-03-21 05:18:34 +03:00
|
|
|
$autoBreak = $autoBreakNext;
|
2015-01-16 04:18:07 +03:00
|
|
|
}
|
|
|
|
|
2018-03-19 01:36:30 +03:00
|
|
|
$markup .= $autoBreak ? "\n" : '';
|
2015-01-16 04:18:07 +03:00
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
2018-12-05 17:07:55 +03:00
|
|
|
protected function li(Lines $Lines)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2018-12-05 17:07:55 +03:00
|
|
|
$Elements = $this->linesElements($Lines);
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2018-12-05 17:07:55 +03:00
|
|
|
if (! $Lines->containsBlankLines()
|
2018-03-21 05:32:01 +03:00
|
|
|
and isset($Elements[0]) and isset($Elements[0]['name'])
|
|
|
|
and $Elements[0]['name'] === 'p'
|
|
|
|
) {
|
|
|
|
unset($Elements[0]['name']);
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
return $Elements;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2014-01-18 17:10:24 +04:00
|
|
|
|
2018-03-19 01:37:40 +03:00
|
|
|
#
|
|
|
|
# AST Convenience
|
|
|
|
#
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace occurrences $regexp with $Elements in $text. Return an array of
|
|
|
|
* elements representing the replacement.
|
|
|
|
*/
|
|
|
|
protected static function pregReplaceElements($regexp, $Elements, $text)
|
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
$newElements = [];
|
2018-03-19 01:37:40 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
while (\preg_match($regexp, $text, $matches, \PREG_OFFSET_CAPTURE)) {
|
2018-03-19 01:37:40 +03:00
|
|
|
$offset = $matches[0][1];
|
2018-12-04 19:24:25 +03:00
|
|
|
$before = \substr($text, 0, $offset);
|
|
|
|
$after = \substr($text, $offset + \strlen($matches[0][0]));
|
2018-03-19 01:37:40 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$newElements[] = ['text' => $before];
|
2018-03-19 01:37:40 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
foreach ($Elements as $Element) {
|
2018-03-19 01:37:40 +03:00
|
|
|
$newElements[] = $Element;
|
|
|
|
}
|
|
|
|
|
|
|
|
$text = $after;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$newElements[] = ['text' => $text];
|
2018-03-19 01:37:40 +03:00
|
|
|
|
|
|
|
return $newElements;
|
|
|
|
}
|
|
|
|
|
2014-02-21 04:22:31 +04:00
|
|
|
#
|
2015-01-16 04:18:07 +03:00
|
|
|
# Deprecated Methods
|
|
|
|
#
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
public function parse($text)
|
2015-01-16 04:18:07 +03:00
|
|
|
{
|
|
|
|
$markup = $this->text($text);
|
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:24:40 +03:00
|
|
|
protected function sanitiseElement(array $Element)
|
|
|
|
{
|
2017-05-03 02:39:01 +03:00
|
|
|
static $goodAttribute = '/^[a-zA-Z0-9][a-zA-Z0-9-_]*+$/';
|
2018-12-04 19:24:25 +03:00
|
|
|
static $safeUrlNameToAtt = [
|
2017-05-01 05:24:40 +03:00
|
|
|
'a' => 'href',
|
|
|
|
'img' => 'src',
|
2018-12-04 19:24:25 +03:00
|
|
|
];
|
2017-05-01 05:24:40 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (! isset($Element['name'])) {
|
2018-03-19 01:36:30 +03:00
|
|
|
unset($Element['attributes']);
|
|
|
|
return $Element;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset($safeUrlNameToAtt[$Element['name']])) {
|
2017-05-01 05:24:40 +03:00
|
|
|
$Element = $this->filterUnsafeUrlInAttribute($Element, $safeUrlNameToAtt[$Element['name']]);
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (! empty($Element['attributes'])) {
|
|
|
|
foreach ($Element['attributes'] as $att => $val) {
|
2017-05-03 02:39:01 +03:00
|
|
|
# filter out badly parsed attribute
|
2018-12-04 19:24:25 +03:00
|
|
|
if (! \preg_match($goodAttribute, $att)) {
|
2017-05-02 02:30:04 +03:00
|
|
|
unset($Element['attributes'][$att]);
|
|
|
|
}
|
2017-05-03 02:39:01 +03:00
|
|
|
# dump onevent attribute
|
2018-12-04 19:24:25 +03:00
|
|
|
elseif (self::striAtStart($att, 'on')) {
|
2017-05-02 02:30:04 +03:00
|
|
|
unset($Element['attributes'][$att]);
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 17:44:04 +03:00
|
|
|
}
|
|
|
|
|
2017-05-01 05:24:40 +03:00
|
|
|
return $Element;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function filterUnsafeUrlInAttribute(array $Element, $attribute)
|
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
foreach ($this->safeLinksWhitelist as $scheme) {
|
|
|
|
if (self::striAtStart($Element['attributes'][$attribute], $scheme)) {
|
2017-05-09 21:31:36 +03:00
|
|
|
return $Element;
|
2017-05-01 05:24:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$Element['attributes'][$attribute] = \str_replace(':', '%3A', $Element['attributes'][$attribute]);
|
2017-05-09 21:31:36 +03:00
|
|
|
|
2017-05-01 05:24:40 +03:00
|
|
|
return $Element;
|
|
|
|
}
|
|
|
|
|
2015-01-16 04:18:07 +03:00
|
|
|
#
|
|
|
|
# Static Methods
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2014-02-21 04:22:31 +04:00
|
|
|
|
2017-05-01 05:24:40 +03:00
|
|
|
protected static function escape($text, $allowQuotes = false)
|
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
return \htmlspecialchars($text, $allowQuotes ? \ENT_NOQUOTES : \ENT_QUOTES, 'UTF-8');
|
2017-05-01 05:24:40 +03:00
|
|
|
}
|
|
|
|
|
2017-05-05 23:55:58 +03:00
|
|
|
protected static function striAtStart($string, $needle)
|
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
$len = \strlen($needle);
|
2017-05-05 23:55:58 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if ($len > \strlen($string)) {
|
2017-05-05 23:55:58 +03:00
|
|
|
return false;
|
2018-12-04 19:24:25 +03:00
|
|
|
} else {
|
|
|
|
return \strtolower(\substr($string, 0, $len)) === \strtolower($needle);
|
2017-05-05 23:55:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
public static function instance($name = 'default')
|
2014-02-21 04:22:31 +04:00
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
if (isset(self::$instances[$name])) {
|
2014-02-21 04:22:31 +04:00
|
|
|
return self::$instances[$name];
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:30:15 +03:00
|
|
|
$instance = new static();
|
2014-02-21 04:22:31 +04:00
|
|
|
|
|
|
|
self::$instances[$name] = $instance;
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
private static $instances = [];
|
2014-02-21 04:22:31 +04:00
|
|
|
|
2014-01-26 21:53:24 +04:00
|
|
|
#
|
2015-01-16 04:18:07 +03:00
|
|
|
# Read-Only
|
2014-01-18 17:10:24 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
protected $specialCharacters = [
|
2018-04-23 16:09:30 +03:00
|
|
|
'\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!', '|', '~'
|
2018-12-04 19:24:25 +03:00
|
|
|
];
|
2014-04-17 11:59:35 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
protected $StrongRegex = [
|
2018-04-08 17:41:18 +03:00
|
|
|
'*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*+[*])+?)[*]{2}(?![*])/s',
|
|
|
|
'_' => '/^__((?:\\\\_|[^_]|_[^_]*+_)+?)__(?!_)/us',
|
2018-12-04 19:24:25 +03:00
|
|
|
];
|
2014-01-18 17:10:24 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
protected $EmRegex = [
|
2014-11-28 15:03:12 +03:00
|
|
|
'*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s',
|
|
|
|
'_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us',
|
2018-12-04 19:24:25 +03:00
|
|
|
];
|
2014-01-27 02:58:18 +04:00
|
|
|
|
2018-04-08 17:41:18 +03:00
|
|
|
protected $regexHtmlAttribute = '[a-zA-Z_:][\w:.-]*+(?:\s*+=\s*+(?:[^"\'=<>`\s]+|"[^"]*+"|\'[^\']*+\'))?+';
|
2015-01-11 03:14:45 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
protected $voidElements = [
|
2014-10-29 23:29:23 +03:00
|
|
|
'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source',
|
2018-12-04 19:24:25 +03:00
|
|
|
];
|
2014-10-29 23:29:23 +03:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
protected $textLevelElements = [
|
2014-01-27 02:58:18 +04:00
|
|
|
'a', 'br', 'bdo', 'abbr', 'blink', 'nextid', 'acronym', 'basefont',
|
|
|
|
'b', 'em', 'big', 'cite', 'small', 'spacer', 'listing',
|
2014-05-01 03:02:14 +04:00
|
|
|
'i', 'rp', 'del', 'code', 'strike', 'marquee',
|
|
|
|
'q', 'rt', 'ins', 'font', 'strong',
|
2017-03-29 19:04:15 +03:00
|
|
|
's', 'tt', 'kbd', 'mark',
|
|
|
|
'u', 'xm', 'sub', 'nobr',
|
|
|
|
'sup', 'ruby',
|
|
|
|
'var', 'span',
|
|
|
|
'wbr', 'time',
|
2018-12-04 19:24:25 +03:00
|
|
|
];
|
2014-01-29 14:30:21 +04:00
|
|
|
}
|