all files / es6/modules/ rawxml.js

100% Statements 36/36
100% Branches 15/15
100% Functions 8/8
100% Lines 35/35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68                   37×   31×             90×     90× 90×     302× 302× 294×       194× 194×   193×     5595× 5589×           90×    
const DocUtils = require("../doc-utils");
const Errors = require("../errors");
 
const moduleName = "rawxml";
const wrapper = require("../module-wrapper");
 
function throwRawTagShouldBeOnlyTextInParagraph(options) {
	const err = new Errors.XTTemplateError("Raw tag should be the only text in paragraph");
	const tag = options.part.value;
	err.properties = {
		id: "raw_xml_tag_should_be_only_text_in_paragraph",
		explanation: `The tag ${tag}`,
		xtag: options.part.value,
		paragraphParts: options.paragraphParts,
	};
	throw err;
}
 
function getInner({part, left, right, postparsed, index}) {
	const paragraphParts = postparsed.slice(left + 1, right);
	paragraphParts.forEach(function (p, i) {
		if (i === index - left - 1) {
			return;
		}
		if (p.type === "placeholder" || (p.type === "content" && p.position === "insidetag")) {
			throwRawTagShouldBeOnlyTextInParagraph({paragraphParts, part});
		}
	});
	return part;
}
 
class RawXmlModule {
	constructor() {
		this.name = "RawXmlModule";
	}
	optionsTransformer(options, docxtemplater) {
		this.fileTypeConfig = docxtemplater.fileTypeConfig;
		return options;
	}
	parse(placeHolderContent) {
		const type = "placeholder";
		if (placeHolderContent[0] !== "@") {
			return null;
		}
		return {type, value: placeHolderContent.substr(1), module: moduleName};
	}
	postparse(parsed) {
		const pp = DocUtils.traits.expandToOne(parsed, {moduleName, getInner, expandTo: this.fileTypeConfig.tagRawXml});
		if (pp.errors.length > 0) {
			Errors.throwMultiError(pp.errors);
		}
		return pp.postparsed;
	}
	render(part, options) {
		if (part.module !== moduleName) {
			return null;
		}
		let value = options.scopeManager.getValue(part.value);
		if (value == null) {
			value = options.nullGetter(part);
		}
		return {value};
	}
}
 
module.exports = () => wrapper(new RawXmlModule());