Skip to content

Commit

Permalink
Call insert-before hook properly (#1709)
Browse files Browse the repository at this point in the history
Fixes #1706 (also includes a demo).

The problem was that the `insert-before` hook was not called if `grammar` is undefined.
This also fixes that `callback` was not called in that case and that `env` of the `complete` hook did not have a `highlightedCode` property.

All other hooks are unchanged. The `insert-before` hook is only used by the Command line plugin, so there should be no other side effects.
  • Loading branch information
RunDevelopment authored and mAAdhaTTah committed Jan 22, 2019
1 parent 6ec71e0 commit 393ab16
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 53 deletions.
46 changes: 20 additions & 26 deletions components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,33 +212,37 @@ var _ = _self.Prism = {
code: code
};

var insertHighlightedCode = function (highlightedCode) {
env.highlightedCode = highlightedCode;

_.hooks.run('before-insert', env);

env.element.innerHTML = env.highlightedCode;

_.hooks.run('after-highlight', env);
_.hooks.run('complete', env);
callback && callback.call(env.element);
}

_.hooks.run('before-sanity-check', env);

if (!env.code || !env.grammar) {
if (env.code) {
_.hooks.run('before-highlight', env);
env.element.textContent = env.code;
_.hooks.run('after-highlight', env);
}
if (!env.code) {
_.hooks.run('complete', env);
return;
}

_.hooks.run('before-highlight', env);

if (!env.grammar) {
insertHighlightedCode(_.util.encode(env.code));
return;
}

if (async && _self.Worker) {
var worker = new Worker(_.filename);

worker.onmessage = function(evt) {
env.highlightedCode = evt.data;

_.hooks.run('before-insert', env);

env.element.innerHTML = env.highlightedCode;

_.hooks.run('after-highlight', env);
_.hooks.run('complete', env);
callback && callback.call(env.element);
insertHighlightedCode(evt.data);
};

worker.postMessage(JSON.stringify({
Expand All @@ -248,17 +252,7 @@ var _ = _self.Prism = {
}));
}
else {
env.highlightedCode = _.highlight(env.code, env.grammar, env.language);

_.hooks.run('before-insert', env);

env.element.innerHTML = env.highlightedCode;

_.hooks.run('after-highlight', env);

_.hooks.run('complete', env);

callback && callback.call(element);
insertHighlightedCode(_.highlight(env.code, env.grammar, env.language));
}
},

Expand Down
2 changes: 1 addition & 1 deletion components/prism-core.min.js

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

46 changes: 20 additions & 26 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,33 +217,37 @@ var _ = _self.Prism = {
code: code
};

var insertHighlightedCode = function (highlightedCode) {
env.highlightedCode = highlightedCode;

_.hooks.run('before-insert', env);

env.element.innerHTML = env.highlightedCode;

_.hooks.run('after-highlight', env);
_.hooks.run('complete', env);
callback && callback.call(env.element);
}

_.hooks.run('before-sanity-check', env);

if (!env.code || !env.grammar) {
if (env.code) {
_.hooks.run('before-highlight', env);
env.element.textContent = env.code;
_.hooks.run('after-highlight', env);
}
if (!env.code) {
_.hooks.run('complete', env);
return;
}

_.hooks.run('before-highlight', env);

if (!env.grammar) {
insertHighlightedCode(_.util.encode(env.code));
return;
}

if (async && _self.Worker) {
var worker = new Worker(_.filename);

worker.onmessage = function(evt) {
env.highlightedCode = evt.data;

_.hooks.run('before-insert', env);

env.element.innerHTML = env.highlightedCode;

_.hooks.run('after-highlight', env);
_.hooks.run('complete', env);
callback && callback.call(env.element);
insertHighlightedCode(evt.data);
};

worker.postMessage(JSON.stringify({
Expand All @@ -253,17 +257,7 @@ var _ = _self.Prism = {
}));
}
else {
env.highlightedCode = _.highlight(env.code, env.grammar, env.language);

_.hooks.run('before-insert', env);

env.element.innerHTML = env.highlightedCode;

_.hooks.run('after-highlight', env);

_.hooks.run('complete', env);

callback && callback.call(element);
insertHighlightedCode(_.highlight(env.code, env.grammar, env.language));
}
},

Expand Down

0 comments on commit 393ab16

Please sign in to comment.