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

Commit

Permalink
fix(highlight): don't remove whitespace-only nodes (#827)
Browse files Browse the repository at this point in the history
* fix(highlight): don't remove whitespace-only nodes

Vue by default removes nodes which are just whitespace: https://github.com/vuejs/vue/blob/ec78fc8b6d03e59da669be1adf4b4b5abf670a34/dist/vue.common.dev.js#L2527

We need to prevent this, since sometimes between highlights, there is just a space, we need to make the value not "wrapped". We can't make the value always "block" (i don't know the correct name for this), since that would add spaces between "inline" highlights.

fixes #826

* Update src/components/Highlighter.vue

* less needless spaces

* use only two spaces, move to parseAlgoliaHit
  • Loading branch information
Haroenv authored Jul 31, 2020
1 parent d45b8e9 commit 1358b6a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
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),
// 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

0 comments on commit 1358b6a

Please sign in to comment.