-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Move utility functions to `util.js` - Replace DOMParser approach with create empty element and `innerHTML` approach - Update test
- Loading branch information
Jaeho Lee
committed
Feb 27, 2017
1 parent
4eb7203
commit a6bac3a
Showing
4 changed files
with
59 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Transform DOM prop/attr names applicable to `<svg>` element but react-limited | ||
|
||
export function convertReactSVGDOMProperty(str) { | ||
return str.replace(/[-|:]([a-z])/g, function (g) { return g[1].toUpperCase(); }) | ||
} | ||
|
||
export function startsWith(str, substring) { | ||
return str.indexOf(substring) === 0; | ||
} | ||
|
||
const DataPropPrefix = 'data-'; | ||
// Serialize `Attr` objects in `NamedNodeMap` | ||
export function serializeAttrs(map) { | ||
const ret = {}; | ||
for (let prop, i = 0; i < map.length; i++) { | ||
const key = map[i].name; | ||
if (!startsWith(key, DataPropPrefix)) { | ||
prop = convertReactSVGDOMProperty(key); | ||
} | ||
ret[prop] = map[i].value; | ||
} | ||
return ret; | ||
} | ||
|
||
export function getSVGFromSource(src) { | ||
const svgContainer = document.createElement('div'); | ||
svgContainer.innerHTML = src; | ||
const svg = svgContainer.firstElementChild; | ||
svg.remove(); // deref from parent element | ||
return svg; | ||
} | ||
|
||
// get <svg /> element props | ||
export function extractSVGProps(src) { | ||
const map = getSVGFromSource(src).attributes; | ||
return (map.length > 0) ? serializeAttrs(map) : null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters