/**
* GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript.
* *
* Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros
* Copyright (C) 2014, 2015 Nik Nyby
*
* This file is part of GNU LibreJS.
*
* GNU LibreJS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GNU LibreJS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU LibreJS. If not, see .
*/
const types = require("./constant_types");
// constants from Narcissus for function types.
const DECLARED_FORM = 0, EXPRESSED_FORM = 1, STATEMENT_FORM = 2;
const token = types.token;
var checkTypes = types.checkTypes;
var utils = {
/**
* nodeContains
* Checks that node contains both a type and a value.
* Shortcut to check for null/undefined.
*
* @param {object} n. The current node being studied.
* @return {boolean} . True if matching.
*/
nodeContains: function (n, type, value) {
if (n != undefined) {
return n.type === type &&
n.value === value;
}
},
/**
* isType
* Checks that node is of a certain type.
* Shortcut to check for null/undefined.
*
* @param {object} n. The current node being studied.
* @return {boolean}. True if it's the right type.
*/
isType: function (n, type) {
return n != undefined && n.type === type;
},
isNotType: function (n, type) {
return n != undefined && n.type !== type;
},
/**
* hasChildren
*
* Checks the token on the left
* and on the right.
*
* @param {object} n. The current node being studied.
* @param {leftType} token constant. The type on the
* left.
* @param {rightType} token constant. The type of child
* on the right
*
*/
hasChildren: function (n, leftType, rightType) {
if (types == undefined) {
return false;
}
return this.isType(n.children[0], leftType) &&
this.isType(n.children[1], rightType);
},
/**
* findScriptTag
*
* This method should probably be replaced with DOM testing
* as regex is rather insufficiant, and this wouldn't cover
* tricky constructs as shown in http://ha.ckers.org/xss.html.
*/
findScriptTag: function (n) {
return n.value != undefined &&
/');
* or any myObj.write(myStringVariable);
* or concatenation such as:
* myObj.write('