diff --git a/lib/tip_tap/html_renderable.rb b/lib/tip_tap/html_renderable.rb index 8879b15..ab389fd 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..0a34942 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..229d007 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