Skip to content

Commit

Permalink
Make icons inline by default without a config file
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Jul 9, 2018
1 parent fad56a6 commit 8747c4f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/SvgFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SvgFactory
private $files;
private $svgCache;
private $config = [
'inline' => false,
'inline' => true,
'class' => 'icon',
'sprite_prefix' => '',
];
Expand Down
38 changes: 19 additions & 19 deletions tests/SvgFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ public function can_render_spritesheet()
}

/** @test */
public function icons_are_rendered_from_spritesheet_by_default()
public function icons_are_rendered_inline_by_default()
{
$factory = new SvgFactory(['class' => 'icon']);
$factory = new SvgFactory(['class' => 'icon', 'svg_path' => __DIR__.'/resources/icons/']);
$result = $factory->svg('arrow-thick-up')->toHtml();
$expected = '<svg class="icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#arrow-thick-up"></use></svg>';
$expected = '<svg class="icon" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M7 10v8h6v-8h5l-8-8-8 8h5z" fill-rule="evenodd"/></svg>';
$this->assertEquals($expected, $result);
}

/** @test */
public function icons_are_given_the_icon_class_by_default()
{
$factory = new SvgFactory();
$factory = new SvgFactory(['svg_path' => __DIR__.'/resources/icons/']);
$result = $factory->svg('arrow-thick-up')->toHtml();
$expected = '<svg class="icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#arrow-thick-up"></use></svg>';
$expected = '<svg class="icon" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M7 10v8h6v-8h5l-8-8-8 8h5z" fill-rule="evenodd"/></svg>';
$this->assertEquals($expected, $result);
}

Expand All @@ -54,63 +54,63 @@ public function can_render_inline_icon()
/** @test */
public function can_render_icon_with_additional_classes()
{
$factory = new SvgFactory();
$factory = new SvgFactory(['svg_path' => __DIR__.'/resources/icons/']);
$result = $factory->svg('arrow-thick-up', 'icon-lg inline-block')->toHtml();
$expected = '<svg class="icon icon-lg inline-block"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#arrow-thick-up"></use></svg>';
$expected = '<svg class="icon icon-lg inline-block" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M7 10v8h6v-8h5l-8-8-8 8h5z" fill-rule="evenodd"/></svg>';
$this->assertEquals($expected, $result);
}

/** @test */
public function can_specify_additional_attributes_as_an_array()
{
$factory = new SvgFactory();
$factory = new SvgFactory(['svg_path' => __DIR__.'/resources/icons/']);
$result = $factory->svg('arrow-thick-up', 'icon-lg', ['alt' => 'Alt text', 'id' => 'arrow-icon'])->toHtml();
$expected = '<svg class="icon icon-lg" alt="Alt text" id="arrow-icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#arrow-thick-up"></use></svg>';
$expected = '<svg class="icon icon-lg" alt="Alt text" id="arrow-icon" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M7 10v8h6v-8h5l-8-8-8 8h5z" fill-rule="evenodd"/></svg>';
$this->assertEquals($expected, $result);
}

/** @test */
public function can_skip_class_parameter()
{
$factory = new SvgFactory();
$factory = new SvgFactory(['svg_path' => __DIR__.'/resources/icons/']);
$result = $factory->svg('arrow-thick-up', ['alt' => 'Alt text', 'id' => 'arrow-icon'])->toHtml();
$expected = '<svg class="icon" alt="Alt text" id="arrow-icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#arrow-thick-up"></use></svg>';
$expected = '<svg class="icon" alt="Alt text" id="arrow-icon" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M7 10v8h6v-8h5l-8-8-8 8h5z" fill-rule="evenodd"/></svg>';
$this->assertEquals($expected, $result);
}

/** @test */
public function attributes_without_keys_are_used_as_valueless_html_attributes()
{
$factory = new SvgFactory();
$factory = new SvgFactory(['svg_path' => __DIR__.'/resources/icons/']);
$result = $factory->svg('arrow-thick-up', ['alt' => 'Alt text', 'data-foo'])->toHtml();
$expected = '<svg class="icon" alt="Alt text" data-foo><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#arrow-thick-up"></use></svg>';
$expected = '<svg class="icon" alt="Alt text" data-foo viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M7 10v8h6v-8h5l-8-8-8 8h5z" fill-rule="evenodd"/></svg>';
$this->assertEquals($expected, $result);
}

/** @test */
public function specifying_class_as_attribute_overrides_default_class()
{
$factory = new SvgFactory(['class' => 'default']);
$factory = new SvgFactory(['class' => 'default', 'svg_path' => __DIR__.'/resources/icons/']);
$result = $factory->svg('arrow-thick-up', ['class' => 'overridden'])->toHtml();
$expected = '<svg class="overridden"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#arrow-thick-up"></use></svg>';
$expected = '<svg class="overridden" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M7 10v8h6v-8h5l-8-8-8 8h5z" fill-rule="evenodd"/></svg>';
$this->assertEquals($expected, $result);
}

/** @test */
public function can_chain_additional_attributes()
{
$factory = new SvgFactory();
$factory = new SvgFactory(['svg_path' => __DIR__.'/resources/icons/']);
$result = $factory->svg('arrow-thick-up')->alt('Alt text')->id('arrow-icon')->toHtml();
$expected = '<svg class="icon" alt="Alt text" id="arrow-icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#arrow-thick-up"></use></svg>';
$expected = '<svg class="icon" alt="Alt text" id="arrow-icon" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M7 10v8h6v-8h5l-8-8-8 8h5z" fill-rule="evenodd"/></svg>';
$this->assertEquals($expected, $result);
}

/** @test */
public function camelcase_attributes_are_dash_cased_when_chaining()
{
$factory = new SvgFactory();
$factory = new SvgFactory(['svg_path' => __DIR__.'/resources/icons/']);
$result = $factory->svg('arrow-thick-up')->dataFoo('bar')->toHtml();
$expected = '<svg class="icon" data-foo="bar"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#arrow-thick-up"></use></svg>';
$expected = '<svg class="icon" data-foo="bar" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M7 10v8h6v-8h5l-8-8-8 8h5z" fill-rule="evenodd"/></svg>';
$this->assertEquals($expected, $result);
}

Expand Down

0 comments on commit 8747c4f

Please sign in to comment.