Skip to content

Commit

Permalink
accepts nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
sballesteros committed Jan 30, 2016
1 parent 2f6ee2e commit 7edbecc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ For convenience, a high level API has been added:
```
import getRdfaGraph from graph-rdfa-processor;
let graph = getRdfaGraph(document, opts);
let opts = {baseURI: 'http://example.com'};
let graph = getRdfaGraph(documentOrNode, opts);
console.log(graph.toString());
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graph-rdfa-processor",
"version": "1.0.1",
"version": "1.1.0",
"description": "Green turtle GraphRdfaProcessor extracted and available as a commonJS module",
"main": "dist/index.js",
"scripts": {
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import GraphRDFaProcessor from './graph-rdfa-processor';
import { RDFaGraph } from './rdfa-graph';

export default function(document, options = {}) {
let baseURI = options.baseURI ? options.baseURI : document.documentElement.baseURI;
let node = document.documentElement || document;
let baseURI = options.baseURI ? options.baseURI : node.baseURI;

let graph = new RDFaGraph();

Expand All @@ -13,6 +14,6 @@ export default function(document, options = {}) {
};

var processor = new GraphRDFaProcessor(target);
processor.process(document.documentElement, options);
processor.process(node, options);
return target.graph;
};
1 change: 0 additions & 1 deletion src/rdfa-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ export default class RDFaProcessor extends URIResolver {


process(node, options) {

if (node.nodeType==Node.DOCUMENT_NODE) {
node = node.documentElement;
this.setContext(node);
Expand Down
24 changes: 16 additions & 8 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,34 @@ import assert from 'assert';
import getRDFaGraph from '../src';

describe('getRDFaGraph', function() {
it('should getRDFaGraph from a document', function() {
let html =
`<div typeof="rdfs:Class" resource="http://schema.org/CreativeWork">
let html =
`<div typeof="rdfs:Class" resource="http://schema.org/CreativeWork">
<span class="h" property="rdfs:label">CreativeWork</span>
<span property="rdfs:comment">The most generic kind of creative work, including books, movies, photographs, software programs, etc.</span>
<span>Subclass of: <a property="rdfs:subClassOf" href="http://schema.org/Thing">Thing</a></span>
<span>Source: <a property="dc:source" href="http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_rNews">rNews</a></span>
</div>`;

let { document } = jsdom(html).defaultView.window;

let graph = getRDFaGraph(document, {baseURI: 'http://localhost'});

let expected = `<http://schema.org/CreativeWork> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class>;
let expected = `<http://schema.org/CreativeWork> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class>;
<http://www.w3.org/2000/01/rdf-schema#label> "CreativeWork";
<http://www.w3.org/2000/01/rdf-schema#comment> "The most generic kind of creative work, including books, movies, photographs, software programs, etc.";
<http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://schema.org/Thing>;
<http://purl.org/dc/terms/source> <http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_rNews> .
`;

assert.equal(graph.toString(), expected);

it('should getRDFaGraph from a document', function() {
let { document } = jsdom(html).defaultView.window;
let graph = getRDFaGraph(document, {baseURI: 'http://localhost'});
assert.equal(graph.toString(), expected);
});


it('should getRDFaGraph from a node', function() {
let { document } = jsdom(html).defaultView.window;
let graph = getRDFaGraph(document.getElementsByTagName('div')[0], {baseURI: 'http://localhost'});
assert.equal(graph.toString(), expected);
});

});

0 comments on commit 7edbecc

Please sign in to comment.