diff --git a/.travis.yml b/.travis.yml index 12e92a8..bb5286b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: node_js sudo: false node_js: - - '6.5' + - '7.0' script: - npm run lint - npm run test diff --git a/JSDOMParser.js b/JSDOMParser.js index 18328f6..7b86601 100644 --- a/JSDOMParser.js +++ b/JSDOMParser.js @@ -553,7 +553,8 @@ }, }; - var Document = function () { + var Document = function (url) { + this.documentURI = url; this.styleSheets = []; this.childNodes = []; this.children = []; @@ -593,6 +594,20 @@ node.textContent = text; return node; }, + + get baseURI() { + if (!this.hasOwnProperty("_baseURI")) { + this._baseURI = this.documentURI; + var baseElements = this.getElementsByTagName("base"); + var href = baseElements[0] && baseElements[0].getAttribute("href"); + if (href) { + try { + this._baseURI = (new URL(href, this._baseURI)).href; + } catch (ex) {/* Just fall back to documentURI */} + } + } + return this._baseURI; + }, }; var Element = function (tag) { @@ -1111,9 +1126,9 @@ /** * Parses an HTML string and returns a JS implementation of the Document. */ - parse: function (html) { + parse: function (html, url) { this.html = html; - var doc = this.doc = new Document(); + var doc = this.doc = new Document(url); this.readChildren(doc); // If this is an HTML document, remove root-level children except for the diff --git a/index.js b/index.js index 214c4f2..8af525e 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ var path = require("path"); var fs = require("fs"); +var url = require("url"); // We want to load Readability and JSDOMParser, which aren't set up as commonjs libraries, // and so we need to do some hocus-pocus with 'vm' to import them on a separate scope @@ -14,6 +15,7 @@ var scopeContext = {}; // in the scope we're using: scopeContext.dump = console.log; scopeContext.console = console; +scopeContext.URL = url.URL; // Actually load files. NB: if either of the files has parse errors, // node is dumb and shows you a syntax error *at this callsite* . Don't try to find diff --git a/package.json b/package.json index f03bdad..5b8b5fb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "readability", - "version": "0.0.1", + "version": "0.1.0", "description": "A standalone version of the readability library used for Firefox Reader View.", "main": "Readability.js", "scripts": { @@ -20,7 +20,7 @@ "url": "https://github.com/mozilla/readability/issues" }, "engines": { - "node" : ">=6.5" + "node" : ">=7.0" }, "homepage": "https://github.com/mozilla/readability", "devDependencies": { diff --git a/test/test-jsdomparser.js b/test/test-jsdomparser.js index 7facd49..38ee52f 100644 --- a/test/test-jsdomparser.js +++ b/test/test-jsdomparser.js @@ -9,7 +9,7 @@ var BASETESTCASE = '
Some text and a
'