34 lines
1.3 KiB
TypeScript
34 lines
1.3 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { validateXmlDocument, XML_PARSER_ERROR_NAMESPACES } from '../src/lib/xml'
|
|
|
|
function parserWithCandidates(candidates: Array<{ localName: string; namespaceURI: string | null; textContent: string | null }>) {
|
|
return {
|
|
parseFromString: () => ({
|
|
getElementsByTagName: (localName: string) => {
|
|
expect(localName).toBe('parsererror')
|
|
return candidates
|
|
},
|
|
}),
|
|
}
|
|
}
|
|
|
|
describe('XML validation', () => {
|
|
it('accepts valid content even when the user root element is named parsererror', () => {
|
|
expect(() => validateXmlDocument('<parsererror>valid</parsererror>', parserWithCandidates([
|
|
{ localName: 'parsererror', namespaceURI: null, textContent: 'valid' },
|
|
]))).not.toThrow()
|
|
})
|
|
|
|
it('reports parser errors from the browser parser-error namespace', () => {
|
|
for (const namespaceURI of XML_PARSER_ERROR_NAMESPACES) {
|
|
expect(() => validateXmlDocument('<root>', parserWithCandidates([
|
|
{ localName: 'parsererror', namespaceURI, textContent: 'XML parsing error\nDetails' },
|
|
]))).toThrow('XML parsing error')
|
|
}
|
|
})
|
|
|
|
it('rejects empty input before invoking the parser', () => {
|
|
expect(() => validateXmlDocument(' ', parserWithCandidates([]))).toThrow('请输入 XML')
|
|
})
|
|
})
|