[almost correct parsing of html links dan@toel.it**20150216141219 Ignore-this: 8a9efe7831bf6c11441a37282444eeb7 ] hunk ./lib/main.js 9 +var URL = require('sdk/url').URL; + + hunk ./lib/main.js 21 -function link2org(nodes) { +function link2org(nodes, url) { hunk ./lib/main.js 23 + + var parser = Cc["@mozilla.org/xmlextras/domparser;1"]. + createInstance(Ci.nsIDOMParser); + + var doc = parser.parseFromString('
', "text/html"); + hunk ./lib/main.js 31 + + var href = node.href; + var text = node.text; + + // relative to absolute + // TODO: is there a general way to solve this? + // TODO: do we need to add a slash? + // TODO: do we need to take care about fragments? YES! + if (!href.match(/^(https?|mailto):/)) { + href = url + href; + } + + // remove new lines in links text + text = text.replace(/(\r\n|\n|\r)/gm,""); + hunk ./lib/main.js 47 - // TODO: do we have document here??? I do not think so because of the error in filterHTML - textnode = document.createTextNode("[[" + node.href.replace("[", "%5B").replace("]", "%5D") + "][" + node.text.replace("[", "{").replace("]", "}") + "]]"); + href = href.replace("[", "%5B").replace("]", "%5D"); + text = text.replace("[", "{").replace("]", "}"); + + if (text) { + textnode = doc.createTextNode("[[" + href + "][" + text + "]]"); + } else { + textnode = doc.createTextNode("[[" + href + "]]"); + } hunk ./lib/main.js 60 - // we do not have the dom, only HTML as string so we recreate the DOM - // TODO: this cause an error - // Message: ReferenceError: document is not defined - var doc = document.implementation.createHTMLDocument("temp"); - doc.documentElement.innerHTML = html; + var parser = Cc["@mozilla.org/xmlextras/domparser;1"]. + createInstance(Ci.nsIDOMParser); + var doc = parser.parseFromString(html, "text/html"); + + // get the base url + // + // TODO: this can break if we have a selection from a previous + // window and click the capture in a more recent one + var url = URL(tabs.activeTab.url); hunk ./lib/main.js 72 - link2org(nodes); + link2org(nodes, url);