Skip to content
Permalink
Browse files
Core: Added setLanguage util function (#3167)
  • Loading branch information
RunDevelopment committed Nov 3, 2021
1 parent ebd59e3 commit b631949acd54f01330dc5be313ff0e632047e901
@@ -19,7 +19,7 @@ var _self = (typeof window !== 'undefined')
var Prism = (function (_self) {

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

// The grammar object for plaintext
@@ -184,15 +184,33 @@ var Prism = (function (_self) {
* @returns {string}
*/
getLanguage: function (element) {
while (element && !lang.test(element.className)) {
while (element) {
var m = lang.exec(element.className);
if (m) {
return m[1].toLowerCase();
}
element = element.parentElement;
}
if (element) {
return (element.className.match(lang) || [, 'none'])[1].toLowerCase();
}
return 'none';
},

/**
* Sets the Prism `language-xxxx` class of the given element.
*
* @param {Element} element
* @param {string} language
* @returns {void}
*/
setLanguage: function (element, language) {
// remove all `language-xxxx` classes
// (this might leave behind a leading space)
element.className = element.className.replace(RegExp(lang, 'gi'), '');

// add the new `language-xxxx` class
// (using `classList` will automatically clean up spaces for us)
element.classList.add('language-' + language);
},

/**
* Returns the script element that is currently executing.
*
@@ -547,12 +565,12 @@ var Prism = (function (_self) {
var grammar = _.languages[language];

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

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

var code = element.textContent;

Some generated files are not rendered by default. Learn more.

@@ -73,7 +73,7 @@ <h2>

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


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

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


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

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


@@ -350,7 +350,7 @@ <h4 class="name" id=".highlight"><span class="type-signature">(static) </span>hi

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


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

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


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

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


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

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


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

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


0 comments on commit b631949

Please sign in to comment.