Skip to content

Commit

Permalink
Add support to textAlign attribute when converting to HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
luiswitz committed Nov 23, 2024
1 parent acc5ff8 commit 18596c3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/tip_tap/html_renderable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions spec/tip_tap/nodes/heading_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
expect(html).to be_a(String)
expect(html).to eq("<h2></h2>")
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('<h2 style="text-align: center;"></h2>')
end
end
end

describe "level" do
Expand Down
10 changes: 10 additions & 0 deletions spec/tip_tap/nodes/paragraph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
expect(html).to be_a(String)
expect(html).to eq("<p></p>")
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('<p style="text-align: center;"></p>')
end
end
end

describe "to_h" do
Expand Down

0 comments on commit 18596c3

Please sign in to comment.