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

Core: Added setLanguage util function #3167

Merged
merged 4 commits into from Nov 3, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 25 additions & 7 deletions components/prism-core.js
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion components/prism-core.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/Prism.hooks.html
Expand Up @@ -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>


Expand Down Expand Up @@ -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>


Expand Down Expand Up @@ -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>


Expand Down
10 changes: 5 additions & 5 deletions docs/Prism.html
Expand Up @@ -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>


Expand Down Expand Up @@ -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>


Expand Down Expand Up @@ -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>


Expand Down Expand Up @@ -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>


Expand Down Expand Up @@ -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>


Expand Down