A platform for community discussion. Free, open, simple.
FIX: Include CDN in result of `avatarUrl` helper (#19298)
David Taylor666fd43c3790
diff --git a/app/assets/javascripts/discourse/app/lib/utilities.js b/app/assets/javascripts/discourse/app/lib/utilities.jsindex a0096b0ee0a..75ba7f02364 100644--- a/app/assets/javascripts/discourse/app/lib/utilities.js+++ b/app/assets/javascripts/discourse/app/lib/utilities.js@@ -58,12 +58,13 @@ export function replaceFormatter(fn) {_usernameFormatDelegate = fn;}-export function avatarUrl(template, size) {+export function avatarUrl(template, size, { customGetURL } = {}) {if (!template) {return "";}const rawSize = getRawSize(translateSize(size));- return template.replace(/\{size\}/g, rawSize);+ const templatedPath = template.replace(/\{size\}/g, rawSize);+ return (customGetURL || getURLWithCDN)(templatedPath);}export function getRawSize(size) {@@ -79,13 +80,12 @@ export function getRawSize(size) {export function avatarImg(options, customGetURL) {const size = translateSize(options.size);- let path = avatarUrl(options.avatarTemplate, size);+ let url = avatarUrl(options.avatarTemplate, size, { customGetURL });// We won't render an invalid url- if (!path || path.length === 0) {+ if (!url) {return "";}- path = (customGetURL || getURLWithCDN)(path);const classes ="avatar" + (options.extraClasses ? " " + options.extraClasses : "");@@ -96,7 +96,7 @@ export function avatarImg(options, customGetURL) {title = ` title='${escaped}' aria-label='${escaped}'`;}- return `<img loading='lazy' alt='' width='${size}' height='${size}' src='${path}' class='${classes}'${title}>`;+ return `<img loading='lazy' alt='' width='${size}' height='${size}' src='${url}' class='${classes}'${title}>`;}export function tinyAvatar(avatarTemplate, options) {diff --git a/app/assets/javascripts/discourse/tests/unit/lib/utilities-test.js b/app/assets/javascripts/discourse/tests/unit/lib/utilities-test.jsindex 2aa232f678d..6473b7e4425 100644--- a/app/assets/javascripts/discourse/tests/unit/lib/utilities-test.js+++ b/app/assets/javascripts/discourse/tests/unit/lib/utilities-test.js@@ -29,6 +29,7 @@ import {import { setupRenderingTest } from "discourse/tests/helpers/component-test";import { click, render } from "@ember/test-helpers";import { hbs } from "ember-cli-htmlbars";+import { setupURL } from "discourse-common/lib/get-url";discourseModule("Unit | Utilities", function () {test("escapeExpression", function (assert) {@@ -98,6 +99,14 @@ discourseModule("Unit | Utilities", function () {"/fake/template/" + rawSize(45) + ".png","different size");++ setupURL("https://app-cdn.example.com", "https://example.com", "");++ assert.strictEqual(+ avatarUrl("/fake/template/{size}.png", "large"),+ "https://app-cdn.example.com/fake/template/" + rawSize(45) + ".png",+ "uses CDN if present"+ );});let setDevicePixelRatio = function (value) {