Skip to content

Commit

Permalink
feat: create processing instruction node serialization feature accord…
Browse files Browse the repository at this point in the history
…ing to the w3c specification
  • Loading branch information
teclone committed Jul 8, 2018
1 parent f9e903b commit c8b2150
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/modules/Serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = `<?${node.target} ${node.data}?>`;

//STEP 4
return markup;
}

/**
* generate document type serialization
*@param {DocumentType} docType - the document type node
Expand Down

0 comments on commit c8b2150

Please sign in to comment.