3

假设我有一堆 HTML,如下所示:

bla bla bla long paragraph here
<br/>
<br/>
bla bla bla more paragraph text
<br/>
<br/>

Javascript有没有一种简单的方法可以将其转换为正确的语义<p>标签?例如:

<p>
  bla bla bla long paragraph here
</p>
<p>
  bla bla bla more paragraph text
</p>

输出间距并不重要,理想情况下它适用于任何输入间距。

我想我可能会尝试编写一个正则表达式,但在我这样做之前,我想确保我是 a) 避免了一个受伤的世界,并且 b) 那里没有别的东西 - 我试图做谷歌搜索,但还没有想出任何东西。

感谢您的任何建议!

4

4 回答 4

7

我无聊。我确定需要进行优化/调整。使用一点 jQuery 来发挥它的魔力。在FF3工作。你的问题的答案是没有一个非常“简单”的方法:)

$(function() {
  $.fn.pmaker = function() {
    var brs = 0;
    var nodes = [];

    function makeP()
    {
      // only bother doing this if we have nodes to stick into a P
      if (nodes.length) {
        var p = $("<p/>");
        p.insertBefore(nodes[0]);  // insert a new P before the content
        p.append(nodes); // add the children        
        nodes = [];
      }
      brs=0;
    }

    this.contents().each(function() {    
      if (this.nodeType == 3) // text node 
      {
        // if the text has non whitespace - reset the BR counter
        if (/\S+/.test(this.data)) {
          nodes.push(this);
          brs = 0;
        }
      } else if (this.nodeType == 1) {
        if (/br/i.test(this.tagName)) {
          if (++brs == 2) {
            $(this).remove(); // remove this BR from the dom
            $(nodes.pop()).remove(); // delete the previous BR from the array and the DOM
            makeP();
          } else {
            nodes.push(this);
          }
        } else if (/^(?:p)$/i.test(this.tagName)) {
          // these tags for the P break but dont scan within
          makeP();
        } else if (/^(?:div)$/i.test(this.tagName)) {
          // force a P break and scan within
          makeP();
          $(this).pmaker();
        } else {
          brs = 0; // some other tag - reset brs.
          nodes.push(this); // add the node 
          // specific nodes to not peek inside of - inline tags
          if (!(/^(?:b|i|strong|em|span|u)$/i.test(this.tagName))) {
            $(this).pmaker(); // peek inside for P needs            
          }
        } 
      } 
    });
    while ((brs--)>0) { // remove any extra BR's at the end
      $(nodes.pop()).remove();
    }
    makeP();
    return this;
  };

  // run it against something:
  $(function(){ 
    $("#worker").pmaker();
  });

这是我测试的 html 部分:

<div id="worker">
bla bla bla long <b>paragraph</b> here
<br/>
<br/>
bla bla bla more paragraph text
<br/>
<br/>
this text should end up in a P
<div class='test'>
  and so should this
  <br/>
  <br/>
  and this<br/>without breaking at the single BR
</div>
and then we have the a "buggy" clause
<p>
  fear the real P!
</p>
and a trailing br<br/>
</div>

结果:

<div id="worker"><p>
bla bla bla long <b>paragraph</b> here
</p>
<p>
bla bla bla more paragraph text
</p>
<p>
this text should end up in a P
</p><div class="test"><p>
  and so should this
  </p>
  <p>
  and this<br/>without breaking at the single BR
</p></div><p>
and then we have the a "buggy" clause
</p><p>
  fear the real P!
</p><p>
and a trailing br</p>
</div>
于 2009-08-14T01:05:47.887 回答
5

扫描每个子元素 + 封闭元素的文本。每次遇到“br”元素时,创建一个“p”元素,并将所有待处理的内容附加到它。起泡,冲洗,重复。

不要忘记删除要重新定位到新“p”元素的内容。

我发现这个库(prototype.js)对这类事情很有用。

于 2009-08-13T23:44:57.143 回答
4

我假设您并没有真正允许任何其他有时您需要保留单个换行符(并非所有<br />元素都是坏的),并且您只想将双实例<br />转换为分段符。

这样做我会:

  1. 删除所有换行符
  2. 将全部内容包装在一个段落中
  3. 替换<br /><br /></p>\n<p>
  4. 最后,删除<p></p>可能已生成的任何空元素

所以代码可能看起来像:

var ConvertToParagraphs = function(text) {
    var lineBreaksRemoved = text.replace(/\n/g, "");
    var wrappedInParagraphs = "<p>" + lineBreaksRemoved + "</p>";
    var brsRemoved = wrappedInParagraphs.replace(/<br[^>]*>[\s]*<br[^>]*>/gi, "</p>\n<p>");
    var emptyParagraphsRemoved = brsRemoved.replace(/<p><\/p>/g, "");
    return emptyParagraphsRemoved;
}

注意:我已经非常详细地展示了这些过程,你当然会简化它。

这会变成你的样本:

bla bla bla long paragraph here
<br/>
<br/>
bla bla bla more paragraph text
<br/>
<br/>

进入:

<p>bla bla bla long paragraph here</p>
<p>bla bla bla more paragraph text</p>

但这样做并没有删除<br />您可能真正想要的任何元素。

于 2009-08-14T01:47:30.060 回答
0

我会分几个阶段来做:

  1. RegExp:将所有 br 标记转换为换行符。
  2. RegExp:去掉所有的空白。
  3. RegExp:将多个换行符转换为单个换行符。
  4. 对结果使用 Array.split('\n') 。

这应该给出一个包含所有“真实”段落的数组(理论上)。然后你可以遍历它并将每一行包装在 p-tags 中。

于 2009-08-13T23:43:29.583 回答