|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import type { SerializableHead } from '@unhead/vue/types' |
| 3 | + |
| 4 | +import { toSafeHeadInput } from '../src/runtime/utils/island-head' |
| 5 | + |
| 6 | +describe('toSafeHeadInput', () => { |
| 7 | + it('should drop input that useHeadSafe does not allow', () => { |
| 8 | + const safe = toSafeHeadInput({ |
| 9 | + htmlAttrs: { lang: 'en', onclick: 'run()' }, |
| 10 | + bodyAttrs: { class: 'page', onload: 'run()' }, |
| 11 | + base: { href: 'https://example.com/' }, |
| 12 | + link: [ |
| 13 | + { rel: 'stylesheet', href: 'javascript:run()' }, |
| 14 | + { rel: 'preload', href: '/asset.js' }, |
| 15 | + ], |
| 16 | + script: [{ textContent: 'run()' }, { src: 'https://example.com/run.js' }], |
| 17 | + style: [{ textContent: 'body { color: red }' }], |
| 18 | + noscript: [{ textContent: '<img src="x" onerror="run()">' }], |
| 19 | + } as SerializableHead) |
| 20 | + |
| 21 | + expect(safe).toEqual({ |
| 22 | + htmlAttrs: { lang: 'en' }, |
| 23 | + bodyAttrs: { class: 'page' }, |
| 24 | + }) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should keep input that useHeadSafe allows', () => { |
| 28 | + const safe = toSafeHeadInput({ |
| 29 | + title: 'Title <b>', |
| 30 | + titleTemplate: '%s - Site', |
| 31 | + htmlAttrs: { class: 'a b', dir: 'ltr' }, |
| 32 | + bodyAttrs: { 'style': 'color:red;margin:0', 'data-theme': 'dark' }, |
| 33 | + meta: [{ name: 'description', content: 'Description' }], |
| 34 | + link: [{ rel: 'icon', href: '/favicon.ico', tagPriority: 'high' }], |
| 35 | + script: [{ type: 'application/ld+json', textContent: { '@type': 'Person', 'name': '</script>' } }], |
| 36 | + } as SerializableHead) |
| 37 | + |
| 38 | + expect(safe).toEqual({ |
| 39 | + title: 'Title <b>', |
| 40 | + titleTemplate: '%s - Site', |
| 41 | + htmlAttrs: { class: 'a b', dir: 'ltr' }, |
| 42 | + bodyAttrs: { 'style': 'color:red;margin:0', 'data-theme': 'dark' }, |
| 43 | + meta: [{ name: 'description', content: 'Description' }], |
| 44 | + link: [{ rel: 'icon', href: '/favicon.ico', tagPriority: 'high' }], |
| 45 | + script: [{ type: 'application/ld+json', textContent: '{"@type":"Person","name":"\\u003C/script>"}' }], |
| 46 | + }) |
| 47 | + }) |
| 48 | +}) |
0 commit comments