From 9e14ed1929e9e7c2240fa438b167d135cc325086 Mon Sep 17 00:00:00 2001 From: Dibasic Date: Thu, 30 Aug 2018 10:16:25 -0500 Subject: [PATCH] Clean up processText and escape special characters Fixes #1 Escape < > and & characters. Remove duplicates from processText and organize by function. --- pen.js | 133 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 74 insertions(+), 59 deletions(-) diff --git a/pen.js b/pen.js index 8b5236c..802cdb6 100644 --- a/pen.js +++ b/pen.js @@ -1,15 +1,15 @@ $(document).ready(function() { - run(); - $('#input').bind('input change', run); + run(); + $('#input').bind('input change', run); - $('#save').click(function() { download('pencode.txt', $('#input').val()) }); + $('#save').click(function() { download('pencode.txt', $('#input').val()) }); }); function run() { - $('#output').html(processText($('#input').val() + '[field]')); - $('#output span.startLarge').nextUntil('#output span.endLarge').addBack().css('font-size', '21'); - $('#output span.startSmall').nextUntil('#output span.endSmall').addBack().css('font-size', '11'); - $('#output span.sig').css('font-style', 'italic'); + $('#output').html(processText($('#input').val() + '[field]')); + $('#output span.startLarge').nextUntil('#output span.endLarge').addBack().css('font-size', '21'); + $('#output span.startSmall').nextUntil('#output span.endSmall').addBack().css('font-size', '11'); + $('#output span.sig').css('font-style', 'italic'); } String.prototype.replaceAll = function(strReplace, strWith) { @@ -20,64 +20,79 @@ String.prototype.replaceAll = function(strReplace, strWith) { }; function processText(str) { - str = (str.replaceAll('\n', '
') - .replaceAll('[center]', '
') - .replaceAll('[/center]', '
') - .replaceAll('[br]', '
') - .replaceAll('[b]', '') - .replaceAll('[/b]', '') - .replaceAll('[i]', '') - .replaceAll('[/i]', '') - .replaceAll('[u]', '') - .replaceAll('[/u]', '') - .replaceAll('[time]', getTimeString()) - .replaceAll('[date]', getDateString()) - .replaceAll('[large]', '') - .replaceAll('[/large]', '') - .replaceAll('[large]', '') - .replaceAll('[/large]', '') - .replaceAll('[field]', '') - .replaceAll('[h1]', '

') - .replaceAll('[/h1]', '

') - .replaceAll('[h2]', '

') - .replaceAll('[/h2]', '

') - .replaceAll('[h3]', '

') - .replaceAll('[/h3]', '

') - .replaceAll('[*]', '
  • ') - .replaceAll('[hr]', '
    ') - .replaceAll('[small]', '') - .replaceAll('[/small]', '') - .replaceAll('[list]', '') - .replaceAll('[table]', '') - .replaceAll('[/table]', '
    ') - .replaceAll('[grid]', '') - .replaceAll('[/grid]', '
    ') - .replaceAll('[row]', '') - .replaceAll('[cell]', '') - .replaceAll('[logo]', '') - .replaceAll('[bluelogo]', '') - .replaceAll('[solcrest]', '') - .replaceAll('[terraseal]', '') - .replaceAll('[editorbr]', '')) - .replaceAll('[sign]', 'John Doe') - return str; + str = ( + str + .replaceAll('<', '<') + .replaceAll('>', '>') + .replaceAll('&', '&') + .replaceAll('\n', '
    ') + + .replaceAll('[center]', '
    ') + .replaceAll('[/center]', '
    ') + + .replaceAll('[br]', '
    ') + .replaceAll('[hr]', '
    ') + + .replaceAll('[b]', '') + .replaceAll('[/b]', '') + .replaceAll('[i]', '') + .replaceAll('[/i]', '') + .replaceAll('[u]', '') + .replaceAll('[/u]', '') + + .replaceAll('[time]', getTimeString()) + .replaceAll('[date]', getDateString()) + .replaceAll('[sign]', 'John Doe') + + .replaceAll('[large]', '') + .replaceAll('[/large]', '') + .replaceAll('[small]', '') + .replaceAll('[/small]', '') + + .replaceAll('[field]', '') + .replaceAll('[h1]', '

    ') + .replaceAll('[/h1]', '

    ') + .replaceAll('[h2]', '

    ') + .replaceAll('[/h2]', '

    ') + .replaceAll('[h3]', '

    ') + .replaceAll('[/h3]', '

    ') + + .replaceAll('[list]', '') + .replaceAll('[*]', '
  • ') + + .replaceAll('[table]', '') + .replaceAll('[/table]', '
    ') + .replaceAll('[grid]', '') + .replaceAll('[/grid]', '
    ') + .replaceAll('[row]', '') + .replaceAll('[cell]', '') + + .replaceAll('[logo]', '') + .replaceAll('[bluelogo]', '') + .replaceAll('[solcrest]', '') + .replaceAll('[terraseal]', '') + + .replaceAll('[editorbr]', '') + ) + + return str; } function getDateString() { - var date = new Date(); - var yyyy = date.getFullYear() + 544; - var mm = date.getMonth() + 1; - mm = mm < 10 ? "0" + mm : mm; - var dd = date.getDate(); - return yyyy + "-" + mm + "-" + dd; + var date = new Date(); + var yyyy = date.getFullYear() + 544; + var mm = date.getMonth() + 1; + mm = mm < 10 ? "0" + mm : mm; + var dd = date.getDate(); + return yyyy + "-" + mm + "-" + dd; } function getTimeString() { - var date = new Date(); - var hh = (date.getHours() < 10 ? "0" : "") + date.getHours(); - var mm = (date.getMinutes() < 10 ? "0" : "") + date.getMinutes(); - return hh + ":" + mm; + var date = new Date(); + var hh = (date.getHours() < 10 ? "0" : "") + date.getHours(); + var mm = (date.getMinutes() < 10 ? "0" : "") + date.getMinutes(); + return hh + ":" + mm; } function download(filename, text) {