-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
71 lines (67 loc) · 2.42 KB
/
plugin.js
File metadata and controls
71 lines (67 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
a = {}
a.debug = (function () {
'use strict'
return {
init: function () {
if ((window.location.hostname === 'ifs.local-dev' && window.location.port !== '4000') && window.location.pathname.indexOf('/prototypes') === -1) {
jQuery(document).on('dblclick', '.govuk-footer', function () {
a.debug.toggleDebug()
})
jQuery(document).on('keyup', '[data-selector-highlighter] code', function () {
var query = jQuery(this).text()
a.debug.resetStyles()
a.debug.highlightSelected(query)
})
if (typeof (Cookies.get('debugSelectors')) !== 'undefined') {
a.debug.enableDebug()
}
// red a11y borders
jQuery('head').append('<style>a[href="#"],a[href=""],a[href*="prototypes"]{ outline:5px solid red !important; }</style>')
}
},
toggleDebug: function () {
if (jQuery('[data-selector-highlighter]').length) {
a.debug.disableDebug()
} else {
a.debug.enableDebug()
}
},
enableDebug: function () {
Cookies.set('debugSelectors', 'true', { expires: 0.05 }) // defined in days, 0.05 = little bit more than one hour
jQuery('body').append('<div data-selector-highlighter=""><code contenteditable></code><div></div">')
jQuery('[data-selector-highlighter] code').focus()
},
disableDebug: function () {
Cookies.remove('debugSelectors')
jQuery('[data-selector-highlighter]').remove()
a.debug.resetStyles()
},
highlightSelected: function (query) {
var result = jQuery('[data-selector-highlighter] div')
query = jQuery.trim(query)
if (query.length) {
try {
var el = jQuery(query)
if (el.length) {
jQuery(el).css('outline', '5px solid red')
var resultText = el.length > 1 ? el.length + ' matches' : '1 match'
result.text(resultText)
} else {
// valid selector no match
result.text('no match')
}
} catch (err) {
// invalid selector
result.text('no match')
}
} else {
// empty string
result.text('no match')
jQuery('code').text(' ')
}
},
resetStyles: function () {
jQuery('[style*="outline"]').removeAttr('style')
}
}
})()