是否有一个 JavaScript 函数可以将字符转换为 &code;相等的?

发布于 2024-12-19 06:19:56 字数 2165 浏览 1 评论 0原文

我有使用 CKeditor 创建的文本,它似乎在应有空格的位置插入 &nbsp; 。它似乎对 >、<、& 等进行了类似的转换,这很好,除了当我进行 DOMSelection 时,这些代码被删除。

所以,这就是选择的内容:

before<a href=\"http://wiki.teamliquid.net/starcraft2/Hatchery\" style=\"text-decoration: none; color: rgb(0, 43, 184); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; \" title=\"Hatchery\">Hatchery</a> (2)

但这就是 DOM 中实际的内容:

before<a href=\"http://wiki.teamliquid.net/starcraft2/Hatchery\" style=\"text-decoration: none; color: rgb(0, 43, 184); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; \" title=\"Hatchery\">Hatchery</a>&nbsp;(2)

请注意,我使用variable.inspect输出了选择内容和存储在数据库中的原始文本,因此所有引号都被转义了(当发送到浏览器)。


为了避免每个人寻找差异的痛苦:
从第一个开始:孵化场 (2) (选择)
从第二个开始:Hatchery&nbsp;(2) (原始)
这些差异是在选择的最后。


所以...我认为可以通过三种方法来解决这个问题。

1) - Replace all characters commonly replaced with codes with their codes, 
     and hope for the best.
2) - Javascript may have some uncommon function / a library may exist that 
     replaces these characters for me (I think this might be the way CKeditor 
     does its character conversion).
3) - Figure out the way CKeditor converts and do the conversion exactly that way.

我正在使用 Ruby on Rails,但这对于这个问题来说应该不重要。

我发现它可以转换的其他一些东西:

1: It seems to only convert spaces to &nbsp; if the space(s) is before or after a tag:
   e.g.: "With quick&nbsp;<a href..."
2: It changes apostrophes to the hex value
   e.g.: "opponent&#39;s"
3: It changes "&" to "&amp;"
4: It changes angle brackets to "&gt;" and "&lt;" appropriately.

有人对此有什么想法吗?

I have text that was created created with CKeditor, and it seems to be inserting   where spaces should be. And it appears to do similar conversions with >, <, &, etc. which is fine, except, when I make a DOMSelection, those codes are removed.

So, this is what is selected:

before<a href=\"http://wiki.teamliquid.net/starcraft2/Hatchery\" style=\"text-decoration: none; color: rgb(0, 43, 184); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; \" title=\"Hatchery\">Hatchery</a> (2)

But this is what is actually in the DOM:

before<a href=\"http://wiki.teamliquid.net/starcraft2/Hatchery\" style=\"text-decoration: none; color: rgb(0, 43, 184); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; \" title=\"Hatchery\">Hatchery</a> (2)

note that I outputted the selection and the original text stored in the database using variable.inspect, so all the quotes are escaped (they wouldn't be when sent to the browser).


To save everyone the pain of looking for the difference:
From the first: Hatchery</a> (2) (The Selection)
From the second: Hatchery</a> (2) (The original)
These differences are at the very end of the selection.


So... there are three ways, I can see of, to approach this.

1) - Replace all characters commonly replaced with codes with their codes, 
     and hope for the best.
2) - Javascript may have some uncommon function / a library may exist that 
     replaces these characters for me (I think this might be the way CKeditor 
     does its character conversion).
3) - Figure out the way CKeditor converts and do the conversion exactly that way.

I'm using Ruby on Rails, but that shouldn't matter for this problem.

Some other things that I found out that it converts:

1: It seems to only convert spaces to   if the space(s) is before or after a tag:
   e.g.: "With quick <a href..."
2: It changes apostrophes to the hex value
   e.g.: "opponent's"
3: It changes "&" to "&"
4: It changes angle brackets to ">" and "<" appropriately.

Does anyone have any thoughts on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-26 06:19:56

str 中编码 html 实体(如果我理解正确的话,您的问题标题要求这样做):

$('<div/>').text(str).html();

str 中解码 html 实体:

$('<div/>').html(str).text();

这些依赖于 jQuery,但普通的替代方案是基本相同但更详细。

str 中的 html 实体进行编码:

var el = document.createElement('div');
el.innerText = str;
el.innerHTML;

str 中的 html 实体进行解码:

var el = document.createElement('div');
el.innerHTML = str;
el.innerText;

To encode html entities in str (your question title asks for this, if I understand correctly):

$('<div/>').text(str).html();

To decode html entities in str:

$('<div/>').html(str).text();

These rely on jQuery, but vanilla alternatives are basically the same but more verbose.

To encode html entities in str:

var el = document.createElement('div');
el.innerText = str;
el.innerHTML;

To decode html entities in str:

var el = document.createElement('div');
el.innerHTML = str;
el.innerText;
愛上了 2024-12-26 06:19:56
  1. 将空格转换为  通常由浏览器在编辑内容时完成。
  2. 将 ' 转换为 '可以通过 http://docs.cksource.com/ckeditor_api/ 进行控制 通常需要符号/CKEDITOR.config.html#.entities_additional
  3. 和 4. 以避免在再次加载该内容时破坏在设计视图中编写的代码。您可以尝试更改 http://docs.cksource.com/ckeditor_api /symbols/CKEDITOR.config.html#.basicEntities 但这通常会导致将来出现问题。
  1. Conversion of spaces to   is usually done by the browser while editing content.
  2. Conversion of ' to ' can be controled with http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.entities_additional
  3. and 4. are usually needed to avoid breaking code that it's written in design view when loading again that content. You can try to change http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.basicEntities but that usually can lead to problems in the future.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文