Skip to content

Commit

Permalink
Merge pull request #75 from DannyBen/add/svg-tag
Browse files Browse the repository at this point in the history
Add `SVG#tag` as the preferred alias to `SVG#element`
  • Loading branch information
DannyBen authored Aug 29, 2024
2 parents 3461552 + dff5399 commit dfda527
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/victor/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Component
extend Forwardable
include Marshaling

def_delegators :svg, :save, :render, :content, :element, :css, :to_s
def_delegators :svg, :save, :render, :content, :element, :tag, :css, :to_s

# Marshaling data
def marshaling = %i[width height x y svg merged_css]
Expand Down
2 changes: 1 addition & 1 deletion lib/victor/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Victor
module DSL
extend Forwardable
def_delegators :svg, :setup, :build, :save, :render, :append, :element, :css
def_delegators :svg, :setup, :build, :save, :render, :append, :element, :tag, :css

def svg
@svg ||= Victor::SVG.new
Expand Down
2 changes: 1 addition & 1 deletion lib/victor/svg.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Victor
class SVG < SVGBase
def method_missing(method_sym, ...)
element(method_sym, ...)
tag(method_sym, ...)
end

def respond_to_missing?(*)
Expand Down
3 changes: 2 additions & 1 deletion lib/victor/svg_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def build(&block)
instance_eval(&block)
end

def element(name, value = nil, attributes = {})
def tag(name, value = nil, attributes = {})
if value.is_a? Hash
attributes = value
value = nil
Expand Down Expand Up @@ -68,6 +68,7 @@ def element(name, value = nil, attributes = {})
content.push "<#{name} #{attributes}/>"
end
end
alias element tag

def css(defs = nil)
@css ||= {}
Expand Down
9 changes: 8 additions & 1 deletion spec/victor/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

context 'when all required methods are implemented' do
let(:svg) do
double save: true, render: true, content: true, element: true, to_s: true
double save: true, render: true, content: true, element: true, tag: true, to_s: true
end

before do
Expand Down Expand Up @@ -66,6 +66,13 @@
end
end

describe '#tag' do
it 'delegates to SVG' do
expect(svg).to receive(:tag).with(:rect)
subject.tag :rect
end
end

describe '#element' do
it 'delegates to SVG' do
expect(svg).to receive(:element).with(:rect)
Expand Down
44 changes: 25 additions & 19 deletions spec/victor/svg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,25 @@
end
end

describe '#element' do
describe '#tag' do
it 'generates xml without attributes' do
subject.element 'anything'
subject.tag 'anything'
expect(subject.content).to eq ['<anything />']
end

it 'generates xml with attributes' do
subject.element 'anything', at: 'all'
subject.tag 'anything', at: 'all'
expect(subject.content).to eq ['<anything at="all"/>']
end

it 'converts snake attributes to kebabs' do
subject.element 'text', font_family: 'arial'
subject.tag 'text', font_family: 'arial'
expect(subject.content).to eq ['<text font-family="arial"/>']
end

context 'with hashed attributes' do
it 'converts attributes to style syntax' do
subject.element 'cool', style: { color: 'red', anything: 10 }
subject.tag 'cool', style: { color: 'red', anything: 10 }
expect(subject.content).to eq ['<cool style="color:red; anything:10"/>']
end
end
Expand All @@ -163,10 +163,10 @@
# https://github.com/DannyBen/victor/pull/59
it "ignores the block's return value" do
subject.build do
element :group do
element :one
element :two
element :three if false
tag :group do
tag :one
tag :two
tag :three if false
end
end
expect(subject.content).to eq ['<group>', '<one />', '<two />', '</group>']
Expand All @@ -175,43 +175,49 @@

context 'with a plain text value' do
it 'generates a container element' do
subject.element 'prison', 'inmate', number: '6'
subject.tag 'prison', 'inmate', number: '6'
expect(subject.content).to eq ['<prison number="6">', 'inmate', '</prison>']
end

it 'escapes XML' do
subject.element 'text', 'For Dumb & Dumber, 2 > 3'
subject.tag 'text', 'For Dumb & Dumber, 2 > 3'
expect(subject.content).to eq ['<text>', 'For Dumb &amp; Dumber, 2 &gt; 3', '</text>']
end

context 'when the element is an underscore' do
it 'generates a tagless element' do
subject.element '_', 'You are (not) surrounded!'
subject.tag '_', 'You are (not) surrounded!'
expect(subject.content).to eq ['You are (not) surrounded!']
end

it 'escapes XML' do
subject.element '_', 'For Dumb & Dumber, 2 > 3'
subject.tag '_', 'For Dumb & Dumber, 2 > 3'
expect(subject.content).to eq ['For Dumb &amp; Dumber, 2 &gt; 3']
end

context 'when the element is _!' do
it 'does not escape XML' do
subject.element '_!', 'For Dumb & Dumber, 2 > 3'
subject.tag '_!', 'For Dumb & Dumber, 2 > 3'
expect(subject.content).to eq ['For Dumb & Dumber, 2 > 3']
end
end
end

context 'when the element name ends with !' do
it 'does not escape XML' do
subject.element 'text!', 'For Dumb & Dumber, 2 > 3'
subject.tag 'text!', 'For Dumb & Dumber, 2 > 3'
expect(subject.content).to eq ['<text>', 'For Dumb & Dumber, 2 > 3', '</text>']
end
end
end
end

describe '#element' do
it 'is an alias for #tag' do
expect(subject.method(:element)).to eq subject.method(:tag)
end
end

describe '#css' do
context 'when no css rules were added' do
it 'returns an empty hash' do
Expand Down Expand Up @@ -243,13 +249,13 @@
end

describe '#method_missing' do
it 'calls #element' do
expect(subject).to receive(:element).with(:anything)
it 'calls #tag' do
expect(subject).to receive(:tag).with(:anything)
subject.anything
end

it 'passes arguments to #element' do
expect(subject).to receive(:element).with(:anything, { at: 'all' })
it 'passes arguments to #tag' do
expect(subject).to receive(:tag).with(:anything, { at: 'all' })
subject.anything at: 'all'
end
end
Expand Down

0 comments on commit dfda527

Please sign in to comment.