Thanks to @Patte that help to found the answer here i post the solution for this case.
public markContent(): void {
const selection = window.getSelection();
const selectionRange = window.getSelection().getRangeAt(0);
const textSeleted = selection.toString();
const range = selectionRange.cloneRange();
if (this.markerEvent !== 'mark') {
const marker = document.createElement('mark');
marker.setAttribute('class', this.colorMarker);
// marker.textContent = textSeleted;
range.surroundContents(marker);
return;
}
}
for the position i use this other function:
public displayBobble(event): void {
const selectionRange = window.getSelection().getRangeAt(0);
this.libraryServices.changeMarkerOptions(event.target.localName);
if (selectionRange.startOffset !== selectionRange.endOffset) {
if (event.target.localName === 'p' || event.target.localName === 'mark' || event.target.localName === 'strong') {
const range = window.getSelection().getRangeAt(0);
const cloneRange = range.cloneRange();
range.collapse(true);
const indexSelection = document.createElement('span');
indexSelection.setAttribute('id', 'selected_text');
indexSelection.innerText = '\ufeff';
range.insertNode(indexSelection);
const el = document.getElementById('selected_text');
const elPosition = el.getBoundingClientRect();
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(cloneRange);
this.libraryServices.changeBobblePosition(
{
top: (elPosition.top - 50),
left: (elPosition.left),
display: 'flex'
}
);
el.remove();
}
}
}
what i´m doing here is inject a span
tag with and id
at the start of the selection and then get the position of that element, after i get the position of the span i remove it.