2014-05-12 20:08:19 +02:00
|
|
|
/**
|
2019-03-29 22:00:08 +01:00
|
|
|
* @license AngularJS v1.7.8
|
|
|
|
* (c) 2010-2018 Google, Inc. http://angularjs.org
|
2014-05-12 20:08:19 +02:00
|
|
|
* License: MIT
|
|
|
|
*/
|
2016-05-16 13:33:49 +02:00
|
|
|
(function(window, angular) {'use strict';
|
|
|
|
|
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* Any commits to this file should be reviewed with security in mind. *
|
|
|
|
* Changes to this file can potentially create security vulnerabilities. *
|
|
|
|
* An approval from 2 Core members with history of modifying *
|
|
|
|
* this file is required. *
|
|
|
|
* *
|
|
|
|
* Does the change somehow allow for arbitrary javascript to be executed? *
|
|
|
|
* Or allows for someone to change the prototype of built-in objects? *
|
|
|
|
* Or gives undesired access to variables likes document or window? *
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
var $sanitizeMinErr = angular.$$minErr('$sanitize');
|
2017-05-13 13:25:33 +02:00
|
|
|
var bind;
|
|
|
|
var extend;
|
|
|
|
var forEach;
|
2019-03-29 22:00:08 +01:00
|
|
|
var isArray;
|
2017-05-13 13:25:33 +02:00
|
|
|
var isDefined;
|
|
|
|
var lowercase;
|
|
|
|
var noop;
|
2019-03-29 22:00:08 +01:00
|
|
|
var nodeContains;
|
2017-05-13 13:25:33 +02:00
|
|
|
var htmlParser;
|
|
|
|
var htmlSanitizeWriter;
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngdoc module
|
|
|
|
* @name ngSanitize
|
|
|
|
* @description
|
|
|
|
*
|
|
|
|
* The `ngSanitize` module provides functionality to sanitize HTML.
|
|
|
|
*
|
|
|
|
* See {@link ngSanitize.$sanitize `$sanitize`} for usage.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngdoc service
|
|
|
|
* @name $sanitize
|
2014-08-10 12:02:48 +02:00
|
|
|
* @kind function
|
2014-05-12 20:08:19 +02:00
|
|
|
*
|
|
|
|
* @description
|
2016-05-16 13:33:49 +02:00
|
|
|
* 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
|
2019-03-29 22:00:08 +01:00
|
|
|
* then serialized back to a properly escaped HTML string. This means that no unsafe input can make
|
2016-05-16 13:33:49 +02:00
|
|
|
* it into the returned string.
|
2014-05-12 20:08:19 +02:00
|
|
|
*
|
2016-05-16 13:33:49 +02:00
|
|
|
* The whitelist for URL sanitization of attribute values is configured using the functions
|
2019-03-29 22:00:08 +01:00
|
|
|
* `aHrefSanitizationWhitelist` and `imgSrcSanitizationWhitelist` of {@link $compileProvider}.
|
2016-05-16 13:33:49 +02:00
|
|
|
*
|
|
|
|
* The input may also contain SVG markup if this is enabled via {@link $sanitizeProvider}.
|
|
|
|
*
|
|
|
|
* @param {string} html HTML input.
|
|
|
|
* @returns {string} Sanitized HTML.
|
2014-05-12 20:08:19 +02:00
|
|
|
*
|
|
|
|
* @example
|
2017-05-13 13:25:33 +02:00
|
|
|
<example module="sanitizeExample" deps="angular-sanitize.js" name="sanitize-service">
|
2014-05-12 20:08:19 +02:00
|
|
|
<file name="index.html">
|
|
|
|
<script>
|
2014-08-10 12:02:48 +02:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
}]);
|
2014-05-12 20:08:19 +02:00
|
|
|
</script>
|
2014-08-10 12:02:48 +02:00
|
|
|
<div ng-controller="ExampleController">
|
2014-05-12 20:08:19 +02:00
|
|
|
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>
|
|
|
|
</file>
|
|
|
|
<file name="protractor.js" type="protractor">
|
|
|
|
it('should sanitize the html snippet by default', function() {
|
2017-05-13 13:25:33 +02:00
|
|
|
expect(element(by.css('#bind-html-with-sanitize div')).getAttribute('innerHTML')).
|
2014-05-12 20:08:19 +02:00
|
|
|
toBe('<p>an html\n<em>click here</em>\nsnippet</p>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should inline raw snippet if bound to a trusted value', function() {
|
2017-05-13 13:25:33 +02:00
|
|
|
expect(element(by.css('#bind-html-with-trust div')).getAttribute('innerHTML')).
|
2014-05-12 20:08:19 +02:00
|
|
|
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() {
|
2017-05-13 13:25:33 +02:00
|
|
|
expect(element(by.css('#bind-default div')).getAttribute('innerHTML')).
|
2014-05-12 20:08:19 +02:00
|
|
|
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>');
|
2017-05-13 13:25:33 +02:00
|
|
|
expect(element(by.css('#bind-html-with-sanitize div')).getAttribute('innerHTML')).
|
2014-05-12 20:08:19 +02:00
|
|
|
toBe('new <b>text</b>');
|
2017-05-13 13:25:33 +02:00
|
|
|
expect(element(by.css('#bind-html-with-trust div')).getAttribute('innerHTML')).toBe(
|
2014-05-12 20:08:19 +02:00
|
|
|
'new <b onclick="alert(1)">text</b>');
|
2017-05-13 13:25:33 +02:00
|
|
|
expect(element(by.css('#bind-default div')).getAttribute('innerHTML')).toBe(
|
2014-05-12 20:08:19 +02:00
|
|
|
"new <b onclick=\"alert(1)\">text</b>");
|
|
|
|
});
|
|
|
|
</file>
|
|
|
|
</example>
|
|
|
|
*/
|
2016-05-16 13:33:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngdoc provider
|
|
|
|
* @name $sanitizeProvider
|
2017-05-13 13:25:33 +02:00
|
|
|
* @this
|
2016-05-16 13:33:49 +02:00
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* Creates and configures {@link $sanitize} instance.
|
|
|
|
*/
|
2014-05-12 20:08:19 +02:00
|
|
|
function $SanitizeProvider() {
|
2019-03-29 22:00:08 +01:00
|
|
|
var hasBeenInstantiated = false;
|
2016-05-16 13:33:49 +02:00
|
|
|
var svgEnabled = false;
|
|
|
|
|
2014-05-12 20:08:19 +02:00
|
|
|
this.$get = ['$$sanitizeUri', function($$sanitizeUri) {
|
2019-03-29 22:00:08 +01:00
|
|
|
hasBeenInstantiated = true;
|
2016-05-16 13:33:49 +02:00
|
|
|
if (svgEnabled) {
|
2017-05-13 13:25:33 +02:00
|
|
|
extend(validElements, svgElements);
|
2016-05-16 13:33:49 +02:00
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
return function(html) {
|
|
|
|
var buf = [];
|
|
|
|
htmlParser(html, htmlSanitizeWriter(buf, function(uri, isImage) {
|
2016-05-16 13:33:49 +02:00
|
|
|
return !/^unsafe:/.test($$sanitizeUri(uri, isImage));
|
2014-05-12 20:08:19 +02:00
|
|
|
}));
|
|
|
|
return buf.join('');
|
|
|
|
};
|
|
|
|
}];
|
2016-05-16 13:33:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
2019-03-29 22:00:08 +01:00
|
|
|
* @returns {boolean|$sanitizeProvider} Returns the currently configured value if called
|
2016-05-16 13:33:49 +02:00
|
|
|
* without an argument or self for chaining otherwise.
|
|
|
|
*/
|
|
|
|
this.enableSvg = function(enableSvg) {
|
2017-05-13 13:25:33 +02:00
|
|
|
if (isDefined(enableSvg)) {
|
2016-05-16 13:33:49 +02:00
|
|
|
svgEnabled = enableSvg;
|
|
|
|
return this;
|
|
|
|
} else {
|
|
|
|
return svgEnabled;
|
|
|
|
}
|
|
|
|
};
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2019-03-29 22:00:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngdoc method
|
|
|
|
* @name $sanitizeProvider#addValidElements
|
|
|
|
* @kind function
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* Extends the built-in lists of valid HTML/SVG elements, i.e. elements that are considered safe
|
|
|
|
* and are not stripped off during sanitization. You can extend the following lists of elements:
|
|
|
|
*
|
|
|
|
* - `htmlElements`: A list of elements (tag names) to extend the current list of safe HTML
|
|
|
|
* elements. HTML elements considered safe will not be removed during sanitization. All other
|
|
|
|
* elements will be stripped off.
|
|
|
|
*
|
|
|
|
* - `htmlVoidElements`: This is similar to `htmlElements`, but marks the elements as
|
|
|
|
* "void elements" (similar to HTML
|
|
|
|
* [void elements](https://rawgit.com/w3c/html/html5.1-2/single-page.html#void-elements)). These
|
|
|
|
* elements have no end tag and cannot have content.
|
|
|
|
*
|
|
|
|
* - `svgElements`: This is similar to `htmlElements`, but for SVG elements. This list is only
|
|
|
|
* taken into account if SVG is {@link ngSanitize.$sanitizeProvider#enableSvg enabled} for
|
|
|
|
* `$sanitize`.
|
|
|
|
*
|
|
|
|
* <div class="alert alert-info">
|
|
|
|
* This method must be called during the {@link angular.Module#config config} phase. Once the
|
|
|
|
* `$sanitize` service has been instantiated, this method has no effect.
|
|
|
|
* </div>
|
|
|
|
*
|
|
|
|
* <div class="alert alert-warning">
|
|
|
|
* Keep in mind that extending the built-in lists of elements may expose your app to XSS or
|
|
|
|
* other vulnerabilities. Be very mindful of the elements you add.
|
|
|
|
* </div>
|
|
|
|
*
|
|
|
|
* @param {Array<String>|Object} elements - A list of valid HTML elements or an object with one or
|
|
|
|
* more of the following properties:
|
|
|
|
* - **htmlElements** - `{Array<String>}` - A list of elements to extend the current list of
|
|
|
|
* HTML elements.
|
|
|
|
* - **htmlVoidElements** - `{Array<String>}` - A list of elements to extend the current list of
|
|
|
|
* void HTML elements; i.e. elements that do not have an end tag.
|
|
|
|
* - **svgElements** - `{Array<String>}` - A list of elements to extend the current list of SVG
|
|
|
|
* elements. The list of SVG elements is only taken into account if SVG is
|
|
|
|
* {@link ngSanitize.$sanitizeProvider#enableSvg enabled} for `$sanitize`.
|
|
|
|
*
|
|
|
|
* Passing an array (`[...]`) is equivalent to passing `{htmlElements: [...]}`.
|
|
|
|
*
|
|
|
|
* @return {$sanitizeProvider} Returns self for chaining.
|
|
|
|
*/
|
|
|
|
this.addValidElements = function(elements) {
|
|
|
|
if (!hasBeenInstantiated) {
|
|
|
|
if (isArray(elements)) {
|
|
|
|
elements = {htmlElements: elements};
|
|
|
|
}
|
|
|
|
|
|
|
|
addElementsTo(svgElements, elements.svgElements);
|
|
|
|
addElementsTo(voidElements, elements.htmlVoidElements);
|
|
|
|
addElementsTo(validElements, elements.htmlVoidElements);
|
|
|
|
addElementsTo(validElements, elements.htmlElements);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngdoc method
|
|
|
|
* @name $sanitizeProvider#addValidAttrs
|
|
|
|
* @kind function
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* Extends the built-in list of valid attributes, i.e. attributes that are considered safe and are
|
|
|
|
* not stripped off during sanitization.
|
|
|
|
*
|
|
|
|
* **Note**:
|
|
|
|
* The new attributes will not be treated as URI attributes, which means their values will not be
|
|
|
|
* sanitized as URIs using `$compileProvider`'s
|
|
|
|
* {@link ng.$compileProvider#aHrefSanitizationWhitelist aHrefSanitizationWhitelist} and
|
|
|
|
* {@link ng.$compileProvider#imgSrcSanitizationWhitelist imgSrcSanitizationWhitelist}.
|
|
|
|
*
|
|
|
|
* <div class="alert alert-info">
|
|
|
|
* This method must be called during the {@link angular.Module#config config} phase. Once the
|
|
|
|
* `$sanitize` service has been instantiated, this method has no effect.
|
|
|
|
* </div>
|
|
|
|
*
|
|
|
|
* <div class="alert alert-warning">
|
|
|
|
* Keep in mind that extending the built-in list of attributes may expose your app to XSS or
|
|
|
|
* other vulnerabilities. Be very mindful of the attributes you add.
|
|
|
|
* </div>
|
|
|
|
*
|
|
|
|
* @param {Array<String>} attrs - A list of valid attributes.
|
|
|
|
*
|
|
|
|
* @returns {$sanitizeProvider} Returns self for chaining.
|
|
|
|
*/
|
|
|
|
this.addValidAttrs = function(attrs) {
|
|
|
|
if (!hasBeenInstantiated) {
|
|
|
|
extend(validAttrs, arrayToMap(attrs, true));
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Private stuff
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
bind = angular.bind;
|
|
|
|
extend = angular.extend;
|
|
|
|
forEach = angular.forEach;
|
2019-03-29 22:00:08 +01:00
|
|
|
isArray = angular.isArray;
|
2017-05-13 13:25:33 +02:00
|
|
|
isDefined = angular.isDefined;
|
2019-03-29 22:00:08 +01:00
|
|
|
lowercase = angular.$$lowercase;
|
2017-05-13 13:25:33 +02:00
|
|
|
noop = angular.noop;
|
|
|
|
|
|
|
|
htmlParser = htmlParserImpl;
|
|
|
|
htmlSanitizeWriter = htmlSanitizeWriterImpl;
|
|
|
|
|
2019-03-29 22:00:08 +01:00
|
|
|
nodeContains = window.Node.prototype.contains || /** @this */ function(arg) {
|
|
|
|
// eslint-disable-next-line no-bitwise
|
|
|
|
return !!(this.compareDocumentPosition(arg) & 16);
|
|
|
|
};
|
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
// 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
|
2019-03-29 22:00:08 +01:00
|
|
|
var voidElements = stringToMap('area,br,col,hr,img,wbr');
|
2017-05-13 13:25:33 +02:00
|
|
|
|
|
|
|
// Elements that you can, intentionally, leave open (and which close themselves)
|
|
|
|
// http://dev.w3.org/html5/spec/Overview.html#optional-tags
|
2019-03-29 22:00:08 +01:00
|
|
|
var optionalEndTagBlockElements = stringToMap('colgroup,dd,dt,li,p,tbody,td,tfoot,th,thead,tr'),
|
|
|
|
optionalEndTagInlineElements = stringToMap('rp,rt'),
|
2017-05-13 13:25:33 +02:00
|
|
|
optionalEndTagElements = extend({},
|
|
|
|
optionalEndTagInlineElements,
|
|
|
|
optionalEndTagBlockElements);
|
|
|
|
|
|
|
|
// Safe Block Elements - HTML5
|
2019-03-29 22:00:08 +01:00
|
|
|
var blockElements = extend({}, optionalEndTagBlockElements, stringToMap('address,article,' +
|
2017-05-13 13:25:33 +02:00
|
|
|
'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
|
2019-03-29 22:00:08 +01:00
|
|
|
var inlineElements = extend({}, optionalEndTagInlineElements, stringToMap('a,abbr,acronym,b,' +
|
2017-05-13 13:25:33 +02:00
|
|
|
'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
|
2019-03-29 22:00:08 +01:00
|
|
|
var svgElements = stringToMap('circle,defs,desc,ellipse,font-face,font-face-name,font-face-src,g,glyph,' +
|
2017-05-13 13:25:33 +02:00
|
|
|
'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)
|
2019-03-29 22:00:08 +01:00
|
|
|
var blockedElements = stringToMap('script,style');
|
2017-05-13 13:25:33 +02:00
|
|
|
|
|
|
|
var validElements = extend({},
|
|
|
|
voidElements,
|
|
|
|
blockElements,
|
|
|
|
inlineElements,
|
|
|
|
optionalEndTagElements);
|
|
|
|
|
|
|
|
//Attributes that have href and hence need to be sanitized
|
2019-03-29 22:00:08 +01:00
|
|
|
var uriAttrs = stringToMap('background,cite,href,longdesc,src,xlink:href,xml:base');
|
2017-05-13 13:25:33 +02:00
|
|
|
|
2019-03-29 22:00:08 +01:00
|
|
|
var htmlAttrs = stringToMap('abbr,align,alt,axis,bgcolor,border,cellpadding,cellspacing,class,clear,' +
|
2017-05-13 13:25:33 +02:00
|
|
|
'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
|
2019-03-29 22:00:08 +01:00
|
|
|
var svgAttrs = stringToMap('accent-height,accumulate,additive,alphabetic,arabic-form,ascent,' +
|
2017-05-13 13:25:33 +02:00
|
|
|
'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);
|
|
|
|
|
2019-03-29 22:00:08 +01:00
|
|
|
function stringToMap(str, lowercaseKeys) {
|
|
|
|
return arrayToMap(str.split(','), lowercaseKeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
function arrayToMap(items, lowercaseKeys) {
|
|
|
|
var obj = {}, i;
|
2017-05-13 13:25:33 +02:00
|
|
|
for (i = 0; i < items.length; i++) {
|
|
|
|
obj[lowercaseKeys ? lowercase(items[i]) : items[i]] = true;
|
|
|
|
}
|
|
|
|
return obj;
|
2016-05-16 13:33:49 +02:00
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2019-03-29 22:00:08 +01:00
|
|
|
function addElementsTo(elementsMap, newElements) {
|
|
|
|
if (newElements && newElements.length) {
|
|
|
|
extend(elementsMap, arrayToMap(newElements));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an inert document that contains the dirty HTML that needs sanitizing
|
|
|
|
* Depending upon browser support we use one of three strategies for doing this.
|
|
|
|
* Support: Safari 10.x -> XHR strategy
|
|
|
|
* Support: Firefox -> DomParser strategy
|
|
|
|
*/
|
|
|
|
var getInertBodyElement /* function(html: string): HTMLBodyElement */ = (function(window, document) {
|
|
|
|
var inertDocument;
|
|
|
|
if (document && document.implementation) {
|
|
|
|
inertDocument = document.implementation.createHTMLDocument('inert');
|
2017-05-13 13:25:33 +02:00
|
|
|
} else {
|
|
|
|
throw $sanitizeMinErr('noinert', 'Can\'t create an inert html document');
|
|
|
|
}
|
2019-03-29 22:00:08 +01:00
|
|
|
var inertBodyElement = (inertDocument.documentElement || inertDocument.getDocumentElement()).querySelector('body');
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2019-03-29 22:00:08 +01:00
|
|
|
// Check for the Safari 10.1 bug - which allows JS to run inside the SVG G element
|
|
|
|
inertBodyElement.innerHTML = '<svg><g onload="this.parentNode.remove()"></g></svg>';
|
|
|
|
if (!inertBodyElement.querySelector('svg')) {
|
|
|
|
return getInertBodyElement_XHR;
|
2017-05-13 13:25:33 +02:00
|
|
|
} else {
|
2019-03-29 22:00:08 +01:00
|
|
|
// Check for the Firefox bug - which prevents the inner img JS from being sanitized
|
|
|
|
inertBodyElement.innerHTML = '<svg><p><style><img src="</style><img src=x onerror=alert(1)//">';
|
|
|
|
if (inertBodyElement.querySelector('svg img')) {
|
|
|
|
return getInertBodyElement_DOMParser;
|
|
|
|
} else {
|
|
|
|
return getInertBodyElement_InertDocument;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getInertBodyElement_XHR(html) {
|
|
|
|
// We add this dummy element to ensure that the rest of the content is parsed as expected
|
|
|
|
// e.g. leading whitespace is maintained and tags like `<meta>` do not get hoisted to the `<head>` tag.
|
|
|
|
html = '<remove></remove>' + html;
|
|
|
|
try {
|
|
|
|
html = encodeURI(html);
|
|
|
|
} catch (e) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
var xhr = new window.XMLHttpRequest();
|
|
|
|
xhr.responseType = 'document';
|
|
|
|
xhr.open('GET', 'data:text/html;charset=utf-8,' + html, false);
|
|
|
|
xhr.send(null);
|
|
|
|
var body = xhr.response.body;
|
|
|
|
body.firstChild.remove();
|
|
|
|
return body;
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
2019-03-29 22:00:08 +01:00
|
|
|
|
|
|
|
function getInertBodyElement_DOMParser(html) {
|
|
|
|
// We add this dummy element to ensure that the rest of the content is parsed as expected
|
|
|
|
// e.g. leading whitespace is maintained and tags like `<meta>` do not get hoisted to the `<head>` tag.
|
|
|
|
html = '<remove></remove>' + html;
|
|
|
|
try {
|
|
|
|
var body = new window.DOMParser().parseFromString(html, 'text/html').body;
|
|
|
|
body.firstChild.remove();
|
|
|
|
return body;
|
|
|
|
} catch (e) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getInertBodyElement_InertDocument(html) {
|
|
|
|
inertBodyElement.innerHTML = html;
|
|
|
|
|
|
|
|
// Support: IE 9-11 only
|
|
|
|
// strip custom-namespaced attributes on IE<=11
|
|
|
|
if (document.documentMode) {
|
|
|
|
stripCustomNsAttrs(inertBodyElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
return inertBodyElement;
|
|
|
|
}
|
|
|
|
})(window, window.document);
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
/**
|
|
|
|
* @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;
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
2019-03-29 22:00:08 +01:00
|
|
|
|
|
|
|
var inertBodyElement = getInertBodyElement(html);
|
|
|
|
if (!inertBodyElement) return '';
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
//mXSS protection
|
|
|
|
var mXSSAttempts = 5;
|
|
|
|
do {
|
|
|
|
if (mXSSAttempts === 0) {
|
|
|
|
throw $sanitizeMinErr('uinput', 'Failed to sanitize html because the input is unstable');
|
|
|
|
}
|
|
|
|
mXSSAttempts--;
|
|
|
|
|
2019-03-29 22:00:08 +01:00
|
|
|
// trigger mXSS if it is going to happen by reading and writing the innerHTML
|
|
|
|
html = inertBodyElement.innerHTML;
|
|
|
|
inertBodyElement = getInertBodyElement(html);
|
2017-05-13 13:25:33 +02:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
var nextNode;
|
|
|
|
if (!(nextNode = node.firstChild)) {
|
2019-03-29 22:00:08 +01:00
|
|
|
if (node.nodeType === 1) {
|
2017-05-13 13:25:33 +02:00
|
|
|
handler.end(node.nodeName.toLowerCase());
|
|
|
|
}
|
2019-03-29 22:00:08 +01:00
|
|
|
nextNode = getNonDescendant('nextSibling', node);
|
2017-05-13 13:25:33 +02:00
|
|
|
if (!nextNode) {
|
|
|
|
while (nextNode == null) {
|
2019-03-29 22:00:08 +01:00
|
|
|
node = getNonDescendant('parentNode', node);
|
2017-05-13 13:25:33 +02:00
|
|
|
if (node === inertBodyElement) break;
|
2019-03-29 22:00:08 +01:00
|
|
|
nextNode = getNonDescendant('nextSibling', node);
|
|
|
|
if (node.nodeType === 1) {
|
2017-05-13 13:25:33 +02:00
|
|
|
handler.end(node.nodeName.toLowerCase());
|
|
|
|
}
|
2016-05-16 13:33:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-13 13:25:33 +02:00
|
|
|
node = nextNode;
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
while ((node = inertBodyElement.firstChild)) {
|
|
|
|
inertBodyElement.removeChild(node);
|
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
function attrToMap(attrs) {
|
|
|
|
var map = {};
|
|
|
|
for (var i = 0, ii = attrs.length; i < ii; i++) {
|
|
|
|
var attr = attrs[i];
|
|
|
|
map[attr.name] = attr.value;
|
|
|
|
}
|
|
|
|
return map;
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2016-05-16 13:33:49 +02:00
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
/**
|
|
|
|
* 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, '&').
|
|
|
|
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, '<').
|
|
|
|
replace(/>/g, '>');
|
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
/**
|
|
|
|
* 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('>');
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line eqeqeq
|
|
|
|
if (tag == ignoreCurrentElement) {
|
|
|
|
ignoreCurrentElement = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
chars: function(chars) {
|
|
|
|
if (!ignoreCurrentElement) {
|
|
|
|
out(encodeEntities(chars));
|
|
|
|
}
|
2016-05-16 13:33:49 +02:00
|
|
|
}
|
2017-05-13 13:25:33 +02:00
|
|
|
};
|
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
while (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--;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 13:33:49 +02:00
|
|
|
}
|
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
var nextNode = node.firstChild;
|
|
|
|
if (nextNode) {
|
|
|
|
stripCustomNsAttrs(nextNode);
|
|
|
|
}
|
2016-05-16 13:33:49 +02:00
|
|
|
|
2019-03-29 22:00:08 +01:00
|
|
|
node = getNonDescendant('nextSibling', node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNonDescendant(propName, node) {
|
|
|
|
// An element is clobbered if its `propName` property points to one of its descendants
|
|
|
|
var nextNode = node[propName];
|
|
|
|
if (nextNode && nodeContains.call(node, nextNode)) {
|
|
|
|
throw $sanitizeMinErr('elclob', 'Failed to sanitize html because the element is clobbered: {0}', node.outerHTML || node.outerText);
|
2017-05-13 13:25:33 +02:00
|
|
|
}
|
2019-03-29 22:00:08 +01:00
|
|
|
return nextNode;
|
2016-05-16 13:33:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
function sanitizeText(chars) {
|
|
|
|
var buf = [];
|
|
|
|
var writer = htmlSanitizeWriter(buf, noop);
|
|
|
|
writer.chars(chars);
|
|
|
|
return buf.join('');
|
|
|
|
}
|
2016-05-16 13:33:49 +02:00
|
|
|
|
|
|
|
|
2014-05-12 20:08:19 +02:00
|
|
|
// define ngSanitize module and register $sanitize service
|
2019-03-29 22:00:08 +01:00
|
|
|
angular.module('ngSanitize', [])
|
|
|
|
.provider('$sanitize', $SanitizeProvider)
|
|
|
|
.info({ angularVersion: '1.7.8' });
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngdoc filter
|
|
|
|
* @name linky
|
2014-08-10 12:02:48 +02:00
|
|
|
* @kind function
|
2014-05-12 20:08:19 +02:00
|
|
|
*
|
|
|
|
* @description
|
2019-03-29 22:00:08 +01:00
|
|
|
* Finds links in text input and turns them into html links. Supports `http/https/ftp/sftp/mailto` and
|
2014-05-12 20:08:19 +02:00
|
|
|
* plain email address links.
|
|
|
|
*
|
|
|
|
* Requires the {@link ngSanitize `ngSanitize`} module to be installed.
|
|
|
|
*
|
|
|
|
* @param {string} text Input text.
|
2019-03-29 22:00:08 +01:00
|
|
|
* @param {string} [target] Window (`_blank|_self|_parent|_top`) or named frame to open links in.
|
2016-05-16 13:33:49 +02:00
|
|
|
* @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.
|
2014-05-12 20:08:19 +02:00
|
|
|
*
|
|
|
|
* @usage
|
|
|
|
<span ng-bind-html="linky_expression | linky"></span>
|
|
|
|
*
|
|
|
|
* @example
|
2017-05-13 13:25:33 +02:00
|
|
|
<example module="linkyExample" deps="angular-sanitize.js" name="linky-filter">
|
2014-05-12 20:08:19 +02:00
|
|
|
<file name="index.html">
|
2014-08-10 12:02:48 +02:00
|
|
|
<div ng-controller="ExampleController">
|
2014-05-12 20:08:19 +02:00
|
|
|
Snippet: <textarea ng-model="snippet" cols="60" rows="3"></textarea>
|
|
|
|
<table>
|
|
|
|
<tr>
|
2016-05-16 13:33:49 +02:00
|
|
|
<th>Filter</th>
|
|
|
|
<th>Source</th>
|
|
|
|
<th>Rendered</th>
|
2014-05-12 20:08:19 +02:00
|
|
|
</tr>
|
|
|
|
<tr id="linky-filter">
|
|
|
|
<td>linky filter</td>
|
|
|
|
<td>
|
|
|
|
<pre><div ng-bind-html="snippet | linky"><br></div></pre>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<div ng-bind-html="snippet | linky"></div>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr id="linky-target">
|
|
|
|
<td>linky target</td>
|
|
|
|
<td>
|
2016-05-16 13:33:49 +02:00
|
|
|
<pre><div ng-bind-html="snippetWithSingleURL | linky:'_blank'"><br></div></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><div ng-bind-html="snippetWithSingleURL | linky:'_self':{rel: 'nofollow'}"><br></div></pre>
|
2014-05-12 20:08:19 +02:00
|
|
|
</td>
|
|
|
|
<td>
|
2016-05-16 13:33:49 +02:00
|
|
|
<div ng-bind-html="snippetWithSingleURL | linky:'_self':{rel: 'nofollow'}"></div>
|
2014-05-12 20:08:19 +02:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr id="escaped-html">
|
|
|
|
<td>no filter</td>
|
|
|
|
<td><pre><div ng-bind="snippet"><br></div></pre></td>
|
|
|
|
<td><div ng-bind="snippet"></div></td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
</file>
|
2016-05-16 13:33:49 +02:00
|
|
|
<file name="script.js">
|
|
|
|
angular.module('linkyExample', ['ngSanitize'])
|
|
|
|
.controller('ExampleController', ['$scope', function($scope) {
|
|
|
|
$scope.snippet =
|
2017-05-13 13:25:33 +02:00
|
|
|
'Pretty text with some links:\n' +
|
|
|
|
'http://angularjs.org/,\n' +
|
|
|
|
'mailto:us@somewhere.org,\n' +
|
|
|
|
'another@somewhere.org,\n' +
|
2016-05-16 13:33:49 +02:00
|
|
|
'and one more: ftp://127.0.0.1/.';
|
|
|
|
$scope.snippetWithSingleURL = 'http://angularjs.org/';
|
|
|
|
}]);
|
|
|
|
</file>
|
2014-05-12 20:08:19 +02:00
|
|
|
<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()).
|
|
|
|
toBe('Pretty text with some links: http://angularjs.org/, us@somewhere.org, ' +
|
|
|
|
'another@somewhere.org, and one more: ftp://127.0.0.1/.');
|
|
|
|
expect(element.all(by.css('#linky-filter a')).count()).toEqual(4);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not linkify snippet without the linky filter', function() {
|
|
|
|
expect(element(by.id('escaped-html')).element(by.binding('snippet')).getText()).
|
|
|
|
toBe('Pretty text with some links: http://angularjs.org/, mailto:us@somewhere.org, ' +
|
|
|
|
'another@somewhere.org, and one more: ftp://127.0.0.1/.');
|
|
|
|
expect(element.all(by.css('#escaped-html a')).count()).toEqual(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update', function() {
|
|
|
|
element(by.model('snippet')).clear();
|
|
|
|
element(by.model('snippet')).sendKeys('new http://link.');
|
|
|
|
expect(element(by.id('linky-filter')).element(by.binding('snippet | linky')).getText()).
|
|
|
|
toBe('new http://link.');
|
|
|
|
expect(element.all(by.css('#linky-filter a')).count()).toEqual(1);
|
|
|
|
expect(element(by.id('escaped-html')).element(by.binding('snippet')).getText())
|
|
|
|
.toBe('new http://link.');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with the target property', function() {
|
|
|
|
expect(element(by.id('linky-target')).
|
2016-05-16 13:33:49 +02:00
|
|
|
element(by.binding("snippetWithSingleURL | linky:'_blank'")).getText()).
|
2014-05-12 20:08:19 +02:00
|
|
|
toBe('http://angularjs.org/');
|
|
|
|
expect(element(by.css('#linky-target a')).getAttribute('target')).toEqual('_blank');
|
|
|
|
});
|
2016-05-16 13:33:49 +02:00
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
2014-05-12 20:08:19 +02:00
|
|
|
</file>
|
|
|
|
</example>
|
|
|
|
*/
|
|
|
|
angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
|
|
|
|
var LINKY_URL_REGEXP =
|
2019-03-29 22:00:08 +01:00
|
|
|
/((s?ftp|https?):\/\/|(www\.)|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"\u201d\u2019]/i,
|
2016-05-16 13:33:49 +02:00
|
|
|
MAILTO_REGEXP = /^mailto:/i;
|
|
|
|
|
|
|
|
var linkyMinErr = angular.$$minErr('linky');
|
2017-05-13 13:25:33 +02:00
|
|
|
var isDefined = angular.isDefined;
|
|
|
|
var isFunction = angular.isFunction;
|
|
|
|
var isObject = angular.isObject;
|
2016-05-16 13:33:49 +02:00
|
|
|
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);
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2017-05-13 13:25:33 +02:00
|
|
|
var attributesFn =
|
|
|
|
isFunction(attributes) ? attributes :
|
|
|
|
isObject(attributes) ? function getAttributesObject() {return attributes;} :
|
|
|
|
function getEmptyAttributesObject() {return {};};
|
|
|
|
|
2014-05-12 20:08:19 +02:00
|
|
|
var match;
|
|
|
|
var raw = text;
|
|
|
|
var html = [];
|
|
|
|
var url;
|
|
|
|
var i;
|
|
|
|
while ((match = raw.match(LINKY_URL_REGEXP))) {
|
|
|
|
// We can not end in these as they are sometimes found at the end of the sentence
|
|
|
|
url = match[0];
|
2016-05-16 13:33:49 +02:00
|
|
|
// if we did not match ftp/http/www/mailto then assume mailto
|
|
|
|
if (!match[2] && !match[4]) {
|
|
|
|
url = (match[3] ? 'http://' : 'mailto:') + url;
|
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
i = match.index;
|
|
|
|
addText(raw.substr(0, i));
|
|
|
|
addLink(url, match[0].replace(MAILTO_REGEXP, ''));
|
|
|
|
raw = raw.substring(i + match[0].length);
|
|
|
|
}
|
|
|
|
addText(raw);
|
|
|
|
return $sanitize(html.join(''));
|
|
|
|
|
|
|
|
function addText(text) {
|
|
|
|
if (!text) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
html.push(sanitizeText(text));
|
|
|
|
}
|
|
|
|
|
|
|
|
function addLink(url, text) {
|
2017-05-13 13:25:33 +02:00
|
|
|
var key, linkAttributes = attributesFn(url);
|
2014-05-12 20:08:19 +02:00
|
|
|
html.push('<a ');
|
2017-05-13 13:25:33 +02:00
|
|
|
|
|
|
|
for (key in linkAttributes) {
|
|
|
|
html.push(key + '="' + linkAttributes[key] + '" ');
|
2016-05-16 13:33:49 +02:00
|
|
|
}
|
2017-05-13 13:25:33 +02:00
|
|
|
|
|
|
|
if (isDefined(target) && !('target' in linkAttributes)) {
|
2016-05-16 13:33:49 +02:00
|
|
|
html.push('target="',
|
|
|
|
target,
|
|
|
|
'" ');
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
2016-05-16 13:33:49 +02:00
|
|
|
html.push('href="',
|
|
|
|
url.replace(/"/g, '"'),
|
|
|
|
'">');
|
2014-05-12 20:08:19 +02:00
|
|
|
addText(text);
|
|
|
|
html.push('</a>');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}]);
|
|
|
|
|
|
|
|
|
|
|
|
})(window, window.angular);
|