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

fix(parser) fixes sublanguage with no rule matches #2506

Merged
merged 3 commits into from Apr 26, 2020
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
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>