Skip to content

Commit

Permalink
Merge branch 'master' into feature/minimalist
Browse files Browse the repository at this point in the history
  • Loading branch information
remy committed Mar 20, 2012
2 parents 282934a + 2ab121c commit da4bd56
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 43 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.9.11
2.9.12
2 changes: 1 addition & 1 deletion js/render/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var ie = (!+"\v1");

function set(el, props, hover) {
for (prop in props) {
for (var prop in props) {
el.style[prop] = props[prop];
}
}
Expand Down
39 changes: 22 additions & 17 deletions js/render/live.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,32 @@ function renderLivePreview() {
document = frame.contentDocument || frame.contentWindow.document,
window = document.defaultView || document.parentWindow,
d = new Date();

if (!useCustomConsole) console.log('--- refreshing live preview @ ' + [two(d.getHours()),two(d.getMinutes()),two(d.getSeconds())].join(':') + ' ---');

// strip autofocus from the markup - prevents the focus switching out of the editable area
source = source.replace(/(<.*?\s)(autofocus)/g, '$1');

document.open();

if (debug) {
document.write('<pre>' + source.replace(/[<>&]/g, function (m) {
if (m == '<') return '&lt;';
if (m == '>') return '&gt;';
if (m == '"') return '&quot;';
}) + '</pre>');
} else {
// nullify the blocking functions
// IE requires that this is done in the script, rather than off the window object outside of the doc.write
document.write('<script>window.print=function(){};window.alert=function(){};window.prompt=function(){};window.confirm=function(){};</script>');
document.write(source);
}
document.close();
// this setTimeout allows the iframe to be rendered before our code
// runs - thus allowing us access to the innerWidth, et al
setTimeout(function () {
document.open();

if (debug) {
document.write('<pre>' + source.replace(/[<>&]/g, function (m) {
if (m == '<') return '&lt;';
if (m == '>') return '&gt;';
if (m == '"') return '&quot;';
}) + '</pre>');
} else {
// nullify the blocking functions
// IE requires that this is done in the script, rather than off the window object outside of the doc.write
document.write('<script>window.print=function(){};window.alert=function(){};window.prompt=function(){};window.confirm=function(){};</script>');
document.write(source);
}
document.close();

}, 10);
}

$live.find('.close').click(function () {
Expand Down
57 changes: 33 additions & 24 deletions js/render/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ var useCustomConsole = !(function () {
return ok;
})();

var re = {
docReady: /\$\(document\)\.ready/,
console: /(^.|\b)console\./g,
script: /<\/script/ig,
code: /%code%/,
title: /<title>(.*)<\/title>/i,
winLoad: /window\.onload\s*=/
}

function getPreparedCode() {
var parts = [],
source = '',
Expand All @@ -22,26 +31,25 @@ function getPreparedCode() {
try {
js = editors.javascript.getCode();
} catch (e) {}


// redirect JS console logged to our custom log while debugging
if (consoleTest.test(js)) {
if (useCustomConsole) {
js = js.replace(/(^.|\b)console\./g, '_console.');
js = js.replace(re.console, '_console.');
} else {
js = js.replace(/(^.|\b)console\./g, 'window.top.console.');
js = js.replace(re.console, 'window.top.console.');
}
}

// escape any script tags in the JS code, because that'll break the mushing together
js = js.replace(/<\/script/ig, '<\\/script');
js = js.replace(re.script, '<\\/script');

// note that I'm using split and reconcat instead of replace, because if the js var
// contains '$$' it's replaced to '$' - thus breaking Prototype code. This method
// gets around the problem.
if (!$.trim(source)) {
source = "<pre>\n" + js + "</pre>";
} else if (/%code%/.test(source)) {
} else if (re.code.test(source)) {
parts = source.split('%code%');
source = parts[0] + js + parts[1];
} else if (js) {
Expand All @@ -51,9 +59,7 @@ function getPreparedCode() {
parts.push(source.substring(source.lastIndexOf('</body>')));

source = parts[0];

close = parts.length == 2 && parts[1] ? parts[1] : '';

}
if (useCustomConsole) {
source += "<script src=\"http://jsbin.com/js/render/console.js\"></script>\n<script>\n";
Expand All @@ -63,12 +69,12 @@ function getPreparedCode() {
}

// specific change for rendering $(document).ready() because iframes doesn't trigger ready (TODO - really test in IE, may have been fixed...)
if (/\$\(document\)\.ready/.test(source)) {
source = source.replace(/\$\(document\)\.ready/, 'window.onload = ');
if (re.docReady.test(source)) {
source = source.replace(re.docReady, 'window.onload = ');
}

// read the element out of the source code and plug it in to our document.title
var newDocTitle = source.match(/<title>(.*)<\/title>/i);
var newDocTitle = source.match(re.title);
if (newDocTitle !== null && newDocTitle[1] !== documentTitle) {
documentTitle = newDocTitle[1];
updateTitle(!/ \[unsaved\]/.test(document.title));
Expand All @@ -81,17 +87,20 @@ function renderPreview() {
var doc = $('#preview iframe')[0],
win = doc.contentDocument || doc.contentWindow.document,
source = getPreparedCode();

win.open();

if (debug) {
win.write('<pre>' + source.replace(/[<>&]/g, function (m) {
if (m == '<') return '&lt;';
if (m == '>') return '&gt;';
if (m == '"') return '&quot;';
}) + '</pre>');
} else {
win.write(source);
}
win.close();

// this setTimeout allows the iframe to be rendered before our code
// runs - thus allowing us access to the innerWidth, et al
setTimeout(function () {
win.open();
if (debug) {
win.write('<pre>' + source.replace(/[<>&]/g, function (m) {
if (m == '<') return '&lt;';
if (m == '>') return '&gt;';
if (m == '"') return '&quot;';
}) + '</pre>');
} else {
win.write(source);
}
win.close();
}, 10);
}

0 comments on commit da4bd56

Please sign in to comment.