snapshot of https://github.com/toeverything/AFFiNE.git (canary, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Build & Test / Analyze (javascript, affine) (push) Failing after 4s
Build & Test / Analyze (typescript, affine) (push) Failing after 3s
Build & Test / Rust test filter (push) Failing after 4s
Build & Test / Check yarn binary (push) Failing after 3s
Build & Test / E2E BlockSuite Test (3) (push) Failing after 3s
Build & Test / Analyze (javascript, blocksuite) (push) Failing after 3s
Build & Test / Analyze (typescript, blocksuite) (push) Failing after 3s
Build & Test / Mobile native build filter (push) Failing after 3s
Build & Test / E2E BlockSuite Test (1) (push) Failing after 4s
Build & Test / E2E BlockSuite Test (2) (push) Failing after 4s
Build & Test / E2E Mobile Test (1) (push) Failing after 3s
Build & Test / Unit Test (1) (push) Has been skipped
Build & Test / Unit Test (2) (push) Has been skipped
Build & Test / Unit Test (3) (push) Has been skipped
Build & Test / Unit Test (4) (push) Has been skipped
Build & Test / Unit Test (5) (push) Has been skipped
Build & Test / Native Unit Test (push) Has been skipped
Build & Test / E2E BlockSuite Test (4) (push) Failing after 3s
Build & Test / E2E BlockSuite Test (5) (push) Failing after 3s
Build & Test / E2E BlockSuite Cross Browser Test (push) Failing after 3s
Build & Test / E2E Mobile Test (2) (push) Failing after 3s
Build & Test / Build AFFiNE native (x86_64-unknown-linux-gnu) (push) Failing after 3s
Build & Test / Build Server native (push) Failing after 3s
Build & Test / Check Git Status (push) Has been skipped
Build & Test / Server Test (0, 4) (push) Has been skipped
Build & Test / Server Test (1, 4) (push) Has been skipped
Build & Test / Server Test (2, 4) (push) Has been skipped
Build & Test / Server Test (3, 4) (push) Has been skipped
Build & Test / Server Test with Elasticsearch (push) Has been skipped
Build & Test / Build @affine/electron renderer (push) Failing after 3s
Build & Test / Copilot test filter (push) Failing after 4s
Build & Test / Lint Rust (push) Has been skipped
Build & Test / Server Test with Embedded Search (push) Has been skipped
Build & Test / Server Test with Manticore Search (push) Has been skipped
Build & Test / Server E2E Test (push) Has been skipped
Build & Test / Cloud Desktop E2E Test (push) Has been skipped
Build & Test / Cloud E2E Test 1/2 (push) Has been skipped
Build & Test / Cloud E2E Test 2/2 (push) Has been skipped
Build & Test / Build Android app (push) Has been skipped
Build & Test / Test Android app (push) Has been skipped
Build & Test / Run native tests (push) Has been skipped
Build & Test / Server Copilot Api Test (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (1, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (2, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (3, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (4, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (5, 5) (push) Has been skipped
Release / Canary Gate (push) Has been skipped
Build & Test / Build AFFiNE native (aarch64-apple-darwin) (push) Has been cancelled
Build & Test / Lint (push) Has been cancelled
Build & Test / Typecheck (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-apple-darwin) (push) Has been cancelled
Build & Test / Desktop Test (ubuntu-latest, linux, x64, x86_64-unknown-linux-gnu, true) (push) Has been cancelled
Build & Test / Desktop Test (windows-latest, windows, x64, x86_64-pc-windows-msvc, true) (push) Has been cancelled
Build & Test / E2E Test (1) (push) Has been cancelled
Build & Test / E2E Test (2) (push) Has been cancelled
Build & Test / E2E Test (3) (push) Has been cancelled
Build & Test / E2E Test (4) (push) Has been cancelled
Build & Test / E2E Test (5) (push) Has been cancelled
Build & Test / Build AFFiNE native (aarch64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, arm64, aarch64-apple-darwin, true) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, x64, x86_64-apple-darwin, false) (push) Has been cancelled
Build & Test / 3, 2, 1 Launch (push) Has been cancelled
Build & Test / Build iOS app (push) Has been cancelled
Copilot Test Automatically / Setup Test (push) Failing after 1s
Release / Prepare (push) Failing after 1s
Release / Release Cloud (push) Has been skipped
Release / Release Desktop (push) Has been skipped
Release / Release Mobile (push) Has been skipped
Release / Release Docker Image (push) Has been skipped
Some checks failed
Build & Test / Analyze (javascript, affine) (push) Failing after 4s
Build & Test / Analyze (typescript, affine) (push) Failing after 3s
Build & Test / Rust test filter (push) Failing after 4s
Build & Test / Check yarn binary (push) Failing after 3s
Build & Test / E2E BlockSuite Test (3) (push) Failing after 3s
Build & Test / Analyze (javascript, blocksuite) (push) Failing after 3s
Build & Test / Analyze (typescript, blocksuite) (push) Failing after 3s
Build & Test / Mobile native build filter (push) Failing after 3s
Build & Test / E2E BlockSuite Test (1) (push) Failing after 4s
Build & Test / E2E BlockSuite Test (2) (push) Failing after 4s
Build & Test / E2E Mobile Test (1) (push) Failing after 3s
Build & Test / Unit Test (1) (push) Has been skipped
Build & Test / Unit Test (2) (push) Has been skipped
Build & Test / Unit Test (3) (push) Has been skipped
Build & Test / Unit Test (4) (push) Has been skipped
Build & Test / Unit Test (5) (push) Has been skipped
Build & Test / Native Unit Test (push) Has been skipped
Build & Test / E2E BlockSuite Test (4) (push) Failing after 3s
Build & Test / E2E BlockSuite Test (5) (push) Failing after 3s
Build & Test / E2E BlockSuite Cross Browser Test (push) Failing after 3s
Build & Test / E2E Mobile Test (2) (push) Failing after 3s
Build & Test / Build AFFiNE native (x86_64-unknown-linux-gnu) (push) Failing after 3s
Build & Test / Build Server native (push) Failing after 3s
Build & Test / Check Git Status (push) Has been skipped
Build & Test / Server Test (0, 4) (push) Has been skipped
Build & Test / Server Test (1, 4) (push) Has been skipped
Build & Test / Server Test (2, 4) (push) Has been skipped
Build & Test / Server Test (3, 4) (push) Has been skipped
Build & Test / Server Test with Elasticsearch (push) Has been skipped
Build & Test / Build @affine/electron renderer (push) Failing after 3s
Build & Test / Copilot test filter (push) Failing after 4s
Build & Test / Lint Rust (push) Has been skipped
Build & Test / Server Test with Embedded Search (push) Has been skipped
Build & Test / Server Test with Manticore Search (push) Has been skipped
Build & Test / Server E2E Test (push) Has been skipped
Build & Test / Cloud Desktop E2E Test (push) Has been skipped
Build & Test / Cloud E2E Test 1/2 (push) Has been skipped
Build & Test / Cloud E2E Test 2/2 (push) Has been skipped
Build & Test / Build Android app (push) Has been skipped
Build & Test / Test Android app (push) Has been skipped
Build & Test / Run native tests (push) Has been skipped
Build & Test / Server Copilot Api Test (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (1, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (2, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (3, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (4, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (5, 5) (push) Has been skipped
Release / Canary Gate (push) Has been skipped
Build & Test / Build AFFiNE native (aarch64-apple-darwin) (push) Has been cancelled
Build & Test / Lint (push) Has been cancelled
Build & Test / Typecheck (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-apple-darwin) (push) Has been cancelled
Build & Test / Desktop Test (ubuntu-latest, linux, x64, x86_64-unknown-linux-gnu, true) (push) Has been cancelled
Build & Test / Desktop Test (windows-latest, windows, x64, x86_64-pc-windows-msvc, true) (push) Has been cancelled
Build & Test / E2E Test (1) (push) Has been cancelled
Build & Test / E2E Test (2) (push) Has been cancelled
Build & Test / E2E Test (3) (push) Has been cancelled
Build & Test / E2E Test (4) (push) Has been cancelled
Build & Test / E2E Test (5) (push) Has been cancelled
Build & Test / Build AFFiNE native (aarch64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, arm64, aarch64-apple-darwin, true) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, x64, x86_64-apple-darwin, false) (push) Has been cancelled
Build & Test / 3, 2, 1 Launch (push) Has been cancelled
Build & Test / Build iOS app (push) Has been cancelled
Copilot Test Automatically / Setup Test (push) Failing after 1s
Release / Prepare (push) Failing after 1s
Release / Release Cloud (push) Has been skipped
Release / Release Desktop (push) Has been skipped
Release / Release Mobile (push) Has been skipped
Release / Release Docker Image (push) Has been skipped
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import {
|
||||
getImageFilesFromLocal,
|
||||
registerNativeImageFilesPicker,
|
||||
} from '../../utils';
|
||||
|
||||
describe('getImageFilesFromLocal', () => {
|
||||
let originalShowPicker: ((this: HTMLInputElement) => void) | undefined;
|
||||
|
||||
beforeEach(() => {
|
||||
originalShowPicker = (
|
||||
HTMLInputElement.prototype as HTMLInputElement & {
|
||||
showPicker?: (this: HTMLInputElement) => void;
|
||||
}
|
||||
).showPicker;
|
||||
|
||||
(
|
||||
HTMLInputElement.prototype as HTMLInputElement & {
|
||||
showPicker?: (this: HTMLInputElement) => void;
|
||||
}
|
||||
).showPicker = vi.fn(function (this: HTMLInputElement) {
|
||||
this.dispatchEvent(new Event('cancel'));
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (originalShowPicker) {
|
||||
(
|
||||
HTMLInputElement.prototype as HTMLInputElement & {
|
||||
showPicker?: (this: HTMLInputElement) => void;
|
||||
}
|
||||
).showPicker = originalShowPicker;
|
||||
} else {
|
||||
delete (
|
||||
HTMLInputElement.prototype as unknown as {
|
||||
showPicker?: unknown;
|
||||
}
|
||||
).showPicker;
|
||||
}
|
||||
|
||||
registerNativeImageFilesPicker(null);
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('prefers the registered native image picker when available', async () => {
|
||||
const file = new File(['picked-image'], 'picked.png', {
|
||||
type: 'image/png',
|
||||
lastModified: 123,
|
||||
});
|
||||
const nativePicker = vi.fn(async () => [file]);
|
||||
registerNativeImageFilesPicker(nativePicker);
|
||||
|
||||
const files = await getImageFilesFromLocal();
|
||||
|
||||
expect(nativePicker).toHaveBeenCalledTimes(1);
|
||||
expect(files).toEqual([file]);
|
||||
});
|
||||
|
||||
it('falls back to the standard picker when the native picker rejects', async () => {
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
const nativePicker = vi.fn(async () => {
|
||||
throw new Error('Native picker failed');
|
||||
});
|
||||
registerNativeImageFilesPicker(nativePicker);
|
||||
|
||||
const files = await getImageFilesFromLocal();
|
||||
|
||||
expect(nativePicker).toHaveBeenCalledTimes(1);
|
||||
expect(files).toEqual([]);
|
||||
expect(
|
||||
(
|
||||
HTMLInputElement.prototype as HTMLInputElement & {
|
||||
showPicker?: (this: HTMLInputElement) => void;
|
||||
}
|
||||
).showPicker
|
||||
).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
108
blocksuite/affine/shared/src/__tests__/utils/range.unit.spec.ts
Normal file
108
blocksuite/affine/shared/src/__tests__/utils/range.unit.spec.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import {
|
||||
beforeEach,
|
||||
describe,
|
||||
expect,
|
||||
it,
|
||||
type MockInstance,
|
||||
vi,
|
||||
} from 'vitest';
|
||||
|
||||
import * as PointToRangeUtils from '../../utils/dom/point-to-range';
|
||||
import { handleNativeRangeAtPoint } from '../../utils/dom/point-to-range';
|
||||
|
||||
describe('test handleNativeRangeAtPoint', () => {
|
||||
let caretRangeFromPointSpy: MockInstance<
|
||||
(clientX: number, clientY: number) => Range | null
|
||||
>;
|
||||
let resetNativeSelectionSpy: MockInstance<(range: Range | null) => void>;
|
||||
|
||||
beforeEach(() => {
|
||||
caretRangeFromPointSpy = vi.spyOn(
|
||||
PointToRangeUtils.api,
|
||||
'caretRangeFromPoint'
|
||||
);
|
||||
resetNativeSelectionSpy = vi.spyOn(
|
||||
PointToRangeUtils.api,
|
||||
'resetNativeSelection'
|
||||
);
|
||||
});
|
||||
|
||||
it('does nothing if caretRangeFromPoint returns null', () => {
|
||||
caretRangeFromPointSpy.mockReturnValue(null);
|
||||
|
||||
handleNativeRangeAtPoint(10, 10);
|
||||
expect(resetNativeSelectionSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('keeps range untouched if startContainer is a Text node', () => {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = 'hello';
|
||||
|
||||
const text = div.firstChild!;
|
||||
|
||||
const range = document.createRange();
|
||||
range.setStart(text, 2);
|
||||
range.collapse(true);
|
||||
|
||||
caretRangeFromPointSpy.mockReturnValue(range);
|
||||
|
||||
handleNativeRangeAtPoint(10, 10);
|
||||
|
||||
expect(range.startContainer).toBe(text);
|
||||
expect(range.startOffset).toBe(2);
|
||||
expect(resetNativeSelectionSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('moves caret into direct text child when clicking element', () => {
|
||||
const div = document.createElement('div');
|
||||
div.append('hello');
|
||||
|
||||
const range = document.createRange();
|
||||
range.setStart(div, 1);
|
||||
range.collapse(true);
|
||||
|
||||
caretRangeFromPointSpy.mockReturnValue(range);
|
||||
|
||||
handleNativeRangeAtPoint(10, 10);
|
||||
|
||||
expect(range.startContainer.nodeType).toBe(Node.TEXT_NODE);
|
||||
expect(range.startContainer.textContent).toBe('hello');
|
||||
expect(range.startOffset).toBe(5);
|
||||
expect(resetNativeSelectionSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('moves caret to last meaningful text inside nested element', () => {
|
||||
const div = document.createElement('div');
|
||||
div.innerHTML = `<span>a</span><span><em>b</em>c</span>`;
|
||||
|
||||
const range = document.createRange();
|
||||
range.setStart(div, 2);
|
||||
range.collapse(true);
|
||||
|
||||
caretRangeFromPointSpy.mockReturnValue(range);
|
||||
|
||||
handleNativeRangeAtPoint(10, 10);
|
||||
|
||||
expect(range.startContainer.nodeType).toBe(Node.TEXT_NODE);
|
||||
expect(range.startContainer.textContent).toBe('c');
|
||||
expect(range.startOffset).toBe(1);
|
||||
expect(resetNativeSelectionSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('falls back to searching startContainer when offset element has no text', () => {
|
||||
const div = document.createElement('div');
|
||||
div.innerHTML = `<span></span><span>ok</span>`;
|
||||
|
||||
const range = document.createRange();
|
||||
range.setStart(div, 1);
|
||||
range.collapse(true);
|
||||
|
||||
caretRangeFromPointSpy.mockReturnValue(range);
|
||||
|
||||
handleNativeRangeAtPoint(10, 10);
|
||||
|
||||
expect(range.startContainer.textContent).toBe('ok');
|
||||
expect(range.startOffset).toBe(2);
|
||||
expect(resetNativeSelectionSpy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
import { Point } from '@blocksuite/global/gfx';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe('Point', () => {
|
||||
it('should return a min point', () => {
|
||||
const a = new Point(0, 0);
|
||||
const b = new Point(-1, 1);
|
||||
expect(Point.min(a, b)).toEqual(new Point(-1, 0));
|
||||
});
|
||||
|
||||
it('should return a max point', () => {
|
||||
const a = new Point(0, 0);
|
||||
const b = new Point(-1, 1);
|
||||
expect(Point.max(a, b)).toEqual(new Point(0, 1));
|
||||
});
|
||||
|
||||
it('should return a clamp point', () => {
|
||||
const min = new Point(0, 0);
|
||||
const max = new Point(1, 1);
|
||||
const a = new Point(-1, 2);
|
||||
expect(Point.clamp(a, min, max)).toEqual(new Point(0, 1));
|
||||
|
||||
const b = new Point(2, 2);
|
||||
expect(Point.clamp(b, min, max)).toEqual(new Point(1, 1));
|
||||
|
||||
const c = new Point(0.5, 0.5);
|
||||
expect(Point.clamp(c, min, max)).toEqual(new Point(0.5, 0.5));
|
||||
});
|
||||
|
||||
it('should return a copy of point', () => {
|
||||
const a = new Point(0, 0);
|
||||
expect(a.clone()).toEqual(new Point(0, 0));
|
||||
});
|
||||
|
||||
it('#set method should set x and y', () => {
|
||||
const p = new Point(0, 0);
|
||||
p.set(1, 2);
|
||||
expect(p).toEqual(new Point(1, 2));
|
||||
});
|
||||
|
||||
it('#add', () => {
|
||||
const a = new Point(1, 2);
|
||||
const b = new Point(3, 4);
|
||||
expect(a.add(b)).toEqual(new Point(4, 6));
|
||||
});
|
||||
|
||||
it('#subtract', () => {
|
||||
const a = new Point(1, 2);
|
||||
const b = new Point(3, 4);
|
||||
expect(a.subtract(b)).toEqual(new Point(-2, -2));
|
||||
});
|
||||
|
||||
it('#scale', () => {
|
||||
const a = new Point(1, 2);
|
||||
expect(a.scale(2)).toEqual(new Point(2, 4));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,69 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { isFuzzyMatch, substringMatchScore } from '../../utils/string.js';
|
||||
|
||||
describe('fuzzyMatch', () => {
|
||||
it('basic case', () => {
|
||||
expect(isFuzzyMatch('John Smith', 'j')).toEqual(true);
|
||||
expect(isFuzzyMatch('John Smith', 'js')).toEqual(true);
|
||||
expect(isFuzzyMatch('John Smith', 'jsa')).toEqual(false);
|
||||
});
|
||||
|
||||
it('should works with CJK', () => {
|
||||
expect(isFuzzyMatch('中', '中')).toEqual(true);
|
||||
expect(isFuzzyMatch('中文', '中')).toEqual(true);
|
||||
expect(isFuzzyMatch('中文字符', '中字')).toEqual(true);
|
||||
expect(isFuzzyMatch('中文字符', '字中')).toEqual(false);
|
||||
});
|
||||
|
||||
it('should works with IME', () => {
|
||||
// IME will generate a space between 'da' and 't'
|
||||
expect(isFuzzyMatch('database', 'da t')).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('substringMatchScore', () => {
|
||||
it('should return a fraction if there exists a common maximal length substring. ', () => {
|
||||
const result = substringMatchScore('testing the function', 'tet');
|
||||
expect(result).toBeLessThan(1);
|
||||
expect(result).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('should return bigger score for longer match', () => {
|
||||
const result = substringMatchScore('testing the function', 'functin');
|
||||
const result2 = substringMatchScore('testing the function', 'tet');
|
||||
// because th length of common substring of 'functin' is bigger than 'tet'
|
||||
expect(result).toBeGreaterThan(result2);
|
||||
});
|
||||
|
||||
it('should return bigger score when using same query to search a shorter string', () => {
|
||||
const result = substringMatchScore('test', 'test');
|
||||
const result2 = substringMatchScore('testing the function', 'test');
|
||||
expect(result).toBeGreaterThan(result2);
|
||||
});
|
||||
|
||||
it('should return 0 when there is no match', () => {
|
||||
const result = substringMatchScore('abc', 'defghijk');
|
||||
expect(result).toBe(0);
|
||||
});
|
||||
|
||||
it('should handle cases where the query is longer than the string', () => {
|
||||
const result = substringMatchScore('short', 'longer substring');
|
||||
expect(result).toBe(0);
|
||||
});
|
||||
|
||||
it('should handle empty strings correctly', () => {
|
||||
const result = substringMatchScore('any string', '');
|
||||
expect(result).toBe(0);
|
||||
});
|
||||
|
||||
it('should handle both strings being empty', () => {
|
||||
const result = substringMatchScore('', '');
|
||||
expect(result).toBe(0);
|
||||
});
|
||||
|
||||
it('should handle cases where both strings are identical', () => {
|
||||
const result = substringMatchScore('identical', 'identical');
|
||||
expect(result).toBe(1);
|
||||
});
|
||||
});
|
||||
191
blocksuite/affine/shared/src/__tests__/utils/svg.unit.spec.ts
Normal file
191
blocksuite/affine/shared/src/__tests__/utils/svg.unit.spec.ts
Normal file
@@ -0,0 +1,191 @@
|
||||
/**
|
||||
* @vitest-environment happy-dom
|
||||
*/
|
||||
import { describe, expect, test } from 'vitest';
|
||||
|
||||
import { sanitizeSvg } from '../../utils/svg.js';
|
||||
|
||||
type HappyDOMWindow = Window & {
|
||||
happyDOM: {
|
||||
setURL: (url: string) => void;
|
||||
};
|
||||
};
|
||||
|
||||
function setLocation(url: string) {
|
||||
(window as unknown as HappyDOMWindow).happyDOM.setURL(url);
|
||||
}
|
||||
|
||||
function svgDataUrl(svg: string) {
|
||||
const bytes = new TextEncoder().encode(svg);
|
||||
let binary = '';
|
||||
bytes.forEach(byte => {
|
||||
binary += String.fromCharCode(byte);
|
||||
});
|
||||
return `data:image/svg+xml;base64,${btoa(binary)}`;
|
||||
}
|
||||
|
||||
function decodeSvgDataUrl(dataUrl: string) {
|
||||
const base64 = dataUrl.split(',')[1];
|
||||
return new TextDecoder().decode(
|
||||
Uint8Array.from(atob(base64), char => char.charCodeAt(0))
|
||||
);
|
||||
}
|
||||
|
||||
describe('sanitizeSvg', () => {
|
||||
test('wraps DOMPurify svg fragments back into an svg root', () => {
|
||||
const sanitized = sanitizeSvg(
|
||||
'<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><rect width="100" height="100"></rect></svg>'
|
||||
);
|
||||
|
||||
expect(sanitized).toContain('<svg');
|
||||
expect(sanitized).toContain('width="100"');
|
||||
expect(sanitized).toContain('<rect');
|
||||
});
|
||||
|
||||
test('accepts svg documents with xml and doctype prefixes', () => {
|
||||
const sanitized = sanitizeSvg(`<?xml version="1.0" standalone="no"?>
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
|
||||
<rect width="100" height="100"></rect>
|
||||
</svg>`);
|
||||
|
||||
expect(sanitized).toContain('<svg');
|
||||
expect(sanitized).toContain('width="100"');
|
||||
expect(sanitized).toContain('<rect');
|
||||
expect(sanitized).not.toContain('<!DOCTYPE');
|
||||
});
|
||||
|
||||
test('rejects non-svg roots', () => {
|
||||
expect(sanitizeSvg('<div><svg></svg></div>')).toBe('');
|
||||
});
|
||||
|
||||
test('rejects malformed doctype prefixes without regexp backtracking', () => {
|
||||
const maliciousPrefix = '<!doctype' + '?><!doctype'.repeat(10_000);
|
||||
|
||||
expect(sanitizeSvg(`${maliciousPrefix}<div></div>`)).toBe('');
|
||||
});
|
||||
|
||||
test('keeps internal glyph references and safe image data urls', () => {
|
||||
const sanitized = sanitizeSvg(`
|
||||
<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<defs><path id="glyph-a" d="M0 0h10v10z"></path></defs>
|
||||
<use href="#glyph-a"></use>
|
||||
<use xlink:href="#glyph-a"></use>
|
||||
<a xlink:href="https://typst.app/docs/tutorial"><path d="M0 0h10v10z"></path></a>
|
||||
<image href="data:image/png;base64,AAAA" width="10" height="10"></image>
|
||||
</svg>
|
||||
`);
|
||||
|
||||
expect(sanitized).toContain('href="#glyph-a"');
|
||||
expect(sanitized).toContain('xlink:href="#glyph-a"');
|
||||
expect(sanitized).toContain('xlink:href="https://typst.app/docs/tutorial"');
|
||||
expect(sanitized).toContain('data:image/png;base64,AAAA');
|
||||
});
|
||||
|
||||
test('removes external glyph references and unsafe css', () => {
|
||||
const sanitized = sanitizeSvg(`
|
||||
<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<style>@import "https://example.com/style.css"; .a { fill: #000; }</style>
|
||||
<use href="https://example.com/glyph.svg#x"></use>
|
||||
<use xlink:href="https://example.com/glyph.svg#x"></use>
|
||||
<a xlink:href="javascript:alert(1)"><path d="M0 0h10v10z"></path></a>
|
||||
<image href="https://example.com/image.png" width="10" height="10"></image>
|
||||
<path style="fill: url(https://example.com/pattern.svg#x)" d="M0 0h10v10z"></path>
|
||||
</svg>
|
||||
`);
|
||||
|
||||
expect(sanitized).not.toContain('https://example.com');
|
||||
expect(sanitized).not.toContain('javascript:');
|
||||
expect(sanitized).not.toContain('@import');
|
||||
expect(sanitized).not.toContain('url(');
|
||||
});
|
||||
|
||||
test('removes links sharing the current registrable domain', () => {
|
||||
setLocation('https://sub.example.co.uk/workspace');
|
||||
|
||||
const sanitized = sanitizeSvg(`
|
||||
<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<a xlink:href="https://sub.example.co.uk/docs"><path d="M0 0h10v10z"></path></a>
|
||||
<a href="https://other.example.co.uk/docs"><path d="M0 0h10v10z"></path></a>
|
||||
<a xlink:href="https://example.com/docs"><path d="M0 0h10v10z"></path></a>
|
||||
</svg>
|
||||
`);
|
||||
|
||||
expect(sanitized).not.toContain('https://sub.example.co.uk/docs');
|
||||
expect(sanitized).not.toContain('https://other.example.co.uk/docs');
|
||||
expect(sanitized).toContain('https://example.com/docs');
|
||||
});
|
||||
|
||||
test('keeps private suffix sibling domains separate', () => {
|
||||
setLocation('https://foo.github.io/workspace');
|
||||
|
||||
const sanitized = sanitizeSvg(`
|
||||
<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<a xlink:href="https://foo.github.io/docs"><path d="M0 0h10v10z"></path></a>
|
||||
<a href="https://bar.github.io/docs"><path d="M0 0h10v10z"></path></a>
|
||||
</svg>
|
||||
`);
|
||||
|
||||
expect(sanitized).not.toContain('https://foo.github.io/docs');
|
||||
expect(sanitized).toContain('https://bar.github.io/docs');
|
||||
});
|
||||
|
||||
test('handles local hostnames by exact hostname', () => {
|
||||
setLocation('http://localhost:3000/workspace');
|
||||
|
||||
const sanitized = sanitizeSvg(`
|
||||
<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<a xlink:href="http://localhost:8080/docs"><path d="M0 0h10v10z"></path></a>
|
||||
<a href="http://share.localhost/docs"><path d="M0 0h10v10z"></path></a>
|
||||
<a href="http://127.0.0.1/docs"><path d="M0 0h10v10z"></path></a>
|
||||
</svg>
|
||||
`);
|
||||
|
||||
expect(sanitized).not.toContain('http://localhost:8080/docs');
|
||||
expect(sanitized).toContain('http://share.localhost/docs');
|
||||
expect(sanitized).toContain('http://127.0.0.1/docs');
|
||||
});
|
||||
|
||||
test('recursively sanitizes svg images', () => {
|
||||
const nestedSvg = svgDataUrl(
|
||||
'<svg xmlns="http://www.w3.org/2000/svg"><defs><path id="glyph-a" d="M0 0h10v10z"></path></defs><use href="#glyph-a"></use><use href="https://example.com/glyph.svg#x"></use></svg>'
|
||||
);
|
||||
const sanitized = sanitizeSvg(`
|
||||
<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<image href="${nestedSvg}" width="10" height="10"></image>
|
||||
</svg>
|
||||
`);
|
||||
const sanitizedImageHref = sanitized.match(/href="([^"]+)"/)?.[1];
|
||||
|
||||
expect(sanitizedImageHref).toMatch(/^data:image\/svg\+xml;base64,/);
|
||||
expect(decodeSvgDataUrl(sanitizedImageHref ?? '')).toContain('<svg');
|
||||
expect(decodeSvgDataUrl(sanitizedImageHref ?? '')).toContain('#glyph-a');
|
||||
expect(decodeSvgDataUrl(sanitizedImageHref ?? '')).not.toContain(
|
||||
'https://example.com'
|
||||
);
|
||||
});
|
||||
|
||||
test('removes svg images nested deeper than two levels', () => {
|
||||
const thirdLevelSvg = svgDataUrl(
|
||||
'<svg xmlns="http://www.w3.org/2000/svg"><rect width="10" height="10"></rect></svg>'
|
||||
);
|
||||
const secondLevelSvg = svgDataUrl(
|
||||
`<svg xmlns="http://www.w3.org/2000/svg"><image href="${thirdLevelSvg}"></image></svg>`
|
||||
);
|
||||
const firstLevelSvg = svgDataUrl(
|
||||
`<svg xmlns="http://www.w3.org/2000/svg"><image href="${secondLevelSvg}"></image></svg>`
|
||||
);
|
||||
const sanitized = sanitizeSvg(`
|
||||
<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<image href="${firstLevelSvg}"></image>
|
||||
</svg>
|
||||
`);
|
||||
const firstLevelHref = sanitized.match(/href="([^"]+)"/)?.[1];
|
||||
const firstLevelSanitizedSvg = decodeSvgDataUrl(firstLevelHref ?? '');
|
||||
const secondLevelHref = firstLevelSanitizedSvg.match(/href="([^"]+)"/)?.[1];
|
||||
const secondLevelSanitizedSvg = decodeSvgDataUrl(secondLevelHref ?? '');
|
||||
|
||||
expect(firstLevelSanitizedSvg).toContain('<image');
|
||||
expect(secondLevelSanitizedSvg).not.toContain('<image');
|
||||
});
|
||||
});
|
||||
139
blocksuite/affine/shared/src/__tests__/utils/url.unit.spec.ts
Normal file
139
blocksuite/affine/shared/src/__tests__/utils/url.unit.spec.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
|
||||
import { isValidUrl, splitTextByUrl } from '../../utils/url.js';
|
||||
|
||||
describe('isValidUrl: determining whether a URL is valid is very complicated', () => {
|
||||
test('basic case', () => {
|
||||
expect(isValidUrl('')).toEqual(false);
|
||||
expect(isValidUrl('1.co')).toEqual(true);
|
||||
expect(isValidUrl('https://www.example.com')).toEqual(true);
|
||||
expect(isValidUrl('www.example.com')).toEqual(true);
|
||||
expect(isValidUrl('http://www.github.com/toeverything/blocksuite')).toEqual(
|
||||
true
|
||||
);
|
||||
});
|
||||
|
||||
test('CAUTION: any link include allowed schema is a valid url!', () => {
|
||||
expect(isValidUrl('http://www.example.cm')).toEqual(true);
|
||||
expect(isValidUrl('https://x ')).toEqual(true);
|
||||
expect(isValidUrl('mailto://w:80')).toEqual(true);
|
||||
});
|
||||
|
||||
test('link include a unknown schema is not a valid url', () => {
|
||||
expect(isValidUrl('xxx://www.example.com')).toEqual(false);
|
||||
expect(isValidUrl('https://')).toEqual(false);
|
||||
expect(isValidUrl('http://w.... !@#*(!!!!')).toEqual(false);
|
||||
});
|
||||
|
||||
test('URL without protocol is a valid URL', () => {
|
||||
expect(isValidUrl('www.example.com')).toEqual(true);
|
||||
expect(isValidUrl('example.co')).toEqual(true);
|
||||
expect(isValidUrl('example.cm')).toEqual(true);
|
||||
expect(isValidUrl('1.1.1.1')).toEqual(false);
|
||||
|
||||
expect(isValidUrl('example.c')).toEqual(false);
|
||||
});
|
||||
|
||||
test('special cases', () => {
|
||||
expect(isValidUrl('example.com.')).toEqual(false);
|
||||
|
||||
// I don't know why
|
||||
// private & local networks is excluded
|
||||
expect(isValidUrl('127.0.0.1')).toEqual(false);
|
||||
expect(isValidUrl('10.0.0.1')).toEqual(false);
|
||||
expect(isValidUrl('localhost')).toEqual(false);
|
||||
expect(isValidUrl('0.0.0.0')).toEqual(false);
|
||||
|
||||
expect(isValidUrl('128.0.0.1')).toEqual(false);
|
||||
expect(isValidUrl('1.0.0.1')).toEqual(false);
|
||||
});
|
||||
|
||||
test('email link is a valid URL', () => {
|
||||
// See https://www.rapidtables.com/web/html/mailto.html
|
||||
expect(isValidUrl('mailto:name@email.com')).toEqual(true);
|
||||
expect(
|
||||
isValidUrl(
|
||||
'mailto:name@rapidtables.com?subject=The%20subject%20of%20the%20mail'
|
||||
)
|
||||
).toEqual(true);
|
||||
expect(
|
||||
isValidUrl(
|
||||
'mailto:name1@rapidtables.com?cc=name2@rapidtables.com&bcc=name3@rapidtables.com&subject=The%20subject%20of%20the%20email&body=The%20body%20of%20the%20email'
|
||||
)
|
||||
).toEqual(true);
|
||||
// multiple email recipients
|
||||
expect(isValidUrl('mailto:name1@mail.com,name2@mail.com')).toEqual(true);
|
||||
});
|
||||
|
||||
test('misc case', () => {
|
||||
// Emoji domain
|
||||
expect(isValidUrl('xn--i-7iq.ws')).toEqual(true);
|
||||
expect(
|
||||
isValidUrl('https://username:password@www.example.com:80/?q_a=1234567')
|
||||
).toEqual(true);
|
||||
|
||||
expect(isValidUrl('新华网.cn')).toEqual(true);
|
||||
expect(isValidUrl('example.com/中文/にほんご')).toEqual(true);
|
||||
|
||||
// It's a valid url, but we don't want to support it
|
||||
// Longest TLD up to date is `.xn--vermgensberatung-pwb`, at 24 characters in Punycode and 17 when decoded [vermögensberatung].
|
||||
// See also https://stackoverflow.com/questions/9238640/how-long-can-a-tld-possibly-be#:~:text=Longest%20TLD%20up%20to%20date,17%20when%20decoded%20%5Bverm%C3%B6gensberatung%5D.
|
||||
expect(isValidUrl('example.xn--vermgensberatung-pwb')).toEqual(false);
|
||||
});
|
||||
|
||||
test('should allow ip address url when origin is same', () => {
|
||||
expect(isValidUrl('http://127.0.0.1', 'http://127.0.0.1')).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('splitTextByUrl', () => {
|
||||
test('should split text and keep url part as link segment', () => {
|
||||
expect(splitTextByUrl('hi - https://google.com')).toEqual([
|
||||
{ text: 'hi - ' },
|
||||
{
|
||||
text: 'https://google.com',
|
||||
link: 'https://google.com',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test('should support prefixed url token without swallowing prefix text', () => {
|
||||
expect(splitTextByUrl('-https://google.com')).toEqual([
|
||||
{ text: '-' },
|
||||
{
|
||||
text: 'https://google.com',
|
||||
link: 'https://google.com',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test('should trim tail punctuations from url token', () => {
|
||||
expect(splitTextByUrl('visit https://google.com, now')).toEqual([
|
||||
{ text: 'visit ' },
|
||||
{
|
||||
text: 'https://google.com',
|
||||
link: 'https://google.com',
|
||||
},
|
||||
{ text: ', now' },
|
||||
]);
|
||||
});
|
||||
|
||||
test('should convert domain token in plain text', () => {
|
||||
expect(splitTextByUrl('google.com and text')).toEqual([
|
||||
{
|
||||
text: 'google.com',
|
||||
link: 'https://google.com',
|
||||
},
|
||||
{ text: ' and text' },
|
||||
]);
|
||||
});
|
||||
|
||||
test('should normalize www domain token link while preserving display text', () => {
|
||||
expect(splitTextByUrl('www.google.com')).toEqual([
|
||||
{
|
||||
text: 'www.google.com',
|
||||
link: 'https://www.google.com',
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user