Preserve QWord internal hyperlinks in DOCX
This commit is contained in:
@@ -104,16 +104,18 @@ describe("wordOffice helpers", () => {
|
||||
|
||||
it("сохраняет HTML-гиперссылки как inline runs", () => {
|
||||
const blocks = htmlToWordBlocks(`
|
||||
<p>Откройте <a href="https://example.com/report"><strong>отчет</strong></a> и <a href="javascript:alert(1)">небезопасную ссылку</a>.</p>
|
||||
<p>Откройте <a href="https://example.com/report"><strong>отчет</strong></a>, <a href="#Section1">раздел</a> и <a href="javascript:alert(1)">небезопасную ссылку</a>.</p>
|
||||
`);
|
||||
|
||||
expect(blocks).toEqual([
|
||||
{
|
||||
type: "paragraph",
|
||||
text: "Откройте отчет и небезопасную ссылку.",
|
||||
text: "Откройте отчет, раздел и небезопасную ссылку.",
|
||||
runs: [
|
||||
{ text: "Откройте " },
|
||||
{ text: "отчет", bold: true, href: "https://example.com/report" },
|
||||
{ text: ", " },
|
||||
{ text: "раздел", href: "#Section1" },
|
||||
{ text: " и небезопасную ссылку." }
|
||||
]
|
||||
}
|
||||
@@ -224,6 +226,17 @@ describe("wordOffice helpers", () => {
|
||||
expect(relationshipsXml).not.toContain("javascript:alert");
|
||||
});
|
||||
|
||||
it("записывает внутренние HTML-гиперссылки как w:hyperlink anchor без внешних relationships", async () => {
|
||||
const blob = await exportDocxBlob("Внутренняя ссылка.docx", '<p>См. <a href="#Section1">раздел</a>.</p>');
|
||||
const zip = await JSZip.loadAsync(await blob.arrayBuffer());
|
||||
const documentXml = await zip.file("word/document.xml")?.async("string");
|
||||
const relationshipsXml = await zip.file("word/_rels/document.xml.rels")?.async("string");
|
||||
|
||||
expect(documentXml).toMatch(/<w:hyperlink\b[^>]*w:anchor="Section1"[^>]*>/);
|
||||
expect(documentXml).toContain('w:rStyle w:val="Hyperlink"');
|
||||
expect(relationshipsXml).not.toContain('Target="#Section1"');
|
||||
});
|
||||
|
||||
it("записывает data URI изображения в DOCX media relationships", async () => {
|
||||
const blob = await exportDocxBlob("Изображение.docx", `<p>Логотип</p><img src="${tinyPngDataUri}" alt="Логотип" width="32" height="32">`);
|
||||
const zip = await JSZip.loadAsync(await blob.arrayBuffer());
|
||||
|
||||
+41
-4
@@ -89,6 +89,7 @@ type DocxModule = {
|
||||
TextRun: new (options: string | Record<string, unknown>) => unknown;
|
||||
WidthType?: Record<string, string>;
|
||||
ExternalHyperlink?: new (options: { children: unknown[]; link: string }) => unknown;
|
||||
InternalHyperlink?: new (options: { children: unknown[]; anchor: string }) => unknown;
|
||||
ImageRun?: new (options: Record<string, unknown>) => unknown;
|
||||
};
|
||||
|
||||
@@ -391,7 +392,17 @@ function wordInlineRunToTextRunOption(run: WordInlineRun) {
|
||||
function wordInlineRunsToTextRuns(runs: WordInlineRun[], docx: DocxModule) {
|
||||
const normalizedRuns = runs.length > 0 ? runs : [{ text: "" }];
|
||||
return normalizedRuns.flatMap((run) => {
|
||||
const href = normalizedHyperlinkTarget(run.href);
|
||||
const internalAnchor = normalizedInternalHyperlinkAnchor(run.href);
|
||||
if (internalAnchor && docx.InternalHyperlink) {
|
||||
return [
|
||||
new docx.InternalHyperlink({
|
||||
anchor: internalAnchor,
|
||||
children: wordInlineRunToTextRunsForRun(run, docx, { style: "Hyperlink" })
|
||||
})
|
||||
];
|
||||
}
|
||||
|
||||
const href = normalizedExternalHyperlinkTarget(run.href);
|
||||
if (href && docx.ExternalHyperlink) {
|
||||
return [
|
||||
new docx.ExternalHyperlink({
|
||||
@@ -842,8 +853,10 @@ function orderedDocxParagraphInlineRecords(paragraph: unknown, hyperlinkTargets:
|
||||
return [];
|
||||
}
|
||||
|
||||
const relationshipId = docxAttribute(orderedElementAttributes(child), "r:id") || docxAttribute(orderedElementAttributes(child), "id");
|
||||
const href = normalizedHyperlinkTarget(hyperlinkTargets[relationshipId]);
|
||||
const attributes = orderedElementAttributes(child);
|
||||
const relationshipId = docxAttribute(attributes, "r:id") || docxAttribute(attributes, "id");
|
||||
const anchor = normalizedDocxAnchor(docxAttribute(attributes, "w:anchor") || docxAttribute(attributes, "anchor"));
|
||||
const href = normalizedExternalHyperlinkTarget(hyperlinkTargets[relationshipId]) || (anchor ? `#${anchor}` : "");
|
||||
return orderedDirectElements(child, "w:r").map((run) => ({
|
||||
run: orderedElementToRecord(run),
|
||||
text: orderedDocxRunText(run),
|
||||
@@ -884,7 +897,7 @@ function docxRelationshipTargetsById(relationshipsXml: unknown) {
|
||||
}
|
||||
|
||||
if (type === wordHyperlinkRelationshipType) {
|
||||
const normalizedTarget = normalizedHyperlinkTarget(target);
|
||||
const normalizedTarget = normalizedExternalHyperlinkTarget(target);
|
||||
if (normalizedTarget) {
|
||||
targets.hyperlinks[id] = normalizedTarget;
|
||||
}
|
||||
@@ -1430,6 +1443,15 @@ function cssFontFamilyValue(value: string) {
|
||||
}
|
||||
|
||||
function normalizedHyperlinkTarget(value?: string | null) {
|
||||
const internalHref = normalizedInternalHyperlinkHref(value);
|
||||
if (internalHref) {
|
||||
return internalHref;
|
||||
}
|
||||
|
||||
return normalizedExternalHyperlinkTarget(value);
|
||||
}
|
||||
|
||||
function normalizedExternalHyperlinkTarget(value?: string | null) {
|
||||
const raw = value?.trim();
|
||||
if (!raw) {
|
||||
return "";
|
||||
@@ -1444,6 +1466,21 @@ function normalizedHyperlinkTarget(value?: string | null) {
|
||||
}
|
||||
}
|
||||
|
||||
function normalizedInternalHyperlinkHref(value?: string | null) {
|
||||
const anchor = normalizedInternalHyperlinkAnchor(value);
|
||||
return anchor ? `#${anchor}` : "";
|
||||
}
|
||||
|
||||
function normalizedInternalHyperlinkAnchor(value?: string | null) {
|
||||
const raw = value?.trim();
|
||||
return raw?.startsWith("#") ? normalizedDocxAnchor(raw.slice(1)) : "";
|
||||
}
|
||||
|
||||
function normalizedDocxAnchor(value?: string | null) {
|
||||
const normalized = value?.trim() ?? "";
|
||||
return /^[A-Za-z_][A-Za-z0-9_.:-]{0,79}$/.test(normalized) ? normalized : "";
|
||||
}
|
||||
|
||||
function imageBlockFromElement(element: Element): WordImageBlock {
|
||||
return {
|
||||
type: "image",
|
||||
|
||||
@@ -167,6 +167,23 @@ describe("wordOffice DOCX import", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("imports DOCX internal hyperlink anchors from OOXML", async () => {
|
||||
const blob = await exportDocxBlob("InternalHyperlink.docx", '<p>Open <a href="#Section1"><strong>section</strong></a>.</p>');
|
||||
const file = new File([blob], "InternalHyperlink.docx", {
|
||||
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||
});
|
||||
const imported = await importDocxFile(file);
|
||||
|
||||
expect(imported.html).toContain('<a href="#Section1">');
|
||||
expect(htmlToWordBlocks(imported.html)).toEqual([
|
||||
{
|
||||
type: "paragraph",
|
||||
text: "Open section.",
|
||||
runs: [{ text: "Open " }, { text: "section", bold: true, href: "#Section1" }, { text: "." }]
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
it("imports simple DOCX tables from OOXML", async () => {
|
||||
const blob = await exportDocxBlob(
|
||||
"Table.docx",
|
||||
|
||||
Reference in New Issue
Block a user