Skip to content
Merged
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,13 @@ You can pass `feather.replace()` an `options` object:
</script>
```

All classes on a placeholder element (i.e. `<i>`) will be copied to the `<svg>` tag:
The id and classes on a placeholder element (i.e. `<i>`) will be copied to the `<svg>` tag:

```html
<i class="foo bar" data-feather="circle"></i>
<i id="my-circle-icon" class="foo bar" data-feather="circle"></i>
<!--
<i> will be replaced with:
<svg class="feather feather-circle foo bar" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle></svg>
<svg id="my-circle-icon" class="feather feather-circle foo bar" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle></svg>
-->

<script>
Expand Down
4 changes: 3 additions & 1 deletion src/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ function replaceElement(element, options) {
}

const elementClassAttr = element.getAttribute('class') || '';
const elementIdAttr = element.getAttribute('id');
const classNames = (
options.class ? `${options.class} ${elementClassAttr}` : elementClassAttr
);

const svgString = toSvg(key, Object.assign({}, options, { class: classNames }));
const svgOptions = Object.assign({}, options, { class: classNames, id: elementIdAttr });
const svgString = toSvg(key, svgOptions);
const svgDocument = new DOMParser().parseFromString(svgString, 'image/svg+xml');
const svgElement = svgDocument.querySelector('svg');

Expand Down
8 changes: 5 additions & 3 deletions src/to-svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function toSvg(key, options = {}) {

combinedOptions.class = addDefaultClassNames(combinedOptions.class, key);

const attributes = optionsToAtrributes(combinedOptions);
const attributes = optionsToAttributes(combinedOptions);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha thanks for catching my typo!


return `<svg ${attributes}>${icons[key]}</svg>`;
}
Expand Down Expand Up @@ -64,11 +64,13 @@ function addDefaultClassNames(classNames, key) {
* @param {Object} options
* @returns {string}
*/
function optionsToAtrributes(options) {
function optionsToAttributes(options) {
const attributes = [];

Object.keys(options).forEach(key => {
attributes.push(`${key}="${options[key]}"`);
if (options[key]) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would options[key] ever be falsey in this context? Object.keys(options) is generating an array of all the keys in the object and we're using forEach to iterate over all the keys. I'm not sure options[key] would ever be undefined.

@bbohen bbohen Oct 5, 2017

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case it was the lack of a value for id where element.getAttribute('id') results in null. This doesn't cause any errors but ends up with an eventual id="null" attribute on the SVG element. The only other situation where I could see this being falsey is if someone was to pass a null value for an option when using replace or toSvg.

I could instead check if id has a value before passing it as an option to toSvg, let me know if that would be preferable here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. I didn't think about that. I think this is fine!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be a good idea to use TypeScript to catch these kinds of bugs 😄

attributes.push(`${key}="${options[key]}"`);
}
});

return attributes.join(' ');
Expand Down