@@ -104,6 +104,7 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
104104 // Create a new active tag and inherit language scope and baseIRI from parent
105105 const activeTag : IActiveTag = {
106106 collectChildTags : parentTag . collectChildTags ,
107+ collectChildTagsForCurrentTag : parentTag . collectChildTagsForCurrentTag ,
107108 incompleteTriples : [ ] ,
108109 inlist : 'inlist' in attributes ,
109110 listMapping : < { [ predicate : string ] : ( RDF . Term | boolean ) [ ] } > < any > [ ] ,
@@ -129,7 +130,7 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
129130 }
130131
131132 const attributesSerialized = Object . keys ( attributes ) . map ( ( key ) => `${ key } ="${ attributes [ key ] } "` ) . join ( ' ' ) ;
132- activeTag . text = [ `<${ name } ${ attributesSerialized ? ' ' + attributesSerialized : '' } >` ] ;
133+ activeTag . textWithTags = [ `<${ name } ${ attributesSerialized ? ' ' + attributesSerialized : '' } >` ] ;
133134 if ( this . features . skipHandlingXmlLiteralChildren ) {
134135 return ;
135136 }
@@ -469,6 +470,7 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
469470 && ( activeTag . datatype . value === Util . RDF + 'XMLLiteral'
470471 || ( this . features . htmlDatatype && activeTag . datatype . value === Util . RDF + 'HTML' ) ) ) {
471472 activeTag . collectChildTags = true ;
473+ activeTag . collectChildTagsForCurrentTag = true ;
472474 }
473475 } else {
474476 // Try to determine resource
@@ -488,6 +490,13 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
488490 }
489491 }
490492
493+ // If we're in a parent tag that collects child tags,
494+ // and we find a tag that does NOT preserve tags,
495+ // we mark this tag (and children) to not preserve it.
496+ if ( ! ( 'datatype' in attributes ) || attributes . datatype === '' ) {
497+ activeTag . collectChildTagsForCurrentTag = false ;
498+ }
499+
491500 if ( 'content' in attributes ) {
492501 // Emit triples based on content attribute has preference over text content
493502 const object = this . util . createLiteral ( attributes . content , activeTag ) ;
@@ -586,10 +595,14 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
586595 }
587596
588597 // Save the text inside the active tag
589- if ( ! activeTag . text ) {
590- activeTag . text = [ ] ;
598+ if ( ! activeTag . textWithTags ) {
599+ activeTag . textWithTags = [ ] ;
591600 }
592- activeTag . text . push ( data ) ;
601+ if ( ! activeTag . textWithoutTags ) {
602+ activeTag . textWithoutTags = [ ] ;
603+ }
604+ activeTag . textWithTags . push ( data ) ;
605+ activeTag . textWithoutTags . push ( data ) ;
593606 }
594607
595608 public onTagClose ( ) {
@@ -628,10 +641,15 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
628641 // Emit all triples that were determined in the active tag
629642 if ( activeTag . predicates ) {
630643 const subject = this . util . getResourceOrBaseIri ( activeTag . subject , activeTag ) ;
631- let textSegments : string [ ] = activeTag . text || [ ] ;
632- if ( activeTag . collectChildTags && parentTag . collectChildTags ) {
633- // If we are inside an XMLLiteral child that also has RDFa content, ignore the tag name that was collected.
634- textSegments = textSegments . slice ( 1 ) ;
644+ let textSegments : string [ ] ;
645+ if ( ! activeTag . collectChildTagsForCurrentTag ) {
646+ textSegments = activeTag . textWithoutTags || [ ] ;
647+ } else {
648+ textSegments = activeTag . textWithTags || [ ] ;
649+ if ( activeTag . collectChildTags && parentTag . collectChildTags ) {
650+ // If we are inside an XMLLiteral child that also has RDFa content, ignore the tag name that was collected.
651+ textSegments = textSegments . slice ( 1 ) ;
652+ }
635653 }
636654 const object = this . util . createLiteral ( textSegments . join ( '' ) , activeTag ) ;
637655 if ( activeTag . inlist ) {
@@ -646,7 +664,8 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
646664
647665 // Reset text, unless the parent is also collecting text
648666 if ( ! parentTag . predicates ) {
649- activeTag . text = null ;
667+ activeTag . textWithoutTags = null ;
668+ activeTag . textWithTags = null ;
650669 }
651670 }
652671
@@ -683,16 +702,23 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
683702 this . activeTagStack . pop ( ) ;
684703
685704 // Save the tag contents if needed
686- if ( activeTag . collectChildTags && activeTag . text ) {
687- activeTag . text . push ( `</${ activeTag . name } >` ) ;
705+ if ( activeTag . collectChildTags && activeTag . textWithTags ) {
706+ activeTag . textWithTags . push ( `</${ activeTag . name } >` ) ;
688707 }
689708
690709 // If we still have text contents, try to append it to the parent tag
691- if ( activeTag . text && parentTag ) {
692- if ( ! parentTag . text ) {
693- parentTag . text = activeTag . text ;
710+ if ( activeTag . textWithTags && parentTag ) {
711+ if ( ! parentTag . textWithTags ) {
712+ parentTag . textWithTags = activeTag . textWithTags ;
713+ } else {
714+ parentTag . textWithTags = parentTag . textWithTags . concat ( activeTag . textWithTags ) ;
715+ }
716+ }
717+ if ( activeTag . textWithoutTags && parentTag ) {
718+ if ( ! parentTag . textWithoutTags ) {
719+ parentTag . textWithoutTags = activeTag . textWithoutTags ;
694720 } else {
695- parentTag . text = parentTag . text . concat ( activeTag . text ) ;
721+ parentTag . textWithoutTags = parentTag . textWithoutTags . concat ( activeTag . textWithoutTags ) ;
696722 }
697723 }
698724 }
0 commit comments