简体   繁体   English

字符串处理以将斜杠添加到自闭合标签(IMG、BR 等)

[英]string processing to add trailing slash to self-closing tags (IMG, BR etc)

If I get the innerHTML of an element, certain child elements should have a trailing slash to be valid XHTML (for example, "<br />"), but they don't, in Chrome, Firefox or IE, regardless of the doctype.如果我得到一个元素的 innerHTML,某些子元素应该有一个尾部斜杠才能成为有效的 XHTML(例如,“<br />”),但在 Chrome、Firefox 或 IE 中,无论文档类型如何,它们都没有.

Obviously this doesn't matter most of the time, but in my case I am using yanking out html from the DOM as part of a templating system -- so if those backslashes are missing, they go that way into the resulting page built using those templates, and that page won't validate as XHTML because of this.显然这在大多数情况下并不重要,但在我的情况下,我使用从 DOM 中提取 html 作为模板系统的一部分——所以如果这些反斜杠丢失,它们 go 以这种方式进入使用这些反斜杠构建的结果页面模板,因此该页面不会验证为 XHTML。 And non-validating pages seem to make my client sad.非验证页面似乎让我的客户感到难过。

So....I'm looking for some javascript code (maybe a regex) that will add that backslash where appropriate.所以....我正在寻找一些 javascript 代码(可能是正则表达式),它将在适当的地方添加反斜杠。 If it worked for these element types that's good enough for me:如果它适用于这些元素类型,那对我来说已经足够好了:

area, base, br, col, embed, hr, img, input, link, meta, param区域、基础、br、col、嵌入、hr、img、输入、链接、元、参数

I guess it has to not get confused if that there is a > in quotes within the tag.我想如果标签内的引号中有 > ,它就不会感到困惑。

I know there is an dom-to-xml library out there (http://xhtmljs.codeplex.com/) that does this, but it also does a lot of other things and is quite brute force.我知道有一个 dom-to-xml 库 (http://xhtmljs.codeplex.com/) 可以做到这一点,但它也做了很多其他的事情,而且是相当蛮力的。 I'm hoping for something much simpler.我希望有更简单的东西。

edit:编辑:

All right, since I didn't get any bites on the string processing approach, I went ahead and did something that does the trick for me.好吧,因为我对字符串处理方法没有任何了解,所以我继续做一些对我有用的事情。 (although it would get confused by a > in quotes, which I'll deal with later): (虽然它会被引号中的 > 弄糊涂,我稍后会处理):

var addClosingSlashes = function (htmlString) {
    var elemTypes = [
    "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param"];
    var inString, outString = htmlString;
    for (var i=0; i<elemTypes.length; i++) {
      var index1 = 0, index2;
      inString = outString;
      outString = '';
      while ((index1 = inString.indexOf("<" + elemTypes[i])) != -1) {
        if ((index2 = inString.indexOf(">", index1)) != -1 && inString.substring(index2 - 1, index2 + 1) != '/>') {
          outString += inString.substring(0, index2) + " />";
          inString = inString.substring(index2+1);
          }
        else {
          break;      
          }
        }
      outString += inString;
      }
    return outString;
    };

Unless this is server-side javascript, this won't do anything.除非这是服务器端 javascript,否则这不会做任何事情。 By the time the browser executes javascript, the DOM is built as a DOM, and not as some kind of text element.当浏览器执行 javascript 时,DOM 被构建为 DOM,而不是某种文本元素。 That is, the elements will have been built into a tree already, and there's nothing more you can do to affect rendering.也就是说,元素已经构建到树中,您无法做任何其他事情来影响渲染。

Try changing the way the source document is served as per the answer in this question: When objects (eg img tag) is created by javascript, it doesn't have the closing tag.尝试根据此问题的答案更改源文档的提供方式: 当对象(例如 img 标记)由 javascript 创建时,它没有结束标记。 How do I make it W3C valid? 如何使其 W3C 有效?

Also, please see the answer of this question... :-) RegEx match open tags except XHTML self-contained tags另外,请参阅此问题的答案... :-) RegEx 匹配开放标签,XHTML 自包含标签除外

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM