-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58143ea
commit 1fbf3a2
Showing
4 changed files
with
106 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
let jsdom = { | ||
env: (config) => { | ||
if (config.done) { | ||
config.done(new Error('Only DOM elements are supported in a browser environment')); | ||
} | ||
}, | ||
export function JSDOM() { | ||
throw new Error('Only DOM elements are supported in a browser environment'); | ||
} | ||
|
||
JSDOM.fromFile = function() { | ||
return Promise.reject( | ||
new Error('Only DOM elements are supported in a browser environment') | ||
); | ||
}; | ||
|
||
export default jsdom; | ||
JSDOM.fromURL = function() { | ||
return Promise.reject( | ||
new Error('Only DOM elements are supported in a browser environment') | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
|
||
import assert from 'assert'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import jsonld from 'jsonld'; | ||
import jsonldRdfaParser from '../src'; | ||
import jsdom from 'jsdom'; | ||
import { JSDOM } from 'jsdom'; | ||
import http from 'http'; | ||
|
||
const htmlPath = path.join(path.dirname(__filename), 'fixtures', 'test.html'); | ||
|
@@ -14,44 +13,43 @@ const expected = { | |
'@type': 'ScholarlyArticle', | ||
name: { | ||
'@type': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML', | ||
'@value': 'De la <em>bombe</em> bébé!', | ||
'@value': 'De la <em>bombe</em> bébé!' | ||
}, | ||
author: { | ||
'@id': '_:b0', | ||
'@type': 'sa:ContributorRole', | ||
roleAffiliation: { | ||
'@id': 'https://science.ai/', | ||
'@type': 'Corporation', | ||
name: 'science.ai', | ||
name: 'science.ai' | ||
}, | ||
roleContactPoint: { | ||
'@id': '_:b1', | ||
'@type': 'ContactPoint', | ||
email: { '@id': 'mailto:[email protected]' }, | ||
email: { '@id': 'mailto:[email protected]' } | ||
}, | ||
author: | ||
{ | ||
author: { | ||
'@id': 'http://berjon.com/', | ||
'@type': 'Person', | ||
familyName: 'Berjon', | ||
givenName: 'Robin', | ||
}, | ||
givenName: 'Robin' | ||
} | ||
}, | ||
contributor: { | ||
'@id': '_:b2', | ||
'@type': 'sa:ContributorRole', | ||
roleAffiliation: { | ||
roleAffiliation: { | ||
'@id': 'https://science.ai/', | ||
'@type': 'Corporation', | ||
name: 'science.ai', | ||
name: 'science.ai' | ||
}, | ||
contributor: { | ||
'@id': 'https://github.com/sballesteros', | ||
'@type': 'Person', | ||
familyName: 'Ballesteros', | ||
givenName: 'Sebastien', | ||
}, | ||
}, | ||
givenName: 'Sebastien' | ||
} | ||
} | ||
}; | ||
|
||
const frame = { | ||
|
@@ -60,28 +58,27 @@ const frame = { | |
sa: 'http://ns.science.ai#', | ||
roleAffiliation: { | ||
'@id': 'sa:roleAffiliation', | ||
'@type': '@id', | ||
'@type': '@id' | ||
}, | ||
roleContactPoint: { | ||
'@id': 'sa:roleContactPoint', | ||
'@type': '@id', | ||
'@type': '@id' | ||
}, | ||
author: { | ||
'@type': '@id', | ||
'@type': '@id' | ||
}, | ||
contributor: { | ||
'@type': '@id', | ||
}, | ||
'@type': '@id' | ||
} | ||
}, | ||
'@embed': '@always', | ||
'@type': 'ScholarlyArticle', | ||
'@type': 'ScholarlyArticle' | ||
}; | ||
|
||
|
||
describe('jsonld-rdfa-parser', () => { | ||
let server; | ||
|
||
before((done) => { | ||
before(done => { | ||
jsonld.registerRDFParser('text/html', jsonldRdfaParser); | ||
server = http.createServer((req, res) => { | ||
fs.readFile(htmlPath, (error, data) => { | ||
|
@@ -92,18 +89,20 @@ describe('jsonld-rdfa-parser', () => { | |
server.listen(3000, '127.0.0.1', done); | ||
}); | ||
|
||
it('should parse RDFa from a file path ', (done) => { | ||
it('should parse RDFa from a file path ', done => { | ||
jsonld.fromRDF(htmlPath, { format: 'text/html' }, (err, data) => { | ||
if (err) return done(err); | ||
jsonld.frame(data, frame, (err, framed) => { | ||
assert.deepEqual(framed['@graph'][0], expected); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should parse RDFa from a string of HTML ', (done) => { | ||
it('should parse RDFa from a string of HTML ', done => { | ||
fs.readFile(htmlPath, { encoding: 'utf8' }, (err, html) => { | ||
jsonld.fromRDF(html, { format: 'text/html' }, (err, data) => { | ||
if (err) return done(err); | ||
jsonld.frame(data, frame, (err, framed) => { | ||
assert.deepEqual(framed['@graph'][0], expected); | ||
done(); | ||
|
@@ -112,11 +111,11 @@ describe('jsonld-rdfa-parser', () => { | |
}); | ||
}); | ||
|
||
it('should parse RDFa from a DOM node', (done) => { | ||
it('should parse RDFa from a DOM node', done => { | ||
fs.readFile(htmlPath, { encoding: 'utf8' }, (err, html) => { | ||
let { body } = jsdom.jsdom(html).defaultView.window.document; | ||
let { body } = new JSDOM(html).window.document; | ||
jsonld.fromRDF(body, { format: 'text/html' }, (err, data) => { | ||
if (err) throw err; | ||
if (err) return done(err); | ||
jsonld.frame(data, frame, (err, framed) => { | ||
assert.deepEqual(framed['@graph'][0], expected); | ||
done(); | ||
|
@@ -125,14 +124,18 @@ describe('jsonld-rdfa-parser', () => { | |
}); | ||
}); | ||
|
||
it('should parse RDFa from a URL', (done) => { | ||
jsonld.fromRDF('http://127.0.0.1:3000', { format: 'text/html' }, (err, data) => { | ||
if (err) throw err; | ||
jsonld.frame(data, frame, (err, framed) => { | ||
assert.deepEqual(framed['@graph'][0], expected); | ||
done(); | ||
}); | ||
}); | ||
it('should parse RDFa from a URL', done => { | ||
jsonld.fromRDF( | ||
'http://127.0.0.1:3000', | ||
{ format: 'text/html' }, | ||
(err, data) => { | ||
if (err) return done(err); | ||
jsonld.frame(data, frame, (err, framed) => { | ||
assert.deepEqual(framed['@graph'][0], expected); | ||
done(); | ||
}); | ||
} | ||
); | ||
}); | ||
|
||
after(() => { | ||
|