发布 0.2.1 并完善工具边界与安全性
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
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')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user