TinyMCE尖括号

时间:2015-07-07 03:56:33

标签: php tags tinymce

我是使用TinyMCE的新手,但对尖括号的行为感到沮丧。它似乎是将<foo><foo>Foo</foo>等输入解释为标记,尽管页面来源显示两种情况分别转换为&lt;foo&gt;&lt;foo&gt;Foo&lt;/foo&gt;

我减少了我的代码,它在下面:

<?php
    // Simplified for SO, no file writing / reading
    $content = isset($_POST["forSo"]) ? $_POST["forSo"] : "";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>For Stack Overflow</title>
    <script src="/tinymce/js/tinymce/tinymce.min.js"></script>
    <script>
        tinymce.init({ selector : "#forSo" });
    </script>
</head>
<body>
<?php
    // Behaves as expected, TinyMCE correctly automatically converts HTML Entities
    echo $content . "\n";
?>
    <form action="/forSo.php" method="post" enctype="multipart/form-data">
        <textarea id="forSo" name="forSo">
<?php
    // Page source shows that this has HTML Entities, still loses information
    echo $content . "\n";
?>
        </textarea><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

如果我输入说<foo>,则结果页面来源为:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>For Stack Overflow</title>
    <script src="/tinymce/js/tinymce/tinymce.min.js"></script>
    <script>
        tinymce.init({ selector : "#forSo" });
    </script>
</head>
<body>
<p>&nbsp;&lt;foo&gt;</p>
    <form action="/forSo.php" method="post" enctype="multipart/form-data">
        <textarea id="forSo" name="forSo">
<p>&nbsp;&lt;foo&gt;</p>
        </textarea><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

然而,TinyMCE似乎抛弃了textarea的内容导致了这一点(再次点击提交会导致所有$content为空字符串):

TinyMCE making me angry

1 个答案:

答案 0 :(得分:0)

<?php echo $content . "\n"; ?>替换textarea内的第二个<?php echo str_replace("&", "&amp;", $content) . "\n"; ?>来完成任务

这确实会阻止编写有效的HTML标记,但是文本意味着以<foo>为字面意思的文本将作为&amp;lt;foo&amp;gt;保留在TinyMCE编辑器中,而不是&lt;foo&gt;,而TinyMCE将其解释为HTML标记