3 * @copyright Copyright (c) 2014 Carsten Brandt
4 * @license https://github.com/cebe/markdown/blob/master/LICENSE
5 * @link https://github.com/cebe/markdown#readme
8 namespace cebe\markdown\block;
11 * Adds inline and block HTML support
16 * @var array HTML elements considered as inline elements.
17 * @see http://www.w3.org/wiki/HTML/Elements#Text-level_semantics
19 protected $inlineHtmlElements = [
20 'a', 'abbr', 'acronym',
21 'b', 'basefont', 'bdo', 'big', 'br', 'button', 'blink',
26 'i', 'img', 'ins', 'input', 'iframe',
34 's', 'samp', 'script', 'select', 'small', 'spacer', 'span', 'strong', 'sub', 'sup',
41 * @var array HTML elements known to be self-closing.
43 protected $selfClosingHtmlElements = [
44 'br', 'hr', 'img', 'input', 'nobr',
48 * identify a line as the beginning of a HTML block.
50 protected function identifyHtml($line, $lines, $current)
52 if ($line[0] !== '<' || isset($line[1]) && $line[1] == ' ') {
53 return false; // no html tag
56 if (strncmp($line, '<!--', 4) === 0) {
57 return true; // a html comment
60 $gtPos = strpos($lines[$current], '>');
61 $spacePos = strpos($lines[$current], ' ');
62 if ($gtPos === false && $spacePos === false) {
63 return false; // no html tag
64 } elseif ($spacePos === false) {
65 $tag = rtrim(substr($line, 1, $gtPos - 1), '/');
67 $tag = rtrim(substr($line, 1, min($gtPos, $spacePos) - 1), '/');
70 if (!ctype_alnum($tag) || in_array(strtolower($tag), $this->inlineHtmlElements)) {
71 return false; // no html tag or inline html tag
77 * Consume lines for an HTML block
79 protected function consumeHtml($lines, $current)
82 if (strncmp($lines[$current], '<!--', 4) === 0) { // html comment
83 for ($i = $current, $count = count($lines); $i < $count; $i++) {
86 if (strpos($line, '-->') !== false) {
91 $tag = rtrim(substr($lines[$current], 1, min(strpos($lines[$current], '>'), strpos($lines[$current] . ' ', ' ')) - 1), '/');
93 if (in_array($tag, $this->selfClosingHtmlElements)) {
96 for ($i = $current, $count = count($lines); $i < $count; $i++) {
99 $level += substr_count($line, "<$tag") - substr_count($line, "</$tag>");
107 'content' => implode("\n", $content),
113 * Renders an HTML block
115 protected function renderHtml($block)
117 return $block['content'] . "\n";
121 * Parses an & or a html entity definition.
124 protected function parseEntity($text)
126 // html entities e.g. © © ©
127 if (preg_match('/^&#?[\w\d]+;/', $text, $matches)) {
128 return [['inlineHtml', $matches[0]], strlen($matches[0])];
130 return [['text', '&'], 1];
135 * renders a html entity.
137 protected function renderInlineHtml($block)
143 * Parses inline HTML.
146 protected function parseInlineHtml($text)
148 if (strpos($text, '>') !== false) {
149 if (preg_match('~^</?(\w+\d?)( .*?)?>~', $text, $matches)) {
151 return [['inlineHtml', $matches[0]], strlen($matches[0])];
152 } elseif (preg_match('~^<!--.*?-->~', $text, $matches)) {
154 return [['inlineHtml', $matches[0]], strlen($matches[0])];
157 return [['text', '<'], 1];
161 * Escapes `>` characters.
164 protected function parseGt($text)
166 return [['text', '>'], 1];