Skip to content
Open
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
11 changes: 6 additions & 5 deletions js/rainbow.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ window['Rainbow'] = (function() {
global_class,

/**
* @type {null|Function}
* @type {Array}
*/
onHighlight;
onHighlight = [];

/**
* cross browser get attribute for an element
Expand Down Expand Up @@ -631,8 +631,9 @@ window['Rainbow'] = (function() {
replacement_positions = {};

// if you have a listener attached tell it that this block is now highlighted
if (onHighlight) {
onHighlight(block, language);
if (onHighlight.length > 0) {
for(ohn=0; ohn<onHighlight.length; ohn++)
onHighlight[ohn](block, language);
}

// process the next block
Expand Down Expand Up @@ -739,7 +740,7 @@ window['Rainbow'] = (function() {
* @param {Function} callback
*/
onHighlight: function(callback) {
onHighlight = callback;
onHighlight.push(callback);
},

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,31 @@ <h1>API Test Page</h1>
$("#stuff").append('<p>got callback<p>');
});
}, 1000);

// Set an onHighlight() callback
setTimeout(function() {
Rainbow.onHighlight(function(){
$("#stuff").append('<p>Got onHighlight() callback</p>')
});

$("#stuff").append('<p>code inserted dynamically with callback:</p><pre><code data-language="javascript">var html = $("#stuff").html();</code></pre>');
Rainbow.color();
}, 1250);

// Set multiple onHighlight() callbacks
setTimeout(function() {
Rainbow.onHighlight(function(){
window._OHTest = 1;
});

Rainbow.onHighlight(function(){
window._OHTest++;
$("#stuff").append('<p>This number should be 2: <b>'
+ window._OHTest + '</b></p>');
});

$("#stuff").append('<p>code inserted dynamically with callback:</p><pre><code data-language="javascript">var html = $("#stuff").html();</code></pre>');
Rainbow.color();
}, 1500);
</script>
</body>