Skip to content

Commit

Permalink
fix: code render for escape html
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed Apr 24, 2023
1 parent a8acce6 commit 225f8ad
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
20 changes: 10 additions & 10 deletions packages/veno-ui/src/components/button/docs/icon.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@

<ve-spacer cols="12">
<ve-spacer>
<ve-button :variant="variant" icon="mdi-star-outline" shape="circle" />
<ve-button :variant="variant" icon="mdi-star-outline" shape="circle" color="primary" />
<ve-button :variant="variant" icon="mdi-star-outline" shape="circle" disabled />
<ve-button :variant="variant" icon="$warning" shape="circle" />
<ve-button :variant="variant" icon="$warning" shape="circle" color="primary" />
<ve-button :variant="variant" icon="$warning" shape="circle" disabled />
</ve-spacer>

<ve-spacer>
<ve-button :variant="variant" icon="mdi-star-outline" />
<ve-button :variant="variant" icon="mdi-star-outline" color="primary" />
<ve-button :variant="variant" icon="mdi-star-outline" disabled />
<ve-button :variant="variant" icon="$warning" />
<ve-button :variant="variant" icon="$warning" color="primary" />
<ve-button :variant="variant" icon="$warning" disabled />
</ve-spacer>

<ve-spacer>
<ve-button :variant="variant" prepend-icon="mdi-star-outline">Star</ve-button>
<ve-button :variant="variant" prepend-icon="mdi-star-outline" color="primary">Star</ve-button>
<ve-button :variant="variant" prepend-icon="mdi-star-outline" disabled>Star</ve-button>
<ve-button :variant="variant" prepend-icon="$warning">Star</ve-button>
<ve-button :variant="variant" prepend-icon="$warning" color="primary">Star</ve-button>
<ve-button :variant="variant" prepend-icon="$warning" disabled>Star</ve-button>
</ve-spacer>

<ve-spacer>
Expand Down Expand Up @@ -62,4 +62,4 @@ export default defineComponent({
})
```

:::
:::
26 changes: 23 additions & 3 deletions packages/veno-ui/src/components/code/code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ import { makeScrollbar, useScrollbar } from '../../composables/scrollbar'
// Types
import type { PropType } from 'vue'

const HTML_ESCAPE_TEST_RE = /[&<>"]/
const HTML_ESCAPE_REPLACE_RE = /[&<>"]/g
const HTML_REPLACEMENTS = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
}

function replaceUnsafeChar(ch: string) {
return HTML_REPLACEMENTS[ch as unknown as keyof typeof HTML_REPLACEMENTS]
}

function escapeHtml(str: string) {
if (HTML_ESCAPE_TEST_RE.test(str)) {
return str.replace(HTML_ESCAPE_REPLACE_RE, replaceUnsafeChar)
}
return str
}

export const Code = defineComponent({
name: 'VeCode',

Expand Down Expand Up @@ -83,7 +103,7 @@ export const Code = defineComponent({
.replace(/\n$/, '')
.replace(/^\n/, '')
})
const highlightedCode = ref(code.value)
const highlightedCode = ref(escapeHtml(code.value))
const { backgroundColorClasses, backgroundColorStyles } = useBackgroundColor(
props, 'color',
)
Expand All @@ -104,7 +124,7 @@ export const Code = defineComponent({
})
})

const highlight = async () => {
async function highlight() {
highlightedCode.value = await highlighter.value?.highlight(
code.value,
language.value,
Expand All @@ -126,7 +146,7 @@ export const Code = defineComponent({
backgroundColorClasses.value,
] }
style={ backgroundColorStyles.value }
v-html={ highlightedCode.value }
v-html={ escapeHtml(highlightedCode.value) }
/>
)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/veno-ui/src/components/tag/docs/closable.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
```html
<ve-spacer>
<ve-tag closable>Tag</ve-tag>
<ve-tag closable prepend-icon="mdi-star-outline">Tag</ve-tag>
<ve-tag closable prepend-icon="$warning">Tag</ve-tag>
</ve-spacer>
```

:::
:::

1 comment on commit 225f8ad

@vercel
Copy link

@vercel vercel bot commented on 225f8ad Apr 24, 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:

veno-ui – ./

veno-ui-qq15725.vercel.app
veno-ui.vercel.app
veno-ui-git-main-qq15725.vercel.app
venojs.com

Please sign in to comment.