Skip to content

Commit

Permalink
Always prefer language- prefixed classes.
Browse files Browse the repository at this point in the history
If a block's class cstring ontains language-something it is a clear
indication that the user wants this class to be treated as a
language name. We should take that into account and not look at
other classes present.

This also allows to disable highlighting in blocks with classes
we recognize as languages but that might have different meaning:

    <code class="c language-text">..</code>
  • Loading branch information
isagalaev committed May 12, 2015
1 parent 0814aed commit dde27ca
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
21 changes: 9 additions & 12 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,24 @@ https://highlightjs.org/
}

function blockLanguage(block) {
var i, language, length,
var i, match, length,
classes = block.className + ' ';

classes += block.parentNode ? block.parentNode.className : '';
classes = classes.split(/\s+/);

// try to find a supported language or no-highlight class
for(i = 0, length = classes.length; i < length; i++) {
language = classes[i].replace(/^lang(uage)?-/, '');

if(getLanguage(language) || isNotHighlighted(language)) {
return language;
}
// language-* takes precedence over non-prefixed class names and
match = /\blang(?:uage)?-([\w-]+)\b/.exec(classes);
if (match) {
return getLanguage(match[1]) ? match[1] : 'no-highlight';
}

// return 'no-highlight' if there is an unsupported lang(uage)-prefixed class
classes = classes.split(/\s+/);
for(i = 0, length = classes.length; i < length; i++) {
if (/^lang(uage)?-/.test(classes[i])) {
return 'no-highlight';
if(getLanguage(classes[i]) || isNotHighlighted(classes[i])) {
return classes[i];
}
}

}

function inherit(parent, obj) {
Expand Down
7 changes: 3 additions & 4 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
<pre><code class="lang-python">def add_one(x):
return x + 1</code></pre>

<!-- html 5 style language class plus unsupported language class -->
<pre><code class="language-foo language-python">for x in [1, 2, 3]:
count(x)</code></pre>

</div>

<div id="custom-markup">
Expand Down Expand Up @@ -84,6 +80,9 @@
<!-- unsupported shortened language-prefixed class -->
<pre><code class="lang-foo">for x in [1, 2, 3]: count(x)</code></pre>

<!-- unsupported prefixed language and supported unprefixed language -->
<pre><code class="python language-foo">for x in [1, 2, 3]: count(x)</code></pre>

</div>

<!-- sub-languages -->
Expand Down
7 changes: 7 additions & 0 deletions test/special/noHighlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ describe('no highlighting', function() {
actual.should.equal(expected);
});

it('should keep block unchanged (unsupported prefixed language)', function() {
var expected = 'for x in [1, 2, 3]: count(x)',
actual = document.querySelector('#no-highlight .python.language-foo').innerHTML;

actual.should.equal(expected);
});

it('should skip pre tags without a child code tag', function() {
var expected = 'Computer output',
actual = document.querySelector('pre samp').innerHTML;
Expand Down

0 comments on commit dde27ca

Please sign in to comment.