parsers.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true */
  9. 'use strict';
  10. var windowObject_ =
  11. typeof window === 'undefined' ? Object.create(null) : window;
  12. module.exports = {
  13. /**
  14. * Converts a string to an XML document. For internal use only.
  15. *
  16. * @function xmlFromString
  17. * @private
  18. * @param {string}
  19. * str The string to convert.
  20. * @returns {string} The XML document from the conversion.
  21. */
  22. xmlFromString: function(str) {
  23. if (windowObject_.ActiveXObject) {
  24. // Remove null terminating character if exists
  25. var strNoNull = str.replace(/\x00+$/, '');
  26. var xmlDoc = new windowObject_.ActiveXObject('Microsoft.XMLDOM');
  27. xmlDoc.async = false;
  28. xmlDoc.loadXML(strNoNull);
  29. return xmlDoc;
  30. } else if (typeof windowObject_.DOMParser !== 'undefined') {
  31. return (new windowObject_.DOMParser()).parseFromString(str, 'text/xml');
  32. } else {
  33. // No native parser, use a JS one.
  34. var DOMParser = require('xmldom').DOMParser;
  35. return new DOMParser().parseFromString(str, 'text/xml');
  36. }
  37. },
  38. /**
  39. * Converts an XML document to a string. For internal use only.
  40. *
  41. * @function xmlToString
  42. * @private
  43. * @param {string}
  44. * xmlDoc xml document to convert, as string.
  45. * @param {boolean}
  46. * removeSelfClosingTags removes self closing tags to explicit
  47. * tags. Example: <tag/> to <tag></tag>
  48. * @returns {string} The string from the conversion.
  49. */
  50. xmlToString: function(xmlDoc, removeSelfClosingTags) {
  51. var result = this.xmlToStringIeCompatible(xmlDoc);
  52. if (removeSelfClosingTags) {
  53. result = this.xmlRemoveSelfClosingTags(result);
  54. }
  55. return result;
  56. },
  57. /**
  58. * Removes all self-closing tags from some XML data. For internal use only.
  59. *
  60. * @function xmlRemoveSelfClosingTags
  61. * @private
  62. * @param {string}
  63. * data The XML data before removing the tags.
  64. * @returns {string} The XML data after removing the tags.
  65. */
  66. xmlRemoveSelfClosingTags: function(data) {
  67. var split = data.split('/>');
  68. var newXml = '';
  69. for (var i = 0; i < split.length - 1; i++) {
  70. var edsplit = split[i].split('<');
  71. newXml +=
  72. split[i] + '></' + edsplit[edsplit.length - 1].split(' ')[0] + '>';
  73. }
  74. return newXml + split[split.length - 1];
  75. },
  76. /**
  77. * Converts an XML document to an IE compatible string. For internal use
  78. * only.
  79. *
  80. * @function xmlToStringIeCompatible
  81. * @private
  82. * @param {string}
  83. * data The XML document to convert.
  84. * @returns {string} The IE compatible string.
  85. */
  86. xmlToStringIeCompatible: function(xmlNode) {
  87. if (windowObject_.XMLSerializer) {
  88. return (new windowObject_.XMLSerializer()).serializeToString(xmlNode);
  89. } else if (xmlNode.xml) {
  90. return xmlNode.xml;
  91. } else {
  92. var XMLSerializer = require('xmldom').XMLSerializer;
  93. return new XMLSerializer().serializeToString(xmlNode);
  94. }
  95. },
  96. /**
  97. * Removes all newline characters from some data. For internal use only.
  98. *
  99. * @function removeNewLineChars
  100. * @private
  101. * @param {string}
  102. * data data from which to remove all newline characters, as
  103. * string.
  104. * @returns {string} data with all newline characters removed, as string.
  105. */
  106. removeNewLineChars: function(data) {
  107. return data.replace(/(\r\n|\n|\r)/gm, '');
  108. },
  109. /**
  110. * Removes all carriage return characters from some data. For internal use
  111. * only.
  112. *
  113. * @param data
  114. * Data from which to remove all carriage return characters, as
  115. * string.
  116. * @return Data with all carriage return characters removed, as string.
  117. */
  118. removeCarriageReturnChars: function(data) {
  119. return data.replace(/\r/gm);
  120. },
  121. /**
  122. * Removes the null terminating character from some data. For internal use
  123. * only.
  124. *
  125. * @function removeNullTerminatingChar
  126. * @private
  127. * @param {string}
  128. * data Data from which to remove the null terminating character,
  129. * as string.
  130. * @returns {string} data with null terminating character removed, as
  131. * string.
  132. */
  133. removeNullTerminatingChar: function(data) {
  134. return data.replace(/\x00+$/, '');
  135. },
  136. /**
  137. * Removes the XML header from a string representing an XML. For internal
  138. * use only.
  139. *
  140. * @function removeXmlHeaderFromString
  141. * @private
  142. * @param data
  143. * The XML string from which to remove the XML header.
  144. * @return XML string with the XML header removed.
  145. */
  146. removeXmlHeaderFromString: function(data) {
  147. return data.replace('<?xml version="1.0" encoding="UTF-8"?>', '');
  148. },
  149. /**
  150. * Removes the signature node from a string representing an XML. For
  151. * internal use only.
  152. *
  153. * @function removeXmlSignature
  154. * @private
  155. * @param data
  156. * The XML string from which to remove the signature node.
  157. * @return XML string with the signature node removed.
  158. */
  159. removeXmlSignature: function(data) {
  160. return data.replace(/<Signature[\s\S]*?Signature>/g, '');
  161. }
  162. };