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

Previewers: Use classList instead of className #2787

Merged
merged 1 commit into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
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
25 changes: 14 additions & 11 deletions plugins/previewers/prism-previewers.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,9 @@
};
};

var tokenRegexp = /(?:^|\s)token(?=$|\s)/;
var activeRegexp = /(?:^|\s)active(?=$|\s)/g;
var flippedRegexp = /(?:^|\s)flipped(?=$|\s)/g;
var TOKEN_CLASS = 'token';
var ACTIVE_CLASS = 'active';
var FLIPPED_CLASS = 'flipped';

/**
* Previewer constructor
Expand All @@ -500,7 +500,6 @@
var Previewer = function (type, updater, supportedLanguages, initializer) {
this._elt = null;
this._type = type;
this._clsRegexp = RegExp('(?:^|\\s)' + type + '(?=$|\\s)');
this._token = null;
this.updater = updater;
this._mouseout = this.mouseout.bind(this);
Expand Down Expand Up @@ -543,6 +542,10 @@
}
};

/**
* @param {Element} token
* @returns {boolean}
*/
Previewer.prototype.isDisabled = function (token) {
do {
if (token.hasAttribute && token.hasAttribute('data-previewers')) {
Expand All @@ -555,14 +558,14 @@

/**
* Checks the class name of each hovered element
* @param token
* @param {Element} token
*/
Previewer.prototype.check = function (token) {
if (tokenRegexp.test(token.className) && this.isDisabled(token)) {
if (token.classList.contains(TOKEN_CLASS) && this.isDisabled(token)) {
return;
}
do {
if (tokenRegexp.test(token.className) && this._clsRegexp.test(token.className)) {
if (token.classList && token.classList.contains(TOKEN_CLASS) && token.classList.contains(this._type)) {
break;
}
} while(token = token.parentNode);
Expand Down Expand Up @@ -597,14 +600,14 @@
this._token.addEventListener('mouseout', this._mouseout, false);

var offset = getOffset(this._token);
this._elt.className += ' active';
this._elt.classList.add(ACTIVE_CLASS);

if (offset.top - this._elt.offsetHeight > 0) {
this._elt.className = this._elt.className.replace(flippedRegexp, '');
this._elt.classList.remove(FLIPPED_CLASS);
this._elt.style.top = offset.top + 'px';
this._elt.style.bottom = '';
} else {
this._elt.className += ' flipped';
this._elt.classList.add(FLIPPED_CLASS);
this._elt.style.bottom = offset.bottom + 'px';
this._elt.style.top = '';
}
Expand All @@ -619,7 +622,7 @@
* Hides the previewer.
*/
Previewer.prototype.hide = function () {
this._elt.className = this._elt.className.replace(activeRegexp, '');
this._elt.classList.remove(ACTIVE_CLASS);
};

/**
Expand Down
Loading