有没有办法从谷歌闭包字段中获取不干净的内容?
我有一个文本编辑器,它提供源代码和富文本编辑器。假设我的文本区域中有以下内容
<p>A Paragraph</p>
<script type="text/javascript">
$$('.a-class');
</script>
,我成功地能够将此文本存储在 RTE 中,但如果我尝试从中取出该数据(通过调用 Field.getCleanContents() ),则会产生以下结果
<p>A Paragraph</p>
<script type="text/javascript">
$('.a-class');
</script>
所以,我我想知道是否有一种简单的方法可以防止关闭删除这个额外的“$”字符。
更新
在挖掘了一堆代码后,我发现发生了 string.replace ,并且有 使用双美元时,Javascript string.replace 函数中有些东西
"foo bar baz".replace("bar", "$$fancy")
> "foo $fancy baz"
我解决这个问题的方法是有效地切割字符串并从中返回一个新字符串。
function glue(msg, old, replacement){
var oldIndex = msg.indexOf(old);
var prefix = msg.substr(0, oldIndex);
var suffix = msg.substr(oldIndex + old.length);
return prefix + replacement + suffix;
}
glue("foo bar baz", "bar", "$$fancy");
> "foo $$fancy baz"
I have a text editor that provides both a source code and Rich Text editor. Let's say I have the following in my text area
<p>A Paragraph</p>
<script type="text/javascript">
$('.a-class');
</script>
I am successfully able to store this text in the RTE, though if I try to take that data out of it (by calling Field.getCleanContents() ) it results in the following
<p>A Paragraph</p>
<script type="text/javascript">
$('.a-class');
</script>
So, I'm wondering if there is an easy way to prevent closure from stripping out this extra '$' character.
Update
After digging through a bunch of code I found out that there was a string.replace happening, and there's something a little sneaky in the Javascript string.replace function when using double dollar
"foo bar baz".replace("bar", "$fancy")
> "foo $fancy baz"
The way I got around this was by effectively cutting up the string and returning a new string from that.
function glue(msg, old, replacement){
var oldIndex = msg.indexOf(old);
var prefix = msg.substr(0, oldIndex);
var suffix = msg.substr(oldIndex + old.length);
return prefix + replacement + suffix;
}
glue("foo bar baz", "bar", "$fancy");
> "foo $fancy baz"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论