1- import type { DomHandler } from 'domhandler' ;
2- import EventEmitter = NodeJS . EventEmitter ;
31import type * as RDF from '@rdfjs/types' ;
2+ import type { DomHandler } from 'domhandler' ;
43import { Parser as HtmlParser } from 'htmlparser2' ;
54import { PassThrough , Transform } from 'readable-stream' ;
65import type { IActiveTag } from './IActiveTag' ;
@@ -12,6 +11,8 @@ import type { IRdfaFeatures, RdfaProfile } from './RdfaProfile';
1211import { RDFA_FEATURES } from './RdfaProfile' ;
1312import { Util } from './Util' ;
1413
14+ type EventEmitter = NodeJS . EventEmitter ;
15+
1516/**
1617 * A stream transformer that parses RDFa (text) streams to an {@link RDF.Stream}.
1718 */
@@ -27,7 +28,7 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
2728
2829 private readonly activeTagStack : IActiveTag [ ] = [ ] ;
2930
30- constructor ( options ?: IRdfaParserOptions ) {
31+ public constructor ( options ?: IRdfaParserOptions ) {
3132 super ( { readableObjectMode : true } ) ;
3233 options = options || { } ;
3334 this . options = options ;
@@ -74,7 +75,7 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
7475 }
7576
7677 public _transform ( chunk : any , encoding : string , callback : ( error ?: Error | null , data ?: any ) => void ) : void {
77- this . parser . write ( chunk . toString ( ) ) ;
78+ this . parser . write ( String ( chunk ) ) ;
7879 callback ( ) ;
7980 }
8081
@@ -83,7 +84,7 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
8384 callback ( ) ;
8485 }
8586
86- public onTagOpen ( name : string , attributes : Record < string , string > ) {
87+ public onTagOpen ( name : string , attributes : Record < string , string > ) : void {
8788 // Determine the parent tag (ignore skipped tags)
8889 let parentTagI : number = this . activeTagStack . length - 1 ;
8990 while ( parentTagI > 0 && this . activeTagStack [ parentTagI ] . skipElement ) {
@@ -238,7 +239,9 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
238239 }
239240
240241 // 3: handle prefixes
241- activeTag . prefixesCustom = Util . parsePrefixes ( attributes , parentTag . prefixesCustom , Boolean ( this . features . xmlnsPrefixMappings ) ) ;
242+ activeTag . prefixesCustom = Util . parsePrefixes (
243+ attributes , parentTag . prefixesCustom , Boolean ( this . features . xmlnsPrefixMappings ) ,
244+ ) ;
242245 activeTag . prefixesAll = Object . keys ( activeTag . prefixesCustom ) . length > 0 ?
243246 { ...parentTag . prefixesAll , ...activeTag . prefixesCustom } :
244247 parentTag . prefixesAll ;
@@ -338,7 +341,8 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
338341 typedResource = newSubject ;
339342 }
340343 }
341- } else { // Either rel or rev is present
344+ } else {
345+ // Either rel or rev is present
342346 // 6: Determine the new subject when rel or rev are present
343347
344348 // Define new subject
@@ -396,15 +400,17 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
396400 if ( currentObjectResource ) {
397401 // Handle list mapping
398402 if ( 'rel' in attributes && 'inlist' in attributes ) {
399- for ( const predicate of this . util . createVocabIris ( attributes . rel , activeTag , allowTermsInRelPredicates , false ) ) {
403+ for ( const predicate of
404+ this . util . createVocabIris ( attributes . rel , activeTag , allowTermsInRelPredicates , false ) ) {
400405 this . addListMapping ( activeTag , newSubject ! , predicate , currentObjectResource ) ;
401406 }
402407 }
403408
404409 // Determine predicates using rel or rev (unless rel and inlist are present)
405410 if ( ! ( 'rel' in attributes && 'inlist' in attributes ) ) {
406411 if ( 'rel' in attributes ) {
407- for ( const predicate of this . util . createVocabIris ( attributes . rel , activeTag , allowTermsInRelPredicates , false ) ) {
412+ for ( const predicate of
413+ this . util . createVocabIris ( attributes . rel , activeTag , allowTermsInRelPredicates , false ) ) {
408414 this . emitTriple (
409415 this . util . getResourceOrBaseIri ( newSubject ! , activeTag ) ,
410416 predicate ,
@@ -413,7 +419,8 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
413419 }
414420 }
415421 if ( 'rev' in attributes ) {
416- for ( const predicate of this . util . createVocabIris ( attributes . rev , activeTag , allowTermsInRevPredicates , false ) ) {
422+ for ( const predicate of
423+ this . util . createVocabIris ( attributes . rev , activeTag , allowTermsInRevPredicates , false ) ) {
417424 this . emitTriple (
418425 this . util . getResourceOrBaseIri ( currentObjectResource , activeTag ) ,
419426 predicate ,
@@ -428,18 +435,21 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
428435 if ( ! currentObjectResource ) {
429436 if ( 'rel' in attributes ) {
430437 if ( 'inlist' in attributes ) {
431- for ( const predicate of this . util . createVocabIris ( attributes . rel , activeTag , allowTermsInRelPredicates , false ) ) {
438+ for ( const predicate of
439+ this . util . createVocabIris ( attributes . rel , activeTag , allowTermsInRelPredicates , false ) ) {
432440 this . addListMapping ( activeTag , newSubject ! , predicate , false ) ;
433441 activeTag . incompleteTriples ! . push ( { predicate, reverse : false , list : true } ) ;
434442 }
435443 } else {
436- for ( const predicate of this . util . createVocabIris ( attributes . rel , activeTag , allowTermsInRelPredicates , false ) ) {
444+ for ( const predicate of
445+ this . util . createVocabIris ( attributes . rel , activeTag , allowTermsInRelPredicates , false ) ) {
437446 activeTag . incompleteTriples ! . push ( { predicate, reverse : false } ) ;
438447 }
439448 }
440449 }
441450 if ( 'rev' in attributes ) {
442- for ( const predicate of this . util . createVocabIris ( attributes . rev , activeTag , allowTermsInRevPredicates , false ) ) {
451+ for ( const predicate of
452+ this . util . createVocabIris ( attributes . rev , activeTag , allowTermsInRevPredicates , false ) ) {
443453 activeTag . incompleteTriples ! . push ( { predicate, reverse : true } ) ;
444454 }
445455 }
@@ -553,30 +563,30 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
553563 this . emitTriple ( object , incompleteTriple . predicate , subject ) ;
554564 } else if ( incompleteTriple . list ) {
555565 // Find the active tag that defined the list by going up the stack
556- let firstInListTag : any = null ;
566+ let firstInListTag : IActiveTag | null = null ;
557567 for ( let i = this . activeTagStack . length - 1 ; i >= 0 ; i -- ) {
558568 if ( this . activeTagStack [ i ] . inlist ) {
559569 firstInListTag = this . activeTagStack [ i ] ;
560570 break ;
561571 }
562572 }
563573 // FirstInListTag is guaranteed to be non-null
564- this . addListMapping ( firstInListTag , newSubject , incompleteTriple . predicate , object ) ;
574+ this . addListMapping ( firstInListTag ! , newSubject , incompleteTriple . predicate , object ) ;
565575 } else {
566576 this . emitTriple ( subject , incompleteTriple . predicate , object ) ;
567577 }
568578 }
569579 }
570580 if ( ! incompleteTriplesCompleted && parentTag . incompleteTriples ! . length > 0 ) {
571- activeTag . incompleteTriples = activeTag . incompleteTriples ! . concat ( parentTag . incompleteTriples ! ) ;
581+ activeTag . incompleteTriples = [ ... activeTag . incompleteTriples ! , ... parentTag . incompleteTriples ! ] ;
572582 }
573583
574584 // 13: Save evaluation context into active tag
575585 activeTag . subject = newSubject || parentTag . subject ;
576586 activeTag . object = currentObjectResource || newSubject ;
577587 }
578588
579- public onText ( data : string ) {
589+ public onText ( data : string ) : void {
580590 const activeTag : IActiveTag = this . activeTagStack . at ( - 1 ) ;
581591
582592 // Collect text in pattern tag if needed
@@ -596,7 +606,7 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
596606 activeTag . textWithoutTags . push ( data ) ;
597607 }
598608
599- public onTagClose ( ) {
609+ public onTagClose ( ) : void {
600610 // Get the active tag
601611 const activeTag : IActiveTag = this . activeTagStack . at ( - 1 ) ;
602612 const parentTag : IActiveTag = this . activeTagStack . at ( - 2 ) ;
@@ -696,21 +706,21 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
696706 // If we still have text contents, try to append it to the parent tag
697707 if ( activeTag . textWithTags && parentTag ) {
698708 if ( parentTag . textWithTags ) {
699- parentTag . textWithTags = parentTag . textWithTags . concat ( activeTag . textWithTags ) ;
709+ parentTag . textWithTags = [ ... parentTag . textWithTags , ... activeTag . textWithTags ] ;
700710 } else {
701711 parentTag . textWithTags = activeTag . textWithTags ;
702712 }
703713 }
704714 if ( activeTag . textWithoutTags && parentTag ) {
705715 if ( parentTag . textWithoutTags ) {
706- parentTag . textWithoutTags = parentTag . textWithoutTags . concat ( activeTag . textWithoutTags ) ;
716+ parentTag . textWithoutTags = [ ... parentTag . textWithoutTags , ... activeTag . textWithoutTags ] ;
707717 } else {
708718 parentTag . textWithoutTags = activeTag . textWithoutTags ;
709719 }
710720 }
711721 }
712722
713- public onEnd ( ) {
723+ public onEnd ( ) : void {
714724 if ( this . features . copyRdfaPatterns ) {
715725 this . features . copyRdfaPatterns = false ;
716726
@@ -747,7 +757,7 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
747757 * @param {string } name The current tag name.
748758 * @returns {boolean } If the subject can be inherited.
749759 */
750- protected isInheritSubjectInHeadBody ( name : string ) {
760+ protected isInheritSubjectInHeadBody ( name : string ) : boolean {
751761 return this . features . inheritSubjectInHeadBody && ( name === 'head' || name === 'body' ) ;
752762 }
753763
@@ -759,12 +769,19 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
759769 * @param {Term } predicate A predicate term.
760770 * @param {Term | boolean } currentObjectResource The current object resource.
761771 */
762- protected addListMapping ( activeTag : IActiveTag , subject : RDF . Quad_Subject | boolean , predicate : RDF . Quad_Predicate , currentObjectResource : RDF . Quad_Object | boolean ) {
772+ protected addListMapping (
773+ activeTag : IActiveTag ,
774+ subject : RDF . Quad_Subject | boolean ,
775+ predicate : RDF . Quad_Predicate ,
776+ currentObjectResource : RDF . Quad_Object | boolean ,
777+ ) : void {
763778 if ( activeTag . explicitNewSubject ) {
764779 const bNode = this . util . createBlankNode ( ) ;
765780 this . emitTriple ( this . util . getResourceOrBaseIri ( subject , activeTag ) , predicate , bNode ) ;
766- this . emitTriple ( bNode , this . util . dataFactory . namedNode ( `${ Util . RDF } first` ) , this . util . getResourceOrBaseIri ( currentObjectResource , activeTag ) ) ;
767- this . emitTriple ( bNode , this . util . dataFactory . namedNode ( `${ Util . RDF } rest` ) , this . util . dataFactory . namedNode ( `${ Util . RDF } nil` ) ) ;
781+ this . emitTriple ( bNode , this . util . dataFactory . namedNode ( `${ Util . RDF } first` ) ,
782+ this . util . getResourceOrBaseIri ( currentObjectResource , activeTag ) ) ;
783+ this . emitTriple ( bNode , this . util . dataFactory . namedNode ( `${ Util . RDF } rest` ) ,
784+ this . util . dataFactory . namedNode ( `${ Util . RDF } nil` ) ) ;
768785 } else {
769786 let predicateList = activeTag . listMappingLocal [ predicate . value ] ;
770787 if ( ! predicateList ) {
@@ -782,7 +799,7 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
782799 * @param {Term } predicate A predicate term.
783800 * @param {Term } object An object term.
784801 */
785- protected emitTriple ( subject : RDF . Quad_Subject , predicate : RDF . Quad_Predicate , object : RDF . Quad_Object ) {
802+ protected emitTriple ( subject : RDF . Quad_Subject , predicate : RDF . Quad_Predicate , object : RDF . Quad_Object ) : void {
786803 // Validate IRIs
787804 if ( ( subject . termType === 'NamedNode' && ! subject . value . includes ( ':' ) ) ||
788805 ( predicate . termType === 'NamedNode' && ! predicate . value . includes ( ':' ) ) ||
@@ -798,7 +815,7 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
798815 * @param {IRdfaPattern } pattern The pattern to instantiate.
799816 * @param {string } rootPatternId The pattern id.
800817 */
801- protected emitPatternCopy ( parentTag : IActiveTag , pattern : IRdfaPattern , rootPatternId : string ) {
818+ protected emitPatternCopy ( parentTag : IActiveTag , pattern : IRdfaPattern , rootPatternId : string ) : void {
802819 this . activeTagStack . push ( parentTag ) ;
803820 pattern . referenced = true ;
804821
@@ -833,7 +850,7 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
833850 * @param {boolean } root If this is the root call for the given pattern.
834851 * @param {string } rootPatternId The pattern id.
835852 */
836- protected emitPatternCopyAbsolute ( pattern : IRdfaPattern , root : boolean , rootPatternId : string ) {
853+ protected emitPatternCopyAbsolute ( pattern : IRdfaPattern , root : boolean , rootPatternId : string ) : void {
837854 // Stop on detection of cyclic patterns
838855 if ( ! root && pattern . attributes . property === 'rdfa:copy' && pattern . attributes . href === rootPatternId ) {
839856 return ;
0 commit comments