/ Published in: JavaScript
only handles child selectors, classnames and IDs
I didn't write this, nor have I tested it. Saving it just in case I need it.
Expand |
Embed | Plain Text
// only handles child selectors, classnames and IDs function css2xpath(css) { var fragments = css.split(/\s+/), xpath = ['.'], child = false xpath.add = function(part) { xpath.push(child ? '/' : '//') child = false xpath.push(part || '*') } fragments.forEach(function(fragment) { if (!fragment) return; if (fragment == '>') child = true; else if (/^([^.]*)\.([\w.-]+)$/.test(fragment)) { xpath.add(RegExp.$1) RegExp.$2.split('.').forEach(function(className) { xpath.push(xpathClass(className)) }) } else if (/^([^.]*)#([\w-]+)$/.test(fragment)) { xpath.add(RegExp.$1) xpath.push('[@id="' + RegExp.$2 + '"]') } else xpath.add(fragment) }) return xpath.join('') }
You need to login to post a comment.
