Skip to content

Commit 372cb39

Browse files
committed
fix(highlights): normalize text nodes before applying highlights
1 parent df28f9b commit 372cb39

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

spec/highlight-text.spec.js

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -121,76 +121,66 @@ describe('highlightText', function () {
121121
highlight(elem, ['b'])
122122
const portions = this.wrapMatch.firstCall.args[0]
123123

124-
expect(portions.length).to.equal(1)
124+
expect(portions.length).to.equal(1, 'portions.length')
125125
expect(portions[0].text).to.equal('b')
126-
expect(portions[0].offset).to.equal(0)
127-
expect(portions[0].length).to.equal(1)
128-
expect(portions[0].isLastPortion).to.equal(true)
126+
expect(portions[0].offset).to.equal(2, 'offset')
127+
expect(portions[0].length).to.equal(1, 'length')
128+
expect(portions[0].isLastPortion).to.equal(true, 'isLastPortion')
129129
})
130130

131131
it('finds a word that is in a text node with a character before', function () {
132132
const elem = createParagraphWithTextNodes('a', ' b', ' c')
133133
highlight(elem, ['b'])
134134
const portions = this.wrapMatch.firstCall.args[0]
135135

136-
expect(portions.length).to.equal(1)
136+
expect(portions.length).to.equal(1, 'portions.length')
137137
expect(portions[0].text).to.equal('b')
138-
expect(portions[0].offset).to.equal(1)
139-
expect(portions[0].length).to.equal(1)
140-
expect(portions[0].isLastPortion).to.equal(true)
138+
expect(portions[0].offset).to.equal(2, 'offset')
139+
expect(portions[0].length).to.equal(1, 'length')
140+
expect(portions[0].isLastPortion).to.equal(true, 'isLastPortion')
141141
})
142142

143143
it('finds a word that is in a text node with a charcter after', function () {
144144
const elem = createParagraphWithTextNodes('a ', 'b x', 'c')
145145
highlight(elem, ['b'])
146146
const portions = this.wrapMatch.firstCall.args[0]
147147

148-
expect(portions.length).to.equal(1)
148+
expect(portions.length).to.equal(1, 'portions.length')
149149
expect(portions[0].text).to.equal('b')
150-
expect(portions[0].offset).to.equal(0)
151-
expect(portions[0].length).to.equal(1)
152-
expect(portions[0].isLastPortion).to.equal(true)
150+
expect(portions[0].offset).to.equal(2, 'offset')
151+
expect(portions[0].length).to.equal(1, 'length')
152+
expect(portions[0].isLastPortion).to.equal(true, 'isLastPortion')
153153
})
154154

155155
it('finds a word that span over two text nodes', function () {
156156
const elem = createParagraphWithTextNodes('a ', 'b', 'c')
157157
highlight(elem, ['bc'])
158158
const portions = this.wrapMatch.firstCall.args[0]
159159

160-
expect(portions.length).to.equal(2)
161-
expect(portions[0].text).to.equal('b')
162-
expect(portions[0].isLastPortion).to.equal(false)
163-
164-
expect(portions[1].text).to.equal('c')
165-
expect(portions[1].isLastPortion).to.equal(true)
160+
expect(portions.length).to.equal(1, portions.length)
161+
expect(portions[0].text).to.equal('bc')
162+
expect(portions[0].isLastPortion).to.equal(true, 'isLastPortion')
166163
})
167164

168165
it('finds a word that spans over three text nodes', function () {
169166
const elem = createParagraphWithTextNodes('a', 'b', 'c')
170167
highlight(elem, ['abc'])
171168
const portions = this.wrapMatch.firstCall.args[0]
172169

173-
expect(portions.length).to.equal(3)
174-
expect(portions[0].text).to.equal('a')
175-
expect(portions[1].text).to.equal('b')
176-
expect(portions[2].text).to.equal('c')
170+
expect(portions.length).to.equal(1, 'portions.length')
171+
expect(portions[0].text).to.equal('abc')
177172
})
178173

179174
it('finds a word that is partially contained in two text nodes', function () {
180175
const elem = createParagraphWithTextNodes('a', ' xx', 'xx ')
181176
highlight(elem, ['xxxx'])
182177
const portions = this.wrapMatch.firstCall.args[0]
183178

184-
expect(portions.length).to.equal(2)
185-
expect(portions[0].text).to.equal('xx')
186-
expect(portions[0].offset).to.equal(1)
187-
expect(portions[0].length).to.equal(2)
188-
expect(portions[0].isLastPortion).to.equal(false)
189-
190-
expect(portions[1].text).to.equal('xx')
191-
expect(portions[1].offset).to.equal(0)
192-
expect(portions[1].length).to.equal(2)
193-
expect(portions[1].isLastPortion).to.equal(true)
179+
expect(portions.length).to.equal(1, 'portions.length')
180+
expect(portions[0].text).to.equal('xxxx')
181+
expect(portions[0].offset).to.equal(2, 'offset')
182+
expect(portions[0].length).to.equal(4, 'length')
183+
expect(portions[0].isLastPortion).to.equal(true)
194184
})
195185
})
196186

src/highlight-text.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export default {
3333
return
3434
}
3535

36+
element.normalize() // mend text nodes
37+
3638
const iterator = new NodeIterator(element)
3739
let currentMatchIndex = 0
3840
let totalOffset = 0

0 commit comments

Comments
 (0)