【问题标题】:Does text from a rich text editor not inherit styles when rendered in an HTML document?富文本编辑器中的文本在 HTML 文档中呈现时是否不会继承样式?
【发布时间】:2020-12-29 07:19:36
【问题描述】:

为了清楚起见,我在后端使用了 RTE 来存储一些描述。后来,通过 api,我收到了描述以及其他详细信息作为响应。现在风格完好无损。例如,粗体标题。但是当我使用 innerHTML 属性在 HTML 文档中呈现它时,我看到的只是未格式化的文本。标题不再是粗体。

这是回复的一部分:

</p>\r\n\n\r\n<p><span style=\"font-weight: bold;\">Features</span> \n </p>\r\n\n\r\n<p>Gives even skin tone, smoother complexion and sculpted facial features.

很明显,在这里可以看到 font-style="bold"。但在此之后,渲染版本不包含这些样式。

以下是完整回复:

"cart_count":2,
"images":[
],
"success":true,
"message":"Sucessfully",
"data":{
"product_id":1,
"name":"Dr G Butterfly Gua Sha",
"category_id":1,
"category":"Skin Tool",
"description":"<p>Dr G Butterfly Rose Quartz Gua Sha is a beauty and wellness tool designed to heal and enhance natural beauty. It lifts and sculpts your face, drains the lymph node, which reduces puffy eyes and face. By scraping with repeated strokes on the surface of the skin, this tool helps stimulate muscles and increases the blood flow. \n </p>\r\n\n\r\n<p><span style=\"font-weight: bold;\">Features</span> \n </p>\r\n\n\r\n<p>Gives even skin tone, smoother complexion and sculpted facial features. Reduces the signs of ageing and gives younger-looking skin. Increases lymphatic function. Stimulates blood circulation. Improves the appearance of dark circles and reduces under-eye puffiness.  </p>\r\n\n\r\n<p><span style=\"font-weight: bold;\">How To Use  \n</span></p>\r\n\n\r\n<p>Apply Dr G oil or Dr G gel as per your skin type covering the face and neck. </p>\r\n<p>Hold the butterfly gua sha tool firmly and sweep across gently up and out, starting with the neck, cheeks, jawline, chin, around the mouth, and slowly glide under the eyes, across your eyebrows and from your forehead up to your hairline. </p>\r\n<p>You can sweep it 3-5 times per area. </p>\r\n<p>Recommended at least a few times a week for best results.  </p>\r\n\n\r\n<p><span style=\"font-weight: bold;\">About Dr G</span> \n </p>\r\n\n\r\n<p>Dr G offers luxury skincare products, backed by over a decade of dermatology expertise and on-ground practice. Made for Indian weather conditions, with variants for different skin types, including sensitive skin, and to address specific skin concerns - these innovative products are a perfect balance of nature and science. Drawing from ancient Ayurveda and combining natural extracts with skin-safe science, Dr G's range of products bridge modern skincare with holistic science.</p>",
"short_description":"Sculpts, Tones, Reduces Puffiness, Lifts",
"max_quantity":500,
"status":1,
"in_stock":1,
"measurement":[
{
"is_cart":true,
"ordered_quantity":2,
"is_wish":false,
"discounted_price":1400.0,
"weight":"200 Gram",
"price":1400.0,
"prod_id":1,
"percentage":100,
"max_quantity":500
}
]
}
}

【问题讨论】:

  • 我必须看到后端的完整响应,但如果我不得不猜测,我会检查编码的引号是否是问题(即。\")。
  • @AldinBradaric,我最初也是这么想的。当作为响应发送时,文本是否被转换为字符串?知道这一点的唯一原因是客户可以在描述中以任何格式(粗体、斜体等)提供任何内容。我们只是不想硬编码任何东西。不过感谢您的帮助!
  • 就像 Aldin Bradaric 所说,需要完整的回应来澄清这个问题。但是您应该先将您的 HTML 编码(例如 Base64 字符串),然后再将其发送到 frondend 并在那里解码。
  • 我已经更新了完整的回复,请看一下。谢谢!

标签: html angular rte


【解决方案1】:

您的回复中的 HTML 无效。如果您将响应中的 HTML 字符串复制到以 .html 文件结尾的文本文件并使用浏览器(例如 index.html)打开它,您可以轻松地对其进行测试。或者使用像这样的验证器:https://www.freeformatter.com/html-validator.html

让我们从 HTML 字符串中挑选一个字符错误并以无格式显示的部分:

&lt;span style=\"font-weight: bold;\"&gt;Features&lt;/span&gt; \n

如果您在此处删除反斜杠 \,则此和平将正确呈现:

&lt;span style="font-weight: bold;"&gt;Features&lt;/span&gt; \n

我建议您在将 HTML 发送到前端之前对其进行编码。您可以使用 Base64,它可以在后端轻松编码并在显示之前在前端解码。

如果在您接收此 HTML(在您的后端)时已经存在此“错误”字符,则必须先对其进行解析以清除它。

【讨论】:

  • 在这里回复,这样其他有类似问题的开发人员就不用浪费很多时间了,所以问题不在于“\n”,这是因为DOMSanitization 角度。更多信息请点击此处:angular.io/api/platform-browser/DomSanitizer 解决方案:如果您信任外部来源,请使用简单的管道绕过清理。
猜你喜欢
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多