Skip to content

Commit

Permalink
pref: remove unnecessary uses.length judgments
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed Sep 10, 2023
1 parent 65e5bf5 commit d1bee55
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions src/clone-svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,38 @@ export async function cloneSvg<T extends SVGElement>(

const uses = clone.querySelectorAll?.('use') ?? []

if (uses.length) {
for (let len = uses.length, i = 0; i < len; i++) {
const use = uses[i]
for (let len = uses.length, i = 0; i < len; i++) {
const use = uses[i]

const href = use.getAttribute('href') // check href first as this is preferred and will be used if both are set
?? use.getAttribute('xlink:href')
const href = use.getAttribute('href') // check href first as this is preferred and will be used if both are set
?? use.getAttribute('xlink:href')

if (!href) continue // skip blank hrefs
if (!href) continue // skip blank hrefs

const [svgUrl, id] = href.split('#')
const [svgUrl, id] = href.split('#')

if (id) {
const query = `#${ id }`
const definition = ownerDocument?.querySelector(`svg ${ query }`)
if (id) {
const query = `#${ id }`
const definition = ownerDocument?.querySelector(`svg ${ query }`)

if (svgUrl) {
// change the href attribute to use a local symbol on this cloned use-node
use.setAttribute('href', query)
// No need to set xlink:href since this is ignored when href is set
}
if (svgUrl) {
// change the href attribute to use a local symbol on this cloned use-node
use.setAttribute('href', query)
// No need to set xlink:href since this is ignored when href is set
}

if (svgDefsElement?.querySelector(query)) continue // already exists in defs

if (definition) { // found local embedded definition
svgDefsElement?.appendChild(await cloneNode(definition, context))
} else if (svgUrl) { // no local definition but found an url
// try to fetch the svg and append it to the svgDefsElement
try { // wrapped in try/catch since this is network calls that is likely to fail
const response = await fetch(svgUrl)
const svgData = await response.text()
svgDefsElement?.insertAdjacentHTML('beforeend', svgData)
} catch (error) {
consoleWarn(`Failed to fetch svg from ${ svgUrl }`, error)
}
if (svgDefsElement?.querySelector(query)) continue // already exists in defs

if (definition) { // found local embedded definition
svgDefsElement?.appendChild(await cloneNode(definition, context))
} else if (svgUrl) { // no local definition but found an url
// try to fetch the svg and append it to the svgDefsElement
try { // wrapped in try/catch since this is network calls that is likely to fail
const response = await fetch(svgUrl)
const svgData = await response.text()
svgDefsElement?.insertAdjacentHTML('beforeend', svgData)
} catch (error) {
consoleWarn(`Failed to fetch svg from ${ svgUrl }`, error)
}
}
}
Expand Down

1 comment on commit d1bee55

@vercel
Copy link

@vercel vercel bot commented on d1bee55 Sep 10, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

modern-screenshot – ./

modern-screenshot-qq15725.vercel.app
modern-screenshot.vercel.app
modern-screenshot-git-main-qq15725.vercel.app

Please sign in to comment.