Skip to content

Commit b062978

Browse files
committedApr 12, 2016
updated copy semantics to match rich-text's deepcopy change
1 parent 7082614 commit b062978

File tree

4 files changed

+20
-29
lines changed

4 files changed

+20
-29
lines changed
 

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ python setup.py test
6868

6969
## API Reference
7070

71-
Fully implements the original [ottypes/rich-text](https://github.com/ottypes/rich-text) interface. So feel free to use their [API reference](https://github.com/ottypes/rich-text).
71+
Fully implements the original [ottypes/rich-text](https://github.com/ottypes/rich-text) interface. So feel free to use its [API reference](https://github.com/ottypes/rich-text).
7272

7373
## References
7474
- [High-Latency, Low-Bandwidth Windowing in the Jupiter Collaboration System, 1995](http://lively-kernel.org/repository/webwerkstatt/projects/Collaboration/paper/Jupiter.pdf)
7575
- [Google Wave Operational Transformation, 2010](http://wave-protocol.googlecode.com/hg/whitepapers/operational-transform/operational-transform.html)
7676
- [Wikipedia: Operational Transformation](https://en.wikipedia.org/wiki/Operational_transformation)
7777
- [ottypes/rich-text](https://github.com/ottypes/rich-text)
78-
- [Quill Rich Text Editor](https://github.com/quilljs/quill/) - WYSIWYG Javascript Editor that products Delta objects on user changes
78+
- [Quill Rich Text Editor](https://github.com/quilljs/quill/) - WYSIWYG Javascript Editor that produces Delta objects on changes
7979
- [ShareJS](https://github.com/share/ShareJS) - NodeJS server that implements the OT collaboration protocol
8080

8181
## License

‎richtextpy/delta.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from copy import deepcopy
88
from diff_match_patch import diff_match_patch
99
from type_utils import is_list, is_string, is_dict, is_number, INFINITY
10-
from op import op_length, op_clone, op_insert_string, attr_compose, attr_transform, attr_diff
10+
from op import op_length, op_insert_string, attr_compose, attr_transform, attr_diff
1111
from opiterator import OpIterator
1212

1313

@@ -68,7 +68,7 @@ def push(self, op):
6868
# this requires combining same operation types when possible
6969
# and re-ordering same-index inserts and deletes to ensure canonical order
7070

71-
new_op = op_clone(op)
71+
new_op = deepcopy(op)
7272

7373
# if no existing ops, simply add new_op
7474
if len(self.ops) == 0:

‎richtextpy/op.py

+8-25
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@ def op_length(op):
1515
elif 'retain' in op:
1616
return op['retain']
1717

18-
def op_clone(op):
19-
if not is_dict(op):
20-
return dict()
21-
22-
cloned_op = deepcopy(op)
23-
24-
# remove top-level null keys
25-
for key in cloned_op:
26-
if cloned_op[key] == None:
27-
del cloned_op[key]
28-
29-
return cloned_op
30-
3118
def op_insert_string(op):
3219
if 'insert' in op:
3320
if is_string(op['insert']):
@@ -37,26 +24,22 @@ def op_insert_string(op):
3724

3825
raise Exception('Diff called on a non-document')
3926

40-
def attr_clone(attributes, keep_null):
41-
if not is_dict(attributes):
42-
return dict()
43-
44-
cloned_attributes = dict()
45-
for key in attributes.keys():
46-
if (attributes[key] != None) or keep_null:
47-
cloned_attributes[key] = attributes[key]
48-
49-
return cloned_attributes
50-
5127
def attr_compose(a, b, keep_null):
5228
if not is_dict(a):
5329
a = dict()
5430
if not is_dict(b):
5531
b = dict()
56-
attributes = attr_clone(b, keep_null)
32+
33+
attributes = deepcopy(b)
34+
if not keep_null:
35+
for key in attributes.keys():
36+
if attributes[key] == None:
37+
del attributes[key]
38+
5739
for key in a.keys():
5840
if key not in b:
5941
attributes[key] = a[key]
42+
6043
if len(attributes.keys()) > 0:
6144
return attributes
6245
else:

‎tests/test_delta.py

+8
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,14 @@ def test_diff(self):
571571
expected = Delta().insert({'image': 'http://quilljs.com'}).delete(1)
572572
self.assertEqual(a.diff(b), expected)
573573

574+
# embed object change
575+
embed = {'image': 'http://quilljs.com'}
576+
a = Delta().insert(embed)
577+
embed['image'] = 'http://github.com'
578+
b = Delta().insert(embed)
579+
expected = Delta().insert({'image': 'http://github.com'}).delete(1)
580+
self.assertEqual(a.diff(b), expected)
581+
574582
# embed false positive
575583
a = Delta().insert(1)
576584
b = Delta().insert(chr(0)) # Placeholder char for embed in diff()

0 commit comments

Comments
 (0)
Please sign in to comment.