Skip to content

Commit

Permalink
Merge pull request #50 from hypermedia-app/label-blank-no-link
Browse files Browse the repository at this point in the history
fix: not links to blank nodes
  • Loading branch information
tpluscode authored Apr 9, 2022
2 parents 9e65a3f + 66a7758 commit 782e233
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-kiwis-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hydrofoil/roadshow": patch
---

`dash:LabelViewer` should not render link for blank nodes
5 changes: 5 additions & 0 deletions .changeset/tall-rabbits-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hydrofoil/roadshow": patch
---

`dash:LabelViewer`: correct precedence of predicates (`skos:xLabel`, then `schema:name`, `rdfs:label` last)
18 changes: 15 additions & 3 deletions packages/roadshow/renderers/Label.ts
Original file line number Diff line number Diff line change
@@ -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`<a href="${resource.values[0]}">${label || resource.value}</a>`
return html`<a href="${resource.values[0]}">${labelResult}</a>`
},
predicates: [skos.prefLabel, skos.altLabel, schema.name, rdfs.label],
}
80 changes: 80 additions & 0 deletions packages/roadshow/test/renderers/Label.test.ts
Original file line number Diff line number Diff line change
@@ -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<any>

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`<div>${Label.render.call(context, pointer)}</div>`)

// then
expect(result).dom.to.eq('<div>foo</div>')
})

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`<div>${Label.render.call(context, pointer)}</div>`)

// then
expect(result).dom.to.eq('<div>foo</div>')
})

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`<div>${Label.render.call(context, pointer)}</div>`)

// then
expect(result).dom.to.eq('<div>bar</div>')
})

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`<div>${Label.render.call(context, pointer)}</div>`)

// then
expect(result).dom.to.eq('<div>bar</div>')
})

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('<a href="urn:foo:bar">baz</a>')
})
})

0 comments on commit 782e233

Please sign in to comment.