views:

151

answers:

3

Sample HTML Data


<body style="width:300px;">
<h3>Long-Text</h3>
A simple tool to store and display texts longer than a few lines.
The search button will highlight all the words matching the name of objects that are members of the classes listed in searchedClasses, itself a member of the KeySet class. The highlighted words are hypertext.
Edit invokes wscripts/acedb.editor, which by default launches emacs. Edit that file to start another editor in its place.
Save will recover from the emacs but will not destroy it.
Read will read a text file, so you could Search it.
**general** grep is a way to annotate a set of longtexts versus the searchedClasses. It outputs an ace file that you can then hand check and read back in acedb to create XREF from longTexts to genes etc.
<h3>World Wide NotePad</h3>
World wide notepad is a small text editor similar to Microsoft's notepad but has some more useful features like an auto typer to make typing the same sentence or word more easy, also World Wide NotePad has a text to speech feature which reads all text in the current open document and speaks it out load to you.
<h3>Etelka Wide Text Pro Bold Italic</h3>
</body>

For example -> "general" (between ** ) is at x=0 and y=465. I know the x,y position. But How to highlight a word located at specific location ?

Let me explain once again. I want to highlight a word by location.

for example I have a location value (x,y)=(0,625). I want to extract the first word by that location ( assume - at that location - we have word "World" ) Then how to highlight that word ?

Edit :
Here Y co-ordinate is absolute position of entire html document.

+1  A: 

The only method I can think of involves wrapping every word in a span element, and then using document.elementFromPoint(x,y) to get the span element at the given location. Something like this:

function highlightWordAtXY(x, y) {
    // Get the element containing the text
    var par = document.elementFromPoint(x, y),
        // textContent or innerText ?
        t = "textContent" in par ? "textContent" : "innerText",
        // Get the text of the element. No pun intended on the par[t].
        text = par[t],
        result;

    // Wrap a span around every word
    par.innerHTML = text.replace(/\b(\w+)\b/g, "<span>$1</span>");

    // Get the elementFromPoint again, should be a span this time
    result = document.elementFromPoint(x, y);

    // Check that we actually clicked on a word
    if (result == par)
        return false;

    // Wrap HTML text around the text at x, y
    result[t] = '<span class="highlight">' + result[t] + '</span>';

    // Restore the content with the wrapped text
    par.innerHTML = par[t];
}

Example at http://jsfiddle.net/BSHYp/1/show/light/ - click a word and watch it highlight.

Some important caveats here:

  • Each block of text must be wrapped in an element (such as <p> or <div>). You should be wrapping paragraphs in <p> tags anyway,
  • The element at the given location (x, y) must only have text in it, no child HTML elements. Text nodes with sibling HTML elements will have them removed (e.g. Clicking "Some" or "here" in Some <b>text</b> here will remove the <b> tags). Dividing them into separate <span> elements would be the only solution without building a much more complex routine,
  • IE will throw an "Unknown runtime error" if you try and add a block level element to a <p> tag,
  • On very, very, very large blocks of text you might run into performance issues. Break them up where applicable.
Andy E
Wow :) excellent one. Superb. Just watching the sample link. :)
sugar
sugar
on your given sample link - on normal clicks, it removes previous highlights. But after clicking blank space it allows multiple highlights ! :( @ # %
sugar
@sugar: that's stepping into complicated territory again because the highlight elements become siblings to the text nodes. One option would be to store a list of co-ordinates in an array and loop over each of those co-ordinates from the lines `result = document.elementFromPoint(x,y);` to `result[t] = ...`. This way, you'd be wrapping several words in the span before `par.innerHTML = par[t];`.
Andy E
@sugar: what you're trying to achieve is complex stuff :-) I'm happy to start you off and help you where you get stuck but the example I wrote was only intended as exactly that - an example. It's likely that it will be far from perfect without much tweaking.
Andy E
isn't it possible to implement by replacing regex for dividing words into span ?
sugar
sugar
@sugar: you're more than welcome :-)
Andy E