diff --git a/addon/model/model-range.ts b/addon/model/model-range.ts index 5c2c53fb2..ca0e36252 100644 --- a/addon/model/model-range.ts +++ b/addon/model/model-range.ts @@ -323,7 +323,6 @@ export default class ModelRange { ), ] as ModelText[]; if (nodes.length) { - console.log(nodes); let result = nodes[0].marks.clone(); for (const node of nodes.slice(1)) { result = result.intersection(node.marks); diff --git a/addon/model/util/datastore/datastore.ts b/addon/model/util/datastore/datastore.ts index 8da2d8257..095ff1575 100644 --- a/addon/model/util/datastore/datastore.ts +++ b/addon/model/util/datastore/datastore.ts @@ -114,6 +114,16 @@ export default interface Datastore { action: (dataset: RDF.Dataset, termconverter: TermConverter) => RDF.Dataset ): Datastore; + /** Transformer method. + * Allows specifying of custom prefixes for use in the {@link match} method + * and the termConverter in the {@link transformDataset} method, + * or overwrite the ones that were scanned from the document. + * + * Reminder: only has an effect on the convenience "concise term syntax". + * Has no effect on the actual rdfa parsing. + */ + withExtraPrefixes(prefixes: Record): this; + /** * Consumer method. * Returns a {@link TermMapping} of the currently valid subjects and the nodes that define them. @@ -229,8 +239,12 @@ export class EditorStore implements Datastore { predicateToNodesMapping, nodeToPredicatesMapping, quadToNodesMapping, + seenPrefixes, } = RdfaParser.parse(config); const prefixMap = new Map(Object.entries(defaultPrefixes)); + for (const [key, value] of seenPrefixes.entries()) { + prefixMap.set(key, value); + } return new EditorStore({ dataset, @@ -301,6 +315,13 @@ export class EditorStore implements Datastore { }); } + withExtraPrefixes(prefixes: Record): this { + for (const [key, value] of Object.entries(prefixes)) { + this._prefixMapping.set(key, value); + } + return this; + } + asSubjectNodeMapping(): TermMapping { return new TermMapping( this.subjectNodeGenerator(), diff --git a/addon/utils/rdfa-parser/rdfa-parser.ts b/addon/utils/rdfa-parser/rdfa-parser.ts index f19405825..3f7b4081f 100644 --- a/addon/utils/rdfa-parser/rdfa-parser.ts +++ b/addon/utils/rdfa-parser/rdfa-parser.ts @@ -70,6 +70,7 @@ export interface RdfaParseResponse { predicateToNodesMapping: Map; quadToNodesMapping: Map; + seenPrefixes: Map; } export class RdfaParser { @@ -97,6 +98,7 @@ export class RdfaParser { private rootModelNode?: ModelNode; private seenNodes: Map; + private globallySeenPrefixes: Map; constructor(options: IRdfaParserOptions) { this.options = options; @@ -113,6 +115,7 @@ export class RdfaParser { this.rdfaPatterns = {}; this.pendingRdfaPatternCopies = {}; this.resultSet = new GraphyDataset(); + this.globallySeenPrefixes = new Map(); this.nodeToSubjectMapping = new Map(); this.subjectToNodesMapping = new Map(); @@ -186,6 +189,7 @@ export class RdfaParser { nodeToObjectsMapping: parser.nodeToObjectsMapping, objectToNodesMapping: parser.objectToNodesMapping, quadToNodesMapping: parser.quadToNodesMapping, + seenPrefixes: parser.globallySeenPrefixes, }; } @@ -386,7 +390,8 @@ export class RdfaParser { activeTag.prefixesCustom = Util.parsePrefixes( attributes, parentTag.prefixesCustom, - this.features.xmlnsPrefixMappings + this.features.xmlnsPrefixMappings, + this.globallySeenPrefixes ); activeTag.prefixesAll = Object.keys(activeTag.prefixesCustom).length > 0 diff --git a/addon/utils/rdfa-parser/util.ts b/addon/utils/rdfa-parser/util.ts index 1d584c6c7..bce6bc222 100644 --- a/addon/utils/rdfa-parser/util.ts +++ b/addon/utils/rdfa-parser/util.ts @@ -111,12 +111,14 @@ export class Util { * @param {{[p: string]: string}} attributes A tag's attributes. * @param {{[p: string]: string}} parentPrefixes The prefixes from the parent tag. * @param {boolean} xmlnsPrefixMappings If prefixes should be extracted from xmlnsPrefixMappings. + * @param globallySeenPrefixes Optionally keep track of all encountered prefixes * @return {{[p: string]: string}} The new prefixes. */ public static parsePrefixes( attributes: { [s: string]: string }, parentPrefixes: { [prefix: string]: string }, - xmlnsPrefixMappings = false + xmlnsPrefixMappings = false, + globallySeenPrefixes?: Map ): { [prefix: string]: string } { const additionalPrefixes: { [prefix: string]: string } = {}; if (xmlnsPrefixMappings) { @@ -137,6 +139,17 @@ export class Util { let prefixMatch = Util.PREFIX_REGEX.exec(attributes.prefix); while (prefixMatch) { prefixes[prefixMatch[1]] = prefixMatch[2]; + if (globallySeenPrefixes) { + const key = prefixMatch[1]; + const uri = prefixMatch[2]; + const previousUri = globallySeenPrefixes.get(key); + if (previousUri && uri !== previousUri) { + console.warn(`Prefix ${key} is defined multiple times with a different uri. + Previous uri was ${previousUri}, now found ${uri}. + Will use latest value in concise-term-mapping logic`); + } + globallySeenPrefixes.set(key, uri); + } prefixMatch = Util.PREFIX_REGEX.exec(attributes.prefix); } }