From 36742a6fdba89466ff609cc8a5a62f9dcd2918f8 Mon Sep 17 00:00:00 2001 From: joyqi Date: Sat, 4 Nov 2017 12:12:30 +0800 Subject: [PATCH] fix #650 ref #580 --- admin/js/hyperdown.js | 30 ++++++++++++++++++++++++++++-- var/HyperDown.php | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/admin/js/hyperdown.js b/admin/js/hyperdown.js index 0211ed87..b130a6bc 100644 --- a/admin/js/hyperdown.js +++ b/admin/js/hyperdown.js @@ -393,7 +393,7 @@ }; Parser.prototype.parseBlock = function(text, lines) { - var align, aligns, block, emptyCount, head, indent, isAfterList, j, key, l, len, len1, len2, line, m, matches, num, ref, row, rows, space, special, tag; + var align, aligns, autoHtml, block, emptyCount, head, indent, isAfterList, j, key, l, len, len1, len2, line, m, matches, num, ref, row, rows, space, special, tag; ref = text.split("\n"); for (j = 0, len = ref.length; j < len; j++) { line = ref[j]; @@ -404,6 +404,7 @@ this.pos = -1; special = (array_keys(this.specialWhiteList)).join('|'); emptyCount = 0; + autoHtml = false; for (key = l = 0, len1 = lines.length; l < len1; key = ++l) { line = lines[key]; block = this.getBlock(); @@ -432,7 +433,7 @@ continue; } if (this.html) { - if (!!(matches = line.match(/^(\s*)!!!(\s*)$/))) { + if (!autoHtml && !!(matches = line.match(/^(\s*)!!!(\s*)$/))) { if (this.isBlock('shtml')) { this.setBlock(key).endBlock(); } else { @@ -443,6 +444,27 @@ this.setBlock(key); continue; } + if (matches = line.match(/^\s*<([a-z0-9-]+)(\s+[^>]*)?>/i)) { + if (this.isBlock('ahtml')) { + this.setBlock(key); + continue; + } else if ((matches[2] === void 0 || matches[2] !== '/') && !matches[1].match(/^(area|base|br|col|embed|hr|img|input|keygen|link|meta|param|source|track|wbr)$/i)) { + this.startBlock('ahtml', key); + if (0 <= line.indexOf("")) { + this.endBlock(); + } else { + autoHtml = matches[1]; + } + continue; + } + } else if (!!autoHtml && 0 <= line.indexOf("")) { + this.setBlock(key).endBlock(); + autoHtml = false; + continue; + } else if (this.isBlock('ahtml')) { + this.setBlock(key); + continue; + } } if (!!(matches = line.match(/^(\s*)\$\$(\s*)$/))) { if (this.isBlock('math')) { @@ -698,6 +720,10 @@ } }; + Parser.prototype.parseAhtml = function(lines) { + return trim(lines.join("\n")); + }; + Parser.prototype.parseShtml = function(lines) { return trim((lines.slice(1, -1)).join("\n")); }; diff --git a/var/HyperDown.php b/var/HyperDown.php index f4a6a50c..950567e9 100644 --- a/var/HyperDown.php +++ b/var/HyperDown.php @@ -527,6 +527,7 @@ class HyperDown $this->_pos = -1; $special = implode("|", array_keys($this->_specialWhiteList)); $emptyCount = 0; + $autoHtml = false; // analyze by line foreach ($lines as $key => $line) { @@ -567,7 +568,7 @@ class HyperDown // super html mode if ($this->_html) { - if (preg_match("/^(\s*)!!!(\s*)$/", $line, $matches)) { + if (!$autoHtml && preg_match("/^(\s*)!!!(\s*)$/", $line, $matches)) { if ($this->isBlock('shtml')) { $this->setBlock($key)->endBlock(); } else { @@ -579,6 +580,30 @@ class HyperDown $this->setBlock($key); continue; } + + // auto html + if (preg_match("/^\s*<([a-z0-9-]+)(\s+[^>]*)?>/i", $line, $matches)) { + if ($this->isBlock('ahtml')) { + $this->setBlock($key); + continue; + } else if ((empty($matches[2]) || $matches[2] != '/') && !preg_match("/^(area|base|br|col|embed|hr|img|input|keygen|link|meta|param|source|track|wbr)$/i", $matches[1])) { + $this->startBlock('ahtml', $key); + + if (strpos($line, "") !== false) { + $this->endBlock(); + } else { + $autoHtml = $matches[1]; + } + continue; + } + } else if (!!$autoHtml && strpos($line, "") !== false) { + $this->setBlock($key)->endBlock(); + $autoHtml = false; + continue; + } else if ($this->isBlock('ahtml')) { + $this->setBlock($key); + continue; + } } // mathjax mode @@ -920,6 +945,17 @@ class HyperDown return preg_match("/^\s*$/", $str) ? '' : '
' . $str . '
'; } + /** + * parseAhtml + * + * @param array $lines + * @return string + */ + private function parseAhtml(array $lines) + { + return trim(implode("\n", $lines)); + } + /** * parseShtml *