Fix map marker photos: escapeCssUrl no longer encodes the letter "s"

The character class /['"()\s<>]/ matched a backslash and the letter "s"
instead of whitespace, so every "s" in a marker image URL became %73
(https:// -> http%73://). The colored circles still rendered, but the
background-image URL was broken. Use \\s (backslash + whitespace).

Add tests: escapeCssUrl keeps a presigned URL intact and still encodes
backslash/whitespace; MapView puts the original imageUrl into the marker
template.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SlimusMinus
2026-10-21 22:51:36 +03:00
parent f25d034cad
commit bfc9a1bd1d
3 changed files with 35 additions and 1 deletions

View File

@@ -237,3 +237,24 @@ test("даже если границы так и не пришли, карта
{ timeout: 8000 } { timeout: 8000 }
); );
}); });
test("в шаблон маркера попадает рабочий URL картинки объявления", async () => {
// Регрессия: escapeCssUrl кодировал каждую букву s, и https:// превращался
// в http%73:// — кружки рисовались, а фото внутри них пропадали.
const imageUrl = "https://s3.example.com/refound/posts/a.jpg?X-Amz-Signature=abc";
fetchMock.mockResolvedValue({
ok: true,
json: async () => [
{ id: 1, latitude: 55.75, longitude: 37.61, type: "FOUND", title: "Ключи", imageUrl },
],
});
const ymaps = installFakeYmaps();
render(<MapView onBack={() => {}} />);
ymaps.publishBounds();
await waitFor(() => expect(window.ymaps.Placemark).toHaveBeenCalled());
const template = window.ymaps.templateLayoutFactory.createClass.mock.calls[0][0];
expect(template).toContain(`background-image:url('${imageUrl}')`);
});

View File

@@ -18,9 +18,13 @@ export function escapeHtml(value) {
/** /**
* URL для CSS url('...') внутри HTML-атрибута style: кавычки, скобки и * URL для CSS url('...') внутри HTML-атрибута style: кавычки, скобки и
* обратный слэш процентно кодируются, чтобы значение не вышло за пределы url(). * обратный слэш процентно кодируются, чтобы значение не вышло за пределы url().
*
* В классе символов `\\` — это обратный слэш, а `\s` — пробельные символы.
* Раньше там стояло `\\s`, то есть «слэш и буква s»: каждая s в URL
* превращалась в %73 (https:// → http%73://), и фото в маркерах карты пропадали.
*/ */
export function escapeCssUrl(value) { export function escapeCssUrl(value) {
return String(value ?? "").replace(/['"()\\s<>]/g, (ch) => return String(value ?? "").replace(/['"()\\\s<>]/g, (ch) =>
`%${ch.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0")}` `%${ch.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0")}`
); );
} }

View File

@@ -35,6 +35,15 @@ describe("escapeHtml", () => {
it("URL не выходит за пределы url('...')", () => { it("URL не выходит за пределы url('...')", () => {
expect(escapeCssUrl("a.jpg');background:url('x")).not.toMatch(/['()]/); expect(escapeCssUrl("a.jpg');background:url('x")).not.toMatch(/['()]/);
}); });
it("обычный presigned URL не меняется (буква s не кодируется)", () => {
const url = "https://s3.example.com/bucket/a.jpg?X-Amz-Signature=abc&presigned=true";
expect(escapeCssUrl(url)).toBe(url);
});
it("кодирует обратный слэш и пробельные символы", () => {
expect(escapeCssUrl("a\\b c\nd")).toBe("a%5Cb%20c%0Ad");
});
}); });
describe("validatePassword", () => { describe("validatePassword", () => {