【问题标题】:Converting textarea newlines into <p> and <br/> tags by JavaScript通过 JavaScript 将 textarea 换行符转换为 <p> 和 <br/> 标签
【发布时间】:2011-11-12 06:30:41
【问题描述】:

我在 html 表单中使用 textarea,并尝试使用 &lt;p&gt;&lt;br/&gt; 标签将其内容重新格式化为有效的 html 格式。

我编写了这个脚本,它似乎可以工作,但我想确保我没有遗漏任何东西。所以我正在征求反馈意见。我知道我没有考虑到用户可能会显式输入 html 标签的可能性,但这没问题,因为无论如何我都会在 PHP 中发布结果。

提前致谢。

输出示例:

<p>Line 1<br/>Line 2</p><p>Line 4<br/><br/><br/>Line 7</p>

和代码:

function getHTML() {

    var v = document.forms[0]['txtArea'].value;
    v = v.replace(/\r?\n/gm, '<br/>');
    v = v.replace(/(?!<br\/>)(.{5})<br\/><br\/>(?!<br\/>)/gi, '$1</p><p>');
    if (v.indexOf("<p>") > v.indexOf("</p>")) v = "<p>" + v;
    if (v.lastIndexOf("</p>") < v.lastIndexOf("<p>")) v += "</p>";
    if (v.length > 1 && v.indexOf("<p>") == -1) v = "<p>" + v + "</p>";
    alert(v);

}

请注意,这是一个旨在成为 CMS 一部分的代码,而我所关心的 JavaScript 就是用这两个标签重建 textarea 结果。所见即所得的问题...

【问题讨论】:

标签: javascript html textarea newline string-formatting


【解决方案1】:

这是我想出的。

function encode4HTML(str) {
    return str
        .replace(/\r\n?/g,'\n')
        // normalize newlines - I'm not sure how these
        // are parsed in PC's. In Mac's they're \n's
        .replace(/(^((?!\n)\s)+|((?!\n)\s)+$)/gm,'')
        // trim each line
        .replace(/(?!\n)\s+/g,' ')
        // reduce multiple spaces to 2 (like in "a    b")
        .replace(/^\n+|\n+$/g,'')
        // trim the whole string
        .replace(/[<>&"']/g,function(a) {
        // replace these signs with encoded versions
            switch (a) {
                case '<'    : return '&lt;';
                case '>'    : return '&gt;';
                case '&'    : return '&amp;';
                case '"'    : return '&quot;';
                case '\''   : return '&apos;';
            }
        })
        .replace(/\n{2,}/g,'</p><p>')
        // replace 2 or more consecutive empty lines with these
        .replace(/\n/g,'<br />')
        // replace single newline symbols with the <br /> entity
        .replace(/^(.+?)$/,'<p>$1</p>');
        // wrap all the string into <p> tags
        // if there's at least 1 non-empty character
}

您需要做的就是使用 textarea 的值调用此函数。

var ta = document.getElementsByTagName('textarea')[0];
console.log(encode4HTML(ta.value));

【讨论】:

    【解决方案2】:
    v = v.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")
        .replace(/([^\r\n]+)\r?\n\r?\n/g, "<p>$1</p>")
        .replace(/\r?\n/g, "<br />");
    

    【讨论】:

    • 谢谢,但是这种格式会产生不同的结果。我正在创建一个所见即所得的区域。
    • 您能说得更具体些吗?什么不是所见即所得?
    • 结果。用 textarea 试试看结果。换行符在视觉上与 textarea 不同。
    【解决方案3】:

    天哪,你是在尝试将 html 代码传递给后端吗?这就是我们所说的 XSS 漏洞。

    http://en.wikipedia.org/wiki/Cross-site_scripting

    ubb 代码怎么样?

    http://en.wikipedia.org/wiki/UBB.threads

    【讨论】:

    • 这并不是一个公共页面。我只编写了一个 CMS 页面。
    • 公开或私下将 HTML 发送到后端是完全可以的。您认为某些评论框和论坛主题编辑器如何将格式化的 HTML 帖子发送到后端数据库?!它将 HTML 传递到您应该担心的前端!只要您在使用 HTML 之前彻底验证它,使用 htmlpurifier.org 或一些 PHP 的内置函数(取决于要求),那么它现在被认为是相对安全的。如果您担心的话,您可以在将 HTML 字符串保存到数据库之前对其进行验证。
    猜你喜欢
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    相关资源
    最近更新 更多