473,388 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How to get nodes from selection?

I have to get every node within range or selection? Is it possible?

Sry for my engrish :]
Ref

Mar 1 '07 #1
4 2999
ASM
reflex a écrit :
I have to get every node within range or selection? Is it possible?
give example of your code to get the selection

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Mar 1 '07 #2

ASM napsal:
reflex a écrit :
I have to get every node within range or selection? Is it possible?

give example of your code to get the selection

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date

I am writing plugin for FCK editor

var oEditor = FCKeditorAPI.GetInstance('FCKeditor1');
var eSelected = oEditor.EditorDocument.selection;
var rSelected = eSelected.createRange();

its same like

var rSelected = document.selection.createRange();

Mar 1 '07 #3
reflex wrote:

Hi Reflex,
I have to get every node within range or selection? Is it possible?
In IE, you can only get nodes of type 'element', which would restrict
your requirement. In browsers supporting the W3C Range Model, you could
get to the node level.

Following is some example of the function you are looking for, tested
IE5+, Firefox 2, Opera 9. You may have to filter for the elements
returned by the function, though. Have fun.
HTH,
Elegie.

---
<style type="text/css">
#s1 { border : solid 1px #c00; }
#s2 { border : solid 1px #0c0; }
#s3 { border : solid 1px #00c; }
#result { color : #aaa; margin : 10px; }
</style>

<div id="result">&nbsp;</div>
<div id="container">
<span id="s1">Old Artist</span>
<span id="s2">Nothing Else</span>
<span id="s3">Skyscraper</span>
</div>

<script type="text/javascript">
function getElementsFromSelection(){
var nodes=null, candidates=[], children, el, parent, rng;

// Main
rng=getSelectionRange();
if(rng) {
parent=getCommonAncestor(rng);
if(parent) {
// adjust from text node to element, if needed
while(parent.nodeType!=1) parent=parent.parentNode;

// obtain all candidates from parent (excluded)
// up to BODY (included)
if(parent.nodeName.toLowerCase()!="body") {
el=parent;
do {
el=el.parentNode;
candidates[candidates.length]=el;
} while(el.nodeName.toLowerCase()!="body");
}

// obtain all candidates down to all children
children=parent.all||parent.getElementsByTagName(" *");
for(var j=0; j<children.length; j++)
candidates[candidates.length]=children[j];

// proceed - keep element when range touches it
nodes=[parent];
for(var ii=0, r2; ii<candidates.length; ii++) {
r2=createRangeFromElement(candidates[ii]);
if(r2 && rangeContact(rng, r2))
nodes[nodes.length]=candidates[ii];
}
}
}
return nodes;

// Helpers
function getSelectionRange() {
var rng=null;
if(window.getSelection) {
rng=window.getSelection();
if(rng && rng.rangeCount && rng.getRangeAt) {
rng=rng.getRangeAt(0);
}
} else if(document.selection && document.selection.type=="Text") {
rng=document.selection.createRange();
}
return rng;
}

function getCommonAncestor(rng) {
return rng.parentElement ?
rng.parentElement() : rng.commonAncestorContainer;
}

function rangeContact(r1, r2) {
var p=null;
if(r1.compareEndPoints) {
p={
method:"compareEndPoints",
StartToStart:"StartToStart",
StartToEnd:"StartToEnd",
EndToEnd:"EndToEnd",
EndToStart:"EndToStart"
}
} else if(r1.compareBoundaryPoints) {
p={
method:"compareBoundaryPoints",
StartToStart:0,
StartToEnd:1,
EndToEnd:2,
EndToStart:3
}
}
return p && !(
r2[p.method](p.StartToStart, r1)==1 &&
r2[p.method](p.EndToEnd, r1)==1 &&
r2[p.method](p.StartToEnd, r1)==1 &&
r2[p.method](p.EndToStart, r1)==1
||
r2[p.method](p.StartToStart, r1)==-1 &&
r2[p.method](p.EndToEnd, r1)==-1 &&
r2[p.method](p.StartToEnd, r1)==-1 &&
r2[p.method](p.EndToStart, r1)==-1
);
}

function createRangeFromElement(el) {
var rng=null;
if(document.body.createTextRange) {
rng=document.body.createTextRange();
rng.moveToElementText(el);
} else if(document.createRange) {
rng=document.createRange();
rng.selectNodeContents(el);
}
return rng;
}
};

// Test
window.onload = function(evt) {
setInterval(
function(){
var buf=[];
var nodes=getElementsFromSelection();
if(nodes) {
for(var ii=0; ii<nodes.length; ii++) {
buf[buf.length]=nodes[ii].nodeName+"&lt;"+nodes[ii].id+"&gt";
}
document.getElementById("result").innerHTML=buf.jo in(", ");
}
},
42
);
}
</script>
---
Mar 1 '07 #4
Thank you so much Elegie. Works great

Mar 1 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Rolf Kemper | last post by:
Dear Experts, I got stuck with the following problem and need your help. What I wnat to do is to get a set of distinct nodes. Before the distinct I have selected the multiple occourences...
2
by: Bryan Galvin | last post by:
Hi all, Is it possible to 'cherry-pick' child nodes but retain their parentage. Given an example... <user> <first_name>George</first_name> <sur_name>George</sur_name> <dob>George</dob>...
2
by: arvindsd | last post by:
Hi all, The code below gets me a list of all the nodes within the node object called xml_tags_root. NodeList nl = xml_tags_root.getChildNodes(); The length of nodelist I get is double the...
19
by: Christian Fowler | last post by:
I have a VERY LARGE pile of geographic data that I am importing into a database (db of choice is postgres, though may hop to oracle if necessary). The data is strictly hierarchical - each node has...
1
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked and if a correspondent node in my xml config file...
1
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked and if a correspondent node in my xml config file...
6
by: serge calderara | last post by:
Dear all, I was wondering is there is a way to customise the treView control in order to get any Control as a Node. What I try to do is having a Progress bar as a treeview node object. Is...
1
by: matth | last post by:
I'm trying to iterate through nodes in a Selection Range, but I'm having a bit of trouble determining why all nodes in the range aren't being hit. It seems like deeply nested nodes aren't being hit...
1
by: karthee | last post by:
I am creating a custom treeview with multiple selection, if i select multiple nodes and right click, the selection is disappearing, i tried with somany things like on mouse click my code is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.