Skip to content

Commit 7d3ecf9

Browse files
Apply review comment
1 parent e1f7309 commit 7d3ecf9

5 files changed

Lines changed: 140 additions & 51 deletions

File tree

packages/react-tei/src/tags/Value.spec.tsx

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,120 @@ describe("Value", () => {
8787
await expect.element(screen.container).toBeEmptyDOMElement();
8888
});
8989
});
90+
91+
describe("MathML", () => {
92+
it.each<DocumentJson["attributes"]>([
93+
{
94+
"@xmlns": "http://www.w3.org/1998/Math/MathML",
95+
},
96+
{},
97+
])("should render MathML tags without prefix", async (attributes) => {
98+
const jsonValue: DocumentJson = {
99+
tag: "math",
100+
attributes,
101+
value: [
102+
{
103+
tag: "mi",
104+
attributes: {},
105+
value: [{ tag: "#text", value: "x" }],
106+
},
107+
{
108+
tag: "mo",
109+
attributes: {},
110+
value: [{ tag: "#text", value: "+" }],
111+
},
112+
{
113+
tag: "mi",
114+
attributes: {},
115+
value: [{ tag: "#text", value: "y" }],
116+
},
117+
],
118+
};
119+
120+
const screen = await render(<Value data={jsonValue} />, {
121+
wrapper: ({ children }) => (
122+
<TagCatalogProvider tagCatalog={tagCatalog}>
123+
{children}
124+
</TagCatalogProvider>
125+
),
126+
});
127+
128+
await expect.element(screen.getByRole("math")).toHaveTextContent("x+y");
129+
});
130+
131+
it.each<string>([
132+
"m",
133+
"mml",
134+
])("should render MathML tags with '%s' namespace prefix and xmlns attribute", async (prefix) => {
135+
const jsonValue: DocumentJson = {
136+
tag: `${prefix}:math`,
137+
attributes: {
138+
[`@xmlns:${prefix}`]: "http://www.w3.org/1998/Math/MathML",
139+
},
140+
value: [
141+
{
142+
tag: `${prefix}:mi`,
143+
attributes: {},
144+
value: [{ tag: "#text", value: "a" }],
145+
},
146+
{
147+
tag: `${prefix}:mo`,
148+
attributes: {},
149+
value: [{ tag: "#text", value: "=" }],
150+
},
151+
{
152+
tag: `${prefix}:mi`,
153+
attributes: {},
154+
value: [{ tag: "#text", value: "b" }],
155+
},
156+
],
157+
};
158+
159+
const screen = await render(<Value data={jsonValue} />, {
160+
wrapper: ({ children }) => (
161+
<TagCatalogProvider tagCatalog={tagCatalog}>
162+
{children}
163+
</TagCatalogProvider>
164+
),
165+
});
166+
167+
await expect.element(screen.getByRole("math")).toHaveTextContent("a=b");
168+
});
169+
170+
it.each<string>([
171+
"m",
172+
"mml",
173+
])("should render MathML tags without xmlns", async (prefix) => {
174+
const jsonValue: DocumentJson = {
175+
tag: `${prefix}:math`,
176+
attributes: {},
177+
value: [
178+
{
179+
tag: `${prefix}:mi`,
180+
attributes: {},
181+
value: [{ tag: "#text", value: "p" }],
182+
},
183+
{
184+
tag: `${prefix}:mo`,
185+
attributes: {},
186+
value: [{ tag: "#text", value: ">" }],
187+
},
188+
{
189+
tag: `${prefix}:mi`,
190+
attributes: {},
191+
value: [{ tag: "#text", value: "q" }],
192+
},
193+
],
194+
};
195+
196+
const screen = await render(<Value data={jsonValue} />, {
197+
wrapper: ({ children }) => (
198+
<TagCatalogProvider tagCatalog={tagCatalog}>
199+
{children}
200+
</TagCatalogProvider>
201+
),
202+
});
203+
204+
await expect.element(screen.getByRole("math")).toHaveTextContent("p>q");
205+
});
206+
});

packages/react-tei/src/tags/Value.tsx

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import { DebugTag } from "../debug/DebugTag";
22
import { IS_DEBUG } from "../debug/debug.const";
33
import type { DocumentJson, DocumentJsonValue } from "../parser/document";
4-
import { MathMLContextProvider } from "./formula/mathml/MathMLContext";
5-
import { MathMLTag } from "./formula/mathml/MathMLTag";
6-
import { useMathMLContext } from "./formula/mathml/useMathMLContext";
74
import { useTagCatalog } from "./TagCatalogProvider";
85

9-
const MATHML_NAMESPACE_URL = "http://www.w3.org/1998/Math/MathML";
10-
116
export function Value({ data }: ValueProps) {
127
const tagCatalog = useTagCatalog();
13-
const mathMLNsPrefix = useMathMLContext();
148

159
if (!data) {
1610
return null;
@@ -24,28 +18,36 @@ export function Value({ data }: ValueProps) {
2418
return data;
2519
}
2620

27-
const { tag, attributes, value } = data as DocumentJson;
21+
const { tag: completeTag, value } = data as DocumentJson;
2822

29-
const mathMLNS = attributes
30-
? Object.keys(attributes).find(
31-
(key) =>
32-
key.startsWith("@xmlns:") && attributes[key] === MATHML_NAMESPACE_URL,
33-
)
34-
: null;
23+
const tag = completeTag.includes(":")
24+
? completeTag.split(":")[1]
25+
: completeTag;
3526

36-
if (mathMLNS) {
27+
if (!tag) {
3728
return (
38-
<MathMLContextProvider nsPrefix={mathMLNS.replace("@xmlns:", "")}>
39-
<MathMLTag data={data} />
40-
</MathMLContextProvider>
29+
<DebugTag
30+
tag={data.tag}
31+
attributes={data.attributes}
32+
message={`Tag with XML prefix and without name`}
33+
payload={data}
34+
type="error"
35+
>
36+
<Value data={data.value} />
37+
</DebugTag>
4138
);
42-
} else if (tag.startsWith(`${mathMLNsPrefix}:`)) {
43-
return <MathMLTag data={data} />;
4439
}
4540

4641
const TagComponent = tagCatalog[tag];
4742
if (TagComponent) {
48-
return <TagComponent data={data} />;
43+
return (
44+
<TagComponent
45+
data={{
46+
...data,
47+
tag,
48+
}}
49+
/>
50+
);
4951
}
5052

5153
if (!IS_DEBUG) {

packages/react-tei/src/tags/formula/mathml/MathMLContext.tsx

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/react-tei/src/tags/formula/mathml/MathMLTag.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import { createElement } from "react";
22
import type { ComponentProps } from "../../type";
33
import { Value } from "../../Value";
4-
import { useMathMLContext } from "./useMathMLContext";
54

65
export function MathMLTag({ data }: ComponentProps) {
7-
const nsPrefix = useMathMLContext();
8-
9-
const tag = nsPrefix ? `${data.tag.replace(`${nsPrefix}:`, "")}` : data.tag;
10-
116
const attributes = data.attributes
127
? Object.fromEntries(
138
Object.entries(data.attributes).map(([key, value]) => [
@@ -18,7 +13,7 @@ export function MathMLTag({ data }: ComponentProps) {
1813
: {};
1914

2015
return createElement(
21-
tag,
16+
data.tag,
2217
attributes,
2318
data.value ? <Value data={data.value} /> : null,
2419
);

packages/react-tei/src/tags/formula/mathml/useMathMLContext.tsx

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)