#2733 Add error if level exceeds 9

This commit is contained in:
Dolan
2021-12-18 15:42:35 +00:00
parent ed8b4180f8
commit 6d3ebf90c1
7 changed files with 92 additions and 9 deletions

View File

@ -12,7 +12,7 @@ const doc = new Document({
new Paragraph({ new Paragraph({
text: "Bullet points", text: "Bullet points",
bullet: { bullet: {
level: 0 //How deep you want the bullet to be level: 0 // How deep you want the bullet to be. Maximum level is 9
} }
}), }),
new Paragraph({ new Paragraph({

46
package-lock.json generated
View File

@ -1690,6 +1690,52 @@
"integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==", "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==",
"dev": true "dev": true
}, },
"cross-env": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-env/-/cross-env-7.0.3.tgz",
"integrity": "sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==",
"dev": true,
"requires": {
"cross-spawn": "^7.0.1"
},
"dependencies": {
"cross-spawn": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
"integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==",
"dev": true,
"requires": {
"path-key": "^3.1.0",
"shebang-command": "^2.0.0",
"which": "^2.0.1"
}
},
"shebang-command": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
"integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==",
"dev": true,
"requires": {
"shebang-regex": "^3.0.0"
}
},
"shebang-regex": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz",
"integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==",
"dev": true
},
"which": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
"integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
"dev": true,
"requires": {
"isexe": "^2.0.0"
}
}
}
},
"cross-spawn": { "cross-spawn": {
"version": "5.1.0", "version": "5.1.0",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-5.1.0.tgz", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-5.1.0.tgz",

View File

@ -5,7 +5,7 @@
"main": "build/index.js", "main": "build/index.js",
"scripts": { "scripts": {
"pretest": "rimraf ./build", "pretest": "rimraf ./build",
"test": "TS_NODE_PROJECT=\"tsconfig.spec.json\" mocha -r ts-node/register -r tsconfig-paths/register \"src/**/*.ts\"", "test": "cross-env TS_NODE_PROJECT=\"tsconfig.spec.json\" mocha -r ts-node/register -r tsconfig-paths/register \"src/**/*.ts\"",
"test.coverage": "nyc npm test", "test.coverage": "nyc npm test",
"test.watch": "npm test -- --watch", "test.watch": "npm test -- --watch",
"prepublishOnly": "npm run build --production", "prepublishOnly": "npm run build --production",
@ -73,6 +73,7 @@
"@types/webpack": "^5.0.0", "@types/webpack": "^5.0.0",
"buffer": "^6.0.3", "buffer": "^6.0.3",
"chai": "^3.5.0", "chai": "^3.5.0",
"cross-env": "^7.0.3",
"docsify-cli": "^4.3.0", "docsify-cli": "^4.3.0",
"glob": "^7.1.2", "glob": "^7.1.2",
"jszip": "^3.1.5", "jszip": "^3.1.5",

View File

@ -0,0 +1,25 @@
import { expect } from "chai";
import { LevelFormat, LevelSuffix } from ".";
import { AlignmentType } from "..";
import { Level } from "./level";
describe("Level", () => {
describe("#constructor", () => {
it("should throw an error if level exceeds 9", () => {
expect(
() =>
new Level({
level: 10,
format: LevelFormat.BULLET,
text: "test",
alignment: AlignmentType.BOTH,
start: 3,
style: { run: {}, paragraph: {} },
suffix: LevelSuffix.SPACE,
}),
).to.throw();
});
});
});

View File

@ -161,6 +161,12 @@ export class LevelBase extends XmlComponent {
this.root.push(this.paragraphProperties); this.root.push(this.paragraphProperties);
this.root.push(this.runProperties); this.root.push(this.runProperties);
if (level > 9) {
throw new Error(
"Level cannot be greater than 9. Read more here: https://answers.microsoft.com/en-us/msoffice/forum/all/does-word-support-more-than-9-list-levels/d130fdcd-1781-446d-8c84-c6c79124e4d7",
);
}
this.root.push( this.root.push(
new LevelAttributes({ new LevelAttributes({
ilvl: decimalNumber(level), ilvl: decimalNumber(level),

View File

@ -5,21 +5,17 @@ import { Formatter } from "export/formatter";
import { NumberProperties } from "./unordered-list"; import { NumberProperties } from "./unordered-list";
describe("NumberProperties", () => { describe("NumberProperties", () => {
let numberProperties: NumberProperties;
beforeEach(() => {
numberProperties = new NumberProperties(5, 10);
});
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a Number Properties with correct root key", () => { it("should create a Number Properties with correct root key", () => {
const numberProperties = new NumberProperties(5, 9);
const tree = new Formatter().format(numberProperties); const tree = new Formatter().format(numberProperties);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:numPr": [ "w:numPr": [
{ {
"w:ilvl": { "w:ilvl": {
_attr: { _attr: {
"w:val": 10, "w:val": 9,
}, },
}, },
}, },
@ -33,5 +29,9 @@ describe("NumberProperties", () => {
], ],
}); });
}); });
it("should throw an error if level exceeds 9", () => {
expect(() => new NumberProperties(5, 10)).to.throw();
});
}); });
}); });

View File

@ -11,6 +11,11 @@ export class NumberProperties extends XmlComponent {
class IndentLevel extends XmlComponent { class IndentLevel extends XmlComponent {
constructor(level: number) { constructor(level: number) {
super("w:ilvl"); super("w:ilvl");
if (level > 9) {
throw new Error("Level cannot be greater than 9. Read more here: https://answers.microsoft.com/en-us/msoffice/forum/all/does-word-support-more-than-9-list-levels/d130fdcd-1781-446d-8c84-c6c79124e4d7");
}
this.root.push( this.root.push(
new Attributes({ new Attributes({
val: level, val: level,