Skip to content

Commit d298d46

Browse files
Previewers: Use classList instead of className (#2787)
1 parent d5e14e1 commit d298d46

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

plugins/previewers/prism-previewers.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@
485485
};
486486
};
487487

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

492492
/**
493493
* Previewer constructor
@@ -500,7 +500,6 @@
500500
var Previewer = function (type, updater, supportedLanguages, initializer) {
501501
this._elt = null;
502502
this._type = type;
503-
this._clsRegexp = RegExp('(?:^|\\s)' + type + '(?=$|\\s)');
504503
this._token = null;
505504
this.updater = updater;
506505
this._mouseout = this.mouseout.bind(this);
@@ -543,6 +542,10 @@
543542
}
544543
};
545544

545+
/**
546+
* @param {Element} token
547+
* @returns {boolean}
548+
*/
546549
Previewer.prototype.isDisabled = function (token) {
547550
do {
548551
if (token.hasAttribute && token.hasAttribute('data-previewers')) {
@@ -555,14 +558,14 @@
555558

556559
/**
557560
* Checks the class name of each hovered element
558-
* @param token
561+
* @param {Element} token
559562
*/
560563
Previewer.prototype.check = function (token) {
561-
if (tokenRegexp.test(token.className) && this.isDisabled(token)) {
564+
if (token.classList.contains(TOKEN_CLASS) && this.isDisabled(token)) {
562565
return;
563566
}
564567
do {
565-
if (tokenRegexp.test(token.className) && this._clsRegexp.test(token.className)) {
568+
if (token.classList && token.classList.contains(TOKEN_CLASS) && token.classList.contains(this._type)) {
566569
break;
567570
}
568571
} while(token = token.parentNode);
@@ -597,14 +600,14 @@
597600
this._token.addEventListener('mouseout', this._mouseout, false);
598601

599602
var offset = getOffset(this._token);
600-
this._elt.className += ' active';
603+
this._elt.classList.add(ACTIVE_CLASS);
601604

602605
if (offset.top - this._elt.offsetHeight > 0) {
603-
this._elt.className = this._elt.className.replace(flippedRegexp, '');
606+
this._elt.classList.remove(FLIPPED_CLASS);
604607
this._elt.style.top = offset.top + 'px';
605608
this._elt.style.bottom = '';
606609
} else {
607-
this._elt.className += ' flipped';
610+
this._elt.classList.add(FLIPPED_CLASS);
608611
this._elt.style.bottom = offset.bottom + 'px';
609612
this._elt.style.top = '';
610613
}
@@ -619,7 +622,7 @@
619622
* Hides the previewer.
620623
*/
621624
Previewer.prototype.hide = function () {
622-
this._elt.className = this._elt.className.replace(activeRegexp, '');
625+
this._elt.classList.remove(ACTIVE_CLASS);
623626
};
624627

625628
/**

0 commit comments

Comments
 (0)