diff --git a/.changeset/hungry-kiwis-behave.md b/.changeset/hungry-kiwis-behave.md new file mode 100644 index 0000000..21b2a9d --- /dev/null +++ b/.changeset/hungry-kiwis-behave.md @@ -0,0 +1,5 @@ +--- +"@hydrofoil/roadshow": patch +--- + +`dash:LabelViewer` should not render link for blank nodes diff --git a/.changeset/tall-rabbits-talk.md b/.changeset/tall-rabbits-talk.md new file mode 100644 index 0000000..e8c6576 --- /dev/null +++ b/.changeset/tall-rabbits-talk.md @@ -0,0 +1,5 @@ +--- +"@hydrofoil/roadshow": patch +--- + +`dash:LabelViewer`: correct precedence of predicates (`skos:xLabel`, then `schema:name`, `rdfs:label` last) diff --git a/packages/roadshow/renderers/Label.ts b/packages/roadshow/renderers/Label.ts index 57f8a3d..fc1a1a4 100644 --- a/packages/roadshow/renderers/Label.ts +++ b/packages/roadshow/renderers/Label.ts @@ -1,12 +1,24 @@ import { dash, rdfs, schema, skos } from '@tpluscode/rdf-ns-builders' import { html } from 'lit' +import { NamedNode } from '@rdfjs/types' import { Renderer } from '../index' -export const Label: Renderer = { +interface LabelRenderer extends Renderer { + predicates: NamedNode[] +} + +export const Label: LabelRenderer = { viewer: dash.LabelViewer, render(resource) { - const [label] = resource.out([rdfs.label, skos.prefLabel, schema.name], { language: [this.params.language, '*'] }).values + const [label] = resource.out(Label.predicates, { language: [this.params.language, '*'] }).values + + const labelResult = html`${label || resource.value}` + + if (resource.term?.termType === 'BlankNode') { + return labelResult + } - return html`${label || resource.value}` + return html`${labelResult}` }, + predicates: [skos.prefLabel, skos.altLabel, schema.name, rdfs.label], } diff --git a/packages/roadshow/test/renderers/Label.test.ts b/packages/roadshow/test/renderers/Label.test.ts new file mode 100644 index 0000000..928d538 --- /dev/null +++ b/packages/roadshow/test/renderers/Label.test.ts @@ -0,0 +1,80 @@ +import { expect, fixture, html } from '@open-wc/testing' +import { rdfs, schema, skos } from '@tpluscode/rdf-ns-builders/strict' +import { Label } from '../../renderers/Label' +import { blankNode, namedNode } from '../_support/clownface' +import { ViewContext } from '../../lib/ViewContext/index' + +describe('@hydrofoil/roadshow/renderers/Label', () => { + let context: ViewContext + + beforeEach(() => { + context = { + params: { + language: 'en', + }, + } as any + }) + + it('should not render anchor when value is a BlankNode', async () => { + // given + const pointer = blankNode() + .addOut(rdfs.label, 'foo') + + // when + const result = await fixture(html`
${Label.render.call(context, pointer)}
`) + + // then + expect(result).dom.to.eq('
foo
') + }) + + it('should use skos:prefLabel before skos:altLabel', async () => { + // given + const pointer = blankNode() + .addOut(skos.prefLabel, 'foo') + .addOut(skos.altLabel, 'bar') + + // when + const result = await fixture(html`
${Label.render.call(context, pointer)}
`) + + // then + expect(result).dom.to.eq('
foo
') + }) + + it('should use skos:altLabel before rdfs:label', async () => { + // given + const pointer = blankNode() + .addOut(rdfs.label, 'foo') + .addOut(skos.altLabel, 'bar') + + // when + const result = await fixture(html`
${Label.render.call(context, pointer)}
`) + + // then + expect(result).dom.to.eq('
bar
') + }) + + it('should use schema:name before rdfs:label', async () => { + // given + const pointer = blankNode() + .addOut(rdfs.label, 'foo') + .addOut(schema.name, 'bar') + + // when + const result = await fixture(html`
${Label.render.call(context, pointer)}
`) + + // then + expect(result).dom.to.eq('
bar
') + }) + + it('should render anchor for named node', async () => { + // given + const pointer = namedNode('urn:foo:bar') + .addOut(rdfs.label, 'baz') + + // when + const result = await fixture(Label.render.call(context, pointer)) + + // then + expect(result).dom.to.eq('baz') + }) +})