Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
RELNOTES[INC]: Delete goog.dom.safe.setImageSrc, just assign to `im…
Browse files Browse the repository at this point in the history
  • Loading branch information
vrana authored and copybara-github committed Sep 12, 2022
1 parent 1e90884 commit 5c479e1
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 79 deletions.
33 changes: 1 addition & 32 deletions closure/goog/dom/safe.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,33 +351,6 @@ goog.dom.safe.setAnchorHref = function(anchor, url) {
};


/**
* Safely assigns a URL to an image element's src property.
*
* If url is of type goog.html.SafeUrl, its value is unwrapped and assigned to
* image's src property. If url is of type string however, it is first
* sanitized using goog.html.SafeUrl.sanitize.
*
* @param {!HTMLImageElement} imageElement The image element whose src property
* is to be assigned to.
* @param {string|!goog.html.SafeUrl} url The URL to assign.
* @return {void}
* @see goog.html.SafeUrl#sanitize
*/
goog.dom.safe.setImageSrc = function(imageElement, url) {
'use strict';
goog.dom.asserts.assertIsHTMLImageElement(imageElement);
/** @type {!goog.html.SafeUrl} */
var safeUrl;
if (url instanceof goog.html.SafeUrl) {
safeUrl = url;
} else {
var allowDataUrl = /^data:image\//i.test(url);
safeUrl = goog.html.SafeUrl.sanitizeAssertUnchanged(url, allowDataUrl);
}
imageElement.src = goog.html.SafeUrl.unwrap(safeUrl);
};

/**
* Safely assigns a URL to a audio element's src property.
*
Expand Down Expand Up @@ -864,11 +837,7 @@ goog.dom.safe.createImageFromBlob = function(blob) {
'use strict';
goog.global.URL.revokeObjectURL(objectUrl);
};
goog.dom.safe.setImageSrc(
image,
goog.html.uncheckedconversions
.safeUrlFromStringKnownToSatisfyTypeContract(
goog.string.Const.from('Image blob URL.'), objectUrl));
image.src = objectUrl;
return image;
};

Expand Down
47 changes: 0 additions & 47 deletions closure/goog/dom/safe_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,53 +448,6 @@ testSuite({
assertEquals('javascript:trusted();', element.action);
},

testSetImageSrc_withSafeUrlObject() {
let mockImageElement = /** @type {!HTMLImageElement} */ ({'src': 'blarg'});
withAssertionFailure(() => {
safe.setImageSrc(mockImageElement, 'javascript:evil();');
});
assertEquals('about:invalid#zClosurez', mockImageElement.src);

mockImageElement = /** @type {!HTMLImageElement} */ ({'src': 'blarg'});
const safeUrl = SafeUrl.fromConstant(Const.from('javascript:trusted();'));
safe.setImageSrc(mockImageElement, safeUrl);
assertEquals('javascript:trusted();', mockImageElement.src);

// Asserts correct runtime type.
if (!userAgent.IE || userAgent.isVersionOrHigher(10)) {
const otherElement = document.createElement('SCRIPT');
const ex = assertThrows(() => {
safe.setImageSrc(
/** @type {!HTMLImageElement} */ (otherElement), safeUrl);
});
assert(googString.contains(
ex.message, 'Argument is not a HTMLImageElement'));
}
},

testSetImageSrc_withHttpsUrl() {
const mockImageElement =
/** @type {!HTMLImageElement} */ ({'src': 'blarg'});

const safeUrl = 'https://trusted_url';
safe.setImageSrc(mockImageElement, safeUrl);
assertEquals(safeUrl, mockImageElement.src);
},

testSetImageSrc_withDataUrl() {
const mockImageElement =
/** @type {!HTMLImageElement} */ ({'src': 'blarg'});
const safeUrl = 'data:image/gif;base64,a';
safe.setImageSrc(mockImageElement, safeUrl);
assertEquals(safeUrl, mockImageElement.src);
assertThrows(() => {
safe.setImageSrc(mockImageElement, 'data:text/plain;base64,a');
});
assertThrows(() => {
safe.setImageSrc(mockImageElement, 'data:image/gif;bad');
});
},

testSetAudioSrc() {
let mockAudioElement = /** @type {!HTMLAudioElement} */ ({'src': 'blarg'});
let safeUrl = 'https://trusted_url';
Expand Down

0 comments on commit 5c479e1

Please sign in to comment.