summaryrefslogtreecommitdiff
path: root/xstatic/pkg/angular/data/angular-sanitize.js
diff options
context:
space:
mode:
Diffstat (limited to 'xstatic/pkg/angular/data/angular-sanitize.js')
-rw-r--r--xstatic/pkg/angular/data/angular-sanitize.js813
1 files changed, 434 insertions, 379 deletions
diff --git a/xstatic/pkg/angular/data/angular-sanitize.js b/xstatic/pkg/angular/data/angular-sanitize.js
index 8ebce08..a283e43 100644
--- a/xstatic/pkg/angular/data/angular-sanitize.js
+++ b/xstatic/pkg/angular/data/angular-sanitize.js
@@ -1,9 +1,9 @@
/**
- * @license AngularJS v1.4.10
- * (c) 2010-2015 Google, Inc. http://angularjs.org
+ * @license AngularJS v1.5.8
+ * (c) 2010-2016 Google, Inc. http://angularjs.org
* License: MIT
*/
-(function(window, angular, undefined) {'use strict';
+(function(window, angular) {'use strict';
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Any commits to this file should be reviewed with security in mind. *
@@ -17,6 +17,14 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
var $sanitizeMinErr = angular.$$minErr('$sanitize');
+var bind;
+var extend;
+var forEach;
+var isDefined;
+var lowercase;
+var noop;
+var htmlParser;
+var htmlSanitizeWriter;
/**
* @ngdoc module
@@ -33,36 +41,23 @@ var $sanitizeMinErr = angular.$$minErr('$sanitize');
* See {@link ngSanitize.$sanitize `$sanitize`} for usage.
*/
-/*
- * HTML Parser By Misko Hevery (misko@hevery.com)
- * based on: HTML Parser By John Resig (ejohn.org)
- * Original code by Erik Arvidsson, Mozilla Public License
- * http://erik.eae.net/simplehtmlparser/simplehtmlparser.js
- *
- * // Use like so:
- * htmlParser(htmlString, {
- * start: function(tag, attrs, unary) {},
- * end: function(tag) {},
- * chars: function(text) {},
- * comment: function(text) {}
- * });
- *
- */
-
-
/**
* @ngdoc service
* @name $sanitize
* @kind function
*
* @description
+ * 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 whitelist) are
* then serialized back to properly escaped html string. This means that no unsafe input can make
- * it into the returned string, however, since our parser is more strict than a typical browser
- * parser, it's possible that some obscure input, which would be recognized as valid HTML by a
- * browser, won't make it through the sanitizer. The input may also contain SVG markup.
- * The whitelist is configured using the functions `aHrefSanitizationWhitelist` and
- * `imgSrcSanitizationWhitelist` of {@link ng.$compileProvider `$compileProvider`}.
+ * it into the returned string.
+ *
+ * The whitelist for URL sanitization of attribute values is configured using the functions
+ * `aHrefSanitizationWhitelist` and `imgSrcSanitizationWhitelist` of {@link ng.$compileProvider
+ * `$compileProvider`}.
+ *
+ * The input may also contain SVG markup if this is enabled via {@link $sanitizeProvider}.
*
* @param {string} html HTML input.
* @returns {string} Sanitized HTML.
@@ -148,401 +143,426 @@ var $sanitizeMinErr = angular.$$minErr('$sanitize');
</file>
</example>
*/
+
+
+/**
+ * @ngdoc provider
+ * @name $sanitizeProvider
+ *
+ * @description
+ * Creates and configures {@link $sanitize} instance.
+ */
function $SanitizeProvider() {
+ var svgEnabled = false;
+
this.$get = ['$$sanitizeUri', function($$sanitizeUri) {
+ if (svgEnabled) {
+ extend(validElements, svgElements);
+ }
return function(html) {
var buf = [];
htmlParser(html, htmlSanitizeWriter(buf, function(uri, isImage) {
- return !/^unsafe/.test($$sanitizeUri(uri, isImage));
+ return !/^unsafe:/.test($$sanitizeUri(uri, isImage));
}));
return buf.join('');
};
}];
-}
-function sanitizeText(chars) {
- var buf = [];
- var writer = htmlSanitizeWriter(buf, angular.noop);
- writer.chars(chars);
- return buf.join('');
-}
+ /**
+ * @ngdoc method
+ * @name $sanitizeProvider#enableSvg
+ * @kind function
+ *
+ * @description
+ * Enables a subset of svg to be supported by the sanitizer.
+ *
+ * <div class="alert alert-warning">
+ * <p>By enabling this setting without taking other precautions, you might expose your
+ * application to click-hijacking attacks. In these attacks, sanitized svg elements could be positioned
+ * outside of the containing element and be rendered over other elements on the page (e.g. a login
+ * link). Such behavior can then result in phishing incidents.</p>
+ *
+ * <p>To protect against these, explicitly setup `overflow: hidden` css rule for all potential svg
+ * tags within the sanitized content:</p>
+ *
+ * <br>
+ *
+ * <pre><code>
+ * .rootOfTheIncludedContent svg {
+ * overflow: hidden !important;
+ * }
+ * </code></pre>
+ * </div>
+ *
+ * @param {boolean=} flag Enable or disable SVG support in the sanitizer.
+ * @returns {boolean|ng.$sanitizeProvider} Returns the currently configured value if called
+ * without an argument or self for chaining otherwise.
+ */
+ this.enableSvg = function(enableSvg) {
+ if (isDefined(enableSvg)) {
+ svgEnabled = enableSvg;
+ return this;
+ } else {
+ return svgEnabled;
+ }
+ };
-// Regular Expressions for parsing tags and attributes
-var START_TAG_REGEXP =
- /^<((?:[a-zA-Z])[\w:-]*)((?:\s+[\w:-]+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)\s*(>?)/,
- END_TAG_REGEXP = /^<\/\s*([\w:-]+)[^>]*>/,
- ATTR_REGEXP = /([\w:-]+)(?:\s*=\s*(?:(?:"((?:[^"])*)")|(?:'((?:[^'])*)')|([^>\s]+)))?/g,
- BEGIN_TAG_REGEXP = /^</,
- BEGING_END_TAGE_REGEXP = /^<\//,
- COMMENT_REGEXP = /<!--(.*?)-->/g,
- DOCTYPE_REGEXP = /<!DOCTYPE([^>]*?)>/i,
- CDATA_REGEXP = /<!\[CDATA\[(.*?)]]>/g,
- SURROGATE_PAIR_REGEXP = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
- // Match everything outside of normal chars and " (quote character)
- NON_ALPHANUMERIC_REGEXP = /([^\#-~| |!])/g;
-
-
-// Good source of info about elements and attributes
-// http://dev.w3.org/html5/spec/Overview.html#semantics
-// http://simon.html5.org/html-elements
-
-// Safe Void Elements - HTML5
-// http://dev.w3.org/html5/spec/Overview.html#void-elements
-var voidElements = makeMap("area,br,col,hr,img,wbr");
-
-// Elements that you can, intentionally, leave open (and which close themselves)
-// http://dev.w3.org/html5/spec/Overview.html#optional-tags
-var optionalEndTagBlockElements = makeMap("colgroup,dd,dt,li,p,tbody,td,tfoot,th,thead,tr"),
- optionalEndTagInlineElements = makeMap("rp,rt"),
- optionalEndTagElements = angular.extend({},
- optionalEndTagInlineElements,
- optionalEndTagBlockElements);
-
-// Safe Block Elements - HTML5
-var blockElements = angular.extend({}, optionalEndTagBlockElements, makeMap("address,article," +
- "aside,blockquote,caption,center,del,dir,div,dl,figure,figcaption,footer,h1,h2,h3,h4,h5," +
- "h6,header,hgroup,hr,ins,map,menu,nav,ol,pre,script,section,table,ul"));
-
-// Inline Elements - HTML5
-var inlineElements = angular.extend({}, optionalEndTagInlineElements, makeMap("a,abbr,acronym,b," +
- "bdi,bdo,big,br,cite,code,del,dfn,em,font,i,img,ins,kbd,label,map,mark,q,ruby,rp,rt,s," +
- "samp,small,span,strike,strong,sub,sup,time,tt,u,var"));
-
-// SVG Elements
-// https://wiki.whatwg.org/wiki/Sanitization_rules#svg_Elements
-// Note: the elements animate,animateColor,animateMotion,animateTransform,set are intentionally omitted.
-// They can potentially allow for arbitrary javascript to be executed. See #11290
-var svgElements = makeMap("circle,defs,desc,ellipse,font-face,font-face-name,font-face-src,g,glyph," +
- "hkern,image,linearGradient,line,marker,metadata,missing-glyph,mpath,path,polygon,polyline," +
- "radialGradient,rect,stop,svg,switch,text,title,tspan,use");
-
-// Special Elements (can contain anything)
-var specialElements = makeMap("script,style");
-
-var validElements = angular.extend({},
- voidElements,
- blockElements,
- inlineElements,
- optionalEndTagElements,
- svgElements);
-
-//Attributes that have href and hence need to be sanitized
-var uriAttrs = makeMap("background,cite,href,longdesc,src,usemap,xlink:href");
-
-var htmlAttrs = makeMap('abbr,align,alt,axis,bgcolor,border,cellpadding,cellspacing,class,clear,' +
- 'color,cols,colspan,compact,coords,dir,face,headers,height,hreflang,hspace,' +
- 'ismap,lang,language,nohref,nowrap,rel,rev,rows,rowspan,rules,' +
- 'scope,scrolling,shape,size,span,start,summary,tabindex,target,title,type,' +
- 'valign,value,vspace,width');
-
-// SVG attributes (without "id" and "name" attributes)
-// https://wiki.whatwg.org/wiki/Sanitization_rules#svg_Attributes
-var svgAttrs = makeMap('accent-height,accumulate,additive,alphabetic,arabic-form,ascent,' +
- 'baseProfile,bbox,begin,by,calcMode,cap-height,class,color,color-rendering,content,' +
- 'cx,cy,d,dx,dy,descent,display,dur,end,fill,fill-rule,font-family,font-size,font-stretch,' +
- 'font-style,font-variant,font-weight,from,fx,fy,g1,g2,glyph-name,gradientUnits,hanging,' +
- 'height,horiz-adv-x,horiz-origin-x,ideographic,k,keyPoints,keySplines,keyTimes,lang,' +
- 'marker-end,marker-mid,marker-start,markerHeight,markerUnits,markerWidth,mathematical,' +
- 'max,min,offset,opacity,orient,origin,overline-position,overline-thickness,panose-1,' +
- 'path,pathLength,points,preserveAspectRatio,r,refX,refY,repeatCount,repeatDur,' +
- 'requiredExtensions,requiredFeatures,restart,rotate,rx,ry,slope,stemh,stemv,stop-color,' +
- 'stop-opacity,strikethrough-position,strikethrough-thickness,stroke,stroke-dasharray,' +
- 'stroke-dashoffset,stroke-linecap,stroke-linejoin,stroke-miterlimit,stroke-opacity,' +
- 'stroke-width,systemLanguage,target,text-anchor,to,transform,type,u1,u2,underline-position,' +
- 'underline-thickness,unicode,unicode-range,units-per-em,values,version,viewBox,visibility,' +
- 'width,widths,x,x-height,x1,x2,xlink:actuate,xlink:arcrole,xlink:role,xlink:show,xlink:title,' +
- 'xlink:type,xml:base,xml:lang,xml:space,xmlns,xmlns:xlink,y,y1,y2,zoomAndPan', true);
-
-var validAttrs = angular.extend({},
- uriAttrs,
- svgAttrs,
- htmlAttrs);
-
-function makeMap(str, lowercaseKeys) {
- var obj = {}, items = str.split(','), i;
- for (i = 0; i < items.length; i++) {
- obj[lowercaseKeys ? angular.lowercase(items[i]) : items[i]] = true;
+ //////////////////////////////////////////////////////////////////////////////////////////////////
+ // Private stuff
+ //////////////////////////////////////////////////////////////////////////////////////////////////
+
+ bind = angular.bind;
+ extend = angular.extend;
+ forEach = angular.forEach;
+ isDefined = angular.isDefined;
+ lowercase = angular.lowercase;
+ noop = angular.noop;
+
+ htmlParser = htmlParserImpl;
+ htmlSanitizeWriter = htmlSanitizeWriterImpl;
+
+ // Regular Expressions for parsing tags and attributes
+ var SURROGATE_PAIR_REGEXP = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
+ // Match everything outside of normal chars and " (quote character)
+ NON_ALPHANUMERIC_REGEXP = /([^\#-~ |!])/g;
+
+
+ // Good source of info about elements and attributes
+ // http://dev.w3.org/html5/spec/Overview.html#semantics
+ // http://simon.html5.org/html-elements
+
+ // Safe Void Elements - HTML5
+ // http://dev.w3.org/html5/spec/Overview.html#void-elements
+ var voidElements = toMap("area,br,col,hr,img,wbr");
+
+ // Elements that you can, intentionally, leave open (and which close themselves)
+ // http://dev.w3.org/html5/spec/Overview.html#optional-tags
+ var optionalEndTagBlockElements = toMap("colgroup,dd,dt,li,p,tbody,td,tfoot,th,thead,tr"),
+ optionalEndTagInlineElements = toMap("rp,rt"),
+ optionalEndTagElements = extend({},
+ optionalEndTagInlineElements,
+ optionalEndTagBlockElements);
+
+ // Safe Block Elements - HTML5
+ var blockElements = extend({}, optionalEndTagBlockElements, toMap("address,article," +
+ "aside,blockquote,caption,center,del,dir,div,dl,figure,figcaption,footer,h1,h2,h3,h4,h5," +
+ "h6,header,hgroup,hr,ins,map,menu,nav,ol,pre,section,table,ul"));
+
+ // Inline Elements - HTML5
+ var inlineElements = extend({}, optionalEndTagInlineElements, toMap("a,abbr,acronym,b," +
+ "bdi,bdo,big,br,cite,code,del,dfn,em,font,i,img,ins,kbd,label,map,mark,q,ruby,rp,rt,s," +
+ "samp,small,span,strike,strong,sub,sup,time,tt,u,var"));
+
+ // SVG Elements
+ // https://wiki.whatwg.org/wiki/Sanitization_rules#svg_Elements
+ // Note: the elements animate,animateColor,animateMotion,animateTransform,set are intentionally omitted.
+ // They can potentially allow for arbitrary javascript to be executed. See #11290
+ var svgElements = toMap("circle,defs,desc,ellipse,font-face,font-face-name,font-face-src,g,glyph," +
+ "hkern,image,linearGradient,line,marker,metadata,missing-glyph,mpath,path,polygon,polyline," +
+ "radialGradient,rect,stop,svg,switch,text,title,tspan");
+
+ // Blocked Elements (will be stripped)
+ var blockedElements = toMap("script,style");
+
+ var validElements = extend({},
+ voidElements,
+ blockElements,
+ inlineElements,
+ optionalEndTagElements);
+
+ //Attributes that have href and hence need to be sanitized
+ var uriAttrs = toMap("background,cite,href,longdesc,src,xlink:href");
+
+ var htmlAttrs = toMap('abbr,align,alt,axis,bgcolor,border,cellpadding,cellspacing,class,clear,' +
+ 'color,cols,colspan,compact,coords,dir,face,headers,height,hreflang,hspace,' +
+ 'ismap,lang,language,nohref,nowrap,rel,rev,rows,rowspan,rules,' +
+ 'scope,scrolling,shape,size,span,start,summary,tabindex,target,title,type,' +
+ 'valign,value,vspace,width');
+
+ // SVG attributes (without "id" and "name" attributes)
+ // https://wiki.whatwg.org/wiki/Sanitization_rules#svg_Attributes
+ var svgAttrs = toMap('accent-height,accumulate,additive,alphabetic,arabic-form,ascent,' +
+ 'baseProfile,bbox,begin,by,calcMode,cap-height,class,color,color-rendering,content,' +
+ 'cx,cy,d,dx,dy,descent,display,dur,end,fill,fill-rule,font-family,font-size,font-stretch,' +
+ 'font-style,font-variant,font-weight,from,fx,fy,g1,g2,glyph-name,gradientUnits,hanging,' +
+ 'height,horiz-adv-x,horiz-origin-x,ideographic,k,keyPoints,keySplines,keyTimes,lang,' +
+ 'marker-end,marker-mid,marker-start,markerHeight,markerUnits,markerWidth,mathematical,' +
+ 'max,min,offset,opacity,orient,origin,overline-position,overline-thickness,panose-1,' +
+ 'path,pathLength,points,preserveAspectRatio,r,refX,refY,repeatCount,repeatDur,' +
+ 'requiredExtensions,requiredFeatures,restart,rotate,rx,ry,slope,stemh,stemv,stop-color,' +
+ 'stop-opacity,strikethrough-position,strikethrough-thickness,stroke,stroke-dasharray,' +
+ 'stroke-dashoffset,stroke-linecap,stroke-linejoin,stroke-miterlimit,stroke-opacity,' +
+ 'stroke-width,systemLanguage,target,text-anchor,to,transform,type,u1,u2,underline-position,' +
+ 'underline-thickness,unicode,unicode-range,units-per-em,values,version,viewBox,visibility,' +
+ 'width,widths,x,x-height,x1,x2,xlink:actuate,xlink:arcrole,xlink:role,xlink:show,xlink:title,' +
+ 'xlink:type,xml:base,xml:lang,xml:space,xmlns,xmlns:xlink,y,y1,y2,zoomAndPan', true);
+
+ var validAttrs = extend({},
+ uriAttrs,
+ svgAttrs,
+ htmlAttrs);
+
+ function toMap(str, lowercaseKeys) {
+ var obj = {}, items = str.split(','), i;
+ for (i = 0; i < items.length; i++) {
+ obj[lowercaseKeys ? lowercase(items[i]) : items[i]] = true;
+ }
+ return obj;
}
- return obj;
-}
+ var inertBodyElement;
+ (function(window) {
+ var doc;
+ if (window.document && window.document.implementation) {
+ doc = window.document.implementation.createHTMLDocument("inert");
+ } else {
+ throw $sanitizeMinErr('noinert', "Can't create an inert html document");
+ }
+ var docElement = doc.documentElement || doc.getDocumentElement();
+ var bodyElements = docElement.getElementsByTagName('body');
-/**
- * @example
- * htmlParser(htmlString, {
- * start: function(tag, attrs, unary) {},
- * end: function(tag) {},
- * chars: function(text) {},
- * comment: function(text) {}
- * });
- *
- * @param {string} html string
- * @param {object} handler
- */
-function htmlParser(html, handler) {
- if (typeof html !== 'string') {
- if (html === null || typeof html === 'undefined') {
- html = '';
+ // usually there should be only one body element in the document, but IE doesn't have any, so we need to create one
+ if (bodyElements.length === 1) {
+ inertBodyElement = bodyElements[0];
} else {
+ var html = doc.createElement('html');
+ inertBodyElement = doc.createElement('body');
+ html.appendChild(inertBodyElement);
+ doc.appendChild(html);
+ }
+ })(window);
+
+ /**
+ * @example
+ * htmlParser(htmlString, {
+ * start: function(tag, attrs) {},
+ * end: function(tag) {},
+ * chars: function(text) {},
+ * comment: function(text) {}
+ * });
+ *
+ * @param {string} html string
+ * @param {object} handler
+ */
+ function htmlParserImpl(html, handler) {
+ if (html === null || html === undefined) {
+ html = '';
+ } else if (typeof html !== 'string') {
html = '' + html;
}
- }
- var index, chars, match, stack = [], last = html, text;
- stack.last = function() { return stack[stack.length - 1]; };
-
- while (html) {
- text = '';
- chars = true;
-
- // Make sure we're not in a script or style element
- if (!stack.last() || !specialElements[stack.last()]) {
+ inertBodyElement.innerHTML = html;
- // Comment
- if (html.indexOf("<!--") === 0) {
- // comments containing -- are not allowed unless they terminate the comment
- index = html.indexOf("--", 4);
+ //mXSS protection
+ var mXSSAttempts = 5;
+ do {
+ if (mXSSAttempts === 0) {
+ throw $sanitizeMinErr('uinput', "Failed to sanitize html because the input is unstable");
+ }
+ mXSSAttempts--;
- if (index >= 0 && html.lastIndexOf("-->", index) === index) {
- if (handler.comment) handler.comment(html.substring(4, index));
- html = html.substring(index + 3);
- chars = false;
- }
- // DOCTYPE
- } else if (DOCTYPE_REGEXP.test(html)) {
- match = html.match(DOCTYPE_REGEXP);
+ // strip custom-namespaced attributes on IE<=11
+ if (window.document.documentMode) {
+ stripCustomNsAttrs(inertBodyElement);
+ }
+ html = inertBodyElement.innerHTML; //trigger mXSS
+ inertBodyElement.innerHTML = html;
+ } while (html !== inertBodyElement.innerHTML);
+
+ var node = inertBodyElement.firstChild;
+ while (node) {
+ switch (node.nodeType) {
+ case 1: // ELEMENT_NODE
+ handler.start(node.nodeName.toLowerCase(), attrToMap(node.attributes));
+ break;
+ case 3: // TEXT NODE
+ handler.chars(node.textContent);
+ break;
+ }
- if (match) {
- html = html.replace(match[0], '');
- chars = false;
+ var nextNode;
+ if (!(nextNode = node.firstChild)) {
+ if (node.nodeType == 1) {
+ handler.end(node.nodeName.toLowerCase());
}
- // end tag
- } else if (BEGING_END_TAGE_REGEXP.test(html)) {
- match = html.match(END_TAG_REGEXP);
-
- if (match) {
- html = html.substring(match[0].length);
- match[0].replace(END_TAG_REGEXP, parseEndTag);
- chars = false;
- }
-
- // start tag
- } else if (BEGIN_TAG_REGEXP.test(html)) {
- match = html.match(START_TAG_REGEXP);
-
- if (match) {
- // We only have a valid start-tag if there is a '>'.
- if (match[4]) {
- html = html.substring(match[0].length);
- match[0].replace(START_TAG_REGEXP, parseStartTag);
+ nextNode = node.nextSibling;
+ if (!nextNode) {
+ while (nextNode == null) {
+ node = node.parentNode;
+ if (node === inertBodyElement) break;
+ nextNode = node.nextSibling;
+ if (node.nodeType == 1) {
+ handler.end(node.nodeName.toLowerCase());
+ }
}
- chars = false;
- } else {
- // no ending tag found --- this piece should be encoded as an entity.
- text += '<';
- html = html.substring(1);
}
}
-
- if (chars) {
- index = html.indexOf("<");
-
- text += index < 0 ? html : html.substring(0, index);
- html = index < 0 ? "" : html.substring(index);
-
- if (handler.chars) handler.chars(decodeEntities(text));
- }
-
- } else {
- // IE versions 9 and 10 do not understand the regex '[^]', so using a workaround with [\W\w].
- html = html.replace(new RegExp("([\\W\\w]*)<\\s*\\/\\s*" + stack.last() + "[^>]*>", 'i'),
- function(all, text) {
- text = text.replace(COMMENT_REGEXP, "$1").replace(CDATA_REGEXP, "$1");
-
- if (handler.chars) handler.chars(decodeEntities(text));
-
- return "";
- });
-
- parseEndTag("", stack.last());
+ node = nextNode;
}
- if (html == last) {
- throw $sanitizeMinErr('badparse', "The sanitizer was unable to parse the following block " +
- "of html: {0}", html);
+ while (node = inertBodyElement.firstChild) {
+ inertBodyElement.removeChild(node);
}
- last = html;
}
- // Clean up any remaining tags
- parseEndTag();
-
- function parseStartTag(tag, tagName, rest, unary) {
- tagName = angular.lowercase(tagName);
- if (blockElements[tagName]) {
- while (stack.last() && inlineElements[stack.last()]) {
- parseEndTag("", stack.last());
- }
- }
-
- if (optionalEndTagElements[tagName] && stack.last() == tagName) {
- parseEndTag("", tagName);
+ function attrToMap(attrs) {
+ var map = {};
+ for (var i = 0, ii = attrs.length; i < ii; i++) {
+ var attr = attrs[i];
+ map[attr.name] = attr.value;
}
-
- unary = voidElements[tagName] || !!unary;
-
- if (!unary) {
- stack.push(tagName);
- }
-
- var attrs = {};
-
- rest.replace(ATTR_REGEXP,
- function(match, name, doubleQuotedValue, singleQuotedValue, unquotedValue) {
- var value = doubleQuotedValue
- || singleQuotedValue
- || unquotedValue
- || '';
-
- attrs[name] = decodeEntities(value);
- });
- if (handler.start) handler.start(tagName, attrs, unary);
+ return map;
}
- function parseEndTag(tag, tagName) {
- var pos = 0, i;
- tagName = angular.lowercase(tagName);
- if (tagName) {
- // Find the closest opened tag of the same type
- for (pos = stack.length - 1; pos >= 0; pos--) {
- if (stack[pos] == tagName) break;
- }
- }
- if (pos >= 0) {
- // Close all the open elements, up the stack
- for (i = stack.length - 1; i >= pos; i--)
- if (handler.end) handler.end(stack[i]);
-
- // Remove the open elements from the stack
- stack.length = pos;
- }
+ /**
+ * Escapes all potentially dangerous characters, so that the
+ * resulting string can be safely inserted into attribute or
+ * element text.
+ * @param value
+ * @returns {string} escaped text
+ */
+ function encodeEntities(value) {
+ return value.
+ replace(/&/g, '&amp;').
+ replace(SURROGATE_PAIR_REGEXP, function(value) {
+ var hi = value.charCodeAt(0);
+ var low = value.charCodeAt(1);
+ return '&#' + (((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000) + ';';
+ }).
+ replace(NON_ALPHANUMERIC_REGEXP, function(value) {
+ return '&#' + value.charCodeAt(0) + ';';
+ }).
+ replace(/</g, '&lt;').
+ replace(/>/g, '&gt;');
}
-}
-var hiddenPre=document.createElement("pre");
-/**
- * decodes all entities into regular string
- * @param value
- * @returns {string} A string with decoded entities.
- */
-function decodeEntities(value) {
- if (!value) { return ''; }
-
- hiddenPre.innerHTML = value.replace(/</g,"&lt;");
- // innerText depends on styling as it doesn't display hidden elements.
- // Therefore, it's better to use textContent not to cause unnecessary reflows.
- return hiddenPre.textContent;
-}
-
-/**
- * Escapes all potentially dangerous characters, so that the
- * resulting string can be safely inserted into attribute or
- * element text.
- * @param value
- * @returns {string} escaped text
- */
-function encodeEntities(value) {
- return value.
- replace(/&/g, '&amp;').
- replace(SURROGATE_PAIR_REGEXP, function(value) {
- var hi = value.charCodeAt(0);
- var low = value.charCodeAt(1);
- return '&#' + (((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000) + ';';
- }).
- replace(NON_ALPHANUMERIC_REGEXP, function(value) {
- return '&#' + value.charCodeAt(0) + ';';
- }).
- replace(/</g, '&lt;').
- replace(/>/g, '&gt;');
-}
-
-/**
- * create an HTML/XML writer which writes to buffer
- * @param {Array} buf use buf.jain('') to get out sanitized html string
- * @returns {object} in the form of {
- * start: function(tag, attrs, unary) {},
- * end: function(tag) {},
- * chars: function(text) {},
- * comment: function(text) {}
- * }
- */
-function htmlSanitizeWriter(buf, uriValidator) {
- var ignore = false;
- var out = angular.bind(buf, buf.push);
- return {
- start: function(tag, attrs, unary) {
- tag = angular.lowercase(tag);
- if (!ignore && specialElements[tag]) {
- ignore = tag;
- }
- if (!ignore && validElements[tag] === true) {
- out('<');
- out(tag);
- angular.forEach(attrs, function(value, key) {
- var lkey=angular.lowercase(key);
- var isImage = (tag === 'img' && lkey === 'src') || (lkey === 'background');
- if (validAttrs[lkey] === true &&
- (uriAttrs[lkey] !== true || uriValidator(value, isImage))) {
- out(' ');
- out(key);
- out('="');
- out(encodeEntities(value));
- out('"');
- }
- });
- out(unary ? '/>' : '>');
- }
- },
- end: function(tag) {
- tag = angular.lowercase(tag);
- if (!ignore && validElements[tag] === true) {
+ /**
+ * create an HTML/XML writer which writes to buffer
+ * @param {Array} buf use buf.join('') to get out sanitized html string
+ * @returns {object} in the form of {
+ * start: function(tag, attrs) {},
+ * end: function(tag) {},
+ * chars: function(text) {},
+ * comment: function(text) {}
+ * }
+ */
+ function htmlSanitizeWriterImpl(buf, uriValidator) {
+ var ignoreCurrentElement = false;
+ var out = bind(buf, buf.push);
+ return {
+ start: function(tag, attrs) {
+ tag = lowercase(tag);
+ if (!ignoreCurrentElement && blockedElements[tag]) {
+ ignoreCurrentElement = tag;
+ }
+ if (!ignoreCurrentElement && validElements[tag] === true) {
+ out('<');
+ out(tag);
+ forEach(attrs, function(value, key) {
+ var lkey = lowercase(key);
+ var isImage = (tag === 'img' && lkey === 'src') || (lkey === 'background');
+ if (validAttrs[lkey] === true &&
+ (uriAttrs[lkey] !== true || uriValidator(value, isImage))) {
+ out(' ');
+ out(key);
+ out('="');
+ out(encodeEntities(value));
+ out('"');
+ }
+ });
+ out('>');
+ }
+ },
+ end: function(tag) {
+ tag = lowercase(tag);
+ if (!ignoreCurrentElement && validElements[tag] === true && voidElements[tag] !== true) {
out('</');
out(tag);
out('>');
}
- if (tag == ignore) {
- ignore = false;
+ if (tag == ignoreCurrentElement) {
+ ignoreCurrentElement = false;
}
},
- chars: function(chars) {
- if (!ignore) {
+ chars: function(chars) {
+ if (!ignoreCurrentElement) {
out(encodeEntities(chars));
}
}
- };
+ };
+ }
+
+
+ /**
+ * When IE9-11 comes across an unknown namespaced attribute e.g. 'xlink:foo' it adds 'xmlns:ns1' attribute to declare
+ * ns1 namespace and prefixes the attribute with 'ns1' (e.g. 'ns1:xlink:foo'). This is undesirable since we don't want
+ * to allow any of these custom attributes. This method strips them all.
+ *
+ * @param node Root element to process
+ */
+ function stripCustomNsAttrs(node) {
+ if (node.nodeType === window.Node.ELEMENT_NODE) {
+ var attrs = node.attributes;
+ for (var i = 0, l = attrs.length; i < l; i++) {
+ var attrNode = attrs[i];
+ var attrName = attrNode.name.toLowerCase();
+ if (attrName === 'xmlns:ns1' || attrName.lastIndexOf('ns1:', 0) === 0) {
+ node.removeAttributeNode(attrNode);
+ i--;
+ l--;
+ }
+ }
+ }
+
+ var nextNode = node.firstChild;
+ if (nextNode) {
+ stripCustomNsAttrs(nextNode);
+ }
+
+ nextNode = node.nextSibling;
+ if (nextNode) {
+ stripCustomNsAttrs(nextNode);
+ }
+ }
+}
+
+function sanitizeText(chars) {
+ var buf = [];
+ var writer = htmlSanitizeWriter(buf, noop);
+ writer.chars(chars);
+ return buf.join('');
}
// define ngSanitize module and register $sanitize service
angular.module('ngSanitize', []).provider('$sanitize', $SanitizeProvider);
-/* global sanitizeText: false */
-
/**
* @ngdoc filter
* @name linky
* @kind function
*
* @description
- * Finds links in text input and turns them into html links. Supports http/https/ftp/mailto and
+ * Finds links in text input and turns them into html links. Supports `http/https/ftp/mailto` and
* plain email address links.
*
* Requires the {@link ngSanitize `ngSanitize`} module to be installed.
*
* @param {string} text Input text.
- * @param {string} target Window (_blank|_self|_parent|_top) or named frame to open links in.
- * @returns {string} Html-linkified text.
+ * @param {string} target Window (`_blank|_self|_parent|_top`) or named frame to open links in.
+ * @param {object|function(url)} [attributes] Add custom attributes to the link element.
+ *
+ * Can be one of:
+ *
+ * - `object`: A map of attributes
+ * - `function`: Takes the url as a parameter and returns a map of attributes
+ *
+ * If the map of attributes contains a value for `target`, it overrides the value of
+ * the target parameter.
+ *
+ *
+ * @returns {string} Html-linkified and {@link $sanitize sanitized} text.
*
* @usage
<span ng-bind-html="linky_expression | linky"></span>
@@ -550,25 +570,13 @@ angular.module('ngSanitize', []).provider('$sanitize', $SanitizeProvider);
* @example
<example module="linkyExample" deps="angular-sanitize.js">
<file name="index.html">
- <script>
- angular.module('linkyExample', ['ngSanitize'])
- .controller('ExampleController', ['$scope', function($scope) {
- $scope.snippet =
- 'Pretty text with some links:\n'+
- 'http://angularjs.org/,\n'+
- 'mailto:us@somewhere.org,\n'+
- 'another@somewhere.org,\n'+
- 'and one more: ftp://127.0.0.1/.';
- $scope.snippetWithTarget = 'http://angularjs.org/';
- }]);
- </script>
<div ng-controller="ExampleController">
Snippet: <textarea ng-model="snippet" cols="60" rows="3"></textarea>
<table>
<tr>
- <td>Filter</td>
- <td>Source</td>
- <td>Rendered</td>
+ <th>Filter</th>
+ <th>Source</th>
+ <th>Rendered</th>
</tr>
<tr id="linky-filter">
<td>linky filter</td>
@@ -582,10 +590,19 @@ angular.module('ngSanitize', []).provider('$sanitize', $SanitizeProvider);
<tr id="linky-target">
<td>linky target</td>
<td>
- <pre>&lt;div ng-bind-html="snippetWithTarget | linky:'_blank'"&gt;<br>&lt;/div&gt;</pre>
+ <pre>&lt;div ng-bind-html="snippetWithSingleURL | linky:'_blank'"&gt;<br>&lt;/div&gt;</pre>
+ </td>
+ <td>
+ <div ng-bind-html="snippetWithSingleURL | linky:'_blank'"></div>
+ </td>
+ </tr>
+ <tr id="linky-custom-attributes">
+ <td>linky custom attributes</td>
+ <td>
+ <pre>&lt;div ng-bind-html="snippetWithSingleURL | linky:'_self':{rel: 'nofollow'}"&gt;<br>&lt;/div&gt;</pre>
</td>
<td>
- <div ng-bind-html="snippetWithTarget | linky:'_blank'"></div>
+ <div ng-bind-html="snippetWithSingleURL | linky:'_self':{rel: 'nofollow'}"></div>
</td>
</tr>
<tr id="escaped-html">
@@ -595,6 +612,18 @@ angular.module('ngSanitize', []).provider('$sanitize', $SanitizeProvider);
</tr>
</table>
</file>
+ <file name="script.js">
+ angular.module('linkyExample', ['ngSanitize'])
+ .controller('ExampleController', ['$scope', function($scope) {
+ $scope.snippet =
+ 'Pretty text with some links:\n'+
+ 'http://angularjs.org/,\n'+
+ 'mailto:us@somewhere.org,\n'+
+ 'another@somewhere.org,\n'+
+ 'and one more: ftp://127.0.0.1/.';
+ $scope.snippetWithSingleURL = 'http://angularjs.org/';
+ }]);
+ </file>
<file name="protractor.js" type="protractor">
it('should linkify the snippet with urls', function() {
expect(element(by.id('linky-filter')).element(by.binding('snippet | linky')).getText()).
@@ -622,10 +651,17 @@ angular.module('ngSanitize', []).provider('$sanitize', $SanitizeProvider);
it('should work with the target property', function() {
expect(element(by.id('linky-target')).
- element(by.binding("snippetWithTarget | linky:'_blank'")).getText()).
+ element(by.binding("snippetWithSingleURL | linky:'_blank'")).getText()).
toBe('http://angularjs.org/');
expect(element(by.css('#linky-target a')).getAttribute('target')).toEqual('_blank');
});
+
+ it('should optionally add custom attributes', function() {
+ expect(element(by.id('linky-custom-attributes')).
+ element(by.binding("snippetWithSingleURL | linky:'_self':{rel: 'nofollow'}")).getText()).
+ toBe('http://angularjs.org/');
+ expect(element(by.css('#linky-custom-attributes a')).getAttribute('rel')).toEqual('nofollow');
+ });
</file>
</example>
*/
@@ -634,8 +670,21 @@ angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
/((ftp|https?):\/\/|(www\.)|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"\u201d\u2019]/i,
MAILTO_REGEXP = /^mailto:/i;
- return function(text, target) {
- if (!text) return text;
+ var linkyMinErr = angular.$$minErr('linky');
+ var isDefined = angular.isDefined;
+ var isFunction = angular.isFunction;
+ var isObject = angular.isObject;
+ var isString = angular.isString;
+
+ return function(text, target, attributes) {
+ if (text == null || text === '') return text;
+ if (!isString(text)) throw linkyMinErr('notstring', 'Expected string but received: {0}', text);
+
+ var attributesFn =
+ isFunction(attributes) ? attributes :
+ isObject(attributes) ? function getAttributesObject() {return attributes;} :
+ function getEmptyAttributesObject() {return {};};
+
var match;
var raw = text;
var html = [];
@@ -664,8 +713,14 @@ angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
}
function addLink(url, text) {
+ var key, linkAttributes = attributesFn(url);
html.push('<a ');
- if (angular.isDefined(target)) {
+
+ for (key in linkAttributes) {
+ html.push(key + '="' + linkAttributes[key] + '" ');
+ }
+
+ if (isDefined(target) && !('target' in linkAttributes)) {
html.push('target="',
target,
'" ');