英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论