Skip to content

Commit

Permalink
Previewers: Use classList instead of className (#2787)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Mar 17, 2021
1 parent d5e14e1 commit d298d46
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
25 changes: 14 additions & 11 deletions plugins/previewers/prism-previewers.js
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

0 comments on commit d298d46

Please sign in to comment.