summaryrefslogtreecommitdiff
path: root/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/js_checker/js_checker.js
diff options
context:
space:
mode:
authorRuben Rodriguez <ruben@gnu.org>2015-07-13 22:53:44 -0500
committerRuben Rodriguez <ruben@gnu.org>2015-07-13 22:53:44 -0500
commit240c29c1d9cf6a2adfad8692917d60cde071cce3 (patch)
tree437c262a88e805a9dd3d381e0e46b77bd286b1eb /data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/js_checker/js_checker.js
parent23103d7773e24fdf15b79ed69c67089b593fb31a (diff)
LibreJS updated to 6.0.9
Diffstat (limited to 'data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/js_checker/js_checker.js')
-rw-r--r--data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/js_checker/js_checker.js87
1 files changed, 73 insertions, 14 deletions
diff --git a/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/js_checker/js_checker.js b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/js_checker/js_checker.js
index 3b03084..da3ebe6 100644
--- a/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/js_checker/js_checker.js
+++ b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/js_checker/js_checker.js
@@ -3,6 +3,7 @@
* *
* Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros
* Copyright (C) 2014, 2015 Nik Nyby
+ * Copyright (C) 2015 Ruben Rodriguez
*
* This file is part of GNU LibreJS.
*
@@ -115,12 +116,51 @@ var JsChecker = function() {
};
/**
+ * isFreeLicensed
+ *
+ * This function returns true if the input script is licensed under
+ * a free license. Otherwise, it returns false.
+ */
+JsChecker.prototype.isFreeLicensed = function(script) {
+ var magnets = '(' +
+ 'magnet:\\?xt=urn:btih:90dc5c0be029de84e523b9b3922520e79e0e6f08&dn=cc0.txt|' +
+ 'magnet:\\?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt|' +
+ 'magnet:\\?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt|' +
+ 'magnet:\\?xt=urn:btih:8e4f440f4c65981c5bf93c76d35135ba5064d8b7&dn=apache-2.0.txt|' +
+ 'magnet:\\?xt=urn:btih:5de60da917303dbfad4f93fb1b985ced5a89eac2&dn=lgpl-2.1.txt|' +
+ 'magnet:\\?xt=urn:btih:0ef1b8170b3b615170ff270def6427c317705f85&dn=lgpl-3.0.txt|' +
+ 'magnet:\\?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt|' +
+ 'magnet:\\?xt=urn:btih:c80d50af7d3db9be66a4d0a86db0286e4fd33292&dn=bsd-3-clause.txt|' +
+ 'magnet:\\?xt=urn:btih:3877d6d54b3accd4bc32f8a48bf32ebc0901502a&dn=mpl-2.0.txt|' +
+ 'magnet:\\?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt|' +
+ 'magnet:\\?xt=urn:btih:5305d91886084f776adcf57509a648432709a7c7&dn=x11.txt|' +
+ 'magnet:\\?xt=urn:btih:12f2ec9e8de2a3b0002a33d518d6010cc8ab2ae9&dn=xfree86.txt|' +
+ 'magnet:\\?xt=urn:btih:87f119ba0b429ba17a44b4bffcab33165ebdacc0&dn=freebsd.txt|' +
+ 'magnet:\\?xt=urn:btih:b8999bbaf509c08d127678643c515b9ab0836bae&dn=ISC.txt|' +
+ 'magnet:\\?xt=urn:btih:54fd2283f9dbdf29466d2df1a98bf8f65cafe314&dn=artistic-2.0.txt|' +
+ 'magnet:\\?xt=urn:btih:e95b018ef3580986a04669f1b5879592219e2a7a&dn=public-domain.txt' +
+ ')';
+
+ // Remove licensed parts
+ var re = new RegExp(
+ '@license +' + magnets + '.*([\n\r].*?)*@license-end', 'g');
+ script = script.replace(re, '');
+
+ // Remove comments and empty lines
+ re = new RegExp('\/\/.*|/\\*.*?\\*/|^\s*[\n\r]*', 'gm');
+
+ script = script.replace(re, "");
+
+ // If only spaces remain, the file has a free license
+ return (script.match(/\S/) === null);
+};
+
+/**
* searchJs
*
* Takes in some javascript code (as string).
* Uses Narcissus parser to build an abstract syntax tree.
* Checks for trivialness.
- *
*/
JsChecker.prototype.searchJs = function(jsCode, resultReady, url) {
var that = this;
@@ -163,20 +203,39 @@ JsChecker.prototype.searchJs = function(jsCode, resultReady, url) {
this.freeToken = types.emptyTypeObj();
this.nontrivialness = types.emptyTypeObj();
- // use this.hash to keep track of comments made by the nontrivial
- // checker code about why/how the code is found to be nontrivial.
- this.nonTrivialChecker =
- nonTrivialModule.nonTrivialChecker(this.hash);
-
- // register callback and hash. So that result
- // can be passed.
- setHashCallback(
- this.hash, this.handleTree.bind(this), this.notification);
+ if (typeof url === 'undefined' || url === null) {
+ // use this.hash to keep track of comments made by the nontrivial
+ // checker code about why/how the code is found to be nontrivial.
+ this.nonTrivialChecker =
+ nonTrivialModule.nonTrivialChecker(this.hash);
+
+ // register callback and hash. So that result
+ // can be passed.
+ setHashCallback(
+ this.hash, this.handleTree.bind(this), this.notification);
+
+ // parse using ChromeWorker.
+ console.debug(
+ 'JsChecker.searchJs(): starting narcissusWorker.parse()');
+ narcissusWorker.parse(this.jsCode, this.hash);
+ } else {
+ // Handle external scripts
+ console.debug('Analyzing external script: ' + url);
+ this.parseTree = {};
+ if (this.isFreeLicensed(this.jsCode)) {
+ this.parseTree.freeTrivialCheck = type.trivialWithComment(
+ 'This script is under free software licenses: ' + url);
+ } else {
+ this.parseTree.freeTrivialCheck = type.nontrivialWithComment(
+ 'This external script is not under free software ' +
+ 'licenses: ' + url);
+ }
- // parse using ChromeWorker.
- console.debug(
- 'JsChecker.searchJs(): starting narcissusWorker.parse()');
- narcissusWorker.parse(this.jsCode, this.hash);
+ // Cache result with hash of script for future checks
+ scriptsCached.addEntry(
+ this.jsCode, this.parseTree.freeTrivialCheck,
+ this.relationChecker, true, this.url);
+ }
} catch (x) {
console.debug('error', x);
this.handleTree(false, x);