public inbox for [email protected]
help / color / mirror / Atom feedFrom: Aditya Toshniwal <[email protected]>
To: pgadmin-hackers <[email protected]>
Subject: [pgAdmin][RM5765] Invalid markup for viewing table with field named "constructor"
Date: Mon, 7 Sep 2020 11:13:39 +0530
Message-ID: <CAM9w-_n8FY_biMAJynRCiq+eyei0wjJw2FzWEue5THj_sr8+Sw@mail.gmail.com> (raw)
Hi Hackers,
Attached is the patch to handle issues in the query tool when columns are
having names same as JS object internal functions like constructor,
hasOwnProperty, etc.
Please review.
--
Thanks,
Aditya Toshniwal
pgAdmin hacker | Sr. Software Engineer | *edbpostgres.com*
<http://edbpostgres.com;
"Don't Complain about Heat, Plant a TREE"
Attachments:
[application/octet-stream] RM5765.patch (5.1K, 3-RM5765.patch)
download | inline diff:
diff --git a/web/pgadmin/static/js/is_native.js b/web/pgadmin/static/js/is_native.js
new file mode 100644
index 000000000..3adc516ca
--- /dev/null
+++ b/web/pgadmin/static/js/is_native.js
@@ -0,0 +1,42 @@
+/* Code to check if the object or function is native JS
+ * Author: John-David Dalton
+ */
+(function() {
+
+ // Used to resolve the internal `[[Class]]` of values
+ var toString = Object.prototype.toString;
+
+ // Used to resolve the decompiled source of functions
+ var fnToString = Function.prototype.toString;
+
+ // Used to detect host constructors (Safari > 4; really typed array specific)
+ var reHostCtor = /^\[object .+?Constructor\]$/;
+
+ // Compile a regexp using a common native method as a template.
+ // We chose `Object#toString` because there's a good chance it is not being mucked with.
+ var reNative = RegExp('^' +
+ // Coerce `Object#toString` to a string
+ String(toString)
+ // Escape any special regexp characters
+ .replace(/[.*+?^${}()|[\]\/\\]/g, '\\$&')
+ // Replace mentions of `toString` with `.*?` to keep the template generic.
+ // Replace thing like `for ...` to support environments like Rhino which add extra info
+ // such as method arity.
+ .replace(/toString|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
+ );
+
+ function isNative(value) {
+ var type = typeof value;
+ return type == 'function'
+ // Use `Function#toString` to bypass the value's own `toString` method
+ // and avoid being faked out.
+ ? reNative.test(fnToString.call(value))
+ // Fallback to a host object check because some environments will represent
+ // things like typed arrays as DOM methods which may not conform to the
+ // normal native pattern.
+ : (value && type == 'object' && reHostCtor.test(toString.call(value))) || false;
+ }
+
+ // export however you want
+ module.exports = isNative;
+}());
diff --git a/web/pgadmin/tools/sqleditor/static/js/sqleditor.js b/web/pgadmin/tools/sqleditor/static/js/sqleditor.js
index a0724ff9e..c19cfcda0 100644
--- a/web/pgadmin/tools/sqleditor/static/js/sqleditor.js
+++ b/web/pgadmin/tools/sqleditor/static/js/sqleditor.js
@@ -40,6 +40,7 @@ define('tools.querytool', [
'sources/csrf',
'tools/datagrid/static/js/datagrid_panel_title',
'sources/window',
+ 'sources/is_native',
'sources/../bundle/slickgrid',
'pgadmin.file_manager',
'slick.pgadmin.formatters',
@@ -54,7 +55,7 @@ define('tools.querytool', [
GeometryViewer, historyColl, queryHist, querySources,
keyboardShortcuts, queryToolActions, queryToolNotifications, Datagrid,
modifyAnimation, calculateQueryRunTime, callRenderAfterPoll, queryToolPref, queryTxnStatus, csrfToken, panelTitleFunc,
- pgWindow) {
+ pgWindow, isNative) {
/* Return back, this has been called more than once */
if (pgAdmin.SqlEditor)
return pgAdmin.SqlEditor;
@@ -789,10 +790,14 @@ define('tools.querytool', [
c.display_name = _.escape(c.display_name);
c.column_type = _.escape(c.column_type);
+ // If the keys have name from existing JS keywords then it may
+ // create problem. eg - contructor, hasOwnProperty.
+ // nonative_field is field with extra double quotes
var options = {
id: _.escape(c.name),
pos: c.pos,
field: c.name,
+ nonative_field: `"${c.name}"`,
name: c.label,
display_name: c.display_name,
column_type: c.column_type,
@@ -809,11 +814,11 @@ define('tools.querytool', [
var column_type = c.column_type.trim();
var label = c.name.length > column_type.length ? _.escape(c.display_name) : column_type;
- if (_.isUndefined(column_size[table_name][c.name])) {
+ if (_.isUndefined(column_size[table_name][options.nonative_field])) {
options['width'] = SqlEditorUtils.calculateColumnWidth(label);
- column_size[table_name][c.name] = options['width'];
+ column_size[table_name][c.nonative_field] = options['width'];
} else {
- options['width'] = column_size[table_name][c.name];
+ options['width'] = column_size[table_name][options.nonative_field];
}
// If grid is editable then add editor else make it readonly
if (c.cell == 'oid' && c.name == 'oid') {
@@ -999,7 +1004,7 @@ define('tools.querytool', [
var cols = this.getColumns();
_.each(cols, function(col) {
var col_size = self.handler['col_size'];
- col_size[self.handler['table_name']][col['id']] = col['width'];
+ col_size[self.handler['table_name']][col['nonative_field']] = col['width'];
});
}.bind(grid));
@@ -1191,6 +1196,13 @@ define('tools.querytool', [
item_current[self.client_primary_key] = _key;
}
+ // When adding new rows, mark all native JS keywords undefined if not already set
+ _.each(args.grid.getColumns(), function(col){
+ if(isNative(item_current[col.field])) {
+ item_current[col.field] = undefined;
+ }
+ });
+
data_view.addItem(item_current);
self.handler.data_store.added[_key] = {
'err': false,
view thread (2+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected]
Subject: Re: [pgAdmin][RM5765] Invalid markup for viewing table with field named "constructor"
In-Reply-To: <CAM9w-_n8FY_biMAJynRCiq+eyei0wjJw2FzWEue5THj_sr8+Sw@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox