Skip to content

Commit

Permalink
(parser) use null prototype objects for languages/aliases (#2636)
Browse files Browse the repository at this point in the history
Fix: Discord uses getLanguage to validate that a language specified exists in highlightJS and retrieve metadata about the language for code block highlighting in chat. Because highlightJS returns prototype values instead of the highlight languages themselves, the result is a few different bugs in our clients which expect the return type to be only `Language | undefined`.
  • Loading branch information
night committed Jul 21, 2020
1 parent eec9b84 commit d8692db
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -8,10 +8,12 @@ Language Improvements:

- enh(matlab) Add new R2019b `arguments` keyword and fix `enumeration` keyword (#2619) [Andrew Janke][]
- fix(kotlin) Remove very old keywords and update example code (#2623) [kageru][]
- fix(night) Prevent object prototypes method values from being returned in `getLanguage` (#2636) [night][]

[Andrew Janke]: https://github.com/apjanke
[Samia Ali]: https://github.com/samiaab1990
[kageru]: https://github.com/kageru
[night]: https://github.com/night


## Version 10.1.1
Expand Down
4 changes: 2 additions & 2 deletions src/highlight.js
Expand Up @@ -29,9 +29,9 @@ const HLJS = function(hljs) {

// Global internal variables used within the highlight.js library.
/** @type {Record<string, Language>} */
var languages = {};
var languages = Object.create(null);
/** @type {Record<string, string>} */
var aliases = {};
var aliases = Object.create(null);
/** @type {HLJSPlugin[]} */
var plugins = [];

Expand Down
12 changes: 12 additions & 0 deletions test/api/getLanguage.js
Expand Up @@ -41,4 +41,16 @@ describe('.getLanguage()', () => {
result.should.have.property('aliases').with.containEql('cs');
should.strictEqual(result, hljs.getLanguage('csharp'))
});

it('should not succeed for constructor', () => {
const result = hljs.getLanguage('constructor');

should.strictEqual(result, undefined);
});

it('should not succeed for __proto__', () => {
const result = hljs.getLanguage('__proto__');

should.strictEqual(result, undefined);
});
});

0 comments on commit d8692db

Please sign in to comment.