@@ -8,6 +8,25 @@ window.POG=(function() {
8
8
// ========================================================================
9
9
// private functions
10
10
11
+ function getClosestSibling ( node , siblings ) {
12
+ var copies = siblings . slice ( 0 ) ;
13
+ copies . push ( node ) ;
14
+ var closest = copies . length - 1 ;
15
+ var nodeIndex = [ ] . indexOf . call ( copies , node ) ;
16
+ var siblingIndex = closest ;
17
+
18
+ for ( var i = 0 , j = copies . length ; i < j ; i ++ ) {
19
+ var delta = Math . abs ( nodeIndex - i ) ;
20
+
21
+ if ( delta < closest ) {
22
+ closest = delta ;
23
+ siblingIndex = i ;
24
+ }
25
+ }
26
+
27
+ return ( siblingIndex === ( copies . length - 1 ) ) ? null : copies [ siblingIndex ] ;
28
+ }
29
+
11
30
function getComments ( root ) {
12
31
var comments = [ ] ;
13
32
var index = - 1 ;
@@ -45,7 +64,7 @@ window.POG=(function() {
45
64
46
65
if ( nodeName === 'INPUT' ) {
47
66
if ( node . getAttribute ( 'type' ) ) {
48
- currentSelector += '[type=\'' + node . getAttribute ( ' type' ) + '\']' ;
67
+ currentSelector += '[type=\'' + node . type + '\']' ;
49
68
}
50
69
else if ( node . getAttribute ( 'data-type' ) ) {
51
70
currentSelector += '[data-type=\'' + node . getAttribute ( 'data-type' ) + '\']' ;
@@ -121,23 +140,42 @@ window.POG=(function() {
121
140
var text = '' ;
122
141
123
142
if ( node . id ) {
124
- label = document . querySelector ( 'label[for="' + node . id + '"]' ) ;
143
+ text = getLabelTextFor ( node . id ) ;
144
+ }
125
145
126
- if ( label ) {
127
- text = label . textContent || label . innerText || '' ;
128
- text = text . trim ( ) ;
129
- }
146
+ if ( text === '' && node . name ) {
147
+ // non-standard, but it happens
148
+ text = getLabelTextFor ( node . name ) ;
130
149
}
131
150
132
151
if ( text === '' ) {
133
152
// find label from siblings
134
- label = Array . filter ( [ ] . slice . call ( node . parentNode . children ) ,
153
+ // TODO: should use more aggressive collector
154
+ labels = Array . filter ( [ ] . slice . call ( node . parentNode . children ) ,
135
155
function ( item , index ) {
136
156
return item . nodeName === 'LABEL' ;
137
157
} ) ;
138
158
139
- if ( label . length ) {
140
- text = label [ 0 ] . textContent || label [ 0 ] . innerText || '' ;
159
+ var label = getClosestSibling ( node , labels ) ;
160
+
161
+ if ( label ) {
162
+ text = label . textContent || label . innerText || '' ;
163
+ text = text . trim ( ) ;
164
+ }
165
+ }
166
+
167
+ return text ;
168
+ }
169
+
170
+ function getLabelTextFor ( identifier ) {
171
+ var label = null ;
172
+ var text = '' ;
173
+
174
+ if ( identifier ) {
175
+ label = document . querySelector ( 'label[for="' + identifier + '"]' ) ;
176
+
177
+ if ( label ) {
178
+ text = label . textContent || label . innerText || '' ;
141
179
text = text . trim ( ) ;
142
180
}
143
181
}
0 commit comments