From 02894e1e86f70b6fbb7a50b389b69ab00a7f0f9a Mon Sep 17 00:00:00 2001 From: Andreas Rohner Date: Mon, 21 Dec 2015 19:00:26 +0100 Subject: [PATCH] Prevent infinite recursion in DFS --- components/prism-core.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/components/prism-core.js b/components/prism-core.js index aef621f40b..8f02f0ff72 100644 --- a/components/prism-core.js +++ b/components/prism-core.js @@ -125,16 +125,19 @@ var _ = _self.Prism = { }, // Traverse a language definition with Depth First Search - DFS: function(o, callback, type) { + DFS: function(o, callback, type, visited) { + visited = visited || {}; for (var i in o) { if (o.hasOwnProperty(i)) { callback.call(o, i, o[i], type || i); - if (_.util.type(o[i]) === 'Object') { - _.languages.DFS(o[i], callback); + if (_.util.type(o[i]) === 'Object' && !visited[o[i]]) { + visited[o[i]] = true; + _.languages.DFS(o[i], callback, null, visited); } - else if (_.util.type(o[i]) === 'Array') { - _.languages.DFS(o[i], callback, i); + else if (_.util.type(o[i]) === 'Array' && !visited[o[i]]) { + visited[o[i]] = true; + _.languages.DFS(o[i], callback, i, visited); } } }