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
276 lines
11 KiB
TypeScript
276 lines
11 KiB
TypeScript
import { expect } from "chai";
|
|
|
|
import { ExternalStylesFactory } from "./external-styles-factory";
|
|
|
|
describe("External styles factory", () => {
|
|
let externalStyles;
|
|
|
|
beforeEach(() => {
|
|
externalStyles = `
|
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
<w:styles xmlns:mc="first" xmlns:r="second">
|
|
<w:docDefaults>
|
|
<w:rPrDefault>
|
|
<w:rPr>
|
|
<w:rFonts w:ascii="Arial" w:eastAsiaTheme="minorHAnsi" w:hAnsi="Arial" w:cstheme="minorHAnsi"/>
|
|
<w:lang w:val="en-US" w:eastAsia="en-US" w:bidi="ar-SA"/>
|
|
</w:rPr>
|
|
</w:rPrDefault>
|
|
<w:pPrDefault>
|
|
<w:pPr>
|
|
<w:spacing w:after="160" w:line="259" w:lineRule="auto"/>
|
|
</w:pPr>
|
|
</w:pPrDefault>
|
|
</w:docDefaults>
|
|
|
|
<w:latentStyles w:defLockedState="1" w:defUIPriority="99">
|
|
</w:latentStyles>
|
|
|
|
<w:style w:type="paragraph" w:default="1" w:styleId="Normal">
|
|
<w:name w:val="Normal"/>
|
|
<w:qFormat/>
|
|
</w:style>
|
|
|
|
<w:style w:type="paragraph" w:styleId="Heading1">
|
|
<w:name w:val="heading 1"/>
|
|
<w:basedOn w:val="Normal"/>
|
|
<w:pPr>
|
|
<w:keepNext/>
|
|
<w:keepLines/>
|
|
|
|
<w:pBdr>
|
|
<w:bottom w:val="single" w:sz="4" w:space="1" w:color="auto"/>
|
|
</w:pBdr>
|
|
</w:pPr>
|
|
</w:style>
|
|
</w:styles>`;
|
|
});
|
|
|
|
describe("#parse", () => {
|
|
it("should parse w:styles attributes", () => {
|
|
// tslint:disable-next-line:no-any
|
|
const importedStyle = new ExternalStylesFactory().newInstance(externalStyles) as any;
|
|
|
|
expect(importedStyle.rootKey).to.equal("w:styles");
|
|
expect(importedStyle.root[0]._attr).to.deep.equal({
|
|
"xmlns:mc": "first",
|
|
"xmlns:r": "second",
|
|
});
|
|
});
|
|
|
|
it("should parse other child elements of w:styles", () => {
|
|
// tslint:disable-next-line:no-any
|
|
const importedStyle = new ExternalStylesFactory().newInstance(externalStyles) as any;
|
|
expect(importedStyle.root[1]).to.deep.equal({
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: {
|
|
"w:ascii": "Arial",
|
|
"w:cstheme": "minorHAnsi",
|
|
"w:eastAsiaTheme": "minorHAnsi",
|
|
"w:hAnsi": "Arial",
|
|
},
|
|
rootKey: "_attr",
|
|
},
|
|
],
|
|
rootKey: "w:rFonts",
|
|
},
|
|
{
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: {
|
|
"w:bidi": "ar-SA",
|
|
"w:eastAsia": "en-US",
|
|
"w:val": "en-US",
|
|
},
|
|
rootKey: "_attr",
|
|
},
|
|
],
|
|
rootKey: "w:lang",
|
|
},
|
|
],
|
|
rootKey: "w:rPr",
|
|
},
|
|
],
|
|
rootKey: "w:rPrDefault",
|
|
},
|
|
{
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: {
|
|
"w:after": "160",
|
|
"w:line": "259",
|
|
"w:lineRule": "auto",
|
|
},
|
|
rootKey: "_attr",
|
|
},
|
|
],
|
|
rootKey: "w:spacing",
|
|
},
|
|
],
|
|
rootKey: "w:pPr",
|
|
},
|
|
],
|
|
rootKey: "w:pPrDefault",
|
|
},
|
|
],
|
|
rootKey: "w:docDefaults",
|
|
});
|
|
expect(importedStyle.root[2]).to.deep.equal({
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: {
|
|
"w:defLockedState": "1",
|
|
"w:defUIPriority": "99",
|
|
},
|
|
rootKey: "_attr",
|
|
},
|
|
],
|
|
rootKey: "w:latentStyles",
|
|
});
|
|
});
|
|
|
|
it("should parse styles elements", () => {
|
|
// tslint:disable-next-line:no-any
|
|
const importedStyle = new ExternalStylesFactory().newInstance(externalStyles) as any;
|
|
|
|
expect(importedStyle.root.length).to.equal(5);
|
|
expect(importedStyle.root[3]).to.deep.equal({
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: {
|
|
"w:default": "1",
|
|
"w:styleId": "Normal",
|
|
"w:type": "paragraph",
|
|
},
|
|
rootKey: "_attr",
|
|
},
|
|
{
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: {
|
|
"w:val": "Normal",
|
|
},
|
|
rootKey: "_attr",
|
|
},
|
|
],
|
|
rootKey: "w:name",
|
|
},
|
|
{
|
|
deleted: false,
|
|
root: [],
|
|
rootKey: "w:qFormat",
|
|
},
|
|
],
|
|
rootKey: "w:style",
|
|
});
|
|
|
|
expect(importedStyle.root[4]).to.deep.equal({
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: {
|
|
"w:styleId": "Heading1",
|
|
"w:type": "paragraph",
|
|
},
|
|
rootKey: "_attr",
|
|
},
|
|
{
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: {
|
|
"w:val": "heading 1",
|
|
},
|
|
rootKey: "_attr",
|
|
},
|
|
],
|
|
rootKey: "w:name",
|
|
},
|
|
{
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: {
|
|
"w:val": "Normal",
|
|
},
|
|
rootKey: "_attr",
|
|
},
|
|
],
|
|
rootKey: "w:basedOn",
|
|
},
|
|
{
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: [],
|
|
rootKey: "w:keepNext",
|
|
},
|
|
{
|
|
deleted: false,
|
|
root: [],
|
|
rootKey: "w:keepLines",
|
|
},
|
|
{
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: [
|
|
{
|
|
deleted: false,
|
|
root: {
|
|
"w:color": "auto",
|
|
"w:space": "1",
|
|
"w:sz": "4",
|
|
"w:val": "single",
|
|
},
|
|
rootKey: "_attr",
|
|
},
|
|
],
|
|
rootKey: "w:bottom",
|
|
},
|
|
],
|
|
rootKey: "w:pBdr",
|
|
},
|
|
],
|
|
rootKey: "w:pPr",
|
|
},
|
|
],
|
|
rootKey: "w:style",
|
|
});
|
|
});
|
|
});
|
|
});
|