Show / Hide Table of Contents
-
ng
- function
- angular.UNSAFE_restoreLegacyJqLiteXHTMLReplacement
- angular.bind
- angular.bootstrap
- angular.copy
- angular.element
- angular.equals
- angular.errorHandlingConfig
- angular.extend
- angular.forEach
- angular.fromJson
- angular.identity
- angular.injector
- angular.isArray
- angular.isDate
- angular.isDefined
- angular.isElement
- angular.isFunction
- angular.isNumber
- angular.isObject
- angular.isString
- angular.isUndefined
- angular.merge
- angular.module
- angular.noop
- angular.reloadWithDebugInfo
- angular.toJson
- directive
- a
- form
- input
- input[checkbox]
- input[date]
- input[datetime-local]
- input[email]
- input[month]
- input[number]
- input[radio]
- input[range]
- input[text]
- input[time]
- input[url]
- input[week]
- ngApp
- ngBind
- ngBindHtml
- ngBindTemplate
- ngBlur
- ngChange
- ngChecked
- ngClass
- ngClassEven
- ngClassOdd
- ngClick
- ngCloak
- ngController
- ngCopy
- ngCsp
- ngCut
- ngDblclick
- ngDisabled
- ngFocus
- ngForm
- ngHide
- ngHref
- ngIf
- ngInclude
- ngInit
- ngJq
- ngKeydown
- ngKeypress
- ngKeyup
- ngList
- ngMaxlength
- ngMinlength
- ngModel
- ngModelOptions
- ngMousedown
- ngMouseenter
- ngMouseleave
- ngMousemove
- ngMouseover
- ngMouseup
- ngNonBindable
- ngOn
- ngOpen
- ngOptions
- ngPaste
- ngPattern
- ngPluralize
- ngProp
- ngReadonly
- ngRef
- ngRepeat
- ngRequired
- ngSelected
- ngShow
- ngSrc
- ngSrcset
- ngStyle
- ngSubmit
- ngSwitch
- ngTransclude
- ngValue
- script
- select
- textarea
- object
- angular.version
- type
- $cacheFactory.Cache
- $compile.directive.Attributes
- $rootScope.Scope
- ModelOptions
- angular.Module
- form.FormController
- ngModel.NgModelController
- select.SelectController
- provider
- $anchorScrollProvider
- $animateProvider
- $compileProvider
- $controllerProvider
- $filterProvider
- $httpProvider
- $interpolateProvider
- $locationProvider
- $logProvider
- $parseProvider
- $qProvider
- $rootScopeProvider
- $sceDelegateProvider
- $sceProvider
- $templateRequestProvider
- service
- $anchorScroll
- $animate
- $animateCss
- $cacheFactory
- $compile
- $controller
- $document
- $exceptionHandler
- $filter
- $http
- $httpBackend
- $httpParamSerializer
- $httpParamSerializerJQLike
- $interpolate
- $interval
- $jsonpCallbacks
- $locale
- $location
- $log
- $parse
- $q
- $rootElement
- $rootScope
- $sce
- $sceDelegate
- $templateCache
- $templateRequest
- $timeout
- $window
- $xhrFactory
- filter
- currency
- date
- filter
- json
- limitTo
- lowercase
- number
- orderBy
- uppercase
- auto
- ngAnimate
- ngAria
- ngComponentRouter
- ngCookies
-
ngMessageFormat
- ngMessages
-
ngMock
- object
- angular.mock
- service
- $animate
- $componentController
- $controller
- $exceptionHandler
- $flushPendingTasks
- $httpBackend
- $interval
- $log
- $timeout
- $verifyNoPendingTasks
- provider
- $exceptionHandlerProvider
- type
- $rootScope.Scope
- angular.mock.TzDate
- function
- angular.mock.dump
- angular.mock.inject
- angular.mock.module
- angular.mock.module.sharedInjector
- browserTrigger
- ngMockE2E
-
ngParseExt
- ngResource
- ngRoute
- ngSanitize
- ngTouch
Loading …
There was an error loading this resource. Please try again later.
Improve this Doc
View Source
$sanitize
- - $sanitizeProvider
- - service in module ngSanitize
Overview
Sanitizes an html string by stripping all potentially dangerous tokens.
The input is sanitized by parsing the HTML into tokens. All safe tokens (from a trusted URI list) are then serialized back to a properly escaped HTML string. This means that no unsafe input can make it into the returned string.
The trusted URIs for URL sanitization of attribute values is configured using the functions
aHrefSanitizationTrustedUrlList
and imgSrcSanitizationTrustedUrlList
of $compileProvider
.
The input may also contain SVG markup if this is enabled via $sanitizeProvider
.
Usage
$sanitize(html);
Arguments
Param | Type | Details |
---|---|---|
html | string |
HTML input. |
Returns
string | Sanitized HTML. |
Example
<script>
angular.module('sanitizeExample', ['ngSanitize'])
.controller('ExampleController', ['$scope', '$sce', function($scope, $sce) {
$scope.snippet =
'<p style="color:blue">an html\n' +
'<em onmouseover="this.textContent=\'PWN3D!\'">click here</em>\n' +
'snippet</p>';
$scope.deliberatelyTrustDangerousSnippet = function() {
return $sce.trustAsHtml($scope.snippet);
};
}]);
</script>
<div ng-controller="ExampleController">
Snippet: <textarea ng-model="snippet" cols="60" rows="3"></textarea>
<table>
<tr>
<td>Directive</td>
<td>How</td>
<td>Source</td>
<td>Rendered</td>
</tr>
<tr id="bind-html-with-sanitize">
<td>ng-bind-html</td>
<td>Automatically uses $sanitize</td>
<td><pre><div ng-bind-html="snippet"><br/></div></pre></td>
<td><div ng-bind-html="snippet"></div></td>
</tr>
<tr id="bind-html-with-trust">
<td>ng-bind-html</td>
<td>Bypass $sanitize by explicitly trusting the dangerous value</td>
<td>
<pre><div ng-bind-html="deliberatelyTrustDangerousSnippet()">
</div></pre>
</td>
<td><div ng-bind-html="deliberatelyTrustDangerousSnippet()"></div></td>
</tr>
<tr id="bind-default">
<td>ng-bind</td>
<td>Automatically escapes</td>
<td><pre><div ng-bind="snippet"><br/></div></pre></td>
<td><div ng-bind="snippet"></div></td>
</tr>
</table>
</div>
it('should sanitize the html snippet by default', function() {
expect(element(by.css('#bind-html-with-sanitize div')).getAttribute('innerHTML')).
toBe('<p>an html\n<em>click here</em>\nsnippet</p>');
});
it('should inline raw snippet if bound to a trusted value', function() {
expect(element(by.css('#bind-html-with-trust div')).getAttribute('innerHTML')).
toBe("<p style=\"color:blue\">an html\n" +
"<em onmouseover=\"this.textContent='PWN3D!'\">click here</em>\n" +
"snippet</p>");
});
it('should escape snippet without any filter', function() {
expect(element(by.css('#bind-default div')).getAttribute('innerHTML')).
toBe("<p style=\"color:blue\">an html\n" +
"<em onmouseover=\"this.textContent='PWN3D!'\">click here</em>\n" +
"snippet</p>");
});
it('should update', function() {
element(by.model('snippet')).clear();
element(by.model('snippet')).sendKeys('new <b onclick="alert(1)">text</b>');
expect(element(by.css('#bind-html-with-sanitize div')).getAttribute('innerHTML')).
toBe('new <b>text</b>');
expect(element(by.css('#bind-html-with-trust div')).getAttribute('innerHTML')).toBe(
'new <b onclick="alert(1)">text</b>');
expect(element(by.css('#bind-default div')).getAttribute('innerHTML')).toBe(
"new <b onclick=\"alert(1)\">text</b>");
});