Skip to content

Commit

Permalink
Doubly check the manual flag (#1957)
Browse files Browse the repository at this point in the history
This fixes the behavior of manual highlighting where it would highlight everything even with `manual: true`.
This also adds doc as to how manual highlighting works.
  • Loading branch information
TimWolla authored and RunDevelopment committed Sep 3, 2019
1 parent f7eaa61 commit d49f0f2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
28 changes: 19 additions & 9 deletions components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,19 +531,29 @@ var script = document.currentScript || [].slice.call(document.getElementsByTagNa

if (script) {
_.filename = script.src;

if (script.hasAttribute('data-manual')) {
_.manual = true;
}
}

if (!_.manual && !script.hasAttribute('data-manual')) {
if(document.readyState !== 'loading') {
if (window.requestAnimationFrame) {
window.requestAnimationFrame(_.highlightAll);
} else {
window.setTimeout(_.highlightAll, 16);
}
if (!_.manual) {
function highlightAutomaticallyCallback() {
if (!_.manual) {
_.highlightAll();
}
else {
document.addEventListener('DOMContentLoaded', _.highlightAll);
}

if(document.readyState !== 'loading') {
if (window.requestAnimationFrame) {
window.requestAnimationFrame(highlightAutomaticallyCallback);
} else {
window.setTimeout(highlightAutomaticallyCallback, 16);
}
}
else {
document.addEventListener('DOMContentLoaded', highlightAutomaticallyCallback);
}
}

return _;
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.

9 changes: 8 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,16 @@ <h1>Basic usage</h1>
<pre><code>&lt;pre>&lt;code class="language-css">p { color: red }&lt;/code>&lt;/pre></code></pre>
<p>If you use that pattern, the <code>&lt;pre></code> will automatically get the <code>language-xxxx</code> class (if it doesn’t already have it) and will be styled as a code block.</p>

<p>If you want to prevent any elements from being automatically highlighted, you can use the attribute <code>data-manual</code> on the <code>&lt;script></code> element you used for prism and use the <a href="extending.html#api">API</a>.
<p>If you want to prevent any elements from being automatically highlighted and instead use the <a href="extending.html#api">API</a>, you can set <code class="language-javascript">Prism.manual</code> to <code class="language-javascript">true</code> before the <code>DOMContentLoaded</code> event is fired. By setting the <code>data-manual</code> attribute on the <code>&lt;script></code> element containing Prism core, this will be done automatically.
Example:</p>
<pre><code>&lt;script src="prism.js" data-manual>&lt;/script></code></pre>
<p>or</p>
<pre><code>&lt;script>
window.Prism = window.Prism || {};
window.Prism.manual = true;
&lt;/script>
&lt;script src="prism.js">&lt;/script></code></pre>

<h2>Usage with CDNs</h2>

<p>In combination with CDNs, we recommend using the <a href="plugins/autoloader">Autoloader plugin</a> which automatically loads languages when necessary.</p>
Expand Down
28 changes: 19 additions & 9 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,19 +536,29 @@ var script = document.currentScript || [].slice.call(document.getElementsByTagNa

if (script) {
_.filename = script.src;

if (script.hasAttribute('data-manual')) {
_.manual = true;
}
}

if (!_.manual && !script.hasAttribute('data-manual')) {
if(document.readyState !== 'loading') {
if (window.requestAnimationFrame) {
window.requestAnimationFrame(_.highlightAll);
} else {
window.setTimeout(_.highlightAll, 16);
}
if (!_.manual) {
function highlightAutomaticallyCallback() {
if (!_.manual) {
_.highlightAll();
}
else {
document.addEventListener('DOMContentLoaded', _.highlightAll);
}

if(document.readyState !== 'loading') {
if (window.requestAnimationFrame) {
window.requestAnimationFrame(highlightAutomaticallyCallback);
} else {
window.setTimeout(highlightAutomaticallyCallback, 16);
}
}
else {
document.addEventListener('DOMContentLoaded', highlightAutomaticallyCallback);
}
}

return _;
Expand Down

0 comments on commit d49f0f2

Please sign in to comment.