简体   繁体   English

将特殊字符解码为 HTML 字符

[英]Decoding special characters into HTML characters

I have been searching the web for a javascript function that turns something like this:我一直在网上搜索一个 javascript 函数,它会变成这样:

Hi.  I will show <span style="font-weight: bold;">HTML</span>.

Into this:进入这个:

Hi.&nbsp; I will show <span style="font-weight: bold;">HTML</span>.

I am using this method:我正在使用这种方法:

htmlDecode: function (input) {
    var doc = new DOMParser().parseFromString(input, "text/html");
    return doc.documentElement.textContent;
}

And it works the first time.它第一次起作用。 But if I try it again, on text like this:但如果我再试一次,在这样的文本上:

Hi.&nbsp; I will show <span style="font-weight: bold;">HTML</span>

It strips out all the html and just leaves me with:它去掉了所有的 html,只给我留下:

Hi.你好。 I will show HTML.我将展示 HTML。

I only want the method to change this:我只想要改变这一点的方法:

Hi.&amp;nbsp; I will show &lt;span style="font-weight: bold;"&gt;HTML&lt;/span&gt;.

Into this:进入这个:

Hi.&nbsp; I will show <span style="font-weight: bold;">HTML</span>.

I don't want it to totally strip out the HTML.我不希望它完全去掉 HTML。

Is there a way to do that?有没有办法做到这一点?

Thanks!谢谢!

You could check if the parsed result contains DOM elements.您可以检查解析结果是否包含 DOM 元素。 If so, then it means the decoding went one step too far, and the original value should be returned:如果是,则说明解码走得太远了,应该返回原值:

 function htmlDecode (input) { let doc = new DOMParser().parseFromString(input, "text/html"); let body = doc.querySelector("body"); return body.children.length ? input : body.textContent; } let s = 'Hi.&amp;nbsp; I will show &lt;span style="font-weight: bold;"&gt;HTML&lt;/span&gt;.'; s = htmlDecode(s); console.log(s); // decoded s = htmlDecode(s); // apply on the result... console.log(s); // ... no change s = htmlDecode("Hi.&nbsp; This is normal text."); console.log(s);

An additional check额外的检查

Another assumption could be that it should be possible to decode the result of a first decoding and get a different result again.另一个假设是应该可以对第一次解码的结果进行解码并再次获得不同的结果。 If it produces twice the same result, then the original input should be returned.如果它产生两次相同的结果,则应返回原始输入。

 function htmlDecode (input) { let parser = new DOMParser(); let doc = parser.parseFromString(input, "text/html"); let { textContent, children } = doc.querySelector("body"); if (children.length) return input; doc = parser.parseFromString(textContent, "text/html"); if (doc.querySelector("body").textContent === textContent) return input; return textContent; } let s = 'Hi.&amp;nbsp; I will show &lt;span style="font-weight: bold;"&gt;HTML&lt;/span&gt;.'; s = htmlDecode(s); console.log(s); // decoded s = htmlDecode(s); // apply on the result... console.log(s); // ... no change s = "Hi.&amp;nbsp; This is normal text."; s = htmlDecode(s); console.log(s); // decoded s = htmlDecode(s); // apply on the result... console.log(s); // ... no change

You can create a div, set it's innerHTML , and then retrieve it's innerText .您可以创建一个 div,将其设置为innerHTML ,然后检索其为innerText

 function htmlDecode(text) { var div = document.createElement('div') div.innerHTML = text return div.innerText } console.log(htmlDecode('Hi.&amp;nbsp; I will show &lt;span style="font-weight: bold;"&gt;HTML&lt;/span&gt;.'))

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

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