MCPcopy
hub / github.com/angular-ui/ui-grid / sanitizeSrcset

Function sanitizeSrcset

lib/test/angular/1.8.0/angular.js:9361–9409  ·  view source on GitHub ↗
(value, invokeType)

Source from the content-addressed store, hash-verified

9359
9360
9361 function sanitizeSrcset(value, invokeType) {
9362 if (!value) {
9363 return value;
9364 }
9365 if (!isString(value)) {
9366 throw $compileMinErr('srcset', 'Can\'t pass trusted values to `{0}`: "{1}"', invokeType, value.toString());
9367 }
9368
9369 // Such values are a bit too complex to handle automatically inside $sce.
9370 // Instead, we sanitize each of the URIs individually, which works, even dynamically.
9371
9372 // It's not possible to work around this using `$sce.trustAsMediaUrl`.
9373 // If you want to programmatically set explicitly trusted unsafe URLs, you should use
9374 // `$sce.trustAsHtml` on the whole `img` tag and inject it into the DOM using the
9375 // `ng-bind-html` directive.
9376
9377 var result = '';
9378
9379 // first check if there are spaces because it's not the same pattern
9380 var trimmedSrcset = trim(value);
9381 // ( 999x ,| 999w ,| ,|, )
9382 var srcPattern = /(\s+\d+x\s*,|\s+\d+w\s*,|\s+,|,\s+)/;
9383 var pattern = /\s/.test(trimmedSrcset) ? srcPattern : /(,)/;
9384
9385 // split srcset into tuple of uri and descriptor except for the last item
9386 var rawUris = trimmedSrcset.split(pattern);
9387
9388 // for each tuples
9389 var nbrUrisWith2parts = Math.floor(rawUris.length / 2);
9390 for (var i = 0; i < nbrUrisWith2parts; i++) {
9391 var innerIdx = i * 2;
9392 // sanitize the uri
9393 result += $sce.getTrustedMediaUrl(trim(rawUris[innerIdx]));
9394 // add the descriptor
9395 result += ' ' + trim(rawUris[innerIdx + 1]);
9396 }
9397
9398 // split the last item into uri and descriptor
9399 var lastTuple = trim(rawUris[i * 2]).split(/\s/);
9400
9401 // sanitize the last uri
9402 result += $sce.getTrustedMediaUrl(trim(lastTuple[0]));
9403
9404 // and add the last descriptor if any
9405 if (lastTuple.length === 2) {
9406 result += (' ' + trim(lastTuple[1]));
9407 }
9408 return result;
9409 }
9410
9411
9412 function Attributes(element, attributesToCopy) {

Callers 2

$CompileProviderFunction · 0.85

Calls 2

isStringFunction · 0.70
trimFunction · 0.70

Tested by

no test coverage detected