1 | _ = require('lodash')
|
2 | cssToXPath = require('css-to-xpath')
|
3 |
|
4 | module.exports = exports = function() {
|
5 | var selector, text
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | if (arguments.length === 1) {
|
13 |
|
14 | if ( /^\(?\/\//.test(arguments[0]) ) {
|
15 | return arguments[0]
|
16 |
|
17 |
|
18 |
|
19 | } else if ( /^[\.#\[\*]/.test(arguments[0]) ) {
|
20 | return cssToXPath(arguments[0])
|
21 | } else {
|
22 | text = xPathStringLiteral(arguments[0])
|
23 | return "(//*[not(self::script)][contains(., " + text + ")])[last()]"
|
24 | }
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | else if (arguments.length === 2) {
|
32 | selector = arguments[0]
|
33 | text = xPathStringLiteral(arguments[1])
|
34 | return cssToXPath(selector).slice(1) + "[contains(., " + text + ")]"
|
35 | }
|
36 |
|
37 | else {
|
38 | throw new Error("Invalid amount of arguments provided - #{ arguments.length }")
|
39 | }
|
40 | }
|
41 |
|
42 |
|
43 | function xPathStringLiteral(s) {
|
44 | if (s.indexOf('"') === -1)
|
45 | return '"'+s+'"'
|
46 | if (s.indexOf("'") === -1)
|
47 | return "'"+s+"'"
|
48 | return 'concat("'+s.replace(/"/g, '",\'"\',"')+'")'
|
49 | }
|
50 |
|
51 | exports.stringLiteral = xPathStringLiteral
|
52 | exports.fromCss = function() {
|
53 | return cssToXPath.apply(this, arguments).slice(1)
|
54 | }
|