|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { HTML_TAG_REGEX, STRICT_HTML_TAG_REGEX } from '../src'; |
| 3 | + |
| 4 | +describe('HTML_TAG_REGEX', () => { |
| 5 | + it('matches opening tags', () => { |
| 6 | + expect('<div>'.match(HTML_TAG_REGEX)).toEqual(['<div>']); |
| 7 | + }); |
| 8 | + |
| 9 | + it('matches closing tags', () => { |
| 10 | + expect('</div>'.match(HTML_TAG_REGEX)).toEqual(['</div>']); |
| 11 | + }); |
| 12 | + |
| 13 | + it('matches self-closing tags', () => { |
| 14 | + expect('<img src="a.png" />'.match(HTML_TAG_REGEX)).toEqual(['<img src="a.png" />']); |
| 15 | + }); |
| 16 | + |
| 17 | + it('matches nested tags', () => { |
| 18 | + expect('<div><span>Hello</span></div>'.match(HTML_TAG_REGEX)).toEqual([ |
| 19 | + '<div>', |
| 20 | + '<span>', |
| 21 | + '</span>', |
| 22 | + '</div>', |
| 23 | + ]); |
| 24 | + }); |
| 25 | + |
| 26 | + it('does not match plain text', () => { |
| 27 | + expect('Hello, world!'.match(HTML_TAG_REGEX)).toBeNull(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('matches malformed tags', () => { |
| 31 | + expect('<>'.match(HTML_TAG_REGEX)).toEqual(['<>']); |
| 32 | + expect('</>'.match(HTML_TAG_REGEX)).toEqual(['</>']); |
| 33 | + }); |
| 34 | +}); |
| 35 | + |
| 36 | +describe('STRICT_HTML_TAG_REGEX', () => { |
| 37 | + it('matches opening tags', () => { |
| 38 | + expect('<div>'.match(STRICT_HTML_TAG_REGEX)).toEqual(['<div>']); |
| 39 | + }); |
| 40 | + |
| 41 | + it('matches closing tags', () => { |
| 42 | + expect('</div>'.match(STRICT_HTML_TAG_REGEX)).toEqual(['</div>']); |
| 43 | + }); |
| 44 | + |
| 45 | + it('matches self-closing tags', () => { |
| 46 | + expect('<img src="a.png" />'.match(STRICT_HTML_TAG_REGEX)).toEqual(['<img src="a.png" />']); |
| 47 | + }); |
| 48 | + |
| 49 | + it('matches uppercase tag names', () => { |
| 50 | + expect('<DIV>'.match(STRICT_HTML_TAG_REGEX)).toEqual(['<DIV>']); |
| 51 | + }); |
| 52 | + |
| 53 | + it('does not match React fragments', () => { |
| 54 | + expect('<>'.match(STRICT_HTML_TAG_REGEX)).toBeNull(); |
| 55 | + expect('</>'.match(STRICT_HTML_TAG_REGEX)).toBeNull(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('does not match plain text', () => { |
| 59 | + expect('Hello'.match(STRICT_HTML_TAG_REGEX)).toBeNull(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('matches nested tags', () => { |
| 63 | + expect('<div><span>Hello</span></div>'.match(STRICT_HTML_TAG_REGEX)).toEqual([ |
| 64 | + '<div>', |
| 65 | + '<span>', |
| 66 | + '</span>', |
| 67 | + '</div>', |
| 68 | + ]); |
| 69 | + }); |
| 70 | +}); |
0 commit comments