Parsedown.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  1. <?php
  2. namespace Vendor\Parsedown;
  3. #
  4. #
  5. # Parsedown
  6. # http://parsedown.org
  7. #
  8. # (c) Emanuil Rusev
  9. # http://erusev.com
  10. #
  11. # For the full license information, view the LICENSE file that was distributed
  12. # with this source code.
  13. #
  14. #
  15. class Parsedown
  16. {
  17. # ~
  18. const version = '1.7.4';
  19. # ~
  20. function text($text)
  21. {
  22. # make sure no definitions are set
  23. $this->DefinitionData = array();
  24. # standardize line breaks
  25. $text = str_replace(array("\r\n", "\r"), "\n", $text);
  26. # remove surrounding line breaks
  27. $text = trim($text, "\n");
  28. # split text into lines
  29. $lines = explode("\n", $text);
  30. # iterate through lines to identify blocks
  31. $markup = $this->lines($lines);
  32. # trim line breaks
  33. $markup = trim($markup, "\n");
  34. return $markup;
  35. }
  36. #
  37. # Setters
  38. #
  39. function setBreaksEnabled($breaksEnabled)
  40. {
  41. $this->breaksEnabled = $breaksEnabled;
  42. return $this;
  43. }
  44. protected $breaksEnabled;
  45. function setMarkupEscaped($markupEscaped)
  46. {
  47. $this->markupEscaped = $markupEscaped;
  48. return $this;
  49. }
  50. protected $markupEscaped;
  51. function setUrlsLinked($urlsLinked)
  52. {
  53. $this->urlsLinked = $urlsLinked;
  54. return $this;
  55. }
  56. protected $urlsLinked = true;
  57. function setSafeMode($safeMode)
  58. {
  59. $this->safeMode = (bool) $safeMode;
  60. return $this;
  61. }
  62. protected $safeMode;
  63. protected $safeLinksWhitelist = array(
  64. 'http://',
  65. 'https://',
  66. 'ftp://',
  67. 'ftps://',
  68. 'mailto:',
  69. 'data:image/png;base64,',
  70. 'data:image/gif;base64,',
  71. 'data:image/jpeg;base64,',
  72. 'irc:',
  73. 'ircs:',
  74. 'git:',
  75. 'ssh:',
  76. 'news:',
  77. 'steam:',
  78. );
  79. #
  80. # Lines
  81. #
  82. protected $BlockTypes = array(
  83. '#' => array('Header'),
  84. '*' => array('Rule', 'List'),
  85. '+' => array('List'),
  86. '-' => array('SetextHeader', 'Table', 'Rule', 'List'),
  87. '0' => array('List'),
  88. '1' => array('List'),
  89. '2' => array('List'),
  90. '3' => array('List'),
  91. '4' => array('List'),
  92. '5' => array('List'),
  93. '6' => array('List'),
  94. '7' => array('List'),
  95. '8' => array('List'),
  96. '9' => array('List'),
  97. ':' => array('Table'),
  98. '<' => array('Comment', 'Markup'),
  99. '=' => array('SetextHeader'),
  100. '>' => array('Quote'),
  101. '[' => array('Reference'),
  102. '_' => array('Rule'),
  103. '`' => array('FencedCode'),
  104. '|' => array('Table'),
  105. '~' => array('FencedCode'),
  106. );
  107. # ~
  108. protected $unmarkedBlockTypes = array(
  109. 'Code',
  110. );
  111. #
  112. # Blocks
  113. #
  114. /**
  115. * @param mixed[] $lines
  116. */
  117. protected function lines($lines)
  118. {
  119. $CurrentBlock = null;
  120. foreach ($lines as $line)
  121. {
  122. if (chop($line) === '')
  123. {
  124. if (isset($CurrentBlock))
  125. {
  126. $CurrentBlock['interrupted'] = true;
  127. }
  128. continue;
  129. }
  130. if (strpos($line, "\t") !== false)
  131. {
  132. $parts = explode("\t", $line);
  133. $line = $parts[0];
  134. unset($parts[0]);
  135. foreach ($parts as $part)
  136. {
  137. $shortage = 4 - mb_strlen($line, 'utf-8') % 4;
  138. $line .= str_repeat(' ', $shortage);
  139. $line .= $part;
  140. }
  141. }
  142. $indent = 0;
  143. while (isset($line[$indent]) and $line[$indent] === ' ')
  144. {
  145. $indent ++;
  146. }
  147. $text = $indent > 0 ? substr($line, $indent) : $line;
  148. # ~
  149. $Line = array('body' => $line, 'indent' => $indent, 'text' => $text);
  150. # ~
  151. if (isset($CurrentBlock['continuable']))
  152. {
  153. $Block = $this->{'block'.$CurrentBlock['type'].'Continue'}($Line, $CurrentBlock);
  154. if (isset($Block))
  155. {
  156. $CurrentBlock = $Block;
  157. continue;
  158. }
  159. else
  160. {
  161. if ($this->isBlockCompletable($CurrentBlock['type']))
  162. {
  163. $CurrentBlock = $this->{'block'.$CurrentBlock['type'].'Complete'}($CurrentBlock);
  164. }
  165. }
  166. }
  167. # ~
  168. $marker = $text[0];
  169. # ~
  170. $blockTypes = $this->unmarkedBlockTypes;
  171. if (isset($this->BlockTypes[$marker]))
  172. {
  173. foreach ($this->BlockTypes[$marker] as $blockType)
  174. {
  175. $blockTypes []= $blockType;
  176. }
  177. }
  178. #
  179. # ~
  180. foreach ($blockTypes as $blockType)
  181. {
  182. $Block = $this->{'block'.$blockType}($Line, $CurrentBlock);
  183. if (isset($Block))
  184. {
  185. $Block['type'] = $blockType;
  186. if ( ! isset($Block['identified']))
  187. {
  188. $Blocks []= $CurrentBlock;
  189. $Block['identified'] = true;
  190. }
  191. if ($this->isBlockContinuable($blockType))
  192. {
  193. $Block['continuable'] = true;
  194. }
  195. $CurrentBlock = $Block;
  196. continue 2;
  197. }
  198. }
  199. # ~
  200. if (isset($CurrentBlock) and ! isset($CurrentBlock['type']) and ! isset($CurrentBlock['interrupted']))
  201. {
  202. $CurrentBlock['element']['text'] .= "\n".$text;
  203. }
  204. else
  205. {
  206. $Blocks []= $CurrentBlock;
  207. $CurrentBlock = $this->paragraph($Line);
  208. $CurrentBlock['identified'] = true;
  209. }
  210. }
  211. # ~
  212. if (isset($CurrentBlock['continuable']) and $this->isBlockCompletable($CurrentBlock['type']))
  213. {
  214. $CurrentBlock = $this->{'block'.$CurrentBlock['type'].'Complete'}($CurrentBlock);
  215. }
  216. # ~
  217. $Blocks []= $CurrentBlock;
  218. unset($Blocks[0]);
  219. # ~
  220. $markup = '';
  221. foreach ($Blocks as $Block)
  222. {
  223. if (isset($Block['hidden']))
  224. {
  225. continue;
  226. }
  227. $markup .= "\n";
  228. $markup .= isset($Block['markup']) ? $Block['markup'] : $this->element($Block['element']);
  229. }
  230. $markup .= "\n";
  231. # ~
  232. return $markup;
  233. }
  234. protected function isBlockContinuable($Type)
  235. {
  236. return method_exists($this, 'block'.$Type.'Continue');
  237. }
  238. protected function isBlockCompletable($Type)
  239. {
  240. return method_exists($this, 'block'.$Type.'Complete');
  241. }
  242. #
  243. # Code
  244. protected function blockCode($Line, $Block = null)
  245. {
  246. if (isset($Block) and ! isset($Block['type']) and ! isset($Block['interrupted']))
  247. {
  248. return;
  249. }
  250. if ($Line['indent'] >= 4)
  251. {
  252. $text = substr($Line['body'], 4);
  253. $Block = array(
  254. 'element' => array(
  255. 'name' => 'pre',
  256. 'handler' => 'element',
  257. 'text' => array(
  258. 'name' => 'code',
  259. 'text' => $text,
  260. ),
  261. ),
  262. );
  263. return $Block;
  264. }
  265. }
  266. protected function blockCodeContinue($Line, $Block)
  267. {
  268. if ($Line['indent'] >= 4)
  269. {
  270. if (isset($Block['interrupted']))
  271. {
  272. $Block['element']['text']['text'] .= "\n";
  273. unset($Block['interrupted']);
  274. }
  275. $Block['element']['text']['text'] .= "\n";
  276. $text = substr($Line['body'], 4);
  277. $Block['element']['text']['text'] .= $text;
  278. return $Block;
  279. }
  280. }
  281. protected function blockCodeComplete($Block)
  282. {
  283. $text = $Block['element']['text']['text'];
  284. $Block['element']['text']['text'] = $text;
  285. return $Block;
  286. }
  287. #
  288. # Comment
  289. protected function blockComment($Line)
  290. {
  291. if ($this->markupEscaped or $this->safeMode)
  292. {
  293. return;
  294. }
  295. if (isset($Line['text'][3]) and $Line['text'][3] === '-' and $Line['text'][2] === '-' and $Line['text'][1] === '!')
  296. {
  297. $Block = array(
  298. 'markup' => $Line['body'],
  299. );
  300. if (preg_match('/-->$/', $Line['text']))
  301. {
  302. $Block['closed'] = true;
  303. }
  304. return $Block;
  305. }
  306. }
  307. /**
  308. * @param mixed[] $Block
  309. */
  310. protected function blockCommentContinue($Line, $Block)
  311. {
  312. if (isset($Block['closed']))
  313. {
  314. return;
  315. }
  316. $Block['markup'] .= "\n" . $Line['body'];
  317. if (preg_match('/-->$/', $Line['text']))
  318. {
  319. $Block['closed'] = true;
  320. }
  321. return $Block;
  322. }
  323. #
  324. # Fenced Code
  325. protected function blockFencedCode($Line)
  326. {
  327. if (preg_match('/^['.$Line['text'][0].']{3,}[ ]*([^`]+)?[ ]*$/', $Line['text'], $matches))
  328. {
  329. $Element = array(
  330. 'name' => 'code',
  331. 'text' => '',
  332. );
  333. if (isset($matches[1]))
  334. {
  335. /**
  336. * https://www.w3.org/TR/2011/WD-html5-20110525/elements.html#classes
  337. * Every HTML element may have a class attribute specified.
  338. * The attribute, if specified, must have a value that is a set
  339. * of space-separated tokens representing the various classes
  340. * that the element belongs to.
  341. * [...]
  342. * The space characters, for the purposes of this specification,
  343. * are U+0020 SPACE, U+0009 CHARACTER TABULATION (tab),
  344. * U+000A LINE FEED (LF), U+000C FORM FEED (FF), and
  345. * U+000D CARRIAGE RETURN (CR).
  346. */
  347. $language = substr($matches[1], 0, strcspn($matches[1], " \t\n\f\r"));
  348. $class = 'language-'.$language;
  349. $Element['attributes'] = array(
  350. 'class' => $class,
  351. );
  352. }
  353. $Block = array(
  354. 'char' => $Line['text'][0],
  355. 'element' => array(
  356. 'name' => 'pre',
  357. 'handler' => 'element',
  358. 'text' => $Element,
  359. ),
  360. );
  361. return $Block;
  362. }
  363. }
  364. protected function blockFencedCodeContinue($Line, $Block)
  365. {
  366. if (isset($Block['complete']))
  367. {
  368. return;
  369. }
  370. if (isset($Block['interrupted']))
  371. {
  372. $Block['element']['text']['text'] .= "\n";
  373. unset($Block['interrupted']);
  374. }
  375. if (preg_match('/^'.$Block['char'].'{3,}[ ]*$/', $Line['text']))
  376. {
  377. $Block['element']['text']['text'] = substr($Block['element']['text']['text'], 1);
  378. $Block['complete'] = true;
  379. return $Block;
  380. }
  381. $Block['element']['text']['text'] .= "\n".$Line['body'];
  382. return $Block;
  383. }
  384. protected function blockFencedCodeComplete($Block)
  385. {
  386. $text = $Block['element']['text']['text'];
  387. $Block['element']['text']['text'] = $text;
  388. return $Block;
  389. }
  390. #
  391. # Header
  392. protected function blockHeader($Line)
  393. {
  394. if (isset($Line['text'][1]))
  395. {
  396. $level = 1;
  397. while (isset($Line['text'][$level]) and $Line['text'][$level] === '#')
  398. {
  399. $level ++;
  400. }
  401. if ($level > 6)
  402. {
  403. return;
  404. }
  405. $text = trim($Line['text'], '# ');
  406. $Block = array(
  407. 'element' => array(
  408. 'name' => 'h' . min(6, $level),
  409. 'text' => $text,
  410. 'handler' => 'line',
  411. ),
  412. );
  413. return $Block;
  414. }
  415. }
  416. #
  417. # List
  418. protected function blockList($Line)
  419. {
  420. list($name, $pattern) = $Line['text'][0] <= '-' ? array('ul', '[*+-]') : array('ol', '[0-9]+[.]');
  421. if (preg_match('/^('.$pattern.'[ ]+)(.*)/', $Line['text'], $matches))
  422. {
  423. $Block = array(
  424. 'indent' => $Line['indent'],
  425. 'pattern' => $pattern,
  426. 'element' => array(
  427. 'name' => $name,
  428. 'handler' => 'elements',
  429. ),
  430. );
  431. if($name === 'ol')
  432. {
  433. $listStart = stristr($matches[0], '.', true);
  434. if($listStart !== '1')
  435. {
  436. $Block['element']['attributes'] = array('start' => $listStart);
  437. }
  438. }
  439. $Block['li'] = array(
  440. 'name' => 'li',
  441. 'handler' => 'li',
  442. 'text' => array(
  443. $matches[2],
  444. ),
  445. );
  446. $Block['element']['text'] []= & $Block['li'];
  447. return $Block;
  448. }
  449. }
  450. /**
  451. * @param mixed[] $Block
  452. */
  453. protected function blockListContinue($Line, $Block)
  454. {
  455. if ($Block['indent'] === $Line['indent'] and preg_match('/^'.$Block['pattern'].'(?:[ ]+(.*)|$)/', $Line['text'], $matches))
  456. {
  457. if (isset($Block['interrupted']))
  458. {
  459. $Block['li']['text'] []= '';
  460. $Block['loose'] = true;
  461. unset($Block['interrupted']);
  462. }
  463. unset($Block['li']);
  464. $text = isset($matches[1]) ? $matches[1] : '';
  465. $Block['li'] = array(
  466. 'name' => 'li',
  467. 'handler' => 'li',
  468. 'text' => array(
  469. $text,
  470. ),
  471. );
  472. $Block['element']['text'] []= & $Block['li'];
  473. return $Block;
  474. }
  475. if ($Line['text'][0] === '[' and $this->blockReference($Line))
  476. {
  477. return $Block;
  478. }
  479. if ( ! isset($Block['interrupted']))
  480. {
  481. $text = preg_replace('/^[ ]{0,4}/', '', $Line['body']);
  482. $Block['li']['text'] []= $text;
  483. return $Block;
  484. }
  485. if ($Line['indent'] > 0)
  486. {
  487. $Block['li']['text'] []= '';
  488. $text = preg_replace('/^[ ]{0,4}/', '', $Line['body']);
  489. $Block['li']['text'] []= $text;
  490. unset($Block['interrupted']);
  491. return $Block;
  492. }
  493. }
  494. /**
  495. * @param mixed[] $Block
  496. */
  497. protected function blockListComplete($Block)
  498. {
  499. if (isset($Block['loose']))
  500. {
  501. foreach ($Block['element']['text'] as &$li)
  502. {
  503. if (end($li['text']) !== '')
  504. {
  505. $li['text'] []= '';
  506. }
  507. }
  508. }
  509. return $Block;
  510. }
  511. #
  512. # Quote
  513. protected function blockQuote($Line)
  514. {
  515. if (preg_match('/^>[ ]?(.*)/', $Line['text'], $matches))
  516. {
  517. $Block = array(
  518. 'element' => array(
  519. 'name' => 'blockquote',
  520. 'handler' => 'lines',
  521. 'text' => (array) $matches[1],
  522. ),
  523. );
  524. return $Block;
  525. }
  526. }
  527. /**
  528. * @param mixed[] $Block
  529. */
  530. protected function blockQuoteContinue($Line, $Block)
  531. {
  532. if ($Line['text'][0] === '>' and preg_match('/^>[ ]?(.*)/', $Line['text'], $matches))
  533. {
  534. if (isset($Block['interrupted']))
  535. {
  536. $Block['element']['text'] []= '';
  537. unset($Block['interrupted']);
  538. }
  539. $Block['element']['text'] []= $matches[1];
  540. return $Block;
  541. }
  542. if ( ! isset($Block['interrupted']))
  543. {
  544. $Block['element']['text'] []= $Line['text'];
  545. return $Block;
  546. }
  547. }
  548. #
  549. # Rule
  550. protected function blockRule($Line)
  551. {
  552. if (preg_match('/^(['.$Line['text'][0].'])([ ]*\1){2,}[ ]*$/', $Line['text']))
  553. {
  554. $Block = array(
  555. 'element' => array(
  556. 'name' => 'hr'
  557. ),
  558. );
  559. return $Block;
  560. }
  561. }
  562. #
  563. # Setext
  564. /**
  565. * @param mixed[]|null $Block
  566. */
  567. protected function blockSetextHeader($Line, $Block = null)
  568. {
  569. if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted']))
  570. {
  571. return;
  572. }
  573. if (chop($Line['text'], $Line['text'][0]) === '')
  574. {
  575. $Block['element']['name'] = $Line['text'][0] === '=' ? 'h1' : 'h2';
  576. return $Block;
  577. }
  578. }
  579. #
  580. # Markup
  581. protected function blockMarkup($Line)
  582. {
  583. if ($this->markupEscaped or $this->safeMode)
  584. {
  585. return;
  586. }
  587. if (preg_match('/^<(\w[\w-]*)(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*(\/)?>/', $Line['text'], $matches))
  588. {
  589. $element = strtolower($matches[1]);
  590. if (in_array($element, $this->textLevelElements))
  591. {
  592. return;
  593. }
  594. $Block = array(
  595. 'name' => $matches[1],
  596. 'depth' => 0,
  597. 'markup' => $Line['text'],
  598. );
  599. $length = strlen($matches[0]);
  600. $remainder = substr($Line['text'], $length);
  601. if (trim($remainder) === '')
  602. {
  603. if (isset($matches[2]) or in_array($matches[1], $this->voidElements))
  604. {
  605. $Block['closed'] = true;
  606. $Block['void'] = true;
  607. }
  608. }
  609. else
  610. {
  611. if (isset($matches[2]) or in_array($matches[1], $this->voidElements))
  612. {
  613. return;
  614. }
  615. if (preg_match('/<\/'.$matches[1].'>[ ]*$/i', $remainder))
  616. {
  617. $Block['closed'] = true;
  618. }
  619. }
  620. return $Block;
  621. }
  622. }
  623. /**
  624. * @param mixed[] $Block
  625. */
  626. protected function blockMarkupContinue($Line, $Block)
  627. {
  628. if (isset($Block['closed']))
  629. {
  630. return;
  631. }
  632. if (preg_match('/^<'.$Block['name'].'(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*>/i', $Line['text'])) # open
  633. {
  634. $Block['depth'] ++;
  635. }
  636. if (preg_match('/(.*?)<\/'.$Block['name'].'>[ ]*$/i', $Line['text'], $matches)) # close
  637. {
  638. if ($Block['depth'] > 0)
  639. {
  640. $Block['depth'] --;
  641. }
  642. else
  643. {
  644. $Block['closed'] = true;
  645. }
  646. }
  647. if (isset($Block['interrupted']))
  648. {
  649. $Block['markup'] .= "\n";
  650. unset($Block['interrupted']);
  651. }
  652. $Block['markup'] .= "\n".$Line['body'];
  653. return $Block;
  654. }
  655. #
  656. # Reference
  657. protected function blockReference($Line)
  658. {
  659. if (preg_match('/^\[(.+?)\]:[ ]*<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches))
  660. {
  661. $id = strtolower($matches[1]);
  662. $Data = array(
  663. 'url' => $matches[2],
  664. 'title' => null,
  665. );
  666. if (isset($matches[3]))
  667. {
  668. $Data['title'] = $matches[3];
  669. }
  670. $this->DefinitionData['Reference'][$id] = $Data;
  671. $Block = array(
  672. 'hidden' => true,
  673. );
  674. return $Block;
  675. }
  676. }
  677. #
  678. # Table
  679. /**
  680. * @param mixed[]|null $Block
  681. */
  682. protected function blockTable($Line, $Block = null)
  683. {
  684. if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted']))
  685. {
  686. return;
  687. }
  688. if (strpos($Block['element']['text'], '|') !== false and chop($Line['text'], ' -:|') === '')
  689. {
  690. $alignments = array();
  691. $divider = $Line['text'];
  692. $divider = trim($divider);
  693. $divider = trim($divider, '|');
  694. $dividerCells = explode('|', $divider);
  695. foreach ($dividerCells as $dividerCell)
  696. {
  697. $dividerCell = trim($dividerCell);
  698. if ($dividerCell === '')
  699. {
  700. continue;
  701. }
  702. $alignment = null;
  703. if ($dividerCell[0] === ':')
  704. {
  705. $alignment = 'left';
  706. }
  707. if (substr($dividerCell, - 1) === ':')
  708. {
  709. $alignment = $alignment === 'left' ? 'center' : 'right';
  710. }
  711. $alignments []= $alignment;
  712. }
  713. # ~
  714. $HeaderElements = array();
  715. $header = $Block['element']['text'];
  716. $header = trim($header);
  717. $header = trim($header, '|');
  718. $headerCells = explode('|', $header);
  719. foreach ($headerCells as $index => $headerCell)
  720. {
  721. $headerCell = trim($headerCell);
  722. $HeaderElement = array(
  723. 'name' => 'th',
  724. 'text' => $headerCell,
  725. 'handler' => 'line',
  726. );
  727. if (isset($alignments[$index]))
  728. {
  729. $alignment = $alignments[$index];
  730. $HeaderElement['attributes'] = array(
  731. 'style' => 'text-align: '.$alignment.';',
  732. );
  733. }
  734. $HeaderElements []= $HeaderElement;
  735. }
  736. # ~
  737. $Block = array(
  738. 'alignments' => $alignments,
  739. 'identified' => true,
  740. 'element' => array(
  741. 'name' => 'table',
  742. 'handler' => 'elements',
  743. ),
  744. );
  745. $Block['element']['text'] []= array(
  746. 'name' => 'thead',
  747. 'handler' => 'elements',
  748. );
  749. $Block['element']['text'] []= array(
  750. 'name' => 'tbody',
  751. 'handler' => 'elements',
  752. 'text' => array(),
  753. );
  754. $Block['element']['text'][0]['text'] []= array(
  755. 'name' => 'tr',
  756. 'handler' => 'elements',
  757. 'text' => $HeaderElements,
  758. );
  759. return $Block;
  760. }
  761. }
  762. /**
  763. * @param mixed[] $Block
  764. */
  765. protected function blockTableContinue($Line, $Block)
  766. {
  767. if (isset($Block['interrupted']))
  768. {
  769. return;
  770. }
  771. if ($Line['text'][0] === '|' or strpos($Line['text'], '|'))
  772. {
  773. $Elements = array();
  774. $row = $Line['text'];
  775. $row = trim($row);
  776. $row = trim($row, '|');
  777. preg_match_all('/(?:(\\\\[|])|[^|`]|`[^`]+`|`)+/', $row, $matches);
  778. foreach ($matches[0] as $index => $cell)
  779. {
  780. $cell = trim($cell);
  781. $Element = array(
  782. 'name' => 'td',
  783. 'handler' => 'line',
  784. 'text' => $cell,
  785. );
  786. if (isset($Block['alignments'][$index]))
  787. {
  788. $Element['attributes'] = array(
  789. 'style' => 'text-align: '.$Block['alignments'][$index].';',
  790. );
  791. }
  792. $Elements []= $Element;
  793. }
  794. $Element = array(
  795. 'name' => 'tr',
  796. 'handler' => 'elements',
  797. 'text' => $Elements,
  798. );
  799. $Block['element']['text'][1]['text'] []= $Element;
  800. return $Block;
  801. }
  802. }
  803. #
  804. # ~
  805. #
  806. protected function paragraph($Line)
  807. {
  808. $Block = array(
  809. 'element' => array(
  810. 'name' => 'p',
  811. 'text' => $Line['text'],
  812. 'handler' => 'line',
  813. ),
  814. );
  815. return $Block;
  816. }
  817. #
  818. # Inline Elements
  819. #
  820. protected $InlineTypes = array(
  821. '"' => array('SpecialCharacter'),
  822. '!' => array('Image'),
  823. '&' => array('SpecialCharacter'),
  824. '*' => array('Emphasis'),
  825. ':' => array('Url'),
  826. '<' => array('UrlTag', 'EmailTag', 'Markup', 'SpecialCharacter'),
  827. '>' => array('SpecialCharacter'),
  828. '[' => array('Link'),
  829. '_' => array('Emphasis'),
  830. '`' => array('Code'),
  831. '~' => array('Strikethrough'),
  832. '\\' => array('EscapeSequence'),
  833. );
  834. # ~
  835. protected $inlineMarkerList = '!"*_&[:<>`~\\';
  836. #
  837. # ~
  838. #
  839. public function line($text, $nonNestables=array())
  840. {
  841. $markup = '';
  842. # $excerpt is based on the first occurrence of a marker
  843. while ($excerpt = strpbrk($text, $this->inlineMarkerList))
  844. {
  845. $marker = $excerpt[0];
  846. $markerPosition = strpos($text, $marker);
  847. $Excerpt = array('text' => $excerpt, 'context' => $text);
  848. foreach ($this->InlineTypes[$marker] as $inlineType)
  849. {
  850. # check to see if the current inline type is nestable in the current context
  851. if ( ! empty($nonNestables) and in_array($inlineType, $nonNestables))
  852. {
  853. continue;
  854. }
  855. $Inline = $this->{'inline'.$inlineType}($Excerpt);
  856. if ( ! isset($Inline))
  857. {
  858. continue;
  859. }
  860. # makes sure that the inline belongs to "our" marker
  861. if (isset($Inline['position']) and $Inline['position'] > $markerPosition)
  862. {
  863. continue;
  864. }
  865. # sets a default inline position
  866. if ( ! isset($Inline['position']))
  867. {
  868. $Inline['position'] = $markerPosition;
  869. }
  870. # cause the new element to 'inherit' our non nestables
  871. foreach ($nonNestables as $non_nestable)
  872. {
  873. $Inline['element']['nonNestables'][] = $non_nestable;
  874. }
  875. # the text that comes before the inline
  876. $unmarkedText = substr($text, 0, $Inline['position']);
  877. # compile the unmarked text
  878. $markup .= $this->unmarkedText($unmarkedText);
  879. # compile the inline
  880. $markup .= isset($Inline['markup']) ? $Inline['markup'] : $this->element($Inline['element']);
  881. # remove the examined text
  882. $text = substr($text, $Inline['position'] + $Inline['extent']);
  883. continue 2;
  884. }
  885. # the marker does not belong to an inline
  886. $unmarkedText = substr($text, 0, $markerPosition + 1);
  887. $markup .= $this->unmarkedText($unmarkedText);
  888. $text = substr($text, $markerPosition + 1);
  889. }
  890. $markup .= $this->unmarkedText($text);
  891. return $markup;
  892. }
  893. #
  894. # ~
  895. #
  896. protected function inlineCode($Excerpt)
  897. {
  898. $marker = $Excerpt['text'][0];
  899. if (preg_match('/^('.$marker.'+)[ ]*(.+?)[ ]*(?<!'.$marker.')\1(?!'.$marker.')/s', $Excerpt['text'], $matches))
  900. {
  901. $text = $matches[2];
  902. $text = preg_replace("/[ ]*\n/", ' ', $text);
  903. return array(
  904. 'extent' => strlen($matches[0]),
  905. 'element' => array(
  906. 'name' => 'code',
  907. 'text' => $text,
  908. ),
  909. );
  910. }
  911. }
  912. protected function inlineEmailTag($Excerpt)
  913. {
  914. if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<((mailto:)?\S+?@\S+?)>/i', $Excerpt['text'], $matches))
  915. {
  916. $url = $matches[1];
  917. if ( ! isset($matches[2]))
  918. {
  919. $url = 'mailto:' . $url;
  920. }
  921. return array(
  922. 'extent' => strlen($matches[0]),
  923. 'element' => array(
  924. 'name' => 'a',
  925. 'text' => $matches[1],
  926. 'attributes' => array(
  927. 'href' => $url,
  928. ),
  929. ),
  930. );
  931. }
  932. }
  933. protected function inlineEmphasis($Excerpt)
  934. {
  935. if ( ! isset($Excerpt['text'][1]))
  936. {
  937. return;
  938. }
  939. $marker = $Excerpt['text'][0];
  940. if ($Excerpt['text'][1] === $marker and preg_match($this->StrongRegex[$marker], $Excerpt['text'], $matches))
  941. {
  942. $emphasis = 'strong';
  943. }
  944. elseif (preg_match($this->EmRegex[$marker], $Excerpt['text'], $matches))
  945. {
  946. $emphasis = 'em';
  947. }
  948. else
  949. {
  950. return;
  951. }
  952. return array(
  953. 'extent' => strlen($matches[0]),
  954. 'element' => array(
  955. 'name' => $emphasis,
  956. 'handler' => 'line',
  957. 'text' => $matches[1],
  958. ),
  959. );
  960. }
  961. protected function inlineEscapeSequence($Excerpt)
  962. {
  963. if (isset($Excerpt['text'][1]) and in_array($Excerpt['text'][1], $this->specialCharacters))
  964. {
  965. return array(
  966. 'markup' => $Excerpt['text'][1],
  967. 'extent' => 2,
  968. );
  969. }
  970. }
  971. protected function inlineImage($Excerpt)
  972. {
  973. if ( ! isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[')
  974. {
  975. return;
  976. }
  977. $Excerpt['text']= substr($Excerpt['text'], 1);
  978. $Link = $this->inlineLink($Excerpt);
  979. if ($Link === null)
  980. {
  981. return;
  982. }
  983. $Inline = array(
  984. 'extent' => $Link['extent'] + 1,
  985. 'element' => array(
  986. 'name' => 'img',
  987. 'attributes' => array(
  988. 'src' => $Link['element']['attributes']['href'],
  989. 'alt' => $Link['element']['text'],
  990. ),
  991. ),
  992. );
  993. $Inline['element']['attributes'] += $Link['element']['attributes'];
  994. unset($Inline['element']['attributes']['href']);
  995. return $Inline;
  996. }
  997. protected function inlineLink($Excerpt)
  998. {
  999. $Element = array(
  1000. 'name' => 'a',
  1001. 'handler' => 'line',
  1002. 'nonNestables' => array('Url', 'Link'),
  1003. 'text' => null,
  1004. 'attributes' => array(
  1005. 'href' => null,
  1006. 'title' => null,
  1007. ),
  1008. );
  1009. $extent = 0;
  1010. $remainder = $Excerpt['text'];
  1011. if (preg_match('/\[((?:[^][]++|(?R))*+)\]/', $remainder, $matches))
  1012. {
  1013. $Element['text'] = $matches[1];
  1014. $extent += strlen($matches[0]);
  1015. $remainder = substr($remainder, $extent);
  1016. }
  1017. else
  1018. {
  1019. return;
  1020. }
  1021. if (preg_match('/^[(]\s*+((?:[^ ()]++|[(][^ )]+[)])++)(?:[ ]+("[^"]*"|\'[^\']*\'))?\s*[)]/', $remainder, $matches))
  1022. {
  1023. $Element['attributes']['href'] = $matches[1];
  1024. if (isset($matches[2]))
  1025. {
  1026. $Element['attributes']['title'] = substr($matches[2], 1, - 1);
  1027. }
  1028. $extent += strlen($matches[0]);
  1029. }
  1030. else
  1031. {
  1032. if (preg_match('/^\s*\[(.*?)\]/', $remainder, $matches))
  1033. {
  1034. $definition = strlen($matches[1]) ? $matches[1] : $Element['text'];
  1035. $definition = strtolower($definition);
  1036. $extent += strlen($matches[0]);
  1037. }
  1038. else
  1039. {
  1040. $definition = strtolower($Element['text']);
  1041. }
  1042. if ( ! isset($this->DefinitionData['Reference'][$definition]))
  1043. {
  1044. return;
  1045. }
  1046. $Definition = $this->DefinitionData['Reference'][$definition];
  1047. $Element['attributes']['href'] = $Definition['url'];
  1048. $Element['attributes']['title'] = $Definition['title'];
  1049. }
  1050. return array(
  1051. 'extent' => $extent,
  1052. 'element' => $Element,
  1053. );
  1054. }
  1055. protected function inlineMarkup($Excerpt)
  1056. {
  1057. if ($this->markupEscaped or $this->safeMode or strpos($Excerpt['text'], '>') === false)
  1058. {
  1059. return;
  1060. }
  1061. if ($Excerpt['text'][1] === '/' and preg_match('/^<\/\w[\w-]*[ ]*>/s', $Excerpt['text'], $matches))
  1062. {
  1063. return array(
  1064. 'markup' => $matches[0],
  1065. 'extent' => strlen($matches[0]),
  1066. );
  1067. }
  1068. if ($Excerpt['text'][1] === '!' and preg_match('/^<!---?[^>-](?:-?[^-])*-->/s', $Excerpt['text'], $matches))
  1069. {
  1070. return array(
  1071. 'markup' => $matches[0],
  1072. 'extent' => strlen($matches[0]),
  1073. );
  1074. }
  1075. if ($Excerpt['text'][1] !== ' ' and preg_match('/^<\w[\w-]*(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*\/?>/s', $Excerpt['text'], $matches))
  1076. {
  1077. return array(
  1078. 'markup' => $matches[0],
  1079. 'extent' => strlen($matches[0]),
  1080. );
  1081. }
  1082. }
  1083. protected function inlineSpecialCharacter($Excerpt)
  1084. {
  1085. if ($Excerpt['text'][0] === '&' and ! preg_match('/^&#?\w+;/', $Excerpt['text']))
  1086. {
  1087. return array(
  1088. 'markup' => '&amp;',
  1089. 'extent' => 1,
  1090. );
  1091. }
  1092. $SpecialCharacter = array('>' => 'gt', '<' => 'lt', '"' => 'quot');
  1093. if (isset($SpecialCharacter[$Excerpt['text'][0]]))
  1094. {
  1095. return array(
  1096. 'markup' => '&'.$SpecialCharacter[$Excerpt['text'][0]].';',
  1097. 'extent' => 1,
  1098. );
  1099. }
  1100. }
  1101. protected function inlineStrikethrough($Excerpt)
  1102. {
  1103. if ( ! isset($Excerpt['text'][1]))
  1104. {
  1105. return;
  1106. }
  1107. if ($Excerpt['text'][1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches))
  1108. {
  1109. return array(
  1110. 'extent' => strlen($matches[0]),
  1111. 'element' => array(
  1112. 'name' => 'del',
  1113. 'text' => $matches[1],
  1114. 'handler' => 'line',
  1115. ),
  1116. );
  1117. }
  1118. }
  1119. protected function inlineUrl($Excerpt)
  1120. {
  1121. if ($this->urlsLinked !== true or ! isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/')
  1122. {
  1123. return;
  1124. }
  1125. if (preg_match('/\bhttps?:[\/]{2}[^\s<]+\b\/*/ui', $Excerpt['context'], $matches, PREG_OFFSET_CAPTURE))
  1126. {
  1127. $url = $matches[0][0];
  1128. $Inline = array(
  1129. 'extent' => strlen($matches[0][0]),
  1130. 'position' => $matches[0][1],
  1131. 'element' => array(
  1132. 'name' => 'a',
  1133. 'text' => $url,
  1134. 'attributes' => array(
  1135. 'href' => $url,
  1136. ),
  1137. ),
  1138. );
  1139. return $Inline;
  1140. }
  1141. }
  1142. protected function inlineUrlTag($Excerpt)
  1143. {
  1144. if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(\w+:\/{2}[^ >]+)>/i', $Excerpt['text'], $matches))
  1145. {
  1146. $url = $matches[1];
  1147. return array(
  1148. 'extent' => strlen($matches[0]),
  1149. 'element' => array(
  1150. 'name' => 'a',
  1151. 'text' => $url,
  1152. 'attributes' => array(
  1153. 'href' => $url,
  1154. ),
  1155. ),
  1156. );
  1157. }
  1158. }
  1159. # ~
  1160. protected function unmarkedText($text)
  1161. {
  1162. if ($this->breaksEnabled)
  1163. {
  1164. $text = preg_replace('/[ ]*\n/', "<br />\n", $text);
  1165. }
  1166. else
  1167. {
  1168. $text = preg_replace('/(?:[ ][ ]+|[ ]*\\\\)\n/', "<br />\n", $text);
  1169. $text = str_replace(" \n", "\n", $text);
  1170. }
  1171. return $text;
  1172. }
  1173. #
  1174. # Handlers
  1175. #
  1176. /**
  1177. * @param mixed[] $Element
  1178. */
  1179. protected function element($Element)
  1180. {
  1181. if ($this->safeMode)
  1182. {
  1183. $Element = $this->sanitiseElement($Element);
  1184. }
  1185. $markup = '<'.$Element['name'];
  1186. if (isset($Element['attributes']))
  1187. {
  1188. foreach ($Element['attributes'] as $name => $value)
  1189. {
  1190. if ($value === null)
  1191. {
  1192. continue;
  1193. }
  1194. $markup .= ' '.$name.'="'.self::escape($value).'"';
  1195. }
  1196. }
  1197. $permitRawHtml = false;
  1198. if (isset($Element['text']))
  1199. {
  1200. $text = $Element['text'];
  1201. }
  1202. // very strongly consider an alternative if you're writing an
  1203. // extension
  1204. elseif (isset($Element['rawHtml']))
  1205. {
  1206. $text = $Element['rawHtml'];
  1207. $allowRawHtmlInSafeMode = isset($Element['allowRawHtmlInSafeMode']) && $Element['allowRawHtmlInSafeMode'];
  1208. $permitRawHtml = !$this->safeMode || $allowRawHtmlInSafeMode;
  1209. }
  1210. if (isset($text))
  1211. {
  1212. $markup .= '>';
  1213. if (!isset($Element['nonNestables']))
  1214. {
  1215. $Element['nonNestables'] = array();
  1216. }
  1217. if (isset($Element['handler']))
  1218. {
  1219. $markup .= $this->{$Element['handler']}($text, $Element['nonNestables']);
  1220. }
  1221. elseif (!$permitRawHtml)
  1222. {
  1223. $markup .= self::escape($text, true);
  1224. }
  1225. else
  1226. {
  1227. $markup .= $text;
  1228. }
  1229. $markup .= '</'.$Element['name'].'>';
  1230. }
  1231. else
  1232. {
  1233. $markup .= ' />';
  1234. }
  1235. return $markup;
  1236. }
  1237. /**
  1238. * @param mixed[] $Elements
  1239. */
  1240. protected function elements($Elements)
  1241. {
  1242. $markup = '';
  1243. foreach ($Elements as $Element)
  1244. {
  1245. $markup .= "\n" . $this->element($Element);
  1246. }
  1247. $markup .= "\n";
  1248. return $markup;
  1249. }
  1250. # ~
  1251. protected function li($lines)
  1252. {
  1253. $markup = $this->lines($lines);
  1254. $trimmedMarkup = trim($markup);
  1255. if ( ! in_array('', $lines) and substr($trimmedMarkup, 0, 3) === '<p>')
  1256. {
  1257. $markup = $trimmedMarkup;
  1258. $markup = substr($markup, 3);
  1259. $position = strpos($markup, "</p>");
  1260. $markup = substr_replace($markup, '', $position, 4);
  1261. }
  1262. return $markup;
  1263. }
  1264. #
  1265. # Deprecated Methods
  1266. #
  1267. function parse($text)
  1268. {
  1269. $markup = $this->text($text);
  1270. return $markup;
  1271. }
  1272. /**
  1273. * @param mixed[] $Element
  1274. */
  1275. protected function sanitiseElement($Element)
  1276. {
  1277. static $goodAttribute = '/^[a-zA-Z0-9][a-zA-Z0-9-_]*+$/';
  1278. static $safeUrlNameToAtt = array(
  1279. 'a' => 'href',
  1280. 'img' => 'src',
  1281. );
  1282. if (isset($safeUrlNameToAtt[$Element['name']]))
  1283. {
  1284. $Element = $this->filterUnsafeUrlInAttribute($Element, $safeUrlNameToAtt[$Element['name']]);
  1285. }
  1286. if ( ! empty($Element['attributes']))
  1287. {
  1288. foreach ($Element['attributes'] as $att => $val)
  1289. {
  1290. # filter out badly parsed attribute
  1291. if ( ! preg_match($goodAttribute, $att))
  1292. {
  1293. unset($Element['attributes'][$att]);
  1294. }
  1295. # dump onevent attribute
  1296. elseif (self::striAtStart($att, 'on'))
  1297. {
  1298. unset($Element['attributes'][$att]);
  1299. }
  1300. }
  1301. }
  1302. return $Element;
  1303. }
  1304. /**
  1305. * @param mixed[] $Element
  1306. */
  1307. protected function filterUnsafeUrlInAttribute($Element, $attribute)
  1308. {
  1309. foreach ($this->safeLinksWhitelist as $scheme)
  1310. {
  1311. if (self::striAtStart($Element['attributes'][$attribute], $scheme))
  1312. {
  1313. return $Element;
  1314. }
  1315. }
  1316. $Element['attributes'][$attribute] = str_replace(':', '%3A', $Element['attributes'][$attribute]);
  1317. return $Element;
  1318. }
  1319. #
  1320. # Static Methods
  1321. #
  1322. protected static function escape($text, $allowQuotes = false)
  1323. {
  1324. return htmlspecialchars($text, $allowQuotes ? ENT_NOQUOTES : ENT_QUOTES, 'UTF-8');
  1325. }
  1326. protected static function striAtStart($string, $needle)
  1327. {
  1328. $len = strlen($needle);
  1329. if ($len > strlen($string))
  1330. {
  1331. return false;
  1332. }
  1333. else
  1334. {
  1335. return strtolower(substr($string, 0, $len)) === strtolower($needle);
  1336. }
  1337. }
  1338. static function instance($name = 'default')
  1339. {
  1340. if (isset(self::$instances[$name]))
  1341. {
  1342. return self::$instances[$name];
  1343. }
  1344. $instance = new static();
  1345. self::$instances[$name] = $instance;
  1346. return $instance;
  1347. }
  1348. private static $instances = array();
  1349. #
  1350. # Fields
  1351. #
  1352. protected $DefinitionData;
  1353. #
  1354. # Read-Only
  1355. protected $specialCharacters = array(
  1356. '\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!', '|',
  1357. );
  1358. protected $StrongRegex = array(
  1359. '*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*[*])+?)[*]{2}(?![*])/s',
  1360. '_' => '/^__((?:\\\\_|[^_]|_[^_]*_)+?)__(?!_)/us',
  1361. );
  1362. protected $EmRegex = array(
  1363. '*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s',
  1364. '_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us',
  1365. );
  1366. protected $regexHtmlAttribute = '[a-zA-Z_:][\w:.-]*(?:\s*=\s*(?:[^"\'=<>`\s]+|"[^"]*"|\'[^\']*\'))?';
  1367. protected $voidElements = array(
  1368. 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source',
  1369. );
  1370. protected $textLevelElements = array(
  1371. 'a', 'br', 'bdo', 'abbr', 'blink', 'nextid', 'acronym', 'basefont',
  1372. 'b', 'em', 'big', 'cite', 'small', 'spacer', 'listing',
  1373. 'i', 'rp', 'del', 'code', 'strike', 'marquee',
  1374. 'q', 'rt', 'ins', 'font', 'strong',
  1375. 's', 'tt', 'kbd', 'mark',
  1376. 'u', 'xm', 'sub', 'nobr',
  1377. 'sup', 'ruby',
  1378. 'var', 'span',
  1379. 'wbr', 'time',
  1380. );
  1381. }