Skip to content

Commit

Permalink
protect agains about:blank
Browse files Browse the repository at this point in the history
  • Loading branch information
sballesteros committed Jan 30, 2016
1 parent e41390b commit 2f6ee2e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ For convenience, a high level API has been added:
```
import getRdfaGraph from graph-rdfa-processor;
let graph = getRdfaGraph(document);
let graph = getRdfaGraph(document, 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.0",
"version": "1.0.1",
"description": "Green turtle GraphRdfaProcessor extracted and available as a commonJS module",
"main": "dist/index.js",
"scripts": {
Expand Down
6 changes: 2 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import URIResolver from './uri-resolver';
import GraphRDFaProcessor from './graph-rdfa-processor';
import { RDFaGraph } from './rdfa-graph';

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

// TODO cleanup the baseURI mess. It's all broken in green-turtle see https://github.com/alexmilowski/green-turtle/issues/6
let graph = new RDFaGraph();
graph.baseURI = new URIResolver().parseURI(baseURI);

let target ={
graph,
baseUri: new URIResolver().parseURI(baseURI)
baseURI: new URIResolver().parseURI(baseURI)
};

var processor = new GraphRDFaProcessor(target);
Expand Down
22 changes: 17 additions & 5 deletions src/rdfa-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,6 @@ export default class RDFaProcessor extends URIResolver {

process(node, options) {

/*
if (!window.console) {
window.console = { log: function() {} };
}*/
if (node.nodeType==Node.DOCUMENT_NODE) {
node = node.documentElement;
this.setContext(node);
Expand Down Expand Up @@ -349,7 +345,23 @@ export default class RDFaProcessor extends URIResolver {
var vocabulary = context.vocabulary;

// TODO: the "base" element may be used for HTML+RDFa 1.1
var base = this.parseURI(removeHash(current.baseURI));
var base;
if (!current.baseURI) {
if (this.target.baseURI) {
base = this.target.baseURI;
} else {
throw new Error('node.baseURI was null as baseURI must be specified as an option');
}
} else if (current.baseURI === 'about:blank') {
if (this.target.baseURI) {
base = this.target.baseURI;
} else {
throw new Error('node.baseURI is about:blank a valid URL must be provided with the baseURI option. If you use JSDOM call it with an `url` parameter or, set the baseURI option of this library');
}
} else {
base = this.parseURI(removeHash(current.baseURI));
}

current.item = null;

// Sequence Step 2: set the default vocabulary
Expand Down
18 changes: 9 additions & 9 deletions src/uri-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default class URIResolver {
parseURI(uri) {
var match = URIResolver.SCHEME.exec(uri);
if (!match) {
throw new Error("Bad URI value, no scheme: "+uri);
throw new Error("Bad URI value, no scheme: " + uri);
}
var parsed = { spec: uri };
parsed.scheme = match[0].substring(0,match[0].length-1);
Expand Down Expand Up @@ -48,8 +48,8 @@ export default class URIResolver {
i--;
}
}
this.path = this.segments.length==0 ? "/" : "/"+this.segments.join("/")+end;
this.schemeSpecificPart = "//"+this.authority+this.path;
this.path = this.segments.length==0 ? "/" : "/" + this.segments.join("/") + end;
this.schemeSpecificPart = "//" + this.authority + this.path;
if (typeof this.query != "undefined") {
this.schemeSpecificPart += "?" + this.query;
}
Expand All @@ -68,7 +68,7 @@ export default class URIResolver {
return lastHash<0 ? this.spec+href : this.spec.substring(0,lastHash)+href;
}
if (!this.isGeneric) {
throw "Cannot resolve uri against non-generic URI: "+this.spec;
throw new Error("Cannot resolve uri against non-generic URI: " + this.spec);
}
var colon = href.indexOf(':');
if (href.charAt(0)=='/') {
Expand Down Expand Up @@ -99,10 +99,10 @@ export default class URIResolver {
return this.spec;
}
if (!this.isGeneric) {
throw "A non generic URI cannot be made relative: "+this.spec;
throw new Error("A non generic URI cannot be made relative: " + this.spec);
}
if (!otherURI.isGeneric) {
throw "Cannot make a relative URI against a non-generic URI: "+otherURI.spec;
throw new Error("Cannot make a relative URI against a non-generic URI: " + otherURI.spec);
}
if (otherURI.authority!=this.authority) {
return this.spec;
Expand Down Expand Up @@ -143,15 +143,15 @@ export default class URIResolver {
}
return relative;
} else {
throw "Cannot calculate a relative URI for "+this.spec+" against "+otherURI.spec;
throw new Error("Cannot calculate a relative URI for "+this.spec+" against " + otherURI.spec);
}
};
return parsed;
}

parseGeneric(parsed) {
if (parsed.schemeSpecificPart.charAt(0)!='/' || parsed.schemeSpecificPart.charAt(1)!='/') {
throw "Generic URI values should start with '//':"+parsed.spec;
throw new Error("Generic URI values should start with '//':" + parsed.spec);
}

var work = parsed.schemeSpecificPart.substring(2);
Expand Down Expand Up @@ -189,7 +189,7 @@ export default class URIResolver {

for (var j=0; j < check.length; j++) {
if (check[j].length>0) {
throw "Unecaped character "+check[j].charAt(0)+" ("+check[j].charCodeAt(0)+") in URI "+parsed.spec;
throw new Error("Unecaped character "+check[j].charAt(0)+" ("+check[j].charCodeAt(0)+") in URI " + parsed.spec);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ describe('getRDFaGraph', function() {
<span>Source: <a property="dc:source" href="http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_rNews">rNews</a></span>
</div>`;

let { document } = jsdom(html, {url: 'file:///'}).defaultView.window;
let { document } = jsdom(html).defaultView.window;

let graph = getRDFaGraph(document);
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>;
<http://www.w3.org/2000/01/rdf-schema#label> "CreativeWork";
Expand Down

0 comments on commit 2f6ee2e

Please sign in to comment.