Skip to content

Commit

Permalink
JSON-px tool: now strips whitespace between HTML tags
Browse files Browse the repository at this point in the history
  • Loading branch information
premasagar committed Feb 5, 2010
1 parent 8e015ec commit acbe8d6
Show file tree
Hide file tree
Showing 3 changed files with 851 additions and 9 deletions.
19 changes: 13 additions & 6 deletions tools/jsonpx.html
@@ -1,11 +1,11 @@
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset=utf-8 />
<meta charset=utf-8>
<title>jsonp-x</title>
<script src=http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js></script>
<script src=../jquery.js></script>
<script src=jsonpx.js></script>

<style>
label, textarea, input, button {
display:block;
Expand All @@ -24,18 +24,25 @@ <h1>JSON-PX-ify your code</h1>
<textarea id=source rows=30 cols=80></textarea>

<label for=callbackName>Callback name</label>
<input id=callbackName value=sqwidget.template />
<input id=callbackName value=mishorts.template>

<label for=stripwhitespace>Strip HTML whitespace</label>
<input id=stripwhitespace type=checkbox checked>

<button id=jsonpxify>Go</button>

<script>
$('#source').val('');
$('#source')
.val('')
.focus(function(){
$(this).select();
});


$('#jsonpxify')
.click(function(){
$('#source').val(
jsonpx($('#source').val(), $('#callbackName').val())
jsonpx($('#source').val(), $('#callbackName').val(), $('#stripwhitespace')[0].checked)
);
});
</script>
Expand Down
25 changes: 22 additions & 3 deletions tools/jsonpx.js
@@ -1,8 +1,27 @@
// E.g. jsonpx(htmlTemplate, 'myFunc');
function jsonpx(html, callbackName){

function jsonpx(html, callbackName, stripwhitespace){
function escape(txt){
return txt.replace(/'/g, "\\'").replace(/(?=\n)/g, '\\');
var s = '[\\0\\t\\n\\v\\f\\r\\s]';

if (stripwhitespace){
txt = txt
.replace(new RegExp('>' + s + '+<', 'g'), '><')
.replace(new RegExp('(<style[^>]*>)' + s + '+(.)', 'g'), '$1$2')
.replace(new RegExp('(<script[^>]*>)' + s + '+(.)', 'g'), '$1$2');
}
txt = txt
.replace(/'/g, "\\'")
.replace(/(?=\n)/g, '\\');

if (stripwhitespace){
txt = txt
.replace(new RegExp('(;?)' + s + '+<\/script>' + s + '*', 'g'), '$1</script>')
.replace(new RegExp('(;?)' + s + '*<\/script>' + s + '+', 'g'), '$1</script>')
.replace(new RegExp('(}?)' + s + '+<\/style>' + s + '*', 'g'), '$1</style>')
.replace(new RegExp('(}?)' + s + '*<\/style>' + s + '+', 'g'), '$1</style>');
}
return txt;
}

return callbackName + "('" + escape(html) + "');";
Expand Down

0 comments on commit acbe8d6

Please sign in to comment.