【发布时间】:2014-01-23 22:12:05
【问题描述】:
我正在尝试检索出现回车时损坏的文本字符串。
有一个多行输入字段,允许用户按下回车键并在下一行输入。有一个按钮可以让用户保存笔记。
保存是这样进行的......
if (save) note = $('#annotation_textarea').val();
导航到 SPA 应用程序中的另一个页面会导致意外令牌错误。
我尝试将回车符转换为
一个正则表达式。
note.replace(/(\r\n|\n|\r)/g,"<br />");
那没用。
这是重现问题的代码:
app.js
var mySceApp = angular.module('mySceApp', ['ngSanitize']);
mySceApp.controller("myAppController", function myAppController($http, $templateCache, $sce) {
var self = this;
$http.get("test_data.json", {cache: $templateCache}).success(function(userComments) {
self.userComments = userComments;
});
self.explicitlyTrustedHtml = $sce.trustAsHtml(
'<span onmouseover="this.textContent="Explicitly trusted HTML bypasses ' +
'sanitization."">Hover over this text.</span>');
});
html
<!doctype html>
<html ng-app="mySceApp">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular-sanitize.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div ng-controller="myAppController as myCtrl">
<i ng-bind-html="myCtrl.explicitlyTrustedHtml" id="explicitlyTrustedHtml"></i><br><br>
<b>User comments</b><br>
By default, HTML that isn't explicitly trusted (e.g. Alice's comment) is sanitized when
$sanitize is available. If $sanitize isn't available, this results in an error instead of an
exploit.
<div class="well">
<div ng-repeat="userComment in myCtrl.userComments">
<b>{{userComment.name}}</b>:
<span ng-bind-html="userComment.htmlComment" class="htmlComment"></span>
<br>
</div>
</div>
</div>
</body>
</html>
测试数据
[
{ "name": "Carriage Return test",
"htmlComment":
"<span onmouseover='this.textContent=\"PWN3D!\"'>Is <i>anyone</i> reading this?</span>"
},
{ "name": "Bob",
"htmlComment": "<i>Yes!</i> Am I the only other one?"
},
{ "name": "Hal",
"htmlComment": "You,
are
not
alone!"
}
]
这是一个 plnkr
http://plnkr.co/edit/RYReiYQfD0Vw8u0BeDVc?p=preview
【问题讨论】:
-
模拟后端数据源并创建一个重现问题的最小示例。否则,我们无法确定这确实是问题所在,或者没有更好的方法可以帮助您。
-
谢谢,我明白了。
-
谢谢。此外,plunkr 应该是补充的。我们需要重现问题的完整(最少)代码应该在您的问题中。原因是,如果 plunkr 出现故障,您的问题对未来的访客毫无用处。我已经继续并将代码编辑到问题中。
-
谢谢乔治!我很感激。
标签: javascript json angularjs