Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/rich-text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ def self.configure

RichText.configure do |c|

c.html_inline_formats = {
c.html_inline_formats ||= {
bold: { tag: 'strong' },
br: { tag: 'br' },
hr: { tag: 'hr', block_format: false },
italic: { tag: 'em' },
link: { tag: 'a', apply: ->(el, op, ctx){ el[:href] = op.attributes[:link] } }
link: { tag: 'a', apply: ->(el, op, ctx){ el[:href] = op.attributes[:link] } },
size: { tag: 'span', apply: ->(el, op, ctx) { el[:style] = el[:style].to_s + "font-size: #{op.attributes[:size]};" } },
color: { tag: 'span', apply: ->(el, op, ctx) { el[:style] = el[:style].to_s + "color: #{op.attributes[:color]};" } },
background: { tag: 'span', apply: ->(el, op, ctx) { el[:style] = el[:style].to_s + "background: #{op.attributes[:background]};" } },
}

c.html_block_formats = {
c.html_block_formats ||= {
align: { apply: ->(el, op, ctx) { el[:style] = "text-align: #{op.attributes[:align]}" } },
firstheader: { tag: 'h1' },
secondheader: { tag: 'h2' },
thirdheader: { tag: 'h3' },
Expand Down
34 changes: 32 additions & 2 deletions test/unit/html_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
bold: { tag: 'strong' },
br: { tag: 'br' },
italic: { tag: 'em' },
link: { tag: 'a', apply: ->(el, op, ctx){ el[:href] = op.attributes[:link] } }
link: { tag: 'a', apply: ->(el, op, ctx){ el[:href] = op.attributes[:link] } },
size: { tag: 'span', apply: ->(el, op, ctx) { el[:style] = el[:style].to_s + "font-size: #{op.attributes[:size]};" } },
color: { tag: 'span', apply: ->(el, op, ctx) { el[:style] = el[:style].to_s + "color: #{op.attributes[:color]};" } },
background: { tag: 'span', apply: ->(el, op, ctx) { el[:style] = el[:style].to_s + "background: #{op.attributes[:background]};" } },
}.freeze

c.html_block_formats = {
align: { apply: ->(el, op, ctx) { el[:style] = el[:style].to_s + "text-align: #{op.attributes[:align]};" } },
firstheader: { tag: 'h1' },
secondheader: { tag: 'h2' },
thirdheader: { tag: 'h3' },
Expand Down Expand Up @@ -350,7 +354,33 @@
assert_equal '<p>mali principii</p><p>malus finis</p>', render_compact_html(d)
end

it 'renders a paragraph with alignment' do
d = RichText::Delta.new([{ insert: "dextra", attributes: { align: "right"}}])
assert_equal '<p style="text-align: right;">dextra</p>', render_compact_html(d)
end

it 'renders a header with alignment' do
d = RichText::Delta.new([{ insert: "dextra", attributes: { align: "right", firstheader: true }}])
assert_equal '<h1 style="text-align: right;">dextra</h1>', render_compact_html(d)
end

it 'renders a paragraph with colors' do
d = RichText::Delta.new([{ insert: "red balloon in blue sky", attributes: { color: "#ff0000", background: "#0000ff" }}])
assert_equal '<p style="color: #ff0000;background: #0000ff;">red balloon in blue sky</p>', render_compact_html(d)
end

it 'renders a paragraph with many attributes' do
d = RichText::Delta.new([{ insert: "hello\n" }, { insert: "goodbye\n" }])
d = RichText::Delta.new([{"attributes"=>{"color"=>"#e60000", "background"=>"#ffff00", "size"=>"21px"}, "insert"=>"hello"}, {"attributes"=>{"bold" => true, "align"=>"left", size: "10px"}, "insert"=>"world"}])
assert_equal '<p style="text-align: left;"><span style="font-size: 21px;background: #ffff00;color:#e60000">hello</span><span style=\"font-size: 10px;\"><strong>world</strong></span></p>', render_compact_html(d)
end

it 'renders a paragraph with size' do
d = RichText::Delta.new([{ insert: "big text", attributes: { size: "50px" }}])
assert_equal '<p style="font-size: 50px;">big text</p>', render_compact_html(d)
end

def render_compact_html(delta, options={})
RichText::HTML.new(options).render(delta).inner_html(save_with: 0)
end
end
end