Skip to content

Commit ee4c08f

Browse files
committed
Fix dup modifying original store
1 parent 0a3ec8f commit ee4c08f

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/attributed_string/klass.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ def initialize(string = "", **attrs)
2020
def dup
2121
super.tap do |copy|
2222
new_store = @store.map do |entry|
23-
entry[:range] = entry[:range].dup
24-
entry.dup
23+
entry_dup = entry.dup
24+
entry_dup[:range] = entry_dup[:range].dup
25+
entry_dup
2526
end
2627
copy.instance_variable_set(:@store, new_store)
2728
end

test/initializers_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,12 @@ def test_dup
2424
end
2525
end
2626

27+
def test_dup_does_not_modify_original_store
28+
@attr_string.add_attrs(0..4, bold: true)
29+
original_range_id = @attr_string.instance_variable_get(:@store).first[:range].object_id
30+
@attr_string.dup
31+
after_range_id = @attr_string.instance_variable_get(:@store).first[:range].object_id
32+
assert_equal original_range_id, after_range_id
33+
end
34+
2735
end

0 commit comments

Comments
 (0)