Skip to content

Commit 02894e1

Browse files
committed
Prevent infinite recursion in DFS
1 parent 2a21d56 commit 02894e1

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

components/prism-core.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,19 @@ var _ = _self.Prism = {
125125
},
126126

127127
// Traverse a language definition with Depth First Search
128-
DFS: function(o, callback, type) {
128+
DFS: function(o, callback, type, visited) {
129+
visited = visited || {};
129130
for (var i in o) {
130131
if (o.hasOwnProperty(i)) {
131132
callback.call(o, i, o[i], type || i);
132133

133-
if (_.util.type(o[i]) === 'Object') {
134-
_.languages.DFS(o[i], callback);
134+
if (_.util.type(o[i]) === 'Object' && !visited[o[i]]) {
135+
visited[o[i]] = true;
136+
_.languages.DFS(o[i], callback, null, visited);
135137
}
136-
else if (_.util.type(o[i]) === 'Array') {
137-
_.languages.DFS(o[i], callback, i);
138+
else if (_.util.type(o[i]) === 'Array' && !visited[o[i]]) {
139+
visited[o[i]] = true;
140+
_.languages.DFS(o[i], callback, i, visited);
138141
}
139142
}
140143
}

0 commit comments

Comments
 (0)