Skip to content

Commit

Permalink
JSON: Kinda fixed comment issue (PrismJS#1853)
Browse files Browse the repository at this point in the history
This is a partial workaround for the greedy matching bug so that pure JSON will always be matched correctly while JSON + comments might suffer from some issues.
  • Loading branch information
RunDevelopment authored and Mattchewone committed Apr 23, 2019
1 parent 77e403c commit af25528
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
2 changes: 1 addition & 1 deletion components/prism-json.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Prism.languages.json = {
'comment': /\/\/.*|\/\*[\s\S]*?(?:\*\/|$)/,
'property': {
pattern: /"(?:\\.|[^\\"\r\n])*"(?=\s*:)/,
greedy: true
Expand All @@ -8,6 +7,7 @@ Prism.languages.json = {
pattern: /"(?:\\.|[^\\"\r\n])*"(?!\s*:)/,
greedy: true
},
'comment': /\/\/.*|\/\*[\s\S]*?(?:\*\/|$)/,
'number': /-?\d+\.?\d*(e[+-]?\d+)?/i,
'punctuation': /[{}[\],]/,
'operator': /:/,
Expand Down
2 changes: 1 addition & 1 deletion components/prism-json.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 43 additions & 13 deletions plugins/line-highlight/prism-line-highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ var isLineHeightRounded = (function() {
}
}());

function makeActions() {
var actions = [];

return {
actions: actions,
apply: function(fn, context, args) {
actions.push(function() {
fn.apply(context, args);
});
},
set: function(obj, key, value) {
actions.push(function() {
obj[key] = value;
});
}
};
}

function highlightLines(pre, lines, classes) {
lines = typeof lines === 'string' ? lines : pre.getAttribute('data-line');

Expand All @@ -44,6 +62,7 @@ function highlightLines(pre, lines, classes) {
var parseMethod = isLineHeightRounded() ? parseInt : parseFloat;
var lineHeight = parseMethod(getComputedStyle(pre).lineHeight);
var hasLineNumbers = hasClass(pre, 'line-numbers');
var action = makeActions();

for (var i=0, currentRange; currentRange = ranges[i++];) {
var range = currentRange.split('-');
Expand All @@ -53,8 +72,8 @@ function highlightLines(pre, lines, classes) {

var line = pre.querySelector('.line-highlight[data-range="' + currentRange + '"]') || document.createElement('div');

line.setAttribute('aria-hidden', 'true');
line.setAttribute('data-range', currentRange);
action.apply(line.setAttribute, line,['aria-hidden', 'true']);
action.apply(line.setAttribute, line,['data-range', currentRange]);
line.className = (classes || '') + ' line-highlight';

//if the line-numbers plugin is enabled, then there is no reason for this plugin to display the line numbers
Expand All @@ -63,32 +82,33 @@ function highlightLines(pre, lines, classes) {
var endNode = Prism.plugins.lineNumbers.getLine(pre, end);

if (startNode) {
line.style.top = startNode.offsetTop + 'px';
action.set(line.style, "top", startNode.offsetTop + 'px');
}

if (endNode) {
line.style.height = (endNode.offsetTop - startNode.offsetTop) + endNode.offsetHeight + 'px';
action.set(line.style, "height", (endNode.offsetTop - startNode.offsetTop) + endNode.offsetHeight + 'px' );
}
} else {
line.setAttribute('data-start', start);
action.apply(line.setAttribute, line, ['data-start', start]);

if(end > start) {
line.setAttribute('data-end', end);
action.apply(line.setAttribute, line, ['data-end', end]);
}

line.style.top = (start - offset - 1) * lineHeight + 'px';
action.set(line.style, "top", (start - offset - 1) * lineHeight + 'px' );

line.textContent = new Array(end - start + 2).join(' \n');
action.set(line, "textContent", new Array(end - start + 2).join(' \n') );
}

//allow this to play nicely with the line-numbers plugin
if(hasLineNumbers) {
//need to attack to pre as when line-numbers is enabled, the code tag is relatively which screws up the positioning
pre.appendChild(line);
action.apply( pre.appendChild, pre, [line] );
} else {
(pre.querySelector('code') || pre).appendChild(line);
action(pre.appendChild, pre.querySelector('code') || pre, [line]);
}
}
return action.actions;
}

function applyHash() {
Expand Down Expand Up @@ -116,7 +136,10 @@ function applyHash() {
pre.setAttribute('data-line', '');
}

highlightLines(pre, range, 'temporary ');
var actions = highlightLines(pre, range, 'temporary ');
actions.forEach(function(action){
action();
});

document.querySelector('.temporary.line-highlight').scrollIntoView();
}
Expand Down Expand Up @@ -165,16 +188,23 @@ Prism.hooks.add('complete', function completeHook(env) {
if (hasClass(pre, 'line-numbers') && hasLineNumbers && !isLineNumbersLoaded) {
Prism.hooks.add('line-numbers', completeHook);
} else {
highlightLines(pre, lines);
var actions = highlightLines(pre, lines);
actions.forEach(function(action){
action();
});
fakeTimer = setTimeout(applyHash, 1);
}
});

window.addEventListener('hashchange', applyHash);
window.addEventListener('resize', function () {
var preElements = document.querySelectorAll('pre[data-line]');
var actions = [];
Array.prototype.forEach.call(preElements, function (pre) {
highlightLines(pre);
actions.push.apply(actions, highlightLines(pre));
});
actions.forEach(function(action) {
action();
});
});

Expand Down
2 changes: 1 addition & 1 deletion plugins/line-highlight/prism-line-highlight.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions tests/languages/json/issue1852.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"A": "/*",
"B": "B",
"C": "C"
}

----------------------------------------------------

[
["punctuation", "{"],
["property", "\"A\""],
["operator", ":"],
["string", "\"/*\""],
["punctuation", ","],
["property", "\"B\""],
["operator", ":"],
["string", "\"B\""],
["punctuation", ","],
["property", "\"C\""],
["operator", ":"],
["string", "\"C\""],
["punctuation", "}"]
]

----------------------------------------------------

Checks for issue #1852.

0 comments on commit af25528

Please sign in to comment.