@@ -3,6 +3,17 @@ describe('axe.utils.getXpath', () => {
3
3
4
4
const fixture = document . getElementById ( 'fixture' ) ;
5
5
6
+ // @see https://stackoverflow.com/a/14284815/2124254
7
+ function getElementByXPath ( path ) {
8
+ return document . evaluate (
9
+ path ,
10
+ document ,
11
+ ( ) => 'http://www.w3.org/1998/Math/MathML' ,
12
+ XPathResult . FIRST_ORDERED_NODE_TYPE ,
13
+ null
14
+ ) . singleNodeValue ;
15
+ }
16
+
6
17
it ( 'should be a function' , ( ) => {
7
18
assert . isFunction ( axe . utils . getXpath ) ;
8
19
} ) ;
@@ -14,16 +25,19 @@ describe('axe.utils.getXpath', () => {
14
25
const sel = axe . utils . getXpath ( node ) ;
15
26
16
27
assert . equal ( sel , "//div[@id='fixture']/div" ) ;
28
+ assert . equal ( node , getElementByXPath ( sel ) ) ;
17
29
} ) ;
18
30
19
31
it ( 'should handle special characters' , ( ) => {
20
32
const node = document . createElement ( 'div' ) ;
21
33
node . id = 'monkeys#are.animals\\ok' ;
22
34
fixture . appendChild ( node ) ;
23
- assert . equal (
24
- axe . utils . getXpath ( node ) ,
25
- "//div[@id='monkeys#are.animals\\ok']"
26
- ) ;
35
+
36
+ const sel = axe . utils . getXpath ( node ) ;
37
+
38
+ assert . equal ( sel , "//div[@id='monkeys#are.animals\\ok']" ) ;
39
+
40
+ assert . equal ( node , getElementByXPath ( sel ) ) ;
27
41
} ) ;
28
42
29
43
it ( 'should stop on unique ID' , ( ) => {
@@ -33,6 +47,7 @@ describe('axe.utils.getXpath', () => {
33
47
34
48
const sel = axe . utils . getXpath ( node ) ;
35
49
assert . equal ( sel , "//div[@id='monkeys']" ) ;
50
+ assert . equal ( node , getElementByXPath ( sel ) ) ;
36
51
} ) ;
37
52
38
53
it ( 'should use the nearest unique ID' , ( ) => {
@@ -51,6 +66,7 @@ describe('axe.utils.getXpath', () => {
51
66
52
67
const sel = axe . utils . getXpath ( node ) ;
53
68
assert . equal ( sel , "//div[@id='monkeys']/div" ) ;
69
+ assert . equal ( node , getElementByXPath ( sel ) ) ;
54
70
} ) ;
55
71
56
72
it ( 'should not use ids if they are not unique' , ( ) => {
@@ -65,6 +81,7 @@ describe('axe.utils.getXpath', () => {
65
81
const sel = axe . utils . getXpath ( node ) ;
66
82
67
83
assert . equal ( sel , "//div[@id='fixture']/div[2]" ) ;
84
+ assert . equal ( node , getElementByXPath ( sel ) ) ;
68
85
} ) ;
69
86
70
87
it ( 'should properly calculate number when siblings are of different type' , ( ) => {
@@ -91,23 +108,27 @@ describe('axe.utils.getXpath', () => {
91
108
const sel = axe . utils . getXpath ( target ) ;
92
109
93
110
assert . equal ( sel , "//div[@id='fixture']/div[2]" ) ;
111
+ assert . equal ( target , getElementByXPath ( sel ) ) ;
94
112
} ) ;
95
113
96
114
it ( 'should work on the documentElement' , ( ) => {
97
115
const sel = axe . utils . getXpath ( document . documentElement ) ;
98
116
assert . equal ( sel , '/html' ) ;
117
+ assert . equal ( document . documentElement , getElementByXPath ( sel ) ) ;
99
118
} ) ;
100
119
101
120
it ( 'should work on the body' , ( ) => {
102
121
const sel = axe . utils . getXpath ( document . body ) ;
103
122
assert . equal ( sel , '/html/body' ) ;
123
+ assert . equal ( document . body , getElementByXPath ( sel ) ) ;
104
124
} ) ;
105
125
106
- it ( 'should work on namespaced elements' , ( ) => {
126
+ it ( 'should work on namespaced elements' , function ( ) {
107
127
fixture . innerHTML = '<hx:include>Hello</hx:include>' ;
108
- const node = fixture . firstChild ;
109
- const sel = axe . utils . getXpath ( node ) ;
128
+ var node = fixture . firstChild ;
129
+ var sel = axe . utils . getXpath ( node ) ;
110
130
111
131
assert . equal ( sel , "//div[@id='fixture']/hx:include" ) ;
132
+ // couldn't figure out how to use document.evaluate to select an element with namespace
112
133
} ) ;
113
134
} ) ;
0 commit comments