-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
clone-svg
rename to embed-svg-use
- Loading branch information
Showing
6 changed files
with
80 additions
and
81 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,27 +1,33 @@ | ||
import { embedImageElement } from './embed-image-element' | ||
import { embedCssStyleImage } from './embed-css-style-image' | ||
import { embedSvgUse } from './embed-svg-use' | ||
import { | ||
isElementNode, | ||
isHTMLElementNode, | ||
isImageElement, | ||
isSVGImageElementNode, | ||
isSVGUseElementNode, | ||
} from './utils' | ||
import type { Context } from './context' | ||
|
||
export function embedNode<T extends Node>(clone: T, context: Context) { | ||
export function embedNode<T extends Node>(cloned: T, context: Context) { | ||
const { tasks } = context | ||
|
||
if (isElementNode(clone)) { | ||
if (isImageElement(clone) || isSVGImageElementNode(clone)) { | ||
tasks.push(...embedImageElement(clone, context)) | ||
if (isElementNode(cloned)) { | ||
if (isImageElement(cloned) || isSVGImageElementNode(cloned)) { | ||
tasks.push(...embedImageElement(cloned, context)) | ||
} | ||
|
||
if (isSVGUseElementNode(cloned)) { | ||
tasks.push(...embedSvgUse(cloned, context)) | ||
} | ||
} | ||
|
||
if (isHTMLElementNode(clone)) { | ||
tasks.push(...embedCssStyleImage(clone.style, context)) | ||
if (isHTMLElementNode(cloned)) { | ||
tasks.push(...embedCssStyleImage(cloned.style, context)) | ||
} | ||
|
||
clone.childNodes.forEach(child => { | ||
cloned.childNodes.forEach(child => { | ||
embedNode(child, context) | ||
}) | ||
} |
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,53 @@ | ||
import { cloneNode } from './clone-node' | ||
import { contextFetch } from './fetch' | ||
import type { Context } from './context' | ||
|
||
export function embedSvgUse<T extends SVGUseElement>( | ||
cloned: T, | ||
context: Context, | ||
): Promise<void>[] { | ||
const { ownerDocument, svgDefsElement } = context | ||
|
||
const href = cloned.getAttribute('href') // check href first as this is preferred and will be used if both are set | ||
?? cloned.getAttribute('xlink:href') | ||
|
||
if (!href) return [] // skip blank hrefs | ||
|
||
const [svgUrl, id] = href.split('#') | ||
|
||
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 | ||
cloned.setAttribute('href', query) | ||
// No need to set xlink:href since this is ignored when href is set | ||
} | ||
|
||
if (svgDefsElement?.querySelector(query)) return [] // already exists in defs | ||
|
||
if (definition) { // found local embedded definition | ||
return [ | ||
cloneNode(definition, context) | ||
.then(clonedChildNode => { | ||
if (!svgDefsElement?.querySelector(query)) { | ||
svgDefsElement?.appendChild(clonedChildNode) | ||
} | ||
}), | ||
] | ||
} else if (svgUrl) { // no local definition but found an url | ||
// try to fetch the svg and append it to the svgDefsElement | ||
return [ | ||
contextFetch(context, { | ||
url: svgUrl, | ||
responseType: 'text', | ||
}).then(svgData => { | ||
svgDefsElement?.insertAdjacentHTML('beforeend', svgData) | ||
}), | ||
] | ||
} | ||
} | ||
|
||
return [] | ||
} |
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
67589ab
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.
Successfully deployed to the following URLs:
modern-screenshot – ./
modern-screenshot.vercel.app
modern-screenshot-git-main-qq15725.vercel.app
modern-screenshot-qq15725.vercel.app