Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ New styles:
none.

Improvements:
- fix(parser): Look-ahead regex now work for end matches also (#2237) [Josh Goebel][]
- fix(parser): Better errors when a language is missing (#2236) [Josh Goebel][]

[Josh Goebel]: https://github.com/yyyc514
Expand Down
9 changes: 5 additions & 4 deletions docs/language-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,14 @@ The goal of Highlight.js is to support whatever regex features Javascript itself

Things we support now that we did not always:

* look-ahead matching for `begin` (#2135)
* look-ahead matching for `illegal` (#2135)
* back-references within your regex (#1897)
* look-ahead regex matching for `begin` (#2135)
* look-ahead regex matching for `end` (#2237)
* look-ahead regex matching for `illegal` (#2135)
* back-references within your regex matches (#1897)
* look-behind matching (when JS supports it) for `begin` (#2135)

Things we currently know are still issues:

* look-ahead matching for `end` matchers
* look-behind matching (when JS supports it) for `end` matchers


Expand Down
3 changes: 2 additions & 1 deletion src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,8 @@ https://highlightjs.org/

function doEndMatch(match) {
var lexeme = match[0];
var end_mode = endOfMode(top, lexeme);
var matchPlusRemainder = value.substr(match.index);
var end_mode = endOfMode(top, matchPlusRemainder);
if (!end_mode) { return; }

var origin = top;
Expand Down
1 change: 1 addition & 0 deletions test/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
describe('hljs', function() {
require('./reuse-endsWithParent');
require('./should-not-destroyData');
require('./look-ahead-end-matchers');
});
31 changes: 31 additions & 0 deletions test/parser/look-ahead-end-matchers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const hljs = require('../../build');

describe("parser specifics", function () {

// CONTEXT: https://github.com/highlightjs/highlight.js/pull/2219
describe("a grammar with look-ahead end matchers", () => {
it("should match successfully", () => {
hljs.registerLanguage('test-language', (hljs) => {

// broken regex from old Fortran ruleset
const PATTERN = {
className: "pattern",
begin: '[A-Z]{3}',
// followed by at least one space
end: '\\d{3}(?=\\s+)'
}

return {
contains: [PATTERN]
};
});

hljs.highlight('test-language', 'ABC123 is the secret. XYZ123. End of string: ABC123').value
.should.equal(
// one full match at beginning, other match begins with XYZ but then never terminates,
// so the end of the parsing finally closes the span tag
'<span class="hljs-pattern">ABC123</span> is the secret. <span class="hljs-pattern">XYZ123. End of string: ABC123</span>'
)
})
})
})