MCPcopy
hub / github.com/witheve/Eve / locateNodeInLineView

Function locateNodeInLineView

src/codemirror.js:1913–1966  ·  view source on GitHub ↗
(lineView, node, offset)

Source from the content-addressed store, hash-verified

1911 }
1912
1913 function locateNodeInLineView(lineView, node, offset) {
1914 var wrapper = lineView.text.firstChild, bad = false;
1915 if (!node || !contains(wrapper, node)) return badPos(Pos(lineNo(lineView.line), 0), true);
1916 if (node == wrapper) {
1917 bad = true;
1918 node = wrapper.childNodes[offset];
1919 offset = 0;
1920 if (!node) {
1921 var line = lineView.rest ? lst(lineView.rest) : lineView.line;
1922 return badPos(Pos(lineNo(line), line.text.length), bad);
1923 }
1924 }
1925
1926 var textNode = node.nodeType == 3 ? node : null, topNode = node;
1927 if (!textNode && node.childNodes.length == 1 && node.firstChild.nodeType == 3) {
1928 textNode = node.firstChild;
1929 if (offset) offset = textNode.nodeValue.length;
1930 }
1931 while (topNode.parentNode != wrapper) topNode = topNode.parentNode;
1932 var measure = lineView.measure, maps = measure.maps;
1933
1934 function find(textNode, topNode, offset) {
1935 for (var i = -1; i < (maps ? maps.length : 0); i++) {
1936 var map = i < 0 ? measure.map : maps[i];
1937 for (var j = 0; j < map.length; j += 3) {
1938 var curNode = map[j + 2];
1939 if (curNode == textNode || curNode == topNode) {
1940 var line = lineNo(i < 0 ? lineView.line : lineView.rest[i]);
1941 var ch = map[j] + offset;
1942 if (offset < 0 || curNode != textNode) ch = map[j + (offset ? 1 : 0)];
1943 return Pos(line, ch);
1944 }
1945 }
1946 }
1947 }
1948 var found = find(textNode, topNode, offset);
1949 if (found) return badPos(found, bad);
1950
1951 // FIXME this is all really shaky. might handle the few cases it needs to handle, but likely to cause problems
1952 for (var after = topNode.nextSibling, dist = textNode ? textNode.nodeValue.length - offset : 0; after; after = after.nextSibling) {
1953 found = find(after, after.firstChild, 0);
1954 if (found)
1955 return badPos(Pos(found.line, found.ch - dist), bad);
1956 else
1957 dist += after.textContent.length;
1958 }
1959 for (var before = topNode.previousSibling, dist = offset; before; before = before.previousSibling) {
1960 found = find(before, before.firstChild, -1);
1961 if (found)
1962 return badPos(Pos(found.line, found.ch + dist), bad);
1963 else
1964 dist += after.textContent.length;
1965 }
1966 }
1967
1968 function domTextBetween(cm, from, to, fromLine, toLine) {
1969 var text = "", closing = false, lineSep = cm.doc.lineSeparator();

Callers 1

domToPosFunction · 0.85

Calls 4

badPosFunction · 0.85
lineNoFunction · 0.85
lstFunction · 0.85
findFunction · 0.70

Tested by

no test coverage detected