From d9eb85c780032f1e20dd566814be30c1dad518fb Mon Sep 17 00:00:00 2001 From: Luis Henrique Witz Date: Fri, 20 Dec 2024 13:17:55 -0300 Subject: [PATCH] Add support to textAlign attribute when converting to HTML (#33) * Add support to textAlign attribute when converting to HTML --- lib/tip_tap/html_renderable.rb | 12 +++++++++++- spec/tip_tap/nodes/heading_spec.rb | 10 ++++++++++ spec/tip_tap/nodes/paragraph_spec.rb | 10 ++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/tip_tap/html_renderable.rb b/lib/tip_tap/html_renderable.rb index 8879b15..012f235 100644 --- a/lib/tip_tap/html_renderable.rb +++ b/lib/tip_tap/html_renderable.rb @@ -54,7 +54,17 @@ def html_class_name end def to_html - content_tag(html_tag, safe_join(content.map(&:to_html)), class: html_class_name) + content_tag(html_tag, safe_join(content.map(&:to_html)), html_attributes) + end + + def html_attributes + {style: inline_styles, class: html_class_name}.reject { |key, value| value.blank? } + end + + def inline_styles + styles = [] + styles << "text-align: #{attrs["textAlign"]};" if attrs["textAlign"] + styles.join(" ") end end end diff --git a/spec/tip_tap/nodes/heading_spec.rb b/spec/tip_tap/nodes/heading_spec.rb index f7b4848..e7fd882 100644 --- a/spec/tip_tap/nodes/heading_spec.rb +++ b/spec/tip_tap/nodes/heading_spec.rb @@ -11,6 +11,16 @@ expect(html).to be_a(String) expect(html).to eq("

") end + + context "when the textAlign attribute is present" do + it "returns a tag with the specified text alignment style" do + node = TipTap::Nodes::Heading.from_json({content: [], attrs: {"textAlign" => "center", :level => 2}}) + html = node.to_html + + expect(html).to be_a(String) + expect(html).to eq('

') + end + end end describe "level" do diff --git a/spec/tip_tap/nodes/paragraph_spec.rb b/spec/tip_tap/nodes/paragraph_spec.rb index a08697a..cf361a9 100644 --- a/spec/tip_tap/nodes/paragraph_spec.rb +++ b/spec/tip_tap/nodes/paragraph_spec.rb @@ -11,6 +11,16 @@ expect(html).to be_a(String) expect(html).to eq("

") end + + context "when the textAlign attribute is present" do + it "returns a p tag with the specified text alignment style" do + node = TipTap::Nodes::Paragraph.from_json({content: [], attrs: {"textAlign" => "center"}}) + html = node.to_html + + expect(html).to be_a(String) + expect(html).to eq('

') + end + end end describe "to_h" do