@@ -63,7 +63,7 @@ export function rich<T>(
6363 }
6464}
6565
66- const tagRegex = / < \/ ? ( [ A - Z a - z ] [ \w . - ] * ) > / g
66+ const tagRegex = / < ( \/ ? ) ( [ A - Z a - z ] [ \w . - ] * ) \s * ( \/ ? ) > / g
6767
6868interface RichFrame < T > {
6969 tag : string
@@ -72,6 +72,7 @@ interface RichFrame<T> {
7272
7373/**
7474 * Maps rich-text tags in a string to chunks using provided tag handlers.
75+ * Self-closing tags are mapped with empty chunks.
7576 * Unknown or malformed tags are ignored as markup and only their text content is kept.
7677 * @param input - Rich-text input string.
7778 * @param tags - Tag handlers used to map known tags.
@@ -91,21 +92,23 @@ export function mapTags<T>(
9192 let match : RegExpExecArray | null
9293
9394 while ( match = tagRegex . exec ( input ) ) {
94- const [ rawTag , tag ] = match
95+ const [ , closing , tag , selfClosing ] = match
9596 const current = stack [ stack . length - 1 ]
9697
9798 if ( match . index > lastIndex ) {
9899 current . chunks . push ( input . slice ( lastIndex , match . index ) )
99100 }
100101
101102 if ( tag in tags ) {
102- if ( rawTag [ 1 ] === '/' ) {
103+ if ( closing ) {
103104 if ( current . tag === tag ) {
104105 stack . pop ( )
105106 stack [ stack . length - 1 ] . chunks . push (
106107 tags [ tag ] ( current . chunks )
107108 )
108109 }
110+ } else if ( selfClosing ) {
111+ current . chunks . push ( tags [ tag ] ( [ ] ) )
109112 } else {
110113 stack . push ( {
111114 tag,
0 commit comments