Summary:
I'm struggling to apply scoped CSS styling to dynamically generated HTML in my Vue component. The generated HTML lacks the necessary data attribute for scoping, making it difficult to style with scoped CSS rules.
Details:
In a function called highlight, I create an HTML string that highlights occurrences of a search term by wrapping them in <span class="match">
tags. However, due to the manual nature of constructing this string, the <span>
elements do not have the required data attribute needed for scoped CSS in Vue components. As a result, I am unable to style these matches using scoped CSS and resort to using unscoped CSS, which is not ideal. Is there a more Vue-specific approach to achieve this styling within the scoped environment?
Here is the code snippet for the highlight function:
// Source: http://stackoverflow.com/a/280805/2874789
function highlight(data, search) {
return data.replace(
new RegExp("(" + preg_quote(search) + ")", 'gi'),
"<span class=match>$1</span>"
);
}
(preg_quote is simply a function used for escaping characters)
Appreciate any assistance!