mir3z / texthighlighter

-- NO LONGER MAINTAINED -- TextHighlighter allows you to highlight text on web pages.
http://mir3z.github.com/texthighlighter/
MIT License
230 stars 101 forks source link

Highlights removed when any part of parent highlighted with its own (current) color #8

Closed ChaseWagoner closed 9 years ago

ChaseWagoner commented 10 years ago

Steps to reproduce (with some pseudo-ML of current functionality):

  1. Highlight a string with any color1. Result:

    <color1>This is now highlighted<color1>
  2. With a different color2, highlight a substring1 of string. Result:

    <color1>This <color2>is now</color2> highlighted</color1>
  3. With color1, highlight a different substring2; it may overlap with substring1 as long as one endpoint of substring2 is outside substring1. The entire string is returned to color 1. Result:

    <color1>This is now highlighted</color1>

This doesn't seem like the intended functionality. Is it? If not, it could be fixed:

Case 1: If substring2 does not overlap with substring1, do nothing (since it's highlighting the whole string redundantly with color1.

Case 2: If substring2 completely contains substring1, replace the node substring1 with its innerHTML (or text(), if more appropriate), deleting it as a child node, and making its content directly part of string.

Case 3: If substring2 does overlap with substring1, "push" the beginning of substring1 to only include the portion that was not highlighted with color1. To visualize, here's some pseudo-ML of fixed functionality:

  1. Highlight string with color1. Result:

    <color1>This is now highlighted<color1>
  2. Highlight substring1 ("is now") with color2. Result:

    <color1>This <color2>is now</color2> highlighted</color1>
  3. Highlight substring2 ("now highlighted") with color1. Result (either of these work, but it depends how much parent/child relationship you want to keep):

    <color1>This <color2>is </color2>now highlighted</color1>, or
    <color1>This </color1><color2>is </color2><color1>now highlighted</color1>
mir3z commented 9 years ago

Thanks for posting issues. Both issues you posted (this and #9 ) are caused by faulty algorithm responsible for normalizing highlights. Nested highlights need to be normalized in order to assure that highlighting is done with use of smallest possible number of highlights. Normalizing is done in 2 steps: flattening nested highlights and merging sibling highlights. It's hard for me to develop correct algorithm because of large number of possibilities. Sooner or later it founds out that algorithm I developed have some bugs, then I try to do fixes and code becomes massive unmaintainable spaghetti. To sum up: I need to create more test cases which covers this kind of issues and once again try to develop new algorithm.