MCPcopy Create free account
hub / github.com/TruthHun/BookStack / locateNodeInLineView

Function locateNodeInLineView

static/mergely/lib/codemirror.js:1908–1961  ·  view source on GitHub ↗
(lineView, node, offset)

Source from the content-addressed store, hash-verified

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

Callers 1

domToPosFunction · 0.70

Calls 5

badPosFunction · 0.70
lineNoFunction · 0.70
lstFunction · 0.70
findFunction · 0.70
containsFunction · 0.50

Tested by

no test coverage detected