Skip to content

Commit

Permalink
fix(parser) fixes sublanguage with no rule matches (#2506)
Browse files Browse the repository at this point in the history
* fix(parser) fixes sublanguage with no rule matches

Resolves #2504.
  • Loading branch information
joshgoebel committed Apr 26, 2020
1 parent 3875088 commit f6813cc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
9 changes: 9 additions & 0 deletions CHANGES.md
@@ -1,3 +1,12 @@
## Version 10.0.1

Parser Engine Changes:

- (bug) Fix sublanguage with no relevance score (#2506) [Josh Goebel][]

[Josh Goebel]: https://github.com/yyyc514


## Version 10.0.0

New languages:
Expand Down
24 changes: 18 additions & 6 deletions src/highlight.js
Expand Up @@ -443,6 +443,22 @@ const HLJS = function(hljs) {
}
}

// returns a valid highlight result, without actually
// doing any actual work, auto highlight starts with
// this and it's possible for small snippets that
// auto-detection may not find a better match
function justTextHighlightResult(code) {
const result = {
relevance: 0,
emitter: new options.__emitter(options),
value: escape(code),
illegal: false,
top: PLAINTEXT_LANGUAGE
};
result.emitter.addText(code)
return result;
}

/*
Highlighting with language detection. Accepts a string with the code to
highlight. Returns an object with the following properties:
Expand All @@ -456,11 +472,7 @@ const HLJS = function(hljs) {
*/
function highlightAuto(code, languageSubset) {
languageSubset = languageSubset || options.languages || Object.keys(languages);
var result = {
relevance: 0,
emitter: new options.__emitter(options),
value: escape(code)
};
var result = justTextHighlightResult(code)
var second_best = result;
languageSubset.filter(getLanguage).filter(autoDetection).forEach(function(name) {
var current = _highlight(name, code, false);
Expand Down Expand Up @@ -589,7 +601,7 @@ const HLJS = function(hljs) {
window.addEventListener('DOMContentLoaded', initHighlighting, false);
}

var PLAINTEXT_LANGUAGE = { disableAutodetect: true };
const PLAINTEXT_LANGUAGE = { disableAutodetect: true, name: 'Plain text' };

function registerLanguage(name, language) {
var lang;
Expand Down
3 changes: 3 additions & 0 deletions test/markup/xml/sublanguage_no_relevancy.expect.txt
@@ -0,0 +1,3 @@
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>foo();<span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>booger<span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>hjk<span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
3 changes: 3 additions & 0 deletions test/markup/xml/sublanguage_no_relevancy.txt
@@ -0,0 +1,3 @@
<script>foo();</script>
<script>booger</script>
<script>hjk</script>

0 comments on commit f6813cc

Please sign in to comment.