diff options
| author | Niklas Baumstark | 2012-01-25 00:23:47 +0100 |
|---|---|---|
| committer | Niklas Baumstark | 2012-04-10 23:57:20 +0200 |
| commit | f41ebfbe6bacbbeb4b1b532bd8d3f9623f91ee63 (patch) | |
| tree | 62c6c98ed4e6a136c7c8556b0a27f74a4a54634f /lib | |
| parent | 951a8535ced42904e97c88b97e98d303bf3672c2 (diff) | |
| download | vimium-f41ebfbe6bacbbeb4b1b532bd8d3f9623f91ee63.tar.bz2 | |
add HTML helpers
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/utils.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/utils.js b/lib/utils.js index a0ca9715..9b5cfcb9 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -20,6 +20,39 @@ var utils = { return func.apply(obj, argArray); }, + /* + * Takes an array of XPath selectors, adds the necessary namespaces (currently only XHTML), and applies them + * to the document root. The namespaceResolver in evaluateXPath should be kept in sync with the namespaces + * here. + */ + makeXPath: function(elementArray) { + var xpath = []; + for (var i in elementArray) + xpath.push("//" + elementArray[i], "//xhtml:" + elementArray[i]); + return xpath.join(" | "); + }, + + evaluateXPath: function(xpath, resultType) { + function namespaceResolver(namespace) { + return namespace == "xhtml" ? "http://www.w3.org/1999/xhtml" : null; + } + return document.evaluate(xpath, document.documentElement, namespaceResolver, resultType, null); + }, + + /** Creates a single DOM element from :html */ + createElementFromHtml: function(html) { + var tmp = document.createElement("div"); + tmp.innerHTML = html; + return tmp.firstChild; + }, + + /** Escapes HTML */ + escapeHtml: function(html) { + var tmp = document.createElement("div"); + tmp.textContent = html; + return tmp.innerHTML; + }, + /** * Generates a unique ID */ |
