diff --git a/.changeset/twelve-experts-pick.md b/.changeset/twelve-experts-pick.md new file mode 100644 index 000000000..8f0355903 --- /dev/null +++ b/.changeset/twelve-experts-pick.md @@ -0,0 +1,5 @@ +--- +'@lblod/ember-rdfa-editor': patch +--- + +Add extra check to `postProcessTagAsRdfaNode` function to correctly parse older style literal nodes diff --git a/packages/ember-rdfa-editor/src/utils/_private/rdfa-parser/post-process-as-rdfa-nodes.ts b/packages/ember-rdfa-editor/src/utils/_private/rdfa-parser/post-process-as-rdfa-nodes.ts index 3edc5893e..0442393ba 100644 --- a/packages/ember-rdfa-editor/src/utils/_private/rdfa-parser/post-process-as-rdfa-nodes.ts +++ b/packages/ember-rdfa-editor/src/utils/_private/rdfa-parser/post-process-as-rdfa-nodes.ts @@ -5,6 +5,7 @@ export interface PostProcessArgs { activeTag: IActiveTag; attributes: Record; isRootTag: boolean; + isExternalTriple: boolean; typedResource: true | ModelBlankNode | ModelNamedNode | null; markAsLiteralNode: ( node: N, @@ -76,11 +77,25 @@ export function postProcessTagAsRdfaNode(args: PostProcessArgs): void { activeTag, attributes, isRootTag, + isExternalTriple, typedResource, markAsLiteralNode, markAsResourceNode, } = args; const node = activeTag.node; + if (node && isExternalTriple) { + markAsResourceNode( + node, + unwrap(activeTag.subject), + activeTag, + activeTag.predicates?.find( + (pred) => pred.value === attributes['property'], + ), + activeTag.datatype, + activeTag.language, + ); + return; + } if (!activeTag.skipElement && node) { // no rel or rev if ( @@ -120,7 +135,9 @@ export function postProcessTagAsRdfaNode(args: PostProcessArgs): void { } else { if ( truthyAttribute(attributes, 'about') && - !truthyAttribute(attributes, 'data-literal-node') + !truthyAttribute(attributes, 'data-literal-node') && + // Is this correct, an alternative solution would be to check on the presence of a `resource` attr here? + !truthyAttribute(attributes, 'datatype') ) { // same exception as above, we always interpret (property +about -content) cases as literal nodes markAsResourceNode( diff --git a/packages/ember-rdfa-editor/src/utils/_private/rdfa-parser/rdfa-parser.ts b/packages/ember-rdfa-editor/src/utils/_private/rdfa-parser/rdfa-parser.ts index de7956051..0acd8aea5 100644 --- a/packages/ember-rdfa-editor/src/utils/_private/rdfa-parser/rdfa-parser.ts +++ b/packages/ember-rdfa-editor/src/utils/_private/rdfa-parser/rdfa-parser.ts @@ -1204,10 +1204,14 @@ export class RdfaParser { } } const isRootTag: boolean = this.activeTagStack.length === 1; + const isExternalTriple = Boolean( + parentTag?.attributes['data-external-triple-container'], + ); postProcessTagAsRdfaNode({ activeTag, attributes: activeTag.attributes, isRootTag, + isExternalTriple, typedResource: activeTag.typedResource ?? null, markAsLiteralNode: this.markAsLiteralNode, markAsResourceNode: this.markAsResourceNode, diff --git a/test-app/tests/unit/rdfa/rdfa-parsing-test.ts b/test-app/tests/unit/rdfa/rdfa-parsing-test.ts index e50909103..6295cef3b 100644 --- a/test-app/tests/unit/rdfa/rdfa-parsing-test.ts +++ b/test-app/tests/unit/rdfa/rdfa-parsing-test.ts @@ -733,4 +733,96 @@ module('rdfa | parsing', function () { const resultingDocument = controller.mainEditorState.doc; assert.propEqual(resultingDocument.toJSON(), initialDocument.toJSON()); }); + test('Older documents with older style literal nodes should be correctly parsed', function (assert) { + QUnit.dump.maxDepth = 10; + const html = ` +
+ +
+
+ +
+

Decision title

+
+
+
+
+ `; + const { doc, block_rdfa, heading } = testBuilders; + const df = new SayDataFactory(); + const expectedDoc = doc( + {}, + block_rdfa( + { + rdfaNodeType: 'resource', + subject: 'http://data.lblod.info/id/besluiten/1', + __rdfaId: '30e2d215-a8b6-4bab-9d8b-aaab634589ea', + properties: [ + { + predicate: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', + object: df.namedNode( + 'http://data.vlaanderen.be/ns/besluit#Besluit', + ), + }, + { + predicate: 'http://data.europa.eu/eli/ontology#title', + object: df.literalNode('literal-1'), + }, + ] satisfies OutgoingTriple[], + }, + heading( + { + rdfaNodeType: 'literal', + __rdfaId: 'literal-1', + backlinks: [ + { + predicate: 'http://data.europa.eu/eli/ontology#title', + subject: sayDataFactory.resourceNode( + 'http://data.lblod.info/id/besluiten/1', + ), + }, + ], + datatype: sayDataFactory.namedNode( + 'http://www.w3.org/2001/XMLSchema#string', + ), + language: '', + level: 4, + }, + 'Decision title', + ), + ), + ); + const { controller } = testEditor(schema, plugins); + controller.initialize(html); + const actualDoc = controller.mainEditorState.doc; + assert.propEqual(actualDoc.toJSON(), expectedDoc.toJSON()); + }); });