Skip to content

Commit 3991583

Browse files
docs($sce): fix code samples and example
The code samples were using `<pre>` tags rather than code fences (```) so they were not being displayed correctly. The inline code example (defined by a `<example>` element) had been placed in an `@example` jsdoc tag, so rather than appearing inline at the declaration point in the text, they were being appended to the end of the document in the `Example` section. Closes angular#8053
1 parent 63e8952 commit 3991583

File tree

1 file changed

+93
-92
lines changed

1 file changed

+93
-92
lines changed

src/ng/sce.js

Lines changed: 93 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,21 @@ function adjustMatchers(matchers) {
112112
*
113113
* Here is what a secure configuration for this scenario might look like:
114114
*
115-
* <pre class="prettyprint">
116-
* angular.module('myApp', []).config(function($sceDelegateProvider) {
117-
* $sceDelegateProvider.resourceUrlWhitelist([
118-
* // Allow same origin resource loads.
119-
* 'self',
120-
* // Allow loading from our assets domain. Notice the difference between * and **.
121-
* 'http://srv*.assets.example.com/**']);
115+
* ```
116+
* angular.module('myApp', []).config(function($sceDelegateProvider) {
117+
* $sceDelegateProvider.resourceUrlWhitelist([
118+
* // Allow same origin resource loads.
119+
* 'self',
120+
* // Allow loading from our assets domain. Notice the difference between * and **.
121+
* 'http://srv*.assets.example.com/**'
122+
* ]);
122123
*
123-
* // The blacklist overrides the whitelist so the open redirect here is blocked.
124-
* $sceDelegateProvider.resourceUrlBlacklist([
125-
* 'http://myapp.example.com/clickThru**']);
126-
* });
127-
* </pre>
124+
* // The blacklist overrides the whitelist so the open redirect here is blocked.
125+
* $sceDelegateProvider.resourceUrlBlacklist([
126+
* 'http://myapp.example.com/clickThru**'
127+
* ]);
128+
* });
129+
* ```
128130
*/
129131

130132
function $SceDelegateProvider() {
@@ -419,10 +421,10 @@ function $SceDelegateProvider() {
419421
*
420422
* Here's an example of a binding in a privileged context:
421423
*
422-
* <pre class="prettyprint">
423-
* <input ng-model="userHtml">
424-
* <div ng-bind-html="userHtml">
425-
* </pre>
424+
* ```
425+
* <input ng-model="userHtml">
426+
* <div ng-bind-html="userHtml"></div>
427+
* ```
426428
*
427429
* Notice that `ng-bind-html` is bound to `userHtml` controlled by the user. With SCE
428430
* disabled, this application allows the user to render arbitrary HTML into the DIV.
@@ -462,15 +464,15 @@ function $SceDelegateProvider() {
462464
* ng.$sce#parseAsHtml $sce.parseAsHtml(binding expression)}. Here's the actual code (slightly
463465
* simplified):
464466
*
465-
* <pre class="prettyprint">
466-
* var ngBindHtmlDirective = ['$sce', function($sce) {
467-
* return function(scope, element, attr) {
468-
* scope.$watch($sce.parseAsHtml(attr.ngBindHtml), function(value) {
469-
* element.html(value || '');
470-
* });
471-
* };
472-
* }];
473-
* </pre>
467+
* ```
468+
* var ngBindHtmlDirective = ['$sce', function($sce) {
469+
* return function(scope, element, attr) {
470+
* scope.$watch($sce.parseAsHtml(attr.ngBindHtml), function(value) {
471+
* element.html(value || '');
472+
* });
473+
* };
474+
* }];
475+
* ```
474476
*
475477
* ## Impact on loading templates
476478
*
@@ -574,66 +576,65 @@ function $SceDelegateProvider() {
574576
*
575577
* ## Show me an example using SCE.
576578
*
577-
* @example
578-
<example module="mySceApp" deps="angular-sanitize.js">
579-
<file name="index.html">
580-
<div ng-controller="myAppController as myCtrl">
581-
<i ng-bind-html="myCtrl.explicitlyTrustedHtml" id="explicitlyTrustedHtml"></i><br><br>
582-
<b>User comments</b><br>
583-
By default, HTML that isn't explicitly trusted (e.g. Alice's comment) is sanitized when
584-
$sanitize is available. If $sanitize isn't available, this results in an error instead of an
585-
exploit.
586-
<div class="well">
587-
<div ng-repeat="userComment in myCtrl.userComments">
588-
<b>{{userComment.name}}</b>:
589-
<span ng-bind-html="userComment.htmlComment" class="htmlComment"></span>
590-
<br>
591-
</div>
592-
</div>
593-
</div>
594-
</file>
595-
596-
<file name="script.js">
597-
var mySceApp = angular.module('mySceApp', ['ngSanitize']);
598-
599-
mySceApp.controller("myAppController", function myAppController($http, $templateCache, $sce) {
600-
var self = this;
601-
$http.get("test_data.json", {cache: $templateCache}).success(function(userComments) {
602-
self.userComments = userComments;
603-
});
604-
self.explicitlyTrustedHtml = $sce.trustAsHtml(
605-
'<span onmouseover="this.textContent=&quot;Explicitly trusted HTML bypasses ' +
606-
'sanitization.&quot;">Hover over this text.</span>');
607-
});
608-
</file>
609-
610-
<file name="test_data.json">
611-
[
612-
{ "name": "Alice",
613-
"htmlComment":
614-
"<span onmouseover='this.textContent=\"PWN3D!\"'>Is <i>anyone</i> reading this?</span>"
615-
},
616-
{ "name": "Bob",
617-
"htmlComment": "<i>Yes!</i> Am I the only other one?"
618-
}
619-
]
620-
</file>
621-
622-
<file name="protractor.js" type="protractor">
623-
describe('SCE doc demo', function() {
624-
it('should sanitize untrusted values', function() {
625-
expect(element(by.css('.htmlComment')).getInnerHtml())
626-
.toBe('<span>Is <i>anyone</i> reading this?</span>');
627-
});
628-
629-
it('should NOT sanitize explicitly trusted values', function() {
630-
expect(element(by.id('explicitlyTrustedHtml')).getInnerHtml()).toBe(
631-
'<span onmouseover="this.textContent=&quot;Explicitly trusted HTML bypasses ' +
632-
'sanitization.&quot;">Hover over this text.</span>');
633-
});
634-
});
635-
</file>
636-
</example>
579+
* <example module="mySceApp" deps="angular-sanitize.js">
580+
* <file name="index.html">
581+
* <div ng-controller="myAppController as myCtrl">
582+
* <i ng-bind-html="myCtrl.explicitlyTrustedHtml" id="explicitlyTrustedHtml"></i><br><br>
583+
* <b>User comments</b><br>
584+
* By default, HTML that isn't explicitly trusted (e.g. Alice's comment) is sanitized when
585+
* $sanitize is available. If $sanitize isn't available, this results in an error instead of an
586+
* exploit.
587+
* <div class="well">
588+
* <div ng-repeat="userComment in myCtrl.userComments">
589+
* <b>{{userComment.name}}</b>:
590+
* <span ng-bind-html="userComment.htmlComment" class="htmlComment"></span>
591+
* <br>
592+
* </div>
593+
* </div>
594+
* </div>
595+
* </file>
596+
*
597+
* <file name="script.js">
598+
* var mySceApp = angular.module('mySceApp', ['ngSanitize']);
599+
*
600+
* mySceApp.controller("myAppController", function myAppController($http, $templateCache, $sce) {
601+
* var self = this;
602+
* $http.get("test_data.json", {cache: $templateCache}).success(function(userComments) {
603+
* self.userComments = userComments;
604+
* });
605+
* self.explicitlyTrustedHtml = $sce.trustAsHtml(
606+
* '<span onmouseover="this.textContent=&quot;Explicitly trusted HTML bypasses ' +
607+
* 'sanitization.&quot;">Hover over this text.</span>');
608+
* });
609+
* </file>
610+
*
611+
* <file name="test_data.json">
612+
* [
613+
* { "name": "Alice",
614+
* "htmlComment":
615+
* "<span onmouseover='this.textContent=\"PWN3D!\"'>Is <i>anyone</i> reading this?</span>"
616+
* },
617+
* { "name": "Bob",
618+
* "htmlComment": "<i>Yes!</i> Am I the only other one?"
619+
* }
620+
* ]
621+
* </file>
622+
*
623+
* <file name="protractor.js" type="protractor">
624+
* describe('SCE doc demo', function() {
625+
* it('should sanitize untrusted values', function() {
626+
* expect(element(by.css('.htmlComment')).getInnerHtml())
627+
* .toBe('<span>Is <i>anyone</i> reading this?</span>');
628+
* });
629+
*
630+
* it('should NOT sanitize explicitly trusted values', function() {
631+
* expect(element(by.id('explicitlyTrustedHtml')).getInnerHtml()).toBe(
632+
* '<span onmouseover="this.textContent=&quot;Explicitly trusted HTML bypasses ' +
633+
* 'sanitization.&quot;">Hover over this text.</span>');
634+
* });
635+
* });
636+
* </file>
637+
* </example>
637638
*
638639
*
639640
*
@@ -647,13 +648,13 @@ function $SceDelegateProvider() {
647648
*
648649
* That said, here's how you can completely disable SCE:
649650
*
650-
* <pre class="prettyprint">
651-
* angular.module('myAppWithSceDisabledmyApp', []).config(function($sceProvider) {
652-
* // Completely disable SCE. For demonstration purposes only!
653-
* // Do not use in new projects.
654-
* $sceProvider.enabled(false);
655-
* });
656-
* </pre>
651+
* ```
652+
* angular.module('myAppWithSceDisabledmyApp', []).config(function($sceProvider) {
653+
* // Completely disable SCE. For demonstration purposes only!
654+
* // Do not use in new projects.
655+
* $sceProvider.enabled(false);
656+
* });
657+
* ```
657658
*
658659
*/
659660
/* jshint maxlen: 100 */

0 commit comments

Comments
 (0)