-
Notifications
You must be signed in to change notification settings - Fork 38
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
Feat: skip dom purify for trusted content #393
Merged
yue4u
merged 3 commits into
pixiv:v4.0.0
from
yue4u:feat/skip-dom-purify-for-trusted-content
Oct 24, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ import warning from 'warning' | |
import { KnownIconFile } from './charcoalIconFiles' | ||
import { getIcon, addCustomIcon } from './loaders' | ||
import { __SERVER__ } from './ssr' | ||
import DOMPurify from 'dompurify' | ||
|
||
const attributes = ['name', 'scale', 'unsafe-non-guideline-scale'] as const | ||
|
||
|
@@ -179,8 +178,11 @@ export class PixivIcon extends HTMLElement { | |
render() { | ||
const size = this.forceResizedSize ?? this.scaledSize | ||
|
||
const style = DOMPurify.sanitize( | ||
`<style> | ||
if (!Number.isFinite(size)) { | ||
throw new TypeError(`icon size must not be NaN`) | ||
} | ||
|
||
const style = `<style> | ||
:host { | ||
display: inline-flex; | ||
--size: ${size}px; | ||
|
@@ -190,23 +192,28 @@ export class PixivIcon extends HTMLElement { | |
width: var(--size); | ||
height: var(--size); | ||
} | ||
</style>`, | ||
{ ALLOWED_TAGS: ['style'], FORCE_BODY: true } | ||
) | ||
</style>` | ||
|
||
const svg = DOMPurify.sanitize( | ||
const svg = | ||
this.svgContent !== undefined | ||
? this.svgContent | ||
: `<svg viewBox="0 0 ${size} ${size}"></svg>`, | ||
{ USE_PROFILES: { svg: true, svgFilters: true } } | ||
) | ||
: `<svg viewBox="0 0 ${size} ${size}"></svg>` | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
this.shadowRoot!.innerHTML = style + svg | ||
} | ||
|
||
private async loadSvg(name: string) { | ||
this.svgContent = await getIcon(name) | ||
const iconResult = await getIcon(name) | ||
|
||
if (iconResult.trusted) { | ||
this.svgContent = iconResult.svgContent | ||
} else { | ||
const { default: DOMPurify } = await import('dompurify') | ||
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. dom-purify is still pretty large so we can do following in the future.
|
||
this.svgContent = DOMPurify.sanitize(iconResult.svgContent, { | ||
USE_PROFILES: { svg: true, svgFilters: true }, | ||
}) | ||
} | ||
this.render() | ||
} | ||
|
||
|
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export interface Loadable { | ||
readonly trusted: boolean | ||
fetch(): Promise<string> | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
[IMO] 実は
Number.isFinite()
だけで良かったりしませんか?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.
1/0
やInfinity
を考えると確かにisFiniteが良さそうです。実際はかなりのedge caseですね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.
37c2245
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.
すみません意図としては
typeof
がnumber
であるかの判定も isFinite() だけで行けるんじゃないですか、みたいなことでした(これでも良いですが)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.
4eb112c 消しました。元々エラー少しわかりやすくなるかもしれないという意図で入れていたが処理的には確か不要そうです