Improve inlining of editor content.

Instead of replacing substrings, write each included file's content into a shared dictionary for lookup later. This eliminates duplication and arguably improves readability.
pull/331/head
Chad Weider 2012-01-14 21:42:47 -08:00
parent 77804673d7
commit 9c91f16c71
2 changed files with 35 additions and 47 deletions

View File

@ -155,50 +155,34 @@ exports.minifyJS = function(req, res, jsFilename)
var type = item.match(/INCLUDE_[A-Z]+/g)[0].substr("INCLUDE_".length); var type = item.match(/INCLUDE_[A-Z]+/g)[0].substr("INCLUDE_".length);
var quote = item.search("_Q") != -1;
//read the included file //read the included file
fs.readFile(filename, "utf-8", function(err, data) fs.readFile(filename, "utf-8", function(err, data)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//compress the file
if(type == "JS") if(type == "JS")
{ {
embeds[item] = "<script>\n" + compressJS([data])+ "\n\\x3c/script>"; embeds[filename] = compressJS([data]);
} }
else else
{ {
embeds[item] = "<style>" + compressCSS([data])+ "</style>"; embeds[filename] = compressCSS([data]);
} }
//do the first escape
embeds[item] = JSON.stringify(embeds[item]).replace(/'/g, "\\'").replace(/\\"/g, "\"");
embeds[item] = embeds[item].substr(1);
embeds[item] = embeds[item].substr(0, embeds[item].length-1);
//add quotes, if wished
if(quote)
{
embeds[item] = "'" + embeds[item] + "'";
}
//do the second escape
embeds[item] = JSON.stringify(embeds[item]).replace(/'/g, "\\'").replace(/\"/g, "\"");
embeds[item] = embeds[item].substr(1);
embeds[item] = embeds[item].substr(0, embeds[item].length-1);
embeds[item] = "'" + embeds[item] + "'";
callback(); callback();
}); });
}, function(err) }, function(err)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//replace the include command with the include fileValues["ace.js"] += ';\n'
for(var i in embeds) fileValues["ace.js"] +=
'Ace2Editor.EMBEDED = Ace2Editor.EMBED || {};\n'
for (var filename in embeds)
{ {
fileValues["ace.js"]=fileValues["ace.js"].replace(i, embeds[i]); fileValues["ace.js"] +=
'Ace2Editor.EMBEDED[' + JSON.stringify(filename) + '] = '
+ JSON.stringify(embeds[filename]) + ';\n';
} }
callback(); callback();

View File

@ -200,24 +200,28 @@ function Ace2Editor()
// calls to these functions ($$INCLUDE_...) are replaced when this file is processed // calls to these functions ($$INCLUDE_...) are replaced when this file is processed
// and compressed, putting the compressed code from the named file directly into the // and compressed, putting the compressed code from the named file directly into the
// source here. // source here.
var $$INCLUDE_CSS = function(fileName) var $$INCLUDE_CSS = function(fileName) {
{ if (Ace2Editor.EMBEDED && Ace2Editor.EMBEDED[fileName]) {
return '<link rel="stylesheet" type="text/css" href="' + fileName + '"/>'; return '<style type="text/css">' + Ace2Editor.EMBEDED[fileName] + '<\/style>';
}; } else {
var $$INCLUDE_JS = function(fileName) return '<link rel="stylesheet" type="text/css" href="' + fileName + '"\/>';
{ }
return '\x3cscript type="text/javascript" src="' + fileName + '">\x3c/script>'; }
var $$INCLUDE_JS = function(fileName) {
if (Ace2Editor.EMBEDED && Ace2Editor.EMBEDED[fileName]) {
return '<script type="text/javascript">' + Ace2Editor.EMBEDED[fileName].replace(/<\//g, '<\\/') + '<\/script>';
} else {
return '<script type="text/javascript" src="' + fileName + '"><\/script>';
}; };
}
var $$INCLUDE_JS_DEV = $$INCLUDE_JS; var $$INCLUDE_JS_DEV = $$INCLUDE_JS;
var $$INCLUDE_CSS_DEV = $$INCLUDE_CSS; var $$INCLUDE_CSS_DEV = $$INCLUDE_CSS;
var $$INCLUDE_CSS_Q = function(fileName) var $$INCLUDE_CSS_Q = function(fileName) {
{ return JSON.stringify(($$INCLUDE_CSS)(fileName));
return '\'<link rel="stylesheet" type="text/css" href="' + fileName + '"/>\'';
}; };
var $$INCLUDE_JS_Q = function(fileName) var $$INCLUDE_JS_Q = function(fileName) {
{ return JSON.stringify(($$INCLUDE_JS)(fileName));
return '\'\\x3cscript type="text/javascript" src="' + fileName + '">\\x3c/script>\'';
}; };
var $$INCLUDE_JS_Q_DEV = $$INCLUDE_JS_Q; var $$INCLUDE_JS_Q_DEV = $$INCLUDE_JS_Q;
var $$INCLUDE_CSS_Q_DEV = $$INCLUDE_CSS_Q; var $$INCLUDE_CSS_Q_DEV = $$INCLUDE_CSS_Q;
@ -273,12 +277,12 @@ function Ace2Editor()
iframeHTML.push('\'</head><body id="innerdocbody" class="syntax" spellcheck="false">&nbsp;</body></html>\''); iframeHTML.push('\'</head><body id="innerdocbody" class="syntax" spellcheck="false">&nbsp;</body></html>\'');
var outerScript = 'editorId = "' + info.id + '"; editorInfo = parent.' + thisFunctionsName + '.registry[editorId]; ' + 'window.onload = function() ' + '{ window.onload = null; setTimeout' + '(function() ' + '{ var iframe = document.createElement("IFRAME"); ' + 'iframe.scrolling = "no"; var outerdocbody = document.getElementById("outerdocbody"); ' + 'iframe.frameBorder = 0; iframe.allowTransparency = true; ' + // for IE var outerScript = 'editorId = "' + info.id + '"; editorInfo = parent.' + thisFunctionsName + '.registry[editorId]; ' + 'window.onload = function() ' + '{ window.onload = null; setTimeout' + '(function() ' + '{ var iframe = document.createElement("IFRAME"); ' + 'iframe.scrolling = "no"; var outerdocbody = document.getElementById("outerdocbody"); ' + 'iframe.frameBorder = 0; iframe.allowTransparency = true; ' + // for IE
'outerdocbody.insertBefore(iframe, outerdocbody.firstChild); ' + 'iframe.ace_outerWin = window; ' + 'readyFunc = function() { editorInfo.onEditorReady(); readyFunc = null; editorInfo = null; }; ' + 'var doc = iframe.contentWindow.document; doc.open(); var text = (' + iframeHTML.join('+') + ').replace(/\\\\x3c/g, \'<\');doc.write(text); doc.close(); ' + '}, 0); }'; 'outerdocbody.insertBefore(iframe, outerdocbody.firstChild); ' + 'iframe.ace_outerWin = window; ' + 'readyFunc = function() { editorInfo.onEditorReady(); readyFunc = null; editorInfo = null; }; ' + 'var doc = iframe.contentWindow.document; doc.open(); var text = (' + iframeHTML.join('+') + ');doc.write(text); doc.close(); ' + '}, 0); }';
var outerHTML = [doctype, '<html><head>', $$INCLUDE_CSS("../static/css/iframe_editor.css"), $$INCLUDE_CSS("../static/css/pad.css"), $$INCLUDE_CSS("../static/custom/pad.css"), var outerHTML = [doctype, '<html><head>', $$INCLUDE_CSS("../static/css/iframe_editor.css"), $$INCLUDE_CSS("../static/css/pad.css"), $$INCLUDE_CSS("../static/custom/pad.css"),
// bizarrely, in FF2, a file with no "external" dependencies won't finish loading properly // bizarrely, in FF2, a file with no "external" dependencies won't finish loading properly
// (throbs busy while typing) // (throbs busy while typing)
'<link rel="stylesheet" type="text/css" href="data:text/css,"/>', '\x3cscript>\n', outerScript, '\n\x3c/script>', '</head><body id="outerdocbody"><div id="sidediv"><!-- --></div><div id="linemetricsdiv">x</div><div id="overlaysdiv"><!-- --></div></body></html>']; '<link rel="stylesheet" type="text/css" href="data:text/css,"/>', '\x3cscript>\n', outerScript.replace(/<\//g, '<\\/'), '\n\x3c/script>', '</head><body id="outerdocbody"><div id="sidediv"><!-- --></div><div id="linemetricsdiv">x</div><div id="overlaysdiv"><!-- --></div></body></html>'];
if (!Array.prototype.map) Array.prototype.map = function(fun) if (!Array.prototype.map) Array.prototype.map = function(fun)
{ //needed for IE { //needed for IE