Automatically detect PaperScript code inlined on page.

This commit is contained in:
Jürg Lehni 2014-04-30 12:32:08 +02:00
parent af9b3cd5c6
commit 9e45d7f5eb
2 changed files with 22 additions and 17 deletions

View file

@ -145,8 +145,8 @@ var PaperScope = Base.extend(/** @lends PaperScope# */{
return this; return this;
}, },
execute: function(code) { execute: function(code, url, options) {
paper.PaperScript.execute(code, this); paper.PaperScript.execute(code, this, url, options);
View.updateFocus(); View.updateFocus();
}, },

View file

@ -113,10 +113,12 @@ Base.exports.PaperScript = (function() {
* *
* @name PaperScript.compile * @name PaperScript.compile
* @function * @function
* @param {String} code The PaperScript code * @param {String} code The PaperScript code.
* @return {String} the compiled PaperScript as JavaScript code * @param {String} url The url of the source, for source-map debugging.
* @return {String} The compiled PaperScript as JavaScript code.
*/ */
function compile(code, url, inlined) { function compile(code, url, options) {
options = options || {};
// Use Acorn or Esprima to translate the code into an AST structure // Use Acorn or Esprima to translate the code into an AST structure
// which is then walked and parsed for operators to overload. Instead of // which is then walked and parsed for operators to overload. Instead of
// modifying the AST and translating it back to code, we directly change // modifying the AST and translating it back to code, we directly change
@ -181,7 +183,7 @@ Base.exports.PaperScript = (function() {
walkAST(value, node); walkAST(value, node);
} }
} }
switch (node && node.type) { switch (node.type) {
case 'UnaryExpression': // -a case 'UnaryExpression': // -a
if (node.operator in unaryOperators if (node.operator in unaryOperators
&& node.argument.type !== 'Literal') { && node.argument.type !== 'Literal') {
@ -236,20 +238,22 @@ Base.exports.PaperScript = (function() {
} }
// Source-map support: // Source-map support:
var sourceMap = null, var sourceMap = null,
version = browser.version; version = browser.version,
lineBreaks = /\r\n|\n|\r/mg;
// TODO: Verify these browser versions for source map support, and check // TODO: Verify these browser versions for source map support, and check
// other browsers. // other browsers.
if (browser.chrome && version >= 30 if (browser.chrome && version >= 30
|| browser.safari && version >= 7 || browser.safari && version >= 7
|| browser.firefox && version >= 23) { || browser.firefox && version >= 23) {
var offset = 0; var offset = 0;
if (inlined) { if (url === window.location.href) {
// Determine the offset of inlined code. // If the code stems from the actual html page, determine the
// offset of inlined code.
var html = document.getElementsByTagName('html')[0].innerHTML; var html = document.getElementsByTagName('html')[0].innerHTML;
// Count the amount of line breaks in the html before this code // Count the amount of line breaks in the html before this code
// to determine the offset. // to determine the offset.
offset = html.substr(0, html.indexOf(code) + 1).match( offset = html.substr(0, html.indexOf(code) + 1).match(
/\r\n|\n|\r/mg).length + 1; lineBreaks).length + 1;
} }
// A hack required by all current browser versions: Instead of // A hack required by all current browser versions: Instead of
// starting the mappings at the given offset, we have to shift the // starting the mappings at the given offset, we have to shift the
@ -260,7 +264,7 @@ Base.exports.PaperScript = (function() {
var mappings = ['AAAA']; var mappings = ['AAAA'];
// Create empty entries by the amount of lines + 1, so join can be // Create empty entries by the amount of lines + 1, so join can be
// used below to produce the actual instructions that many times. // used below to produce the actual instructions that many times.
mappings.length = code.match(/\r\n|\n|\r/mg).length + 1 + offset; mappings.length = code.match(lineBreaks).length + 1 + offset;
sourceMap = { sourceMap = {
version: 3, version: 3,
file: url, file: url,
@ -275,7 +279,7 @@ Base.exports.PaperScript = (function() {
}; };
// Include the original code in the sourceMap if there is no linked // Include the original code in the sourceMap if there is no linked
// source file so the debugger can still display it correctly. // source file so the debugger can still display it correctly.
if (!url) if (!url || options.inline)
sourceMap.sourcesContent = [code]; sourceMap.sourcesContent = [code];
} }
// Now do the parsing magic // Now do the parsing magic
@ -304,10 +308,11 @@ Base.exports.PaperScript = (function() {
* *
* @name PaperScript.execute * @name PaperScript.execute
* @function * @function
* @param {String} code The PaperScript code * @param {String} code The PaperScript code.
* @param {PaperScript} scope The scope for which the code is executed * @param {PaperScript} scope The scope for which the code is executed.
* @param {String} url The url of the source, for source-map debugging.
*/ */
function execute(code, scope, url, inlined) { function execute(code, scope, url, options) {
// Set currently active scope. // Set currently active scope.
paper = scope; paper = scope;
var view = scope.getView(), var view = scope.getView(),
@ -330,7 +335,7 @@ Base.exports.PaperScript = (function() {
params = [], params = [],
args = [], args = [],
func; func;
code = compile(code, url, inlined); code = compile(code, url, options);
function expose(scope, hidden) { function expose(scope, hidden) {
// Look through all enumerable properties on the scope and expose // Look through all enumerable properties on the scope and expose
// these too as pseudo-globals, but only if they seem to be in use. // these too as pseudo-globals, but only if they seem to be in use.
@ -439,7 +444,7 @@ Base.exports.PaperScript = (function() {
}); });
} else { } else {
// We can simply get the code form the script tag. // We can simply get the code form the script tag.
execute(script.innerHTML, scope, script.baseURI, true); execute(script.innerHTML, scope, script.baseURI);
} }
// Mark script as loaded now. // Mark script as loaded now.
script.setAttribute('data-paper-ignore', true); script.setAttribute('data-paper-ignore', true);