Translator sandbox: Update attr()/text() and add innerText()

- Remove TODO comment to add a warning if a Document is passed, since a
  document will always be required when operating on something other than
  the main page (e.g., for multiples).
- Always return a string instead of null. A translator can always use
  querySelector() if it really needs to know if a node exists.
- Automatically trim the returned string
- Add innerText() function that uses .innerText instead of .textContent,
  since there are situations where that might be helpful. Some of those we
  currently address with trimInternal(), but there can be other
  cases where .innerText gives cleaner, user-centric output.

Translators won't be able to rely on these changes for a while, so we'll
need to be careful to watch for problems here, or translators will need
to include a v3 polyfill.

Addresses zotero/translators#1913
This commit is contained in:
Dan Stillman 2020-09-11 02:27:05 -04:00
parent 8fc316f727
commit 4efc0af119

View file

@ -1851,11 +1851,14 @@ Zotero.Translate.Base.prototype = {
if (this.type == 'web') {
this._sandboxManager.sandbox.attr = this._attr.bind(this);
this._sandboxManager.sandbox.text = this._text.bind(this);
this._sandboxManager.sandbox.innerText = this._innerText.bind(this);
}
},
/**
* Helper function to extract HTML attribute text
*
* Text is automatically trimmed
*/
_attr: function (selector, attr, index) {
if (typeof arguments[0] == 'string') {
@ -1863,18 +1866,18 @@ Zotero.Translate.Base.prototype = {
}
// Document or element passed as first argument
else {
// TODO: Warn if Document rather than Element is passed once we drop 4.0 translator
// support
[docOrElem, selector, attr, index] = arguments;
}
var elem = index
? docOrElem.querySelectorAll(selector).item(index)
: docOrElem.querySelector(selector);
return elem ? elem.getAttribute(attr) : null;
return (elem ? elem.getAttribute(attr) : "").trim();
},
/**
* Helper function to extract HTML element text
*
* Text is extracted using textContent and is automatically trimmed
*/
_text: function (selector, index) {
if (typeof arguments[0] == 'string') {
@ -1882,14 +1885,32 @@ Zotero.Translate.Base.prototype = {
}
// Document or element passed as first argument
else {
// TODO: Warn if Document rather than Element is passed once we drop 4.0 translator
// support
[docOrElem, selector, index] = arguments;
}
var elem = index
? docOrElem.querySelectorAll(selector).item(index)
: docOrElem.querySelector(selector);
return elem ? elem.textContent : null;
return (elem ? elem.textContent : "").trim();
},
/**
* Helper function to extract rendered HTML element text
*
* Text is extracted using innerText, not textContent, so it reflects the rendered content, and
* is automatically trimmed
*/
_innerText: function (selector, index) {
if (typeof arguments[0] == 'string') {
var docOrElem = this.document;
}
// Document or element passed as first argument
else {
[docOrElem, selector, index] = arguments;
}
var elem = index
? docOrElem.querySelectorAll(selector).item(index)
: docOrElem.querySelector(selector);
return (elem ? elem.innerText : "").trim();
},
/**