Skip to content

Commit

Permalink
Core: Allow cloning of circular structures. (#1345)
Browse files Browse the repository at this point in the history
  • Loading branch information
Golmote authored Mar 13, 2018
1 parent efdccbf commit f90d555
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
21 changes: 18 additions & 3 deletions components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,38 @@ var _ = _self.Prism = {
},

// Deep clone a language definition (e.g. to extend it)
clone: function (o) {
clone: function (o, visited) {
var type = _.util.type(o);
visited = visited || {};

switch (type) {
case 'Object':
if (visited[_.util.objId(o)]) {
return visited[_.util.objId(o)];
}
var clone = {};
visited[_.util.objId(o)] = clone;

for (var key in o) {
if (o.hasOwnProperty(key)) {
clone[key] = _.util.clone(o[key]);
clone[key] = _.util.clone(o[key], visited);
}
}

return clone;

case 'Array':
return o.map(function(v) { return _.util.clone(v); });
if (visited[_.util.objId(o)]) {
return visited[_.util.objId(o)];
}
var clone = [];
visited[_.util.objId(o)] = clone;

o.forEach(function (v, i) {
clone[i] = _.util.clone(v, visited);
});

return clone;
}

return o;
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.

21 changes: 18 additions & 3 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,38 @@ var _ = _self.Prism = {
},

// Deep clone a language definition (e.g. to extend it)
clone: function (o) {
clone: function (o, visited) {
var type = _.util.type(o);
visited = visited || {};

switch (type) {
case 'Object':
if (visited[_.util.objId(o)]) {
return visited[_.util.objId(o)];
}
var clone = {};
visited[_.util.objId(o)] = clone;

for (var key in o) {
if (o.hasOwnProperty(key)) {
clone[key] = _.util.clone(o[key]);
clone[key] = _.util.clone(o[key], visited);
}
}

return clone;

case 'Array':
return o.map(function(v) { return _.util.clone(v); });
if (visited[_.util.objId(o)]) {
return visited[_.util.objId(o)];
}
var clone = [];
visited[_.util.objId(o)] = clone;

o.forEach(function (v, i) {
clone[i] = _.util.clone(v, visited);
});

return clone;
}

return o;
Expand Down

0 comments on commit f90d555

Please sign in to comment.