Compare commits

...

4 Commits
8.0.2 ... 8.0.3

Author SHA1 Message Date
bb686bbcbe Version bump 2023-04-20 00:58:11 +01:00
c898d0a3c3 Merge pull request #2087 from lentyaevpk/fix/myoffice-numbering-render
fix: added unique numeric id creator to avoid numbering render errors
2023-04-19 18:09:42 +01:00
704c678333 fix: unit tests fix 2023-04-19 16:51:03 +03:00
c59c5350fd fix: added unique numeric id creator to avoid numbering render errors 2023-04-18 15:15:38 +03:00
12 changed files with 36 additions and 26 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "docx", "name": "docx",
"version": "8.0.2", "version": "8.0.3",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "docx", "name": "docx",
"version": "8.0.2", "version": "8.0.3",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@types/node": "^18.0.0", "@types/node": "^18.0.0",

View File

@ -1,6 +1,6 @@
{ {
"name": "docx", "name": "docx",
"version": "8.0.2", "version": "8.0.3",
"description": "Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.", "description": "Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.",
"main": "build/index.js", "main": "build/index.js",
"scripts": { "scripts": {

View File

@ -41,11 +41,11 @@ const createAnchor = (drawingOptions: IDrawingOptions): Anchor =>
describe("Anchor", () => { describe("Anchor", () => {
before(() => { before(() => {
stub(convenienceFunctions, "uniqueNumericId").callsFake(() => 0); stub(convenienceFunctions, "docPropertiesUniqueNumericId").callsFake(() => 0);
}); });
after(() => { after(() => {
(convenienceFunctions.uniqueNumericId as SinonStub).restore(); (convenienceFunctions.docPropertiesUniqueNumericId as SinonStub).restore();
}); });
let anchor: Anchor; let anchor: Anchor;

View File

@ -2,7 +2,7 @@
import { IContext, IXmlableObject, NextAttributeComponent, XmlComponent } from "@file/xml-components"; import { IContext, IXmlableObject, NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { ConcreteHyperlink } from "@file/paragraph"; import { ConcreteHyperlink } from "@file/paragraph";
import { uniqueNumericId } from "@util/convenience-functions"; import { docPropertiesUniqueNumericId } from "@util/convenience-functions";
import { createHyperlinkClick } from "./doc-properties-children"; import { createHyperlinkClick } from "./doc-properties-children";
@ -32,7 +32,7 @@ export class DocProperties extends XmlComponent {
new NextAttributeComponent({ new NextAttributeComponent({
id: { id: {
key: "id", key: "id",
value: uniqueNumericId(), value: docPropertiesUniqueNumericId(),
}, },
name: { name: {
key: "name", key: "name",

View File

@ -31,11 +31,11 @@ const createDrawing = (drawingOptions?: IDrawingOptions): Drawing =>
describe("Drawing", () => { describe("Drawing", () => {
before(() => { before(() => {
stub(convenienceFunctions, "uniqueNumericId").callsFake(() => 0); stub(convenienceFunctions, "docPropertiesUniqueNumericId").callsFake(() => 0);
}); });
after(() => { after(() => {
(convenienceFunctions.uniqueNumericId as SinonStub).restore(); (convenienceFunctions.docPropertiesUniqueNumericId as SinonStub).restore();
}); });
let currentBreak: Drawing; let currentBreak: Drawing;

View File

@ -8,11 +8,13 @@ import { Numbering } from "./numbering";
describe("Numbering", () => { describe("Numbering", () => {
before(() => { before(() => {
stub(convenienceFunctions, "uniqueNumericId").callsFake(() => 0); stub(convenienceFunctions, "abstractNumUniqueNumericId").callsFake(() => 0);
stub(convenienceFunctions, "concreteNumUniqueNumericId").callsFake(() => 0);
}); });
after(() => { after(() => {
(convenienceFunctions.uniqueNumericId as SinonStub).restore(); (convenienceFunctions.abstractNumUniqueNumericId as SinonStub).restore();
(convenienceFunctions.concreteNumUniqueNumericId as SinonStub).restore();
}); });
describe("#constructor", () => { describe("#constructor", () => {

View File

@ -2,7 +2,7 @@
// https://stackoverflow.com/questions/58622437/purpose-of-abstractnum-and-numberinginstance // https://stackoverflow.com/questions/58622437/purpose-of-abstractnum-and-numberinginstance
import { AlignmentType } from "@file/paragraph"; import { AlignmentType } from "@file/paragraph";
import { IContext, IXmlableObject, XmlComponent } from "@file/xml-components"; import { IContext, IXmlableObject, XmlComponent } from "@file/xml-components";
import { convertInchesToTwip, uniqueNumericId } from "@util/convenience-functions"; import { abstractNumUniqueNumericId, concreteNumUniqueNumericId, convertInchesToTwip } from "@util/convenience-functions";
import { DocumentAttributes } from "../document/document-attributes"; import { DocumentAttributes } from "../document/document-attributes";
import { AbstractNumbering } from "./abstract-numbering"; import { AbstractNumbering } from "./abstract-numbering";
@ -55,7 +55,7 @@ export class Numbering extends XmlComponent {
}), }),
); );
const abstractNumbering = new AbstractNumbering(uniqueNumericId(), [ const abstractNumbering = new AbstractNumbering(abstractNumUniqueNumericId(), [
{ {
level: 0, level: 0,
format: LevelFormat.BULLET, format: LevelFormat.BULLET,
@ -176,7 +176,7 @@ export class Numbering extends XmlComponent {
this.abstractNumberingMap.set("default-bullet-numbering", abstractNumbering); this.abstractNumberingMap.set("default-bullet-numbering", abstractNumbering);
for (const con of options.config) { for (const con of options.config) {
this.abstractNumberingMap.set(con.reference, new AbstractNumbering(uniqueNumericId(), con.levels)); this.abstractNumberingMap.set(con.reference, new AbstractNumbering(abstractNumUniqueNumericId(), con.levels));
this.referenceConfigMap.set(con.reference, con.levels); this.referenceConfigMap.set(con.reference, con.levels);
} }
} }
@ -209,7 +209,7 @@ export class Numbering extends XmlComponent {
const firstLevelStartNumber = referenceConfigLevels && referenceConfigLevels[0].start; const firstLevelStartNumber = referenceConfigLevels && referenceConfigLevels[0].start;
const concreteNumberingSettings = { const concreteNumberingSettings = {
numId: uniqueNumericId(), numId: concreteNumUniqueNumericId(),
abstractNumId: abstractNumbering.id, abstractNumId: abstractNumbering.id,
reference, reference,
instance, instance,

View File

@ -1,6 +1,6 @@
// http://officeopenxml.com/WPbookmark.php // http://officeopenxml.com/WPbookmark.php
import { XmlComponent } from "@file/xml-components"; import { XmlComponent } from "@file/xml-components";
import { uniqueNumericId } from "@util/convenience-functions"; import { bookmarkUniqueNumericId } from "@util/convenience-functions";
import { ParagraphChild } from "../paragraph"; import { ParagraphChild } from "../paragraph";
import { BookmarkEndAttributes, BookmarkStartAttributes } from "./bookmark-attributes"; import { BookmarkEndAttributes, BookmarkStartAttributes } from "./bookmark-attributes";
@ -11,7 +11,7 @@ export class Bookmark {
public readonly end: BookmarkEnd; public readonly end: BookmarkEnd;
public constructor(options: { readonly id: string; readonly children: readonly ParagraphChild[] }) { public constructor(options: { readonly id: string; readonly children: readonly ParagraphChild[] }) {
const linkId = uniqueNumericId(); const linkId = bookmarkUniqueNumericId();
this.start = new BookmarkStart(options.id, linkId); this.start = new BookmarkStart(options.id, linkId);
this.children = options.children; this.children = options.children;

View File

@ -20,12 +20,12 @@ import { TextRun } from "./run";
describe("Paragraph", () => { describe("Paragraph", () => {
before(() => { before(() => {
stub(convenienceFunctions, "uniqueId").callsFake(() => "test-unique-id"); stub(convenienceFunctions, "uniqueId").callsFake(() => "test-unique-id");
stub(convenienceFunctions, "uniqueNumericId").callsFake(() => -101); stub(convenienceFunctions, "bookmarkUniqueNumericId").callsFake(() => -101);
}); });
after(() => { after(() => {
(convenienceFunctions.uniqueId as SinonStub).restore(); (convenienceFunctions.uniqueId as SinonStub).restore();
(convenienceFunctions.uniqueNumericId as SinonStub).restore(); (convenienceFunctions.bookmarkUniqueNumericId as SinonStub).restore();
}); });
describe("#constructor()", () => { describe("#constructor()", () => {

View File

@ -11,12 +11,12 @@ import { ImageRun } from "./image-run";
describe("ImageRun", () => { describe("ImageRun", () => {
before(() => { before(() => {
stub(convenienceFunctions, "uniqueId").callsFake(() => "test-unique-id"); stub(convenienceFunctions, "uniqueId").callsFake(() => "test-unique-id");
stub(convenienceFunctions, "uniqueNumericId").callsFake(() => 0); stub(convenienceFunctions, "docPropertiesUniqueNumericId").callsFake(() => 0);
}); });
after(() => { after(() => {
(convenienceFunctions.uniqueId as SinonStub).restore(); (convenienceFunctions.uniqueId as SinonStub).restore();
(convenienceFunctions.uniqueNumericId as SinonStub).restore(); (convenienceFunctions.docPropertiesUniqueNumericId as SinonStub).restore();
}); });
describe("#constructor()", () => { describe("#constructor()", () => {

View File

@ -1,6 +1,6 @@
import { expect } from "chai"; import { expect } from "chai";
import { convertInchesToTwip, convertMillimetersToTwip, uniqueId, uniqueNumericId } from "./convenience-functions"; import { convertInchesToTwip, convertMillimetersToTwip, uniqueId, uniqueNumericIdCreator } from "./convenience-functions";
describe("Utility", () => { describe("Utility", () => {
describe("#convertMillimetersToTwip", () => { describe("#convertMillimetersToTwip", () => {
@ -17,8 +17,9 @@ describe("Utility", () => {
}); });
}); });
describe("#uniqueNumericId", () => { describe("#uniqueNumericIdCreator", () => {
it("should generate a unique incrementing ID", () => { it("should generate a unique incrementing ID", () => {
const uniqueNumericId = uniqueNumericIdCreator();
expect(uniqueNumericId()).to.not.be.undefined; expect(uniqueNumericId()).to.not.be.undefined;
}); });
}); });

View File

@ -1,12 +1,19 @@
import { nanoid } from "nanoid/non-secure"; import { nanoid } from "nanoid/non-secure";
let currentCount = 0;
// Twip - twentieths of a point // Twip - twentieths of a point
export const convertMillimetersToTwip = (millimeters: number): number => Math.floor((millimeters / 25.4) * 72 * 20); export const convertMillimetersToTwip = (millimeters: number): number => Math.floor((millimeters / 25.4) * 72 * 20);
export const convertInchesToTwip = (inches: number): number => Math.floor(inches * 72 * 20); export const convertInchesToTwip = (inches: number): number => Math.floor(inches * 72 * 20);
export const uniqueNumericId = (): number => ++currentCount; export const uniqueNumericIdCreator = (initial = 0): (() => number) => {
let currentCount = initial;
return () => ++currentCount;
};
export const abstractNumUniqueNumericId = uniqueNumericIdCreator();
export const concreteNumUniqueNumericId = uniqueNumericIdCreator(1); // Setting initial to 1 as we have numId = 1 for "default-bullet-numbering"
export const docPropertiesUniqueNumericId = uniqueNumericIdCreator();
export const bookmarkUniqueNumericId = uniqueNumericIdCreator();
export const uniqueId = (): string => nanoid().toLowerCase(); export const uniqueId = (): string => nanoid().toLowerCase();