]> git.gir.st - VimFx.git/blob - extension/lib/parse-prefs.coffee
Do not inject Console.jsm into frame scripts
[VimFx.git] / extension / lib / parse-prefs.coffee
1 # This file parses string prefs into more easily used data structures.
2
3 defaults = require('./defaults')
4 prefs = require('./prefs')
5 utils = require('./utils')
6
7 MIN_NUM_HINT_CHARS = 2
8
9 parsePref = (pref) ->
10 # Parsed options are not stored in Firefox’s prefs system, and are therefore
11 # always read from the defaults. The only way to override them is via the
12 # config file API.
13 if pref of defaults.parsed_options
14 return defaults.parsed_options[pref]
15
16 value = prefs.get(pref)
17
18 if pref of parsers
19 {parsed, normalized} = parsers[pref](value, defaults.all_options[pref])
20 if normalized? and normalized != value and prefs.has(pref)
21 prefs.set(pref, normalized)
22 return parsed
23
24 return value
25
26 # Splits a whitespace delimited string into an array, with empty elements and
27 # duplicates filtered.
28 parseSpaceDelimitedString = (value) ->
29 parsed = utils.removeDuplicates(value.split(/\s+/)).filter(Boolean)
30 return {parsed, normalized: parsed.join(' ')}
31
32 parseHintChars = (value, defaultValue) ->
33 [leading..., end] = value.trim().split(/\s+/)
34 parsed = if leading.length > 0 then "#{leading.join('')} #{end}" else end
35 parsed = utils.removeDuplicateChars(parsed)
36
37 # Make sure that hint chars contain at least the required amount of chars.
38 diff = MIN_NUM_HINT_CHARS - parsed.length
39 if diff > 0
40 parsed = parsed + defaultValue[...diff]
41
42 unless parsed.includes(' ')
43 numDefaultSecondaryHintChars =
44 defaultValue.length - 1 - defaultValue.indexOf(' ')
45 index = Math.min(parsed.length // 2, numDefaultSecondaryHintChars)
46 parsed = "#{parsed[...-index]} #{parsed[-index..]}"
47
48 return {parsed, normalized: parsed}
49
50 parsePatterns = (value) ->
51 result = parseSpaceDelimitedString(value)
52 # The patterns are case insensitive regexes and must match either in the
53 # beginning or at the end of a string. They do not match in the middle of
54 # words, so "previous" does not match "previously" (`previous\S*` can be used
55 # for that case). Note: `\s` is used instead of `\b` since it works better
56 # with non-English characters.
57 result.parsed = result.parsed.map((pattern) ->
58 patternRegex =
59 try
60 RegExp(pattern).source
61 catch
62 utils.regexEscape(pattern)
63 return ///
64 ^\s* (?:#{patternRegex}) (?:\s|$)
65 |
66 (?:\s|^) (?:#{patternRegex}) \s*$
67 ///i
68 )
69 return result
70
71 parseBlacklist = (value) ->
72 result = parseSpaceDelimitedString(value)
73 result.parsed = result.parsed.map((pattern) ->
74 return ///^#{utils.regexEscape(pattern).replace(/\\\*/g, '.*')}$///i
75 )
76 return result
77
78 parsers = {
79 'hints.chars': parseHintChars
80
81 prev_patterns: parsePatterns
82 next_patterns: parsePatterns
83
84 blacklist: parseBlacklist
85
86 prevent_autofocus_modes: parseSpaceDelimitedString
87
88 adjustable_element_keys: parseSpaceDelimitedString
89 activatable_element_keys: parseSpaceDelimitedString
90
91 pattern_attrs: parseSpaceDelimitedString
92 }
93
94 module.exports = {
95 parsePref
96 parseSpaceDelimitedString
97 }
Imprint / Impressum