easy-template-x
Advanced tools
Comparing version 3.2.0 to 3.2.1
import { IMap } from '../types'; | ||
export declare enum XmlNodeType { | ||
Text = "Text", | ||
General = "General" | ||
General = "General", | ||
Comment = "Comment" | ||
} | ||
export declare type XmlNode = XmlTextNode | XmlGeneralNode; | ||
export declare type XmlNode = XmlTextNode | XmlGeneralNode | XmlCommentNode; | ||
export interface XmlNodeBase { | ||
@@ -15,2 +16,3 @@ nodeType: XmlNodeType; | ||
export declare const TEXT_NODE_NAME = "#text"; | ||
export declare const COMMENT_NODE_NAME = "#comment"; | ||
export interface XmlTextNode extends XmlNodeBase { | ||
@@ -21,2 +23,7 @@ nodeType: XmlNodeType.Text; | ||
} | ||
export interface XmlCommentNode extends XmlNodeBase { | ||
nodeType: XmlNodeType.Comment; | ||
nodeName: typeof COMMENT_NODE_NAME; | ||
commentContent: string; | ||
} | ||
export interface XmlGeneralNode extends XmlNodeBase { | ||
@@ -29,2 +36,3 @@ nodeType: XmlNodeType.General; | ||
createGeneralNode(name: string): XmlGeneralNode; | ||
createCommentNode(text?: string): XmlCommentNode; | ||
encodeValue(str: string): string; | ||
@@ -34,2 +42,3 @@ serialize(node: XmlNode): string; | ||
isTextNode(node: XmlNode): node is XmlTextNode; | ||
isCommentNode(node: XmlNode): node is XmlCommentNode; | ||
cloneNode<T extends XmlNode>(node: T, deep: boolean): T; | ||
@@ -36,0 +45,0 @@ insertBefore(newNode: XmlNode, referenceNode: XmlNode): void; |
{ | ||
"name": "easy-template-x", | ||
"version": "3.2.0", | ||
"version": "3.2.1", | ||
"description": "Generate docx documents from templates, in Node or in the browser.", | ||
@@ -67,2 +67,3 @@ "keywords": [ | ||
"jest": "28.1.2", | ||
"jest-html-reporters": "3.1.7", | ||
"jest-junit": "14.0.0", | ||
@@ -69,0 +70,0 @@ "lorem-ipsum": "2.0.8", |
@@ -124,3 +124,3 @@ # easy-template-x | ||
Checkout this [live demo](https://codesandbox.io/s/easy-template-x-demo-x4ppu?fontsize=14&module=%2Findex.ts) on CodeSandbox 😎 | ||
Checkout this [live demo](https://codesandbox.io/p/sandbox/easy-template-x-demo-x4ppu?file=%2Findex.ts) on CodeSandbox 😎 | ||
@@ -127,0 +127,0 @@ ## Plugins |
@@ -117,3 +117,6 @@ import { MalformedFileError } from '../errors'; | ||
const docRoot = await this.mainDocument.xmlRoot(); | ||
const body = docRoot.childNodes[0]; | ||
const body = docRoot.childNodes.find(node => node.nodeName == 'w:body'); | ||
if (body == null) | ||
return null; | ||
const sectionProps = last(body.childNodes.filter(node => node.nodeType === XmlNodeType.General)); | ||
@@ -120,0 +123,0 @@ if (sectionProps.nodeName != 'w:sectPr') |
@@ -211,4 +211,6 @@ import { XmlGeneralNode, XmlNode, XmlParser, XmlTextNode } from '../xml'; | ||
if (curWordTextNode.nodeName !== DocxParser.TEXT_NODE) | ||
if (curWordTextNode.nodeName !== DocxParser.TEXT_NODE) { | ||
curWordTextNode = curWordTextNode.nextSibling; | ||
continue; | ||
} | ||
@@ -215,0 +217,0 @@ // move text to first node |
@@ -7,6 +7,7 @@ import { MissingArgumentError } from '../errors'; | ||
Text = "Text", | ||
General = "General" | ||
General = "General", | ||
Comment = "Comment", | ||
} | ||
export type XmlNode = XmlTextNode | XmlGeneralNode; | ||
export type XmlNode = XmlTextNode | XmlGeneralNode | XmlCommentNode; | ||
@@ -22,2 +23,3 @@ export interface XmlNodeBase { | ||
export const TEXT_NODE_NAME = '#text'; // see: https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName | ||
export const COMMENT_NODE_NAME = '#comment'; | ||
@@ -30,2 +32,8 @@ export interface XmlTextNode extends XmlNodeBase { | ||
export interface XmlCommentNode extends XmlNodeBase { | ||
nodeType: XmlNodeType.Comment; | ||
nodeName: typeof COMMENT_NODE_NAME; | ||
commentContent: string; | ||
} | ||
export interface XmlGeneralNode extends XmlNodeBase { | ||
@@ -57,2 +65,10 @@ nodeType: XmlNodeType.General; | ||
createCommentNode(text?: string): XmlCommentNode { | ||
return { | ||
nodeType: XmlNodeType.Comment, | ||
nodeName: COMMENT_NODE_NAME, | ||
commentContent: text | ||
}; | ||
}, | ||
// | ||
@@ -89,2 +105,6 @@ // serialization | ||
if (this.isCommentNode(node)) { | ||
return `<!-- ${this.encodeValue(node.commentContent || '')} -->`; | ||
} | ||
// attributes | ||
@@ -133,21 +153,23 @@ let attributes = ''; | ||
// basic properties | ||
if (domNode.nodeType === domNode.TEXT_NODE) { | ||
xmlNode = this.createTextNode(domNode.textContent); | ||
} else { | ||
xmlNode = this.createGeneralNode(domNode.nodeName); | ||
// attributes | ||
if (domNode.nodeType === domNode.ELEMENT_NODE) { | ||
switch (domNode.nodeType) { | ||
case domNode.TEXT_NODE: | ||
xmlNode = this.createTextNode(domNode.textContent); | ||
break; | ||
case domNode.COMMENT_NODE: | ||
xmlNode = this.createCommentNode(domNode.textContent?.trim()); | ||
break; | ||
case domNode.ELEMENT_NODE: | ||
const generalNode = xmlNode = this.createGeneralNode(domNode.nodeName); | ||
const attributes = (domNode as Element).attributes; | ||
if (attributes) { | ||
(xmlNode as XmlGeneralNode).attributes = {}; | ||
generalNode.attributes = {}; | ||
for (let i = 0; i < attributes.length; i++) { | ||
const curAttribute = attributes.item(i); | ||
(xmlNode as XmlGeneralNode).attributes[curAttribute.name] = curAttribute.value; | ||
generalNode.attributes[curAttribute.name] = curAttribute.value; | ||
} | ||
} | ||
} | ||
break; | ||
default: | ||
xmlNode = this.createGeneralNode(domNode.nodeName); | ||
break; | ||
} | ||
@@ -192,2 +214,12 @@ | ||
isCommentNode(node: XmlNode): node is XmlCommentNode { | ||
if (node.nodeType === XmlNodeType.Comment || node.nodeName === COMMENT_NODE_NAME) { | ||
if (!(node.nodeType === XmlNodeType.Comment && node.nodeName === COMMENT_NODE_NAME)) { | ||
throw new Error(`Invalid comment node. Type: '${node.nodeType}', Name: '${node.nodeName}'.`); | ||
} | ||
return true; | ||
} | ||
return false; | ||
}, | ||
cloneNode<T extends XmlNode>(node: T, deep: boolean): T { | ||
@@ -194,0 +226,0 @@ if (!node) |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
396547
9909
29