From c8b2150efce03b85f8627a0e9feb8f41cd46b021 Mon Sep 17 00:00:00 2001 From: Harrison Ifeanyichukwu Date: Sun, 8 Jul 2018 20:00:36 +0100 Subject: [PATCH] feat: create processing instruction node serialization feature according to the w3c specification --- src/modules/Serializer.js | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/modules/Serializer.js b/src/modules/Serializer.js index 599b947..66b8215 100644 --- a/src/modules/Serializer.js +++ b/src/modules/Serializer.js @@ -98,6 +98,24 @@ export default class Serializser { return this.validateChar(systemId) && !(/[']/.test(systemId) && /["]/.test(systemId)); } + /** + * validates xml processing instruction target value + *@param {string} target - the target text + *@returns {boolean} + */ + validatePITarget(target) { + return this.validateChar(target) && target.indexOf(':') < 0 && target.toLowerCase() !== 'xml'; + } + + /** + * validates xml processing instruction data value + *@param {string} data - the data value + *@returns {boolean} + */ + validatePIData(data) { + return this.validateChar(data) && data.indexOf('?>') < 0; + } + /** * checks if the given tuple consisting of namespaceURI and localName pair exists in the records *@param {Array} records - tuple records @@ -278,6 +296,29 @@ export default class Serializser { return result; } + /** + * serializes processing instruction node + *@param {ProcessingInstruction} node - the processing instruction node + *@param {boolean} requireWellFormed - boolean value indicating if well formedness is a + * requirement + *@returns {string} + */ + serializeProcessingInstruction(node, requireWellFormed) { + //STEP 1 + if (requireWellFormed && !this.validatePITarget(node.target)) + throw new Error(node.target + ' is not a valid processing instruction target value'); + + //STEP 2 + if (requireWellFormed && !this.validatePIData(node.data)) + throw new Error(node.data + ' contains invalid processing instruction character values'); + + //STEP 4 + let markup = ``; + + //STEP 4 + return markup; + } + /** * generate document type serialization *@param {DocumentType} docType - the document type node