@@ -2,7 +2,11 @@ import { defineComponent, h, markRaw } from 'vue'
22import { describe , expect , it } from 'vitest'
33import { renderToString } from '@vue/test-utils'
44import { useSlotText } from '@/utils/vnode'
5- import MarkdowView , { preprocessCustomRawComponents , preprocessIncompleteTags } from './MarkdownView'
5+ import MarkdowView , {
6+ preprocessCustomRawComponents ,
7+ preprocessIncompleteTags ,
8+ preprocessInlineComponents
9+ } from './MarkdownView'
610
711describe ( 'preprocessCustomRawComponents' , ( ) => {
812 it ( 'should convert custom raw components to <pre> tags' , ( ) => {
@@ -98,6 +102,51 @@ describe('preprocessCustomRawComponents', () => {
98102 } )
99103} )
100104
105+ describe ( 'preprocessInlineComponents' , ( ) => {
106+ it ( 'should separate self-closing tag from next line text with blank line' , ( ) => {
107+ const value = '<x-comp />\nHello world'
108+ const tagNames = [ 'x-comp' ]
109+ const result = preprocessInlineComponents ( value , tagNames )
110+ expect ( result ) . toBe ( '<x-comp />\n\nHello world' )
111+ } )
112+ it ( 'should separate self-closing tag from next line text with blank line with attributes' , ( ) => {
113+ const value = '<x-comp some="foo" />\nHello world'
114+ const tagNames = [ 'x-comp' ]
115+ const result = preprocessInlineComponents ( value , tagNames )
116+ expect ( result ) . toBe ( '<x-comp some="foo" />\n\nHello world' )
117+ } )
118+ it ( 'should preserve leading spaces of next line' , ( ) => {
119+ const value = '<x-comp />\n Hello'
120+ const tagNames = [ 'x-comp' ]
121+ const result = preprocessInlineComponents ( value , tagNames )
122+ expect ( result ) . toBe ( '<x-comp />\n\n Hello' )
123+ } )
124+ it ( 'should ignore when there is a blank line' , ( ) => {
125+ const value = '<x-comp />\n\nHello'
126+ const tagNames = [ 'x-comp' ]
127+ const result = preprocessInlineComponents ( value , tagNames )
128+ expect ( result ) . toBe ( '<x-comp />\n\nHello' )
129+ } )
130+ it ( 'should not change when inline already' , ( ) => {
131+ const value = '<x-comp />Hello\nWorld'
132+ const tagNames = [ 'x-comp' ]
133+ const result = preprocessInlineComponents ( value , tagNames )
134+ expect ( result ) . toBe ( '<x-comp />Hello\nWorld' )
135+ } )
136+ it ( 'should match spaces before newline' , ( ) => {
137+ const value = '<x-comp /> \nHello'
138+ const tagNames = [ 'x-comp' ]
139+ const result = preprocessInlineComponents ( value , tagNames )
140+ expect ( result ) . toBe ( '<x-comp />\n\nHello' )
141+ } )
142+ it ( 'should not change when not self-closing' , ( ) => {
143+ const value = '<x-comp></x-comp>\nHello'
144+ const tagNames = [ 'x-comp' ]
145+ const result = preprocessInlineComponents ( value , tagNames )
146+ expect ( result ) . toBe ( '<x-comp></x-comp>\nHello' )
147+ } )
148+ } )
149+
101150describe ( 'preprocessIncompleteTags' , ( ) => {
102151 it ( 'should remove the last incomplete tag' , ( ) => {
103152 const value = 'Before<custom-incomplete-component>Content'
@@ -255,4 +304,38 @@ After`,
255304<!--]--></div>
256305<p><div class="test-comp-1"></div>aaa<div class="test-comp-2">bbb</div><div class="test-comp-2">ccc</div>ddd</p><div class="test-comp-3"><!--[--><p></p><!--]--></div></div>` )
257306 } )
307+ it ( 'should handle a custom self-closing tag with text on the next line' , async ( ) => {
308+ const testComp1 = {
309+ template : '<div class="test-comp-1">hello world</div>'
310+ }
311+ const result = await renderToString ( MarkdowView , {
312+ props : {
313+ value : `Before<test-comp-1 />
314+ After` ,
315+ components : markRaw ( {
316+ custom : {
317+ 'test-comp-1' : testComp1
318+ }
319+ } )
320+ }
321+ } )
322+ expect ( result ) . toBe ( '<div><p>Before<div class="test-comp-1">hello world</div></p>\n<p>After</p></div>' )
323+ } )
324+ it ( 'should handle a custom self-closing tag when text follows immediately on the next line' , async ( ) => {
325+ const testComp1 = {
326+ template : '<div class="test-comp-1">hello world</div>'
327+ }
328+ const result = await renderToString ( MarkdowView , {
329+ props : {
330+ value : `Before<test-comp-1 />middle
331+ After` ,
332+ components : markRaw ( {
333+ custom : {
334+ 'test-comp-1' : testComp1
335+ }
336+ } )
337+ }
338+ } )
339+ expect ( result ) . toBe ( '<div><p>Before<div class="test-comp-1">hello world</div>middle\nAfter</p></div>' )
340+ } )
258341} )
0 commit comments