{
+ void navigator.clipboard?.writeText(text).then(
+ () => setToast(str.edit.linkCopied),
+ () => {},
+ )
+ focusGrid()
+ }}
+ onEdit={editable ? () => void startEdit() : undefined}
+ />
+ )
+ }
+ }
+
return (
<>
@@ -2664,6 +2823,7 @@ function SheetGrid({ store, sheet, meta, client, edit, setEdit, hold, onSheet, g
{assist}
)}
+ {linkCard}
diff --git a/test/xlsx-links.test.ts b/test/xlsx-links.test.ts
new file mode 100644
index 0000000..ea2784e
--- /dev/null
+++ b/test/xlsx-links.test.ts
@@ -0,0 +1,143 @@
+/**
+ * Links of cells: the file's ``s, the HYPERLINK function, and the web / mail addresses written in a text.
+ */
+import { describe, expect, test } from 'bun:test'
+import { findLinks } from '../src/core/url'
+import { at, book, openBook, rows } from './xlsx-builder'
+
+const hrefs = (text: string) => findLinks(text).map((l) => [text.slice(l.start, l.end), l.href])
+
+describe('findLinks', () => {
+ test('web addresses with a scheme or www., mail addresses with mailto: or as name@host', () => {
+ expect(hrefs('https://www.google.com/')).toEqual([['https://www.google.com/', 'https://www.google.com/']])
+ expect(hrefs('see http://a.example/x?q=1#f and ftp://files.example/a')).toEqual([
+ ['http://a.example/x?q=1#f', 'http://a.example/x?q=1#f'],
+ ['ftp://files.example/a', 'ftp://files.example/a'],
+ ])
+ expect(hrefs('www.example.org/path')).toEqual([['www.example.org/path', 'https://www.example.org/path']])
+ expect(hrefs('mail an.ton+x@mail.example.de bitte')).toEqual([['an.ton+x@mail.example.de', 'mailto:an.ton+x@mail.example.de']])
+ expect(hrefs('mailto:a@b.co')).toEqual([['mailto:a@b.co', 'mailto:a@b.co']])
+ })
+
+ test('what a sentence puts after an address is not part of it', () => {
+ expect(hrefs('Go to https://example.com/a.')).toEqual([['https://example.com/a', 'https://example.com/a']])
+ expect(hrefs('(see https://example.com/a), then')).toEqual([['https://example.com/a', 'https://example.com/a']])
+ expect(hrefs('https://en.wikipedia.org/wiki/Foo_(bar)')).toEqual([['https://en.wikipedia.org/wiki/Foo_(bar)', 'https://en.wikipedia.org/wiki/Foo_(bar)']])
+ expect(hrefs('"https://example.com"')).toEqual([['https://example.com', 'https://example.com']])
+ })
+
+ test('no address: nothing', () => {
+ for (const t of ['', 'plain text', '12:30', 'https://', 'www.', 'www.example', 'a@b', 'x@localhost', 'Ratio 1:2', 'javascript:alert(1)', 'xhttps://a.b'])
+ expect(hrefs(t), t).toEqual([])
+ })
+})
+
+const REL = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'
+const linked = (sheetRels: string, tail: string, more: Partial[0]> = {}) =>
+ book({
+ sheets: [
+ { name: 'Data', rows: rows([['Google', 'Two'], ['To B3', 'x'], ['plain']]), tail },
+ { name: 'Other Sheet', rows: rows([[1]]) },
+ ],
+ parts: { 'xl/worksheets/_rels/sheet1.xml.rels': `${sheetRels}` },
+ ...more,
+ })
+
+const linksOf = async (o: Awaited>, sheet: number, ref: string) => {
+ const { r, c } = at(ref)
+ return ((await o.engine.call('xlsx.cell', [sheet, r, c])) as { links?: unknown[] } | undefined)?.links
+}
+
+describe('xlsx: the links of a cell', () => {
+ test('a link of the file to a web address, with its hint; one to a place in the workbook', async () => {
+ const o = await openBook(
+ await linked(
+ ``,
+ ``,
+ ),
+ )
+ expect(await linksOf(o, 0, 'A1')).toEqual([{ from: 'file', href: 'https://www.google.com/', tooltip: 'Search' }])
+ expect(await linksOf(o, 0, 'A2')).toEqual([{ from: 'file', location: "'Other Sheet'!B3", goto: { sheet: 1, r: 2, c: 1 } }])
+ expect(await linksOf(o, 0, 'A3')).toBeUndefined()
+ // the cell's value is told as before
+ expect(((await o.engine.call('xlsx.cell', [0, 0, 0])) as { value: string }).value).toBe('Google')
+ })
+
+ test('a link on a range is on each of its cells, an empty one too; a fragment goes with the address', async () => {
+ const o = await openBook(
+ await linked(
+ ``,
+ ``,
+ ),
+ )
+ for (const ref of ['B1', 'B2', 'C1', 'C2']) expect(await linksOf(o, 0, ref), ref).toEqual([{ from: 'file', href: 'https://example.com/page#part' }])
+ expect(await linksOf(o, 0, 'D1')).toBeUndefined()
+ })
+
+ test('a link follows its cell when rows are inserted or deleted, and goes with it', async () => {
+ const o = await openBook(
+ await linked(``, ``),
+ )
+ await o.engine.call('xlsx.sheetOp', [0, { op: 'insertRows', at: 0, n: 2 }])
+ expect(await linksOf(o, 0, 'A2')).toBeUndefined()
+ expect(await linksOf(o, 0, 'A4')).toEqual([{ from: 'file', href: 'https://example.com/' }])
+ await o.engine.call('xlsx.undo', [])
+ expect(await linksOf(o, 0, 'A2')).toEqual([{ from: 'file', href: 'https://example.com/' }])
+ await o.engine.call('xlsx.sheetOp', [0, { op: 'deleteRows', at: 1, n: 1 }])
+ expect(await linksOf(o, 0, 'A2')).toBeUndefined()
+ })
+
+ test('the place a link names follows a renamed sheet; a defined name leads to its cell', async () => {
+ const o = await openBook(
+ await linked('', ``, {
+ names: `'Other Sheet'!$D$7`,
+ }),
+ )
+ await o.engine.call('xlsx.bookOp', [{ op: 'rename', index: 1, name: 'Renamed' }])
+ expect(await linksOf(o, 0, 'A1')).toEqual([{ from: 'file', location: 'Renamed!C5', goto: { sheet: 1, r: 4, c: 2 } }])
+ expect(await linksOf(o, 0, 'B1')).toEqual([{ from: 'file', location: 'Target', goto: { sheet: 1, r: 6, c: 3 } }])
+ })
+
+ test('a link to another file or of an unknown relationship: shown as it is, with no place to go', async () => {
+ const o = await openBook(
+ await linked(``, ``),
+ )
+ expect(await linksOf(o, 0, 'A1')).toEqual([{ from: 'file', href: 'other.xlsx' }])
+ // (a relationship the file does not have: no link, the text of the cell is looked at)
+ expect(await linksOf(o, 0, 'B1')).toBeUndefined()
+ })
+
+ test('the HYPERLINK function of a formula', async () => {
+ const o = await openBook(
+ await book({
+ sheets: [
+ { name: 'S', rows: rows([[{ f: 'HYPERLINK("https://example.com/a","Example")', v: 'Example' }, { f: 'HYPERLINK("#S!C3","Go")', v: 'Go' }, { f: 'HYPERLINK(A3)', v: 'x' }], [], ['https://example.com/b']]) },
+ ],
+ }),
+ )
+ expect(await linksOf(o, 0, 'A1')).toEqual([{ from: 'formula', href: 'https://example.com/a' }])
+ expect(await linksOf(o, 0, 'B1')).toEqual([{ from: 'formula', location: 'S!C3', goto: { sheet: 0, r: 2, c: 2 } }])
+ // (not a text: no link told; its value is not one either)
+ expect(await linksOf(o, 0, 'C1')).toBeUndefined()
+ })
+
+ test('the addresses written in a text, typed or read', async () => {
+ const o = await openBook(await book({ sheets: [{ name: 'S', rows: rows([['see www.example.org.', 'a@b.example', 12, 'none here']]) }] }))
+ expect(await linksOf(o, 0, 'A1')).toEqual([{ from: 'text', href: 'https://www.example.org' }])
+ expect(await linksOf(o, 0, 'B1')).toEqual([{ from: 'text', href: 'mailto:a@b.example' }])
+ expect(await linksOf(o, 0, 'C1')).toBeUndefined()
+ expect(await linksOf(o, 0, 'D1')).toBeUndefined()
+ await o.set(0, 'C1', 'https://example.com/x')
+ expect(await linksOf(o, 0, 'C1')).toEqual([{ from: 'text', href: 'https://example.com/x' }])
+ })
+
+ test('a link of the file is saved where its cell is', async () => {
+ const o = await openBook(
+ await linked(``, ``),
+ )
+ await o.engine.call('xlsx.sheetOp', [0, { op: 'insertRows', at: 0, n: 1 }])
+ const xml = await (await o.save()).text('xl/worksheets/sheet1.xml')
+ expect(xml).toContain('')
+ expect(await linksOf(o, 0, 'A3')).toEqual([{ from: 'file', href: 'https://example.com/' }])
+ })
+})