util.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. var handleDecodedXML = function(decodedXml) {
  2. let rerurnXml = '';
  3. const blockArr = decodedXml.split('<');
  4. for (let i = 0; i < blockArr.length; i++) {
  5. let blockStr = blockArr[i];
  6. let handleBlockStr = '';
  7. let returnBlockStr = '';
  8. const sliceBlockStr = blockStr.split(' ');
  9. for(let j = 0; j < sliceBlockStr.length; j++) {
  10. const subBlockStr = sliceBlockStr[j];
  11. const eIndex = subBlockStr.indexOf('=');
  12. if (eIndex !== -1) {
  13. handleBlockStr += ' <span class="attr-name">' + subBlockStr.slice(0, eIndex) +'</span>' + subBlockStr.slice(eIndex);
  14. } else {
  15. handleBlockStr += subBlockStr;
  16. }
  17. }
  18. // console.log(sliceBlockStr);
  19. const blockEndIndexB = handleBlockStr.indexOf(' ');
  20. const blockEndIndexR = handleBlockStr.indexOf('&gt;');
  21. // Handle XMLTag
  22. if (blockEndIndexB === -1 && blockEndIndexR === -1) {
  23. continue;
  24. }
  25. const endBlockFlag = handleBlockStr[0] === '/';
  26. if (blockEndIndexR !== -1) {
  27. handleBlockStr += '<br>'
  28. }
  29. if (blockEndIndexR < blockEndIndexB) {
  30. returnBlockStr = '&lt;' + (endBlockFlag ? '/' : '') + '<span class="block-name">' + handleBlockStr.slice(endBlockFlag ? 1 : 0, blockEndIndexR) + '</span>' + handleBlockStr.slice(blockEndIndexR);
  31. } else if (blockEndIndexB !== -1) {
  32. returnBlockStr = '&lt;' + (endBlockFlag ? '/' : '') +'<span class="block-name">' + handleBlockStr.slice(endBlockFlag ? 1 : 0, blockEndIndexB) + '</span>' + handleBlockStr.slice(blockEndIndexB);
  33. } else if (blockEndIndexR !== -1) {
  34. returnBlockStr = '&lt;' + (endBlockFlag ? '/' : '') + '<span class="block-name">' + handleBlockStr.slice(endBlockFlag ? 1 : 0, blockEndIndexR) + '</span>' + handleBlockStr.slice(blockEndIndexR);
  35. }
  36. rerurnXml += returnBlockStr;
  37. }
  38. return rerurnXml;
  39. }
  40. var escapeMarkup = function(dangerousInput) {
  41. const dangerousString = String(dangerousInput);
  42. const matchHtmlRegExp = /["'&<>]/;
  43. const match = matchHtmlRegExp.exec(dangerousString);
  44. if (!match) {
  45. return dangerousInput;
  46. }
  47. const encodedSymbolMap = {
  48. '"': '&quot;',
  49. '\'': '&#39;',
  50. '&': '&amp;',
  51. '<': '&lt;',
  52. '>': '&gt;'
  53. };
  54. const dangerousCharacters = dangerousString.split('');
  55. const safeCharacters = dangerousCharacters.map(function (character) {
  56. return encodedSymbolMap[character] || character;
  57. });
  58. const safeString = safeCharacters.join('');
  59. return safeString;
  60. }
  61. module.exports = {
  62. handleDecodedXML,
  63. escapeMarkup
  64. }