Skip to content

Commit b631949

Browse files
Core: Added setLanguage util function (#3167)
1 parent ebd59e3 commit b631949

11 files changed

+101
-77
lines changed

components/prism-core.js

+25-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var _self = (typeof window !== 'undefined')
1919
var Prism = (function (_self) {
2020

2121
// Private helper vars
22-
var lang = /\blang(?:uage)?-([\w-]+)\b/i;
22+
var lang = /(?:^|\s)lang(?:uage)?-([\w-]+)(?=\s|$)/i;
2323
var uniqueId = 0;
2424

2525
// The grammar object for plaintext
@@ -184,15 +184,33 @@ var Prism = (function (_self) {
184184
* @returns {string}
185185
*/
186186
getLanguage: function (element) {
187-
while (element && !lang.test(element.className)) {
187+
while (element) {
188+
var m = lang.exec(element.className);
189+
if (m) {
190+
return m[1].toLowerCase();
191+
}
188192
element = element.parentElement;
189193
}
190-
if (element) {
191-
return (element.className.match(lang) || [, 'none'])[1].toLowerCase();
192-
}
193194
return 'none';
194195
},
195196

197+
/**
198+
* Sets the Prism `language-xxxx` class of the given element.
199+
*
200+
* @param {Element} element
201+
* @param {string} language
202+
* @returns {void}
203+
*/
204+
setLanguage: function (element, language) {
205+
// remove all `language-xxxx` classes
206+
// (this might leave behind a leading space)
207+
element.className = element.className.replace(RegExp(lang, 'gi'), '');
208+
209+
// add the new `language-xxxx` class
210+
// (using `classList` will automatically clean up spaces for us)
211+
element.classList.add('language-' + language);
212+
},
213+
196214
/**
197215
* Returns the script element that is currently executing.
198216
*
@@ -547,12 +565,12 @@ var Prism = (function (_self) {
547565
var grammar = _.languages[language];
548566

549567
// Set language on the element, if not present
550-
element.className = element.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
568+
_.util.setLanguage(element, language);
551569

552570
// Set language on the parent, for styling
553571
var parent = element.parentElement;
554572
if (parent && parent.nodeName.toLowerCase() === 'pre') {
555-
parent.className = parent.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
573+
_.util.setLanguage(parent, language);
556574
}
557575

558576
var code = element.textContent;

components/prism-core.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/Prism.hooks.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ <h2>
7373

7474
<dt class="tag-source">Source:</dt>
7575
<dd class="tag-source"><ul class="dummy"><li>
76-
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line696">line 696</a>
76+
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line714">line 714</a>
7777
</li></ul></dd>
7878

7979

@@ -152,7 +152,7 @@ <h4 class="name" id=".add"><span class="type-signature">(static) </span>add<span
152152

153153
<dt class="tag-source">Source:</dt>
154154
<dd class="tag-source"><ul class="dummy"><li>
155-
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line711">line 711</a>
155+
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line729">line 729</a>
156156
</li></ul></dd>
157157

158158

@@ -314,7 +314,7 @@ <h4 class="name" id=".run"><span class="type-signature">(static) </span>run<span
314314

315315
<dt class="tag-source">Source:</dt>
316316
<dd class="tag-source"><ul class="dummy"><li>
317-
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line728">line 728</a>
317+
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line746">line 746</a>
318318
</li></ul></dd>
319319

320320

docs/Prism.html

+5-5
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ <h4 class="name" id=".highlight"><span class="type-signature">(static) </span>hi
350350

351351
<dt class="tag-source">Source:</dt>
352352
<dd class="tag-source"><ul class="dummy"><li>
353-
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line637">line 637</a>
353+
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line655">line 655</a>
354354
</li></ul></dd>
355355

356356

@@ -567,7 +567,7 @@ <h4 class="name" id=".highlightAll"><span class="type-signature">(static) </span
567567

568568
<dt class="tag-source">Source:</dt>
569569
<dd class="tag-source"><ul class="dummy"><li>
570-
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line479">line 479</a>
570+
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line497">line 497</a>
571571
</li></ul></dd>
572572

573573

@@ -763,7 +763,7 @@ <h4 class="name" id=".highlightAllUnder"><span class="type-signature">(static) <
763763

764764
<dt class="tag-source">Source:</dt>
765765
<dd class="tag-source"><ul class="dummy"><li>
766-
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line498">line 498</a>
766+
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line516">line 516</a>
767767
</li></ul></dd>
768768

769769

@@ -998,7 +998,7 @@ <h4 class="name" id=".highlightElement"><span class="type-signature">(static) </
998998

999999
<dt class="tag-source">Source:</dt>
10001000
<dd class="tag-source"><ul class="dummy"><li>
1001-
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line544">line 544</a>
1001+
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line562">line 562</a>
10021002
</li></ul></dd>
10031003

10041004

@@ -1244,7 +1244,7 @@ <h4 class="name" id=".tokenize"><span class="type-signature">(static) </span>tok
12441244

12451245
<dt class="tag-source">Source:</dt>
12461246
<dd class="tag-source"><ul class="dummy"><li>
1247-
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line673">line 673</a>
1247+
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line691">line 691</a>
12481248
</li></ul></dd>
12491249

12501250

0 commit comments

Comments
 (0)