Files
docx-js/src/file/table-of-contents/table-of-contents.spec.ts
Bruce Duncan 816cb54b14 Optimize XML output by properly constructing objects to send to the xml library so that it can produce proper empty elements.
Rework the way attributes are stored in ImportedXmlComponent to match elsewhere (required allowing for a null xmlKeys in the XmlAttributeComponent interface).
Rework the way paragraphs get added to the end of table cells if needed.
The goal in both reworks is to not mess around with the objects output from `prepForXml` if we can avoid it.
Made the output of RunProperties, ParagraphProperties, TableCellProperties, TableRowProperties, and TableProperties all optional based on whether they contain any attributes or children.  Changed code in PageBorders, TableCellMargin, and TableCellBorders that implemented this same thing by overriding `prepForXml` so that it uses the new XmlComponent subclass instead.
Removed commented out code that attempted to fix-up XML output and make proper empty elements.
Fixed all affected tests.
Turn off `no-null-keyword` in the linter as we need to use null to signal to the `xml` library to create an empty element with no attributes (`undefined` will not work in its place).

Fixes #306
2019-04-09 05:27:18 -04:00

182 lines
6.3 KiB
TypeScript

import { expect } from "chai";
import { Formatter } from "export/formatter";
import { TableOfContents } from "./table-of-contents";
import { StyleLevel } from "./table-of-contents-properties";
describe("Table of Contents", () => {
describe("#constructor", () => {
it("should construct a TOC without options", () => {
const toc = new TableOfContents();
const tree = new Formatter().format(toc);
expect(tree).to.be.deep.equal(DEFAULT_TOC);
});
it("should construct a TOC with all the options and alias", () => {
const styles = new Array<StyleLevel>();
styles.push(new StyleLevel("SL", 1));
styles.push(new StyleLevel("SL", 2));
const props = {
captionLabel: "A",
entriesFromBookmark: "B",
captionLabelIncludingNumbers: "C",
sequenceAndPageNumbersSeparator: "D",
tcFieldIdentifier: "F",
hyperlink: true,
tcFieldLevelRange: "L",
pageNumbersEntryLevelsRange: "N",
headingStyleRange: "O",
entryAndPageNumberSeparator: "P",
seqFieldIdentifierForPrefix: "S",
stylesWithLevels: styles,
useAppliedParagraphOutlineLevel: true,
preserveTabInEntries: true,
preserveNewLineInEntries: true,
hideTabAndPageNumbersInWebView: true,
};
const toc = new TableOfContents("Summary", props);
const tree = new Formatter().format(toc);
expect(tree).to.be.deep.equal(COMPLETE_TOC);
});
});
});
const DEFAULT_TOC = {
"w:sdt": [
{
"w:sdtPr": [
{
"w:alias": {
_attr: {
"w:val": "Table of Contents",
},
},
},
],
},
{
"w:sdtContent": [
{
"w:p": [
{
"w:r": [
{
"w:fldChar": {
_attr: {
"w:fldCharType": "begin",
"w:dirty": true,
},
},
},
{
"w:instrText": [
{
_attr: {
"xml:space": "preserve",
},
},
"TOC",
],
},
{
"w:fldChar": {
_attr: {
"w:fldCharType": "separate",
},
},
},
],
},
],
},
{
"w:p": [
{
"w:r": [
{
"w:fldChar": {
_attr: {
"w:fldCharType": "end",
},
},
},
],
},
],
},
],
},
],
};
const COMPLETE_TOC = {
"w:sdt": [
{
"w:sdtPr": [
{
"w:alias": {
_attr: {
"w:val": "Summary",
},
},
},
],
},
{
"w:sdtContent": [
{
"w:p": [
{
"w:r": [
{
"w:fldChar": {
_attr: {
"w:fldCharType": "begin",
"w:dirty": true,
},
},
},
{
"w:instrText": [
{
_attr: {
"xml:space": "preserve",
},
},
'TOC \\a "A" \\b "B" \\c "C" \\d "D" \\f "F" \\h \\l "L" \\n "N" \\o "O" \\p "P" \\s "S" \\t "SL;1;SL;2" \\u \\w \\x \\z',
],
},
{
"w:fldChar": {
_attr: {
"w:fldCharType": "separate",
},
},
},
],
},
],
},
{
"w:p": [
{
"w:r": [
{
"w:fldChar": {
_attr: {
"w:fldCharType": "end",
},
},
},
],
},
],
},
],
},
],
};