1 { createElement } = require 'utils'
2 { SerializableBloomFilter
3 , DummyBloomFilter } = require 'mode-hints/bloomfilter'
5 { getPref } = require 'prefs'
7 HTMLDocument = Ci.nsIDOMHTMLDocument
8 HTMLAnchorElement = Ci.nsIDOMHTMLAnchorElement
10 Z_INDEX_START = 99999999 # The highest `z-index` used in style.css
11 _zIndex = Z_INDEX_START
12 # Each marker will get a `z-index` of `_zIndex++`. In theory, `z-index` can be infinitely large. In
13 # practice, Firefox uses a 32-bit signed integer to store it, so the maximum value is 2147483647
14 # (http://www.puidokas.com/max-z-index/). However, we do not need to worry about hitting the limit,
15 # since the user would have to browse through a bit more than 2 billion links in a single Firefox
16 # session before that happens.
18 realBloomFilter = new SerializableBloomFilter('hints_bloom_data', 256 * 32, 16)
19 dummyBloomFilter = new DummyBloomFilter()
21 # Wraps the markable element and provides methods to manipulate the markers
23 # Creates the marker DOM node
24 constructor: (@element) ->
25 document = @element.ownerDocument
26 window = document.defaultView
27 @markerElement = createElement(document, 'div', class: 'VimFxHintMarker')
29 Object.defineProperty this, 'bloomFilter',
30 get: -> if getPref('hints_bloom_on') then realBloomFilter else dummyBloomFilter
32 show: -> @setVisibility(true)
33 hide: -> @setVisibility(false)
34 setVisibility: (visible) ->
35 method = if visible then 'remove' else 'add'
36 @markerElement.classList[method]('VimFxHiddenHintMarker')
38 setPosition: (top, left) ->
39 # The positioning is absulute
40 @markerElement.style.top = "#{ top }px"
41 @markerElement.style.left = "#{ left }px"
44 @position = {top, left}
46 # Each marker gets a unique `z-index`, so that it can be determined if a marker overlaps another.
47 @markerElement.style.setProperty('z-index', _zIndex++, 'important')
49 # To be called when the marker has been both assigned a hint and inserted into the DOM, and thus
50 # gotten a height and width.
53 position: { top, left }
54 markerElement: { offsetHeight: height, offsetWidth: width }
56 @position = {top, bottom: top + height, left, right: left + width, height, width}
58 setHint: (@hintChars) ->
59 # Hint chars that have been matched so far
60 @enteredHintChars = ''
62 document = @element.ownerDocument
64 while @markerElement.hasChildNodes()
65 @markerElement.firstChild.remove()
67 fragment = document.createDocumentFragment()
68 for char in @hintChars
69 charContainer = createElement(document, 'span')
70 charContainer.textContent = char.toUpperCase()
71 fragment.appendChild(charContainer)
73 @markerElement.appendChild(fragment)
75 matchHintChar: (char) ->
76 @updateEnteredHintChars(char)
79 @updateEnteredHintChars(false)
81 updateEnteredHintChars: (char) ->
84 @enteredHintChars = @enteredHintChars[...-1]
88 @enteredHintChars += char.toLowerCase()
91 @markerElement.children[@enteredHintChars.length + offset]?.classList[method]('VimFxCharMatch')
92 if @hintChars.startsWith(@enteredHintChars) then @show() else @hide()
95 return @hintChars == @enteredHintChars
101 # Returns string features of the element that can be used in the bloom filter
102 # in order to add relevance to the hint marker
103 extractBloomFeatures: ->
106 # Class name of an element (walks up the node tree to find first element with at least one class)
109 while el.classList?.length == 0 and el not instanceof HTMLDocument
110 suffix += " #{ el.tagName }"
113 for className in el.classList
114 features["#{ el.tagName }.#{ className }#{ suffix }"] = 10
117 features["#{ el.tagName }.#{ @element.id }"] = 5
119 if @element instanceof HTMLAnchorElement
120 features["a"] = 20 # Reward links no matter what
121 features["#{ el.tagName }.#{ @element.href }"] = 60
122 features["#{ el.tagName }.#{ @element.title }"] = 40
126 # Returns rating of all present bloom features (plus 1)
129 for feature, weight of @extractBloomFeatures()
130 rating += if @bloomFilter.test(feature) then weight else 0
135 for feature, weight of @extractBloomFeatures()
136 @bloomFilter.add(feature)
139 exports.Marker = Marker