From d4124e6a6793daa849aacc76daecdd9eada82e12 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Fri, 17 May 2013 11:39:01 +0100 Subject: [PATCH] Make sure that the doctype is first thing on the page. --- public/js/render/live.js | 29 ++++++++++++++++++++++++++--- public/js/render/render.js | 4 ++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/public/js/render/live.js b/public/js/render/live.js index 555e6fdd..05e9dfc4 100644 --- a/public/js/render/live.js +++ b/public/js/render/live.js @@ -35,12 +35,27 @@ var iframedelay = (function () { return iframedelay; }()); +/** + * Grab the doctype from a string. + * + * Returns an object with doctype and tail keys. + */ +function getDoctype(str) { + // [\s\S] matches multiline doctypes + var doctype = (str.match(//i) || [''])[0], + tail = str.substr(doctype.length); + return { + doctype: doctype, + tail: tail + }; +} + function sendReload() { if (saveChecksum) { $.ajax({ url: jsbin.getURL() + '/reload', - data: { - code: jsbin.state.code, + data: { + code: jsbin.state.code, revision: jsbin.state.revision, checksum: saveChecksum }, @@ -127,6 +142,14 @@ function renderLivePreview(withalerts) { if (m == '"') return '"'; }) + ''); } else { + + // Make sure the doctype is the first thing in the source + var doctypeObj = getDoctype(source), + doctype = doctypeObj.doctype; + console.log('sup2'); + source = doctypeObj.tail; + combinedSource.push(doctype); + // nullify the blocking functions // IE requires that this is done in the script, rather than off the window object outside of the doc.write if (withalerts !== true) { @@ -198,7 +221,7 @@ function renderLivePreview(withalerts) { if (jsbin.embed) { // allow the iframe to be resized (function () { var dragging = false, - height = false, + height = false, $window = $(win); $(doc.documentElement).mousedown(function (event) { if (event.pageY > $window.height() - 40) { diff --git a/public/js/render/render.js b/public/js/render/render.js index 6aef2467..2b0b6fc6 100644 --- a/public/js/render/render.js +++ b/public/js/render/render.js @@ -26,7 +26,7 @@ var iframedelay = (function () { } catch (e) { iframedelay.active = true; } - + return iframedelay; }()); @@ -197,7 +197,7 @@ function getPreparedCode(nojs) { } // function renderPreview() { -// var doc = $('#preview iframe')[0], +// var doc = $('#preview iframe')[0], // win = doc.contentDocument || doc.contentWindow.document, // source = getPreparedCode();