validate.spec.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { isEncodedDataUrl, isSvgDataUrl, isValid } from "../validate";
  2. describe('isValid', () => {
  3. it('invalid onload attr', () => {
  4. const el = {
  5. nodeType: 1,
  6. nodeName: 'svg',
  7. attributes: [{ name: 'onload' }],
  8. childNodes: []
  9. };
  10. expect(isValid(el)).toBe(false);
  11. });
  12. it('invalid onclick attr', () => {
  13. const el = {
  14. nodeType: 1,
  15. nodeName: 'svg',
  16. attributes: [{ name: 'OnClIcK' }],
  17. childNodes: []
  18. };
  19. expect(isValid(el)).toBe(false);
  20. });
  21. it('invalid child SCRIPT elm', () => {
  22. const el = {
  23. nodeType: 1, nodeName: 'svg', attributes: [], childNodes: [
  24. { nodeType: 1, nodeName: 'SCRIPT', attributes: [], childNodes: [] }
  25. ]
  26. };
  27. expect(isValid(el)).toBe(false);
  28. });
  29. it('invalid script elm', () => {
  30. const el = { nodeType: 1, nodeName: 'script', attributes: [], childNodes: [] };
  31. expect(isValid(el)).toBe(false);
  32. });
  33. it('is valid circle elm', () => {
  34. const el = { nodeType: 1, nodeName: 'circle', attributes: [], childNodes: [] };
  35. expect(isValid(el)).toBe(true);
  36. });
  37. it('is valid SVG elm', () => {
  38. const el = {
  39. nodeType: 1, nodeName: 'SVG', attributes: [], childNodes: [
  40. { nodeType: 1, nodeName: 'line', attributes: [], childNodes: [] }
  41. ]
  42. };
  43. expect(isValid(el)).toBe(true);
  44. });
  45. it('is valid text node', () => {
  46. const el = { nodeType: 3, nodeName: '#text' };
  47. expect(isValid(el)).toBe(true);
  48. });
  49. });
  50. it('isSvgDataUrl', () => {
  51. expect(isSvgDataUrl('data:image/svg+xml;base64,xxx')).toBe(true);
  52. expect(isSvgDataUrl('data:image/svg+xml;utf8,<svg></svg>')).toBe(true);
  53. expect(isSvgDataUrl('https://example.com/icon.svg')).toBe(false);
  54. expect(isSvgDataUrl('http://example.com/icon.svg')).toBe(false);
  55. });
  56. it('isEncodedDataUrl', () => {
  57. expect(isEncodedDataUrl('data:image/svg+xml;base64,xxx')).toBe(false);
  58. expect(isEncodedDataUrl('data:image/svg+xml;utf8,<svg></svg>')).toBe(true);
  59. expect(isEncodedDataUrl('https://example.com/icon.svg')).toBe(false);
  60. expect(isEncodedDataUrl('http://example.com/icon.svg')).toBe(false);
  61. });