Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

fix(highlight): don't remove whitespace-only nodes #827

Merged
merged 5 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/components/Highlighter.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<template>
<span
:class="suit()"
>
<span :class="suit()">
<component
v-for="({ value, isHighlighted }, index) in parsedHighlights"
:class="[isHighlighted && suit('highlighted')]"
Expand Down
22 changes: 22 additions & 0 deletions src/components/__tests__/Highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,25 @@ test('allows usage of dot delimited path to access nested attribute', () => {

expect(wrapper.html()).toMatchSnapshot();
});

test('retains whitespace nodes', () => {
const hit = {
_highlightResult: {
attr: {
value: `con<mark>tent</mark> <mark>search</mark>ing`,
},
},
};

const wrapper = mount(Highlight, {
propsData: {
attribute: 'attr',
highlightedTagName: 'marquee',
hit,
},
});

expect(wrapper.html()).toBe(
`<span class="ais-Highlight">con<marquee class="ais-Highlight-highlighted">tent</marquee> <marquee class="ais-Highlight-highlighted">search</marquee>ing</span>`
);
});
5 changes: 4 additions & 1 deletion src/util/parseAlgoliaHit.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ function parseHighlightedAttribute({ preTag, postTag, highlightedValue = '' }) {

if (splitByPostTag[1] !== '') {
elements.push({
value: splitByPostTag[1],
// Vue removes nodes which are just a single space (vuejs/vue#9208),
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
// we replace this by two spaces, which does not have an impact,
// unless someone would have `white-space: pre` on the highlights
value: splitByPostTag[1] === ' ' ? ' ' : splitByPostTag[1],
isHighlighted: false,
});
}
Expand Down