Commit 292cebd278570da01baa2086d788694b72dd1262

Lea Verou 2015-12-24T14:16:10

Merge pull request #841 from zeitgeist87/TestBranch Prevent infinite recursion in DFS

diff --git a/components/prism-core.js b/components/prism-core.js
index aef621f..8f02f0f 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);
 					}
 				}
 			}