Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 1920420

Browse files
committed
fixup! feat($sanitize): support enhancing white-list
1 parent 0b51380 commit 1920420

File tree

1 file changed

+62
-24
lines changed

1 file changed

+62
-24
lines changed

src/ngSanitize/sanitize.js

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ var $sanitizeMinErr = angular.$$minErr('$sanitize');
1515
var bind;
1616
var extend;
1717
var forEach;
18-
var isDefined;
1918
var isArray;
19+
var isDefined;
2020
var isObject;
2121
var lowercase;
2222
var noop;
@@ -146,8 +146,8 @@ var htmlSanitizeWriter;
146146
* Creates and configures {@link $sanitize} instance.
147147
*/
148148
function $SanitizeProvider() {
149-
var svgEnabled = false;
150149
var hasBeenInstantiated = false;
150+
var svgEnabled = false;
151151

152152
this.$get = ['$$sanitizeUri', function($$sanitizeUri) {
153153
hasBeenInstantiated = true;
@@ -191,7 +191,7 @@ function $SanitizeProvider() {
191191
* </div>
192192
*
193193
* @param {boolean=} flag Enable or disable SVG support in the sanitizer.
194-
* @returns {boolean|ng.$sanitizeProvider} Returns the currently configured value if called
194+
* @returns {boolean|$sanitizeProvider} Returns the currently configured value if called
195195
* without an argument or self for chaining otherwise.
196196
*/
197197
this.enableSvg = function(enableSvg) {
@@ -209,27 +209,51 @@ function $SanitizeProvider() {
209209
* @name $sanitizeProvider#addValidElements
210210
* @kind function
211211
*
212-
* @param {Array|Object} elements List of valid elements.
212+
* @description
213+
* Extends the built-in lists of valid HTML/SVG elements, i.e. elements that are considered safe
214+
* and are not stripped off during sanitization. You can extend the following lists:
215+
*
216+
* - `htmlElements`: A list of elements (tag names) to extend the current list of safe HTML
217+
* elements. HTML elements considered safe will not be removed during sanitization. All other
218+
* elements will be stripped off.
219+
*
220+
* - `htmlVoidElements`: This is similar to `htmlElements`, but in addition allows the specified
221+
* elements to have no end tag (similar to HTML
222+
* [void elements](https://rawgit.com/w3c/html/html5.1-2/single-page.html#void-elements)).
223+
*
224+
* - `svgElements`: This is similar to `htmlElements`, but for SVG elements. This list is only
225+
* taken into account if SVG is {@link ngSanitize.$sanitizeProvider#enableSvg enabled} for
226+
* `$sanitize`.
227+
*
228+
* <div class="alert alert-warning">
229+
* This method must be called during the {@link angular.Module#config config} phase. Once the
230+
* `$sanitize` service has been instantiated, this method has no effect.
231+
* </div>
232+
*
233+
* @param {Array<String>|Object} elements - A list of valid HTML elements or an object with one or
234+
* more of the following properties:
235+
* - **htmlElements** - `{Array<String>}` - A list of elements to extend the current list of
236+
* HTML elements.
237+
* - **htmlVoidElements** - `{Array<String>}` - A list of elements to extend the current list of
238+
* void HTML elements; i.e. elements that do not have an end tag.
239+
* - **svgElements** - `{Array<String>}` - A list of elements to extend the current list of SVG
240+
* elements. The list of SVG elements is only taken into account if SVG is
241+
* {@link ngSanitize.$sanitizeProvider#enableSvg enabled} for `$sanitize`.
213242
*
214-
* Object properties:
243+
* Passing an array (`[...]`) is equivalent to passing `{htmlElements: [...]}`.
215244
*
216-
* - `svgElements` – `{string[]=}` – An array of SVG elements' names.
217-
* - `htmlVoidElements` – `{string[]=}` – An array of void elements' names.
218-
* - `htmlElements` – `{string[]=}` – An array of html elements' names.
245+
* @return {$sanitizeProvider} Returns self for chaining.
219246
*/
220247
this.addValidElements = function(elements) {
221-
if (hasBeenInstantiated) return this;
222-
223-
if (isArray(elements)) {
224-
addElementsTo(validElements, elements);
225-
return this;
226-
}
248+
if (!hasBeenInstantiated) {
249+
if (isArray(elements)) {
250+
elements = {htmlElements: elements};
251+
}
227252

228-
if (isObject(elements)) {
229-
addElementsTo(svgElements, elements['svgElements']);
230-
addElementsTo(voidElements, elements['htmlVoidElements']);
231-
addElementsTo(validElements, elements['htmlVoidElements']);
232-
addElementsTo(validElements, elements['htmlElements']);
253+
addElementsTo(svgElements, elements.svgElements);
254+
addElementsTo(voidElements, elements.htmlVoidElements);
255+
addElementsTo(validElements, elements.htmlVoidElements);
256+
addElementsTo(validElements, elements.htmlElements);
233257
}
234258

235259
return this;
@@ -242,14 +266,28 @@ function $SanitizeProvider() {
242266
* @kind function
243267
*
244268
* @description
245-
* The added attributes will not be treated as URI attributes, which means their values will
246-
* not sanitized as URIs using the aHrefSanitizationWhitelist and imgSrcSanitizationWhitelist of {@link ng.$compileProvider $compileProvider}.
269+
* Extends the built-in list of valid attributes, i.e. attributes that are considered safe and are
270+
* not stripped off during sanitization.
271+
*
272+
* **Note**:
273+
* The new attributes will not be treated as URI attributes, which means their values will not be
274+
* sanitized as URIs using `$compileProvider`'s
275+
* {@link ng.$compileProvider#aHrefSanitizationWhitelist aHrefSanitizationWhitelist} and
276+
* {@link ng.$compileProvider#imgSrcSanitizationWhitelist imgSrcSanitizationWhitelist}.
247277
*
248-
* @param {Array} attrs List of valid attributes
278+
* <div class="alert alert-warning">
279+
* This method must be called during the {@link angular.Module#config config} phase. Once the
280+
* `$sanitize` service has been instantiated, this method has no effect.
281+
* </div>
282+
*
283+
* @param {Array<String>} attrs - A list of valid attributes.
284+
*
285+
* @returns {$sanitizeProvider} Returns self for chaining.
249286
*/
250287
this.addValidAttrs = function(attrs) {
251-
if (hasBeenInstantiated) return this;
252-
extend(validAttrs, arrayToMap(attrs, true));
288+
if (!hasBeenInstantiated) {
289+
extend(validAttrs, arrayToMap(attrs, true));
290+
}
253291
return this;
254292
};
255293

0 commit comments

Comments
 (0)