Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GrabToPan] Prefer the standard, rather than a prefixed, matchesSelector value #13142

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions web/grab_to_pan.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { shadow } from "pdfjs-lib";

/**
* Construct a GrabToPan instance for a given HTML element.
* @param options.element {Element}
Expand Down Expand Up @@ -95,9 +97,9 @@ GrabToPan.prototype = {
* @returns {boolean} Whether to not react to the click event.
*/
ignoreTarget: function GrabToPan_ignoreTarget(node) {
// Use matchesSelector to check whether the clicked element
// is (a child of) an input element / link
return node[matchesSelector](
// Use `this._matchesSelector` to check whether the clicked element
// is (a child of) an input element / link.
return node[this._matchesSelector](
"a[href], a[href] *, input, textarea, button, button *, select, option"
);
},
Expand Down Expand Up @@ -176,21 +178,27 @@ GrabToPan.prototype = {
// Note: ChildNode.remove doesn't throw if the parentNode is undefined.
this.overlay.remove();
},
};

// Get the correct (vendor-prefixed) name of the matches method.
let matchesSelector;
["webkitM", "mozM", "m"].some(function (prefix) {
let name = prefix + "atches";
if (name in document.documentElement) {
matchesSelector = name;
}
name += "Selector";
if (name in document.documentElement) {
matchesSelector = name;
}
return matchesSelector; // If found, then truthy, and [].some() ends.
});
/**
* @private
*/
get _matchesSelector() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could perhaps even add a fast-path for MOZCENTRAL here, e.g. as outlined below, however given that this code no longer runs unconditionally that's probably not necessary.

if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
  return shadow(this, "matchesSelector", "matches");
}

// Get the correct (vendor-prefixed) name of the matches method.
let matchesSelector;
["m", "mozM", "webkitM"].some(function (prefix) {
let name = prefix + "atches";
if (name in document.documentElement) {
matchesSelector = name;
}
name += "Selector";
if (name in document.documentElement) {
matchesSelector = name;
}
return matchesSelector; // If found, then truthy, and [].some() ends.
});
return shadow(this, "_matchesSelector", matchesSelector);
},
};

// Browser sniffing because it's impossible to feature-detect
// whether event.which for onmousemove is reliable
Expand Down