From e549492c934d615189b56f179cc6212267f3e972 Mon Sep 17 00:00:00 2001 From: megawac Date: Fri, 17 Jan 2014 15:59:16 -0500 Subject: [PATCH 1/2] HTML escaping, remove uncompleted fields, templates support using key multiple times, refactoring --- js/app.js | 84 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 26 deletions(-) diff --git a/js/app.js b/js/app.js index 34cdfb6..9dea371 100644 --- a/js/app.js +++ b/js/app.js @@ -1,25 +1,55 @@ window.app = ( function( window, $, snack ){ 'use strict'; + + //From Handlebars.Utils.escapeExpression + var escape = { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + '\'': ''', + '`': '`' + }; + var badChars = /[&<>"'`]/g; + var possible = /[&<>"'`]/; + + var escapeChar = function(chr) { + return escape[chr] || chr; + }; + var escapeStr = function(string) { + if (!possible.test(string)) { + return string; + } + return string.replace(badChars, escapeChar); + }; - var navHeight = 0; + var format = function(str, context) { + return str.replace(/\$\{([^{}]+)\}/g, function(match, key) { + if (context.hasOwnProperty(key)) { + return escapeStr(context[key]); + } else { + return ''; + } + }); + }; + + var remove = function(ele) { + ele.parentNode.removeChild(ele); + return ele; + }; var parseData = function( data ) { - var json = snack.parseJSON( data ), - key = null, - ele = null, - val = null, - sections = $('[data-content]'), - section = null; + var json = snack.parseJSON(data), + ele = null, + sections = $('[data-content]'); document.title = 'Resumé for ' + json.name; - $( '#contact' )[0].innerHTML = json.contact; + // $( '#contact' )[0].innerHTML = json.contact; - for( key in json ){ - - val = json[ key ]; - ele = $( '[data-content=' + key + ']' )[ 0 ]; + snack.each(json, function(val, key) { + ele = $('[data-content=' + key + ']')[0]; if( ele ){ //standard keys just populate the section @@ -37,12 +67,10 @@ window.app = ( function( window, $, snack ){ //template found, key is a string if( template && typeof item === 'string' ){ - - frag = frag + template.replace('${' + key + '}', item ); + frag += template.replace(new RegExp('\\$\\{' + key + '\\}', 'g'), item); } else if( template && typeof item === 'object' ){ - - var cur = template; + var cur = format(template, item); for( var innerKey in item ){ cur = cur.replace('${' + innerKey + '}', item[ innerKey ] ); @@ -52,23 +80,27 @@ window.app = ( function( window, $, snack ){ cur = cur.replace('
  • ','
  • '); } - frag = frag + cur; + frag += cur; } }); ele.innerHTML = frag; + + //remove unused fields + snack.each($('li .content', ele), function(child) { + if (!/\w/.test(child.innerHTML)) { + remove(child.parentNode); + } + }); } } - } + }); - for( var i = 0; i < sections.length; i++ ){ - section = sections[ i ]; - - if( !section.innerHTML.match( /\w/ ) ){ - - section.parentNode.style.display = 'none'; - } - } + snack.each(sections, function(section) { + if (!/\w/.test(section.innerHTML)) { + remove(section.parentNode); + } + }); }; return { From 2ee4f9a6547cb5fbf94a08ccc6be4769bc308ee3 Mon Sep 17 00:00:00 2001 From: megawac Date: Fri, 17 Jan 2014 16:00:18 -0500 Subject: [PATCH 2/2] Cleaner index.html and seperate app.js from snack+sizzle --- index.html | 7 +- js/jsonme.js | 0 js/jsonme.min.js | 8 - js/sizzle.js | 1688 ---------------------------------------- js/snack-sizzle.min.js | 10 + js/snack.min.js | 10 - 6 files changed, 14 insertions(+), 1709 deletions(-) delete mode 100644 js/jsonme.js delete mode 100644 js/jsonme.min.js delete mode 100755 js/sizzle.js create mode 100644 js/snack-sizzle.min.js delete mode 100644 js/snack.min.js diff --git a/index.html b/index.html index 3b33d6a..dc027fc 100644 --- a/index.html +++ b/index.html @@ -2,7 +2,7 @@ - + @@ -27,7 +27,7 @@

    -
    +
    @@ -196,7 +196,8 @@

    Education

  • ${name}
  • - + +