commit 2bc44dddd19d6bbaef59c5524c251d226c2bcdb2
parent 53509316174b5e2eb4619a895b287d1fbdd79973
Author: Dan Stillman <dstillman@zotero.org>
Date: Wed, 27 Dec 2017 18:18:20 -0500
Add attr()/text() to translator sandbox
The current document is automatically used (but can still be provided as
the first argument to avoid accidental bugs during the transition).
Closes #1323
Addresses zotero/translators#1277
Diffstat:
1 file changed, 38 insertions(+), 0 deletions(-)
diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js
@@ -1866,6 +1866,44 @@ Zotero.Translate.Base.prototype = {
this._sandboxManager.sandbox.Z = this._sandboxZotero;
this._sandboxManager.sandbox.ZU = this._sandboxZotero.Utilities;
this._transferItem = this._sandboxZotero._transferItem;
+
+ // Add web helper functions
+ if (this.type == 'web') {
+ this._sandboxManager.sandbox.attr = this._attr.bind(this);
+ this._sandboxManager.sandbox.text = this._text.bind(this);
+ }
+ },
+
+ /**
+ * Helper function to extract HTML attribute text
+ */
+ _attr: function (selector, attr, index) {
+ if (typeof arguments[0] == 'string') {
+ var doc = this.document;
+ }
+ // Support legacy polyfill signature
+ else {
+ this._debug("WARNING: attr() no longer requires a document as the first argument");
+ [doc, selector, attr, index] = arguments;
+ }
+ var elem = index ? doc.querySelectorAll(selector).item(index) : doc.querySelector(selector);
+ return elem ? elem.getAttribute(attr) : null;
+ },
+
+ /**
+ * Helper function to extract HTML element text
+ */
+ _text: function (selector, index) {
+ if (typeof arguments[0] == 'string') {
+ var doc = this.document;
+ }
+ // Support legacy polyfill signature
+ else {
+ this._debug("WARNING: text() no longer requires a document as the first argument");
+ [doc, selector, attr, index] = arguments;
+ }
+ var elem = index ? doc.querySelectorAll(selector).item(index) : doc.querySelector(selector);
+ return elem ? elem.textContent : null;
},
/**