Skip to content

Commit

Permalink
Fix DFS bug
Browse files Browse the repository at this point in the history
The DFS function uses an object as a key into another object. This
doesn't work, because the key gets stringified to "[object
Object]" and is therefore useless.
  • Loading branch information
zeitgeist87 committed Feb 3, 2016
1 parent 4562b18 commit b86c727
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
16 changes: 12 additions & 4 deletions components/prism-core.js
Expand Up @@ -16,6 +16,7 @@ var Prism = (function(){

// Private helper vars
var lang = /\blang(?:uage)?-(\w+)\b/i;
var uniqueId = 0;

var _ = _self.Prism = {
util: {
Expand All @@ -33,6 +34,13 @@ var _ = _self.Prism = {
return Object.prototype.toString.call(o).match(/\[object (\w+)\]/)[1];
},

objId: function (obj) {
if (!obj['__id']) {
Object.defineProperty(obj, '__id', { value: ++uniqueId });
}
return obj['__id'];
},

// Deep clone a language definition (e.g. to extend it)
clone: function (o) {
var type = _.util.type(o);
Expand Down Expand Up @@ -131,12 +139,12 @@ var _ = _self.Prism = {
if (o.hasOwnProperty(i)) {
callback.call(o, i, o[i], type || i);

if (_.util.type(o[i]) === 'Object' && !visited[o[i]]) {
visited[o[i]] = true;
if (_.util.type(o[i]) === 'Object' && !visited[_.util.objId(o[i])]) {
visited[_.util.objId(o[i])] = true;
_.languages.DFS(o[i], callback, null, visited);
}
else if (_.util.type(o[i]) === 'Array' && !visited[o[i]]) {
visited[o[i]] = true;
else if (_.util.type(o[i]) === 'Array' && !visited[_.util.objId(o[i])]) {
visited[_.util.objId(o[i])] = true;
_.languages.DFS(o[i], callback, i, visited);
}
}
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.

16 changes: 12 additions & 4 deletions prism.js
Expand Up @@ -21,6 +21,7 @@ var Prism = (function(){

// Private helper vars
var lang = /\blang(?:uage)?-(\w+)\b/i;
var uniqueId = 0;

var _ = _self.Prism = {
util: {
Expand All @@ -38,6 +39,13 @@ var _ = _self.Prism = {
return Object.prototype.toString.call(o).match(/\[object (\w+)\]/)[1];
},

objId: function (obj) {
if (!obj['__id']) {
Object.defineProperty(obj, '__id', { value: ++uniqueId });
}
return obj['__id'];
},

// Deep clone a language definition (e.g. to extend it)
clone: function (o) {
var type = _.util.type(o);
Expand Down Expand Up @@ -136,12 +144,12 @@ var _ = _self.Prism = {
if (o.hasOwnProperty(i)) {
callback.call(o, i, o[i], type || i);

if (_.util.type(o[i]) === 'Object' && !visited[o[i]]) {
visited[o[i]] = true;
if (_.util.type(o[i]) === 'Object' && !visited[_.util.objId(o[i])]) {
visited[_.util.objId(o[i])] = true;
_.languages.DFS(o[i], callback, null, visited);
}
else if (_.util.type(o[i]) === 'Array' && !visited[o[i]]) {
visited[o[i]] = true;
else if (_.util.type(o[i]) === 'Array' && !visited[_.util.objId(o[i])]) {
visited[_.util.objId(o[i])] = true;
_.languages.DFS(o[i], callback, i, visited);
}
}
Expand Down

0 comments on commit b86c727

Please sign in to comment.