@@ -14,6 +14,7 @@ export const MetaContext = createContext<MetaContextType>();
1414interface TagDescription {
1515 tag : string ;
1616 props : Record < string , unknown > ;
17+ setting ?: { escape ?: boolean } ;
1718 id : string ;
1819 name ?: string ;
1920 ref ?: Element ;
@@ -174,14 +175,15 @@ const MetaProvider: ParentComponent<{ tags?: Array<TagDescription> }> = props =>
174175 return < MetaContext . Provider value = { actions } > { props . children } </ MetaContext . Provider > ;
175176} ;
176177
177- const MetaTag = ( tag : string , props : { [ k : string ] : any } ) => {
178+ const MetaTag = ( tag : string , props : { [ k : string ] : any } , setting ?: { escape ?: boolean } ) => {
178179 const id = createUniqueId ( ) ;
179180 const c = useContext ( MetaContext ) ;
180181 if ( ! c ) throw new Error ( "<MetaProvider /> should be in the tree" ) ;
181182
182183 useHead ( {
183184 tag,
184185 props,
186+ setting,
185187 id,
186188 get name ( ) {
187189 return props . name || props . property ;
@@ -193,12 +195,7 @@ const MetaTag = (tag: string, props: { [k: string]: any }) => {
193195
194196export { MetaProvider } ;
195197
196- export function useHead ( tagDesc : {
197- tag : string ;
198- props : { [ k : string ] : any } ;
199- id : string ;
200- name : any ;
201- } ) {
198+ export function useHead ( tagDesc : TagDescription ) {
202199 const { addClientTag, removeClientTag, addServerTag } = useContext ( MetaContext ) ! ;
203200
204201 createRenderEffect ( ( ) => {
@@ -214,30 +211,44 @@ export function useHead(tagDesc: {
214211 }
215212}
216213
214+ function escapeHTML ( html : string ) {
215+ return html . replace ( / < / g, "<" ) . replace ( / > / g, ">" ) ;
216+ }
217+
218+ function escapeString ( str : any ) {
219+ if ( typeof str === "string" ) {
220+ return str . replace ( / " / g, '"' ) ;
221+ }
222+ return str ;
223+ }
224+
217225export function renderTags ( tags : Array < TagDescription > ) {
218226 return tags
219227 . map ( tag => {
220228 const keys = Object . keys ( tag . props ) ;
221- const props = keys . map ( k => ( k === "children" ? "" : ` ${ k } ="${ tag . props [ k ] } "` ) ) . join ( "" ) ;
222- return tag . props . children
223- ? `<${ tag . tag } data-sm="${ tag . id } "${ props } >${
229+ const props = keys . map ( k => ( k === "children" ? "" : ` ${ k } ="${ escapeString ( tag . props [ k ] ) } ` ) ) . join ( "" ) ;
230+ if ( tag . props . children ) {
224231 // Tags might contain multiple text children:
225232 // <Title>example - {myCompany}</Title>
226- Array . isArray ( tag . props . children ) ? tag . props . children . join ( "" ) : tag . props . children
227- } </${ tag . tag } >`
228- : `<${ tag . tag } data-sm="${ tag . id } "${ props } />` ;
233+ const children = Array . isArray ( tag . props . children ) ? tag . props . children . join ( "" ) : tag . props . children ;
234+ if ( tag . setting ?. escape && typeof children === "string" ) {
235+ return `<${ tag . tag } data-sm="${ tag . id } "${ props } >${ escapeHTML ( children ) } </${ tag . tag } >` ;
236+ }
237+ return `<${ tag . tag } data-sm="${ tag . id } "${ props } >${ children } </${ tag . tag } >` ;
238+ }
239+ return `<${ tag . tag } data-sm="${ tag . id } "${ props } />` ;
229240 } )
230241 . join ( "" ) ;
231242}
232243
233244export const Title : Component < JSX . HTMLAttributes < HTMLTitleElement > > = props =>
234- MetaTag ( "title" , props ) ;
245+ MetaTag ( "title" , props , { escape : true } ) ;
235246
236247export const Style : Component < JSX . StyleHTMLAttributes < HTMLStyleElement > > = props =>
237248 MetaTag ( "style" , props ) ;
238249
239250export const Meta : Component < JSX . MetaHTMLAttributes < HTMLMetaElement > > = props =>
240- MetaTag ( "meta" , props ) ;
251+ MetaTag ( "meta" , props , { escape : true } ) ;
241252
242253export const Link : Component < JSX . LinkHTMLAttributes < HTMLLinkElement > > = props =>
243254 MetaTag ( "link" , props ) ;
0 commit comments