Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/RdfaParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
const suffix = parentTag.prefixesCustom[prefix];
const attributeKey = prefix === '' ? 'xmlns' : 'xmlns:' + prefix;
if (!(attributeKey in attributes)) {
if (prefix !== '')
this.emitPrefix(prefix, suffix);
attributes[attributeKey] = suffix;
}
}
Expand Down Expand Up @@ -238,7 +240,7 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.

// 3: handle prefixes
activeTag.prefixesCustom = Util.parsePrefixes(attributes, parentTag.prefixesCustom,
this.features.xmlnsPrefixMappings);
this.features.xmlnsPrefixMappings, (prefix, suffix) => this.emitPrefix(prefix, suffix));
activeTag.prefixesAll = Object.keys(activeTag.prefixesCustom).length > 0
? { ...parentTag.prefixesAll, ...activeTag.prefixesCustom } : parentTag.prefixesAll;

Expand Down Expand Up @@ -782,6 +784,18 @@ export class RdfaParser extends Transform implements RDF.Sink<EventEmitter, RDF.
this.push(this.util.dataFactory.quad(subject, predicate, object, this.defaultGraph));
}

// TODO: Make sure we are not emitting things before the stream starts
/**
* Emit the prefix using the 'prefix' event
* @param prefix The prefix to emit.
* @param suffix The suffix to prefix.
*/
protected emitPrefix(prefix: string, suffix: string) {
console.log('emit prefix function called', prefix, suffix)
if (suffix.indexOf(':') < 0)
this.emit('prefix', prefix, this.util.dataFactory.namedNode(suffix));
}

/**
* Emit an instantiation of the given pattern with the given parent tag.
* @param {IActiveTag} parentTag The parent tag to instantiate in.
Expand Down
5 changes: 4 additions & 1 deletion lib/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ export class Util {
*/
public static parsePrefixes(attributes: {[s: string]: string},
parentPrefixes: {[prefix: string]: string},
xmlnsPrefixMappings: boolean): {[prefix: string]: string} {
xmlnsPrefixMappings: boolean,
cb?: (prefix: string, suffix: string) => void): {[prefix: string]: string} {
const additionalPrefixes: {[prefix: string]: string} = {};
if (xmlnsPrefixMappings) {
for (const attribute in attributes) {
if (attribute.startsWith('xmlns')) {
cb?.(attribute.substr(6), attributes[attribute])
additionalPrefixes[attribute.substr(6)] = attributes[attribute];
}
}
Expand All @@ -67,6 +69,7 @@ export class Util {
let prefixMatch;
// tslint:disable-next-line:no-conditional-assignment
while (prefixMatch = Util.PREFIX_REGEX.exec(attributes.prefix)) {
cb?.(prefixMatch[1], prefixMatch[2]);
prefixes[prefixMatch[1]] = prefixMatch[2];
}
}
Expand Down
Loading