Skip to content

Commit

Permalink
Preserve whitespace in text nodes (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwhitehouse authored Sep 19, 2024
1 parent 0b5bc30 commit 3b3b87f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/tip_tap/nodes/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def self.from_json(json)
end

def to_h
data = {type: type_name, text: text.presence || ""}
data = {type: type_name, text: text || ""}
data[:marks] = marks.map(&:deep_symbolize_keys) unless marks.empty?
data
end
Expand Down
19 changes: 15 additions & 4 deletions spec/tip_tap/nodes/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,22 @@
end

describe "to_h" do
it "returns a JSON object" do
node = TipTap::Nodes::Text.new("Hello World!", marks: [{type: "bold"}, {type: "italic"}])
json = node.to_h
context "with marks" do
it "returns a JSON object with marks" do
node = TipTap::Nodes::Text.new("Hello World!", marks: [{type: "bold"}, {type: "italic"}])
json = node.to_h

expect(json).to eq({type: "text", text: "Hello World!", marks: [{type: "bold"}, {type: "italic"}]})
end
end

context "whitespace" do
it "returns a JSON object that preserves whitespace" do
node = TipTap::Nodes::Text.new(" ")
json = node.to_h

expect(json).to eq({type: "text", text: "Hello World!", marks: [{type: "bold"}, {type: "italic"}]})
expect(json).to eq({type: "text", text: " "})
end
end
end
end

0 comments on commit 3b3b87f

Please sign in to comment.