首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >突出显示文本会在文本的开头和结尾插入重复项

突出显示文本会在文本的开头和结尾插入重复项
EN

Stack Overflow用户
提问于 2021-05-17 06:34:49
回答 1查看 36关注 0票数 0

我只是需要你的帮助和洞察力关于我继承的一个项目。有一个功能,用户可以突出显示一个文本,并从该文本中打开一个注释框。这样做效果很好。但我遇到的问题是,当您从右到左突出显示文本时,它会在突出显示的文本的开头和结尾插入实际的文本。

举个例子,这就是发生的事情。

“这是文本突出显示示例”

当我从右向左突出显示sample text highlight时,它将插入在文本开头和末尾突出显示的任何内容,它将如下所示

“这是示例文本高亮显示sample text highlight示例文本突出显示”

我对开发和编码非常陌生,我正在尝试反向工程,并试图理解语法的含义,但到目前为止,我被卡住了。

当从左到右突出显示时,这非常有效,并且没有发现任何问题。

这是函数的注释

代码语言:javascript
复制
  Basically, the member comments need to be highlighted from `startCharacter`
    to `endCharacter`. To achieve this we will insert some <span> tags around
    these regions and then render the HTML by marking it as "Safe" with `htmlSafe()`
  HOWEVER. This creates a problem: If the user includes HTML in their plan text,
    it will be rendered/executed.
  NOT WHAT YOU WANT (for Security reasons).
  SO. We need to first escape the user's plan text using `htmlEscape()`. 
  HOWEVER. This creates a SECONDARY problem, because now the indices stored
    in `startCharacter` and `endCharacter` are now incorrect, as we have changed
    the length of the string by changing `<` to `&lt;` etc. 
  SO HERE'S THE FINAL SOLUTION. 
      - FIRST we insert some very-unique strings at the `startCharacter` and
        `endCharacter` positions that are completely html-safe.
      - THEN we `htmlEscape()` the text. ```
      - THEN we find-replace the very-unique strings we inserted with the actual
        HTML we want.
      - THEN finally we mark the string as `htmlSafe()` so that our markup is rendered.

Update:
Here's some snippet of where I think the logic is:

```                function calculatePlanMarkup(planText, memberComments, currentCommentBeingEditedIndex) {
代码语言:javascript
复制
              // "Very-unique" HTML-safe string for the start of the highlight
代码语言:javascript
复制
              const START_MARKER_TEXT = (id) => `[@@__INSERT_SPAN_START(${id})]`;
代码语言:javascript
复制
              // A regex that will match `START_MARKER_TEXT` and parse its `id` value
代码语言:javascript
复制
              const START_MARKER_REGEX = /\[@@__INSERT_SPAN_START\((\d+)\)\]/g;
代码语言:javascript
复制
              // The HTML to insert in place of START_MARKER
代码语言:javascript
复制
              const START_MARKER_HTML = (index) => {
代码语言:javascript
复制
                let commentHighlightId = getCommentHighlightSpanId(index);
代码语言:javascript
复制
                // If this highlight is for the comment currently being edited, add `is-active` to it, too
代码语言:javascript
复制
                let isCurrentCommentBeingEdited = index === currentCommentBeingEditedIndex;
代码语言:javascript
复制
                return `<span class="${selectionClass} ${isCurrentCommentBeingEdited ? 'is-active' : ''}" id="${commentHighlightId}">`;
代码语言:javascript
复制
              };
代码语言:javascript
复制
              // "Very-unique" HTML-safe string for the end of the highlight
代码语言:javascript
复制
              const END_MARKER_TEXT = () => `[@@__INSERT_SPAN_END]`;
代码语言:javascript
复制
              // A regex that will match `END_MARKER_TEXT`
代码语言:javascript
复制
              const END_MARKER_REGEX = /\[@@__INSERT_SPAN_END\]/g;
代码语言:javascript
复制
              // The HTML to insert in place of END_MARKER
代码语言:javascript
复制
              const END_MARKER_HTML = () => `</span>`;
代码语言:javascript
复制
              let markup = planText;
代码语言:javascript
复制
              let totalOffset = 0;
代码语言:javascript
复制
              _.each(memberComments, (memberComment, index) => {
代码语言:javascript
复制
                let startMarker = START_MARKER_TEXT(index);
代码语言:javascript
复制
                let endMarker = END_MARKER_TEXT();
代码语言:javascript
复制
                let startOffset = totalOffset + parseInt(memberComment.startCharacter);
代码语言:javascript
复制
                let endOffset = totalOffset + parseInt(memberComment.endCharacter);
代码语言:javascript
复制
                markup = markup.substring(0, startOffset) + /* Text, from the start, up to the selection region beginning */
代码语言:javascript
复制
                  startMarker + /* "Very Unique" start marker */
代码语言:javascript
复制
                  markup.substring(startOffset, endOffset) +  /* Text within the selection region */
代码语言:javascript
复制
                  endMarker + /* "Very Unique" end marker */
代码语言:javascript
复制
                  markup.substring(endOffset);  /* Text, from the end of the selection region, to the end */
代码语言:javascript
复制
                totalOffset += startMarker.length + endMarker.length;
代码语言:javascript
复制
              });
代码语言:javascript
复制
              markup = htmlEscape(markup);
代码语言:javascript
复制
              markup = markup.replace(START_MARKER_REGEX, (match, index) => START_MARKER_HTML(Number(index)));
代码语言:javascript
复制
              markup = markup.replace(END_MARKER_REGEX, END_MARKER_HTML);
代码语言:javascript
复制
              return htmlSafe(markup);```
代码语言:javascript
复制
EN

回答 1

Stack Overflow用户

发布于 2021-05-20 09:20:21

这个问题已经解决了。我们对startIndexendIndex进行了交换,这样即使startIndex大于endIndex,它仍然可以正常工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67562102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档