-
Notifications
You must be signed in to change notification settings - Fork 353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow arbitrarily nested <use> elements #117
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
/*! svg4everybody v2.1.0 | github.com/jonathantneal/svg4everybody */ | ||
|
||
function embed(svg, target) { | ||
function embed(node, target) { | ||
// if the target exists | ||
if (target) { | ||
// create a document fragment to hold the contents of the target | ||
var fragment = document.createDocumentFragment(); | ||
|
||
var svg = getSVGAncestor(node); | ||
|
||
// cache the closest matching viewBox | ||
var viewBox = !svg.getAttribute('viewBox') && target.getAttribute('viewBox'); | ||
var viewBox = !svg.hasAttribute('viewBox') && target.getAttribute('viewBox'); | ||
|
||
// conditionally set the viewBox on the svg | ||
if (viewBox) { | ||
|
@@ -23,7 +25,7 @@ function embed(svg, target) { | |
} | ||
|
||
// append the fragment into the svg | ||
svg.appendChild(fragment); | ||
node.appendChild(fragment); | ||
} | ||
} | ||
|
||
|
@@ -55,7 +57,7 @@ function loadreadystatechange(xhr) { | |
} | ||
|
||
// embed the target into the svg | ||
embed(item.svg, target); | ||
embed(item.node, target); | ||
}); | ||
} | ||
}; | ||
|
@@ -122,9 +124,10 @@ function svg4everybody(rawopts) { | |
var use = uses[index]; | ||
|
||
// get the current <svg> | ||
var svg = use.parentNode; | ||
var parent = use.parentNode; | ||
var svg = getSVGAncestor(parent); | ||
|
||
if (svg && /svg/i.test(svg.nodeName)) { | ||
if (svg) { | ||
var src = use.getAttribute('xlink:href'); | ||
|
||
// if running with legacy support | ||
|
@@ -143,11 +146,11 @@ function svg4everybody(rawopts) { | |
img.src = fallback(src, svg, use); | ||
|
||
// replace the <use> with the fallback image | ||
svg.replaceChild(img, use); | ||
parent.replaceChild(img, use); | ||
} else if (polyfill) { | ||
if (!opts.validate || opts.validate(src, svg, use)) { | ||
// remove the <use> element | ||
svg.removeChild(use); | ||
parent.removeChild(use); | ||
|
||
// parse the src and get the url and id | ||
var srcSplit = src.split('#'); | ||
|
@@ -172,15 +175,15 @@ function svg4everybody(rawopts) { | |
|
||
// add the svg and id as an item to the xhr embeds list | ||
xhr._embeds.push({ | ||
svg: svg, | ||
node: parent, | ||
id: id | ||
}); | ||
|
||
// prepare the xhr ready state change event | ||
loadreadystatechange(xhr); | ||
} else { | ||
// embed the local id into the svg | ||
embed(svg, document.getElementById(id)); | ||
embed(parent, document.getElementById(id)); | ||
} | ||
} | ||
} | ||
|
@@ -199,3 +202,14 @@ function svg4everybody(rawopts) { | |
oninterval(); | ||
} | ||
} | ||
|
||
function getSVGAncestor(node) { | ||
var svg = node; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very minor: I think leaving it as node is clearer |
||
while (svg.nodeName.toLowerCase() !== 'svg') { | ||
svg = svg.parentNode; | ||
if (!svg) { | ||
break; | ||
} | ||
} | ||
return svg; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: would it be better to pass in both the
svg
andparent
so that we don't have to search through the DOM again? Its likely that svg and use are close together, but its would be more efficient this way. You would have to change the obj being passed intoxhr._embeds
as well.