Skip to content
This repository has been archived by the owner on Nov 9, 2018. It is now read-only.

Support for TSpan #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions src/__tests__/fixtures.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ const SIMPLE_SVG = `<?xml version="1.0" encoding="UTF-8"?>
<text font-family="Verdana" text-anchor="middle" font-size="36">baby</text>
</g>
</g>
<g>
<path d="M0,57.8h85c19.1,0,19.5-13.9,19.1-17.9L97.8,0H0.1L0,57.8z" fill="red" />
</g>
<text x="52" y="28.9" dy="0.33em" text-anchor='middle' font-size="44" font-family="AkzidenzGrotesk-Black"><tspan>T</tspan></text>
</svg>
`

Expand Down
10 changes: 9 additions & 1 deletion src/__tests__/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('svg-parser main lib', () => {
expect(dom.childNodes[2].tagName).toBe('svg')
expect(dom.documentElement.tagName).toBe('svg')
expect(dom.documentElement.namespaceURI).toBe('http://www.w3.org/2000/svg')
expect(dom.documentElement.childNodes.length).toBe(5)
expect(dom.documentElement.childNodes.length).toBe(9)
})

it('should return an svg in react native SVG format', () => {
Expand Down Expand Up @@ -73,6 +73,14 @@ describe('svg-parser main lib', () => {
expect(textElement.props.children).toEqual('baby')
})

it('handles tspan elements', () => {
const svg = parser(SIMPLE_SVG, SIMPLE_CSS)
const textElement = svg.props.children[3]
const tspanElement = textElement.props.children[0]

expect(tspanElement.props.children).toEqual('T')
})

it('readme example works', () => {
const svgString = `<svg height="100" width="100">
<circle cx="50" cy="50" r="40" class="red-circle" />
Expand Down
38 changes: 27 additions & 11 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import Svg, {
Text,
Use,
Defs,
Stop
Stop,
TSpan
} from 'react-native-svg'

const mapping = {
Expand All @@ -35,7 +36,8 @@ const mapping = {
'radialGradient': RadialGradient,
'use': Use,
'defs': Defs,
'stop': Stop
'stop': Stop,
'tspan': TSpan
}

function extractViewbox (markup) {
Expand Down Expand Up @@ -170,28 +172,42 @@ function traverse (markup, config, i = 0) {
}

// map the tag to an element.
const Elem = mapping[ tagName.toLowerCase() ]
const Elem = mapping[tagName.toLowerCase()]

// Note, if the element is not found it was not in the mapping.
if (!Elem) {
return null
}

const children = (Elem === Text && markup.childNodes.length === 1)
? markup.childNodes[0].data
: markup.childNodes.length ? Object.values(markup.childNodes).map((child) => {
return traverse(child, config, ++i)
}).filter((node) => {
return !!node
}) : []
let children = []
if (markup.childNodes) {
children = traverseChildNodes(getChildNodes(markup), config, ++i)
}

const elemAttributes = {}
attrs.forEach((attr) => {
elemAttributes[attr.name] = attr.value
})

const k = i + Math.random()
return <Elem {...elemAttributes} key={k}>{ children }</Elem>
return <Elem {...elemAttributes} key={k}>{children}</Elem>
}

function getChildNodes (markup) {
return Object.keys(markup.childNodes)
.filter(n => n !== 'length')
.map(i => markup.childNodes[i])
}

function traverseChildNodes (nodes, config, i) {
// if there is just one node and that node have data return the data value
if (nodes.length === 1 && nodes[0].data) {
return nodes[0].data
}

return nodes
.map(child => traverse(child, config, ++i))
.filter(node => !!node)
}

export { extractViewbox, getCssRulesForAttr, findApplicableCssProps, addNonCssAttributes }
Expand Down