CodenameOne – 对HTML字符串进行编码

huangapple go评论73阅读模式
英文:

CodenameOne - encoding HTML string

问题

My CodenameOne app needs to pass HTML code to HTML elements inside a BrowserComponent with Javascript, so characters have to be escaped more than one time, they have to be encoded in fact.

I need something like TextUtils.htmlEncode().

In Codename it seems to be HTMLUtils but is deprecated, and I am not sure there is the equivalent method.

So what is the equivalent of TextUtils.htmlEncode() in CodenameOne?

英文:

My CodenameOne app needs to pass HTML code to HTML elements inside a BrowserComponent with Javascript, so characters have to be escaped more than one time, they have to be encoded in fact.

I need something like TextUtils.htmlEncode().

In Codename it seems to be HTMLUtils but is deprecated, and I am not sure there is the equivalent method.

So what is the equivalent of TextUtils.htmlEncode() in CodenameOne?

答案1

得分: 1

实际上,我只需要处理 iFrame 的 srcdoc 参数中的双引号,所以如果我没错的话,解决方案是将

"

替换为

\"

在 Java 中:

String htmlText = originalHTMLToEmbed.replace("\"", "\\\"");

我尝试过,它有效。

如果 HTML 中涉及更多级别,则它们已经被转义,因此在最终字符串中可能会找到

\\\"

(以此类推)的出现。

英文:

In fact I just need to manage double quotes for the srcdoc parameter of iFrames, so if I am not wrong the solution is to replace

" 

with

\"

In Java:

String htmlText=originalHTMLToEmbed.replace("\"","\\\"");

I tried it and it works.

If more levels are involved in HTML, they are already escaped, so occurrences of

\\\" 

(and so on) are possible to be found in the final string.

huangapple
  • 本文由 发表于 2020年8月18日 18:13:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63466499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定