@flighthq/text-markup 0.5.1-next.973.788d3d7 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/.tsbuildinfo.contract +1 -0
- package/dist/.tsbuildinfo.runtime +1 -0
- package/dist/contract.d.ts +9 -5
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +10 -5
- package/dist/contract.js.map +1 -1
- package/dist/contract.text-markup-types.d.ts +2 -0
- package/dist/contract.text-markup-types.d.ts.map +1 -0
- package/dist/contract.text-markup-types.js +4 -0
- package/dist/contract.text-markup-types.js.map +1 -0
- package/dist/enableTextMarkupGuards.d.ts +6 -0
- package/dist/enableTextMarkupGuards.d.ts.map +1 -0
- package/dist/enableTextMarkupGuards.js +31 -0
- package/dist/enableTextMarkupGuards.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/markupClassStyles.d.ts +2 -1
- package/dist/markupClassStyles.d.ts.map +1 -1
- package/dist/markupClassStyles.js +1 -1
- package/dist/markupClassStyles.js.map +1 -1
- package/dist/markupNamedColors.d.ts +1 -1
- package/dist/markupNamedColors.d.ts.map +1 -1
- package/dist/markupNamedColors.js +1 -1
- package/dist/markupNamedColors.js.map +1 -1
- package/dist/markupTagRegistry.d.ts +4 -3
- package/dist/markupTagRegistry.d.ts.map +1 -1
- package/dist/markupTagRegistry.js +12 -12
- package/dist/markupTagRegistry.js.map +1 -1
- package/dist/normalizeMarkupTagResult.d.ts +6 -0
- package/dist/normalizeMarkupTagResult.d.ts.map +1 -0
- package/dist/normalizeMarkupTagResult.js +24 -0
- package/dist/normalizeMarkupTagResult.js.map +1 -0
- package/dist/textMarkup.d.ts +6 -2
- package/dist/textMarkup.d.ts.map +1 -1
- package/dist/textMarkup.js +129 -35
- package/dist/textMarkup.js.map +1 -1
- package/dist/textMarkupGuards.d.ts +4 -0
- package/dist/textMarkupGuards.d.ts.map +1 -0
- package/dist/textMarkupGuards.js +8 -0
- package/dist/textMarkupGuards.js.map +1 -0
- package/dist/types.d.ts +44 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +24 -0
- package/dist/types.js.map +1 -0
- package/package.json +9 -6
- package/src/enableTextMarkupGuards.test.ts +55 -0
- package/src/markupClassStyles.test.ts +16 -4
- package/src/markupNamedColors.test.ts +5 -4
- package/src/markupTagRegistry.test.ts +5 -10
- package/src/normalizeMarkupTagResult.test.ts +46 -0
- package/src/textMarkup.test.ts +61 -5
- package/src/textMarkupGuards.test.ts +24 -0
- package/src/types.test.ts +40 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Generated from crates/flight-text-markup/tests/normalize_markup_tag_result_test.rs by flight-compiler.
|
|
2
|
+
// Rust is authoritative; run `npm run generate` after changing it.
|
|
3
|
+
|
|
4
|
+
import type { TextFormat } from '@flighthq/textformat/contract';
|
|
5
|
+
import { describe, expect, it } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { normalizeMarkupTagResult } from './contract.ts';
|
|
8
|
+
import type { MarkupTagEffect } from './contract.ts';
|
|
9
|
+
|
|
10
|
+
function boldTextFormat(): TextFormat {
|
|
11
|
+
return {
|
|
12
|
+
align: undefined,
|
|
13
|
+
blockIndent: undefined,
|
|
14
|
+
bold: true,
|
|
15
|
+
bullet: undefined,
|
|
16
|
+
color: undefined,
|
|
17
|
+
font: undefined,
|
|
18
|
+
indent: undefined,
|
|
19
|
+
italic: undefined,
|
|
20
|
+
kerning: undefined,
|
|
21
|
+
leading: undefined,
|
|
22
|
+
leftMargin: undefined,
|
|
23
|
+
letterSpacing: undefined,
|
|
24
|
+
listMarker: undefined,
|
|
25
|
+
rightMargin: undefined,
|
|
26
|
+
size: undefined,
|
|
27
|
+
strikethrough: undefined,
|
|
28
|
+
tabStops: undefined,
|
|
29
|
+
target: undefined,
|
|
30
|
+
underline: undefined,
|
|
31
|
+
url: undefined,
|
|
32
|
+
variations: undefined,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe('normalizeMarkupTagResult', () => {
|
|
37
|
+
it('preserves effects and wraps bare formats', () => {
|
|
38
|
+
const effect: MarkupTagEffect = { breakBefore: true, format: undefined, text: '\n' };
|
|
39
|
+
expect(normalizeMarkupTagResult(effect)).toEqual(effect);
|
|
40
|
+
const format = boldTextFormat();
|
|
41
|
+
const normalized = normalizeMarkupTagResult(format);
|
|
42
|
+
expect(normalized.breakBefore).toEqual(undefined);
|
|
43
|
+
expect(normalized.format).toEqual(format);
|
|
44
|
+
expect(normalized.text).toEqual(undefined);
|
|
45
|
+
});
|
|
46
|
+
});
|
package/src/textMarkup.test.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
1
|
+
import { EntityRuntimeKey } from '@flighthq/entity/contract';
|
|
2
|
+
import { createTextFormatRange } from '@flighthq/textformat/contract';
|
|
3
|
+
import type { TextFormat } from '@flighthq/textformat/contract';
|
|
4
|
+
import type { RichTextContent } from '@flighthq/textlayout/contract';
|
|
4
5
|
import { describe, expect, it } from 'vitest';
|
|
5
6
|
|
|
6
|
-
import { createMarkupTagRegistry, registerMarkupTag } from './markupTagRegistry';
|
|
7
|
-
import { formatTextMarkup, parseTextMarkup } from './textMarkup';
|
|
7
|
+
import { createMarkupTagRegistry, registerMarkupTag } from './markupTagRegistry.ts';
|
|
8
|
+
import { explainTextMarkup, formatTextMarkup, parseTextMarkup } from './textMarkup.ts';
|
|
8
9
|
|
|
9
10
|
function formatAt(content: RichTextContent, index: number): TextFormat {
|
|
10
11
|
for (const range of content.formatRanges) {
|
|
@@ -13,6 +14,24 @@ function formatAt(content: RichTextContent, index: number): TextFormat {
|
|
|
13
14
|
return {};
|
|
14
15
|
}
|
|
15
16
|
|
|
17
|
+
describe('explainTextMarkup', () => {
|
|
18
|
+
it('reports unknown tags, unresolved colors, and sanitized href values as plain data', () => {
|
|
19
|
+
expect(
|
|
20
|
+
explainTextMarkup('<widget>x</widget><font color="mystery">y</font><a href="javascript:run()">z</a>'),
|
|
21
|
+
).toEqual({
|
|
22
|
+
issues: [
|
|
23
|
+
{ kind: 'unknown-tag', offset: 0, tag: 'widget', value: null },
|
|
24
|
+
{ kind: 'unresolved-font-color', offset: 18, tag: 'font', value: 'mystery' },
|
|
25
|
+
{ kind: 'unsafe-href', offset: 48, tag: 'a', value: 'javascript:run()' },
|
|
26
|
+
],
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('returns no issues for fully represented safe markup', () => {
|
|
31
|
+
expect(explainTextMarkup('<b>x</b><a href="https://flight.dev">y</a>')).toEqual({ issues: [] });
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
16
35
|
describe('formatTextMarkup', () => {
|
|
17
36
|
it('returns an empty string for empty content', () => {
|
|
18
37
|
expect(formatTextMarkup({ [EntityRuntimeKey]: undefined, formatRanges: [], text: '' })).toBe('');
|
|
@@ -74,6 +93,30 @@ describe('formatTextMarkup', () => {
|
|
|
74
93
|
};
|
|
75
94
|
expect(formatTextMarkup(content)).toBe('<li type="square">item</li>');
|
|
76
95
|
});
|
|
96
|
+
|
|
97
|
+
it('resolves overlapping ranges by array order without allocating formats per character', () => {
|
|
98
|
+
const content: RichTextContent = {
|
|
99
|
+
[EntityRuntimeKey]: undefined,
|
|
100
|
+
formatRanges: [
|
|
101
|
+
createTextFormatRange({ bold: true, color: 0xff0000ff }, 0, 5),
|
|
102
|
+
createTextFormatRange({ color: 0x0000ffff, italic: true }, 2, 4),
|
|
103
|
+
],
|
|
104
|
+
text: 'abcde',
|
|
105
|
+
};
|
|
106
|
+
expect(formatTextMarkup(content)).toBe(
|
|
107
|
+
'<font color="#ff0000"><b>ab</b></font><font color="#0000ff"><b><i>cd</i></b></font><font color="#ff0000"><b>e</b></font>',
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('serializes resolved span classes as standard tags rather than retaining class names', () => {
|
|
112
|
+
const content: RichTextContent = {
|
|
113
|
+
[EntityRuntimeKey]: undefined,
|
|
114
|
+
formatRanges: [createTextFormatRange({ bold: true }, 0, 1)],
|
|
115
|
+
text: 'x',
|
|
116
|
+
};
|
|
117
|
+
expect(formatTextMarkup(content)).toBe('<b>x</b>');
|
|
118
|
+
expect(formatTextMarkup(content)).not.toContain('class');
|
|
119
|
+
});
|
|
77
120
|
});
|
|
78
121
|
|
|
79
122
|
describe('parseTextMarkup', () => {
|
|
@@ -135,6 +178,13 @@ describe('parseTextMarkup', () => {
|
|
|
135
178
|
expect(formatAt(parseTextMarkup('<font size="24" face="Arial">x</font>'), 0)).toEqual({ font: 'Arial', size: 24 });
|
|
136
179
|
});
|
|
137
180
|
|
|
181
|
+
it('resolves relative font sizes against the enclosing absolute size', () => {
|
|
182
|
+
const content = parseTextMarkup('<font size="16">a<font size="+2">b</font><font size="-1">c</font></font>');
|
|
183
|
+
expect(formatAt(content, 0).size).toBe(16);
|
|
184
|
+
expect(formatAt(content, 1).size).toBe(18);
|
|
185
|
+
expect(formatAt(content, 2).size).toBe(15);
|
|
186
|
+
});
|
|
187
|
+
|
|
138
188
|
it('ignores an unparseable font color rather than throwing', () => {
|
|
139
189
|
expect(formatAt(parseTextMarkup('<font color="notacolor">x</font>'), 0).color).toBeUndefined();
|
|
140
190
|
});
|
|
@@ -144,6 +194,12 @@ describe('parseTextMarkup', () => {
|
|
|
144
194
|
expect(format).toEqual({ target: '_blank', url: 'https://a.test' });
|
|
145
195
|
});
|
|
146
196
|
|
|
197
|
+
it('drops unsafe href schemes while retaining anchor text and harmless target metadata', () => {
|
|
198
|
+
const content = parseTextMarkup('<a href="java\nscript:run()" target="_blank">x</a>');
|
|
199
|
+
expect(content.text).toBe('x');
|
|
200
|
+
expect(formatAt(content, 0)).toEqual({ target: '_blank' });
|
|
201
|
+
});
|
|
202
|
+
|
|
147
203
|
it('parses p align into the align field', () => {
|
|
148
204
|
expect(formatAt(parseTextMarkup('<p align="center">x</p>'), 0)).toEqual({ align: 'center' });
|
|
149
205
|
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { reportTextMarkupIssue, setTextMarkupGuard } from './textMarkupGuards.ts';
|
|
2
|
+
import type { TextMarkupIssue } from './types.ts';
|
|
3
|
+
|
|
4
|
+
const issue: TextMarkupIssue = { kind: 'unknown-tag', offset: 0, tag: 'widget', value: null };
|
|
5
|
+
|
|
6
|
+
afterEach(() => setTextMarkupGuard(null));
|
|
7
|
+
|
|
8
|
+
describe('reportTextMarkupIssue', () => {
|
|
9
|
+
it('is silent when no guard is installed', () => {
|
|
10
|
+
expect(() => reportTextMarkupIssue(issue)).not.toThrow();
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe('setTextMarkupGuard', () => {
|
|
15
|
+
it('replaces the active guard instead of accumulating callbacks', () => {
|
|
16
|
+
const first: TextMarkupIssue[] = [];
|
|
17
|
+
const second: TextMarkupIssue[] = [];
|
|
18
|
+
setTextMarkupGuard((value) => first.push(value));
|
|
19
|
+
setTextMarkupGuard((value) => second.push(value));
|
|
20
|
+
reportTextMarkupIssue(issue);
|
|
21
|
+
expect(first).toEqual([]);
|
|
22
|
+
expect(second).toEqual([issue]);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Generated from crates/flight-text-markup/tests/types_test.rs by flight-compiler.
|
|
2
|
+
// Rust is authoritative; run `npm run generate` after changing it.
|
|
3
|
+
|
|
4
|
+
import * as flightEntity from '@flighthq/entity/contract';
|
|
5
|
+
import { describe, expect, it } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
TextMarkupIssueKind,
|
|
9
|
+
createTextMarkupGuardState,
|
|
10
|
+
getTextMarkupIssueKindString,
|
|
11
|
+
parseTextMarkupIssueKind,
|
|
12
|
+
} from './contract.ts';
|
|
13
|
+
|
|
14
|
+
describe('createTextMarkupGuardState', () => {
|
|
15
|
+
it('allocates a distinct entity per guard slot', () => {
|
|
16
|
+
let first = createTextMarkupGuardState();
|
|
17
|
+
let second = createTextMarkupGuardState();
|
|
18
|
+
expect(flightEntity.getEntityUid(first)).not.toEqual(flightEntity.getEntityUid(second));
|
|
19
|
+
});
|
|
20
|
+
it('creates an empty guard slot', () => {
|
|
21
|
+
const state = createTextMarkupGuardState();
|
|
22
|
+
expect(state.guard === undefined).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('getTextMarkupIssueKindString', () => {
|
|
27
|
+
it('maps each closed issue kind to its source name', () => {
|
|
28
|
+
expect(getTextMarkupIssueKindString(TextMarkupIssueKind.UnknownTag)).toEqual('unknown-tag');
|
|
29
|
+
expect(getTextMarkupIssueKindString(TextMarkupIssueKind.UnresolvedFontColor)).toEqual('unresolved-font-color');
|
|
30
|
+
expect(getTextMarkupIssueKindString(TextMarkupIssueKind.UnsafeHref)).toEqual('unsafe-href');
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('parseTextMarkupIssueKind', () => {
|
|
35
|
+
it('parses the source names and rejects anything else', () => {
|
|
36
|
+
expect(parseTextMarkupIssueKind('unknown-tag')).toEqual(TextMarkupIssueKind.UnknownTag);
|
|
37
|
+
expect(parseTextMarkupIssueKind('unsafe-href')).toEqual(TextMarkupIssueKind.UnsafeHref);
|
|
38
|
+
expect(parseTextMarkupIssueKind('')).toEqual(undefined);
|
|
39
|
+
});
|
|
40
|
+
});
|