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

Commit 43d3f62

Browse files
committed
refactor($compile): move img[srcset] sanitizing to helper method
1 parent a90d0cb commit 43d3f62

File tree

1 file changed

+54
-46
lines changed

1 file changed

+54
-46
lines changed

src/ng/compile.js

+54-46
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,57 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
16321632
}
16331633

16341634

1635+
function sanitizeSrcset(value) {
1636+
if (!value) {
1637+
return value;
1638+
}
1639+
if (!isString(value)) {
1640+
throw $compileMinErr('srcset', 'Can\'t pass trusted values to `$set(\'srcset\', value)`: "{0}"', value.toString());
1641+
}
1642+
1643+
// Such values are a bit too complex to handle automatically inside $sce.
1644+
// Instead, we sanitize each of the URIs individually, which works, even dynamically.
1645+
1646+
// It's not possible to work around this using `$sce.trustAsMediaUrl`.
1647+
// If you want to programmatically set explicitly trusted unsafe URLs, you should use
1648+
// `$sce.trustAsHtml` on the whole `img` tag and inject it into the DOM using the
1649+
// `ng-bind-html` directive.
1650+
1651+
var result = '';
1652+
1653+
// first check if there are spaces because it's not the same pattern
1654+
var trimmedSrcset = trim(value);
1655+
// ( 999x ,| 999w ,| ,|, )
1656+
var srcPattern = /(\s+\d+x\s*,|\s+\d+w\s*,|\s+,|,\s+)/;
1657+
var pattern = /\s/.test(trimmedSrcset) ? srcPattern : /(,)/;
1658+
1659+
// split srcset into tuple of uri and descriptor except for the last item
1660+
var rawUris = trimmedSrcset.split(pattern);
1661+
1662+
// for each tuples
1663+
var nbrUrisWith2parts = Math.floor(rawUris.length / 2);
1664+
for (var i = 0; i < nbrUrisWith2parts; i++) {
1665+
var innerIdx = i * 2;
1666+
// sanitize the uri
1667+
result += $sce.getTrustedMediaUrl(trim(rawUris[innerIdx]));
1668+
// add the descriptor
1669+
result += ' ' + trim(rawUris[innerIdx + 1]);
1670+
}
1671+
1672+
// split the last item into uri and descriptor
1673+
var lastTuple = trim(rawUris[i * 2]).split(/\s/);
1674+
1675+
// sanitize the last uri
1676+
result += $sce.getTrustedMediaUrl(trim(lastTuple[0]));
1677+
1678+
// and add the last descriptor if any
1679+
if (lastTuple.length === 2) {
1680+
result += (' ' + trim(lastTuple[1]));
1681+
}
1682+
return result;
1683+
}
1684+
1685+
16351686
function Attributes(element, attributesToCopy) {
16361687
if (attributesToCopy) {
16371688
var keys = Object.keys(attributesToCopy);
@@ -1767,52 +1818,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
17671818

17681819
nodeName = nodeName_(this.$$element);
17691820

1770-
// Sanitize img[srcset] values.
1771-
if (nodeName === 'img' && key === 'srcset' && value) {
1772-
if (!isString(value)) {
1773-
throw $compileMinErr('srcset', 'Can\'t pass trusted values to `$set(\'srcset\', value)`: "{0}"', value.toString());
1774-
}
1775-
1776-
// Such values are a bit too complex to handle automatically inside $sce.
1777-
// Instead, we sanitize each of the URIs individually, which works, even dynamically.
1778-
1779-
// It's not possible to work around this using `$sce.trustAsMediaUrl`.
1780-
// If you want to programmatically set explicitly trusted unsafe URLs, you should use
1781-
// `$sce.trustAsHtml` on the whole `img` tag and inject it into the DOM using the
1782-
// `ng-bind-html` directive.
1783-
1784-
var result = '';
1785-
1786-
// first check if there are spaces because it's not the same pattern
1787-
var trimmedSrcset = trim(value);
1788-
// ( 999x ,| 999w ,| ,|, )
1789-
var srcPattern = /(\s+\d+x\s*,|\s+\d+w\s*,|\s+,|,\s+)/;
1790-
var pattern = /\s/.test(trimmedSrcset) ? srcPattern : /(,)/;
1791-
1792-
// split srcset into tuple of uri and descriptor except for the last item
1793-
var rawUris = trimmedSrcset.split(pattern);
1794-
1795-
// for each tuples
1796-
var nbrUrisWith2parts = Math.floor(rawUris.length / 2);
1797-
for (var i = 0; i < nbrUrisWith2parts; i++) {
1798-
var innerIdx = i * 2;
1799-
// sanitize the uri
1800-
result += $sce.getTrustedMediaUrl(trim(rawUris[innerIdx]));
1801-
// add the descriptor
1802-
result += ' ' + trim(rawUris[innerIdx + 1]);
1803-
}
1804-
1805-
// split the last item into uri and descriptor
1806-
var lastTuple = trim(rawUris[i * 2]).split(/\s/);
1807-
1808-
// sanitize the last uri
1809-
result += $sce.getTrustedMediaUrl(trim(lastTuple[0]));
1810-
1811-
// and add the last descriptor if any
1812-
if (lastTuple.length === 2) {
1813-
result += (' ' + trim(lastTuple[1]));
1814-
}
1815-
this[key] = value = result;
1821+
// Sanitize img[srcset] + source[srcset] values.
1822+
if ((nodeName === 'img' || nodeName === 'source') && key === 'srcset') {
1823+
this[key] = value = sanitizeSrcset(value);
18161824
}
18171825

18181826
if (writeAttr !== false) {

0 commit comments

Comments
 (0)