mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-20 23:46:14 -04:00
contentcollector: Simplify child node access
This commit is contained in:
parent
1cb5453aeb
commit
275f041fbb
1 changed files with 8 additions and 26 deletions
|
@ -48,24 +48,9 @@ const makeContentCollector = (collectStyles, abrowser, apool, className2Author)
|
||||||
tagName: (n) => n.tagName && n.tagName.toLowerCase(),
|
tagName: (n) => n.tagName && n.tagName.toLowerCase(),
|
||||||
// .nodeValue works with DOM and cheerio 0.22.0.
|
// .nodeValue works with DOM and cheerio 0.22.0.
|
||||||
nodeValue: (n) => n.nodeValue,
|
nodeValue: (n) => n.nodeValue,
|
||||||
// Returns the number of Node children (n.childNodes.length), not the number of Element children
|
// .childNodes works with DOM and cheerio 0.22.0, except in cheerio the .childNodes property
|
||||||
// (n.children.length in DOM).
|
// does not exist on text nodes (and maybe other non-element nodes).
|
||||||
numChildNodes: (n) => {
|
childNodes: (n) => n.childNodes || [],
|
||||||
// .childNodes.length works with DOM and cheerio 0.22.0, except in cheerio the .childNodes
|
|
||||||
// property does not exist on text nodes (and maybe other non-element nodes).
|
|
||||||
if (n.childNodes == null) return 0;
|
|
||||||
return n.childNodes.length;
|
|
||||||
},
|
|
||||||
// Returns the i'th Node child (n.childNodes[i]), not the i'th Element child (n.children[i] in
|
|
||||||
// DOM).
|
|
||||||
childNode: (n, i) => {
|
|
||||||
if (n.childNodes.item == null) {
|
|
||||||
// .childNodes[] works with DOM and cheerio 0.22.0.
|
|
||||||
return n.childNodes[i];
|
|
||||||
}
|
|
||||||
// .childNodes.item() works with DOM but not with cheerio 0.22.0.
|
|
||||||
return n.childNodes.item(i);
|
|
||||||
},
|
|
||||||
nodeProp: (n, p) => n[p],
|
nodeProp: (n, p) => n[p],
|
||||||
getAttribute: (n, a) => {
|
getAttribute: (n, a) => {
|
||||||
// .getAttribute() works with DOM but not with cheerio 0.22.0.
|
// .getAttribute() works with DOM but not with cheerio 0.22.0.
|
||||||
|
@ -146,13 +131,13 @@ const makeContentCollector = (collectStyles, abrowser, apool, className2Author)
|
||||||
let selEnd = [-1, -1];
|
let selEnd = [-1, -1];
|
||||||
const _isEmpty = (node, state) => {
|
const _isEmpty = (node, state) => {
|
||||||
// consider clean blank lines pasted in IE to be empty
|
// consider clean blank lines pasted in IE to be empty
|
||||||
if (dom.numChildNodes(node) === 0) return true;
|
if (dom.childNodes(node).length === 0) return true;
|
||||||
if (dom.numChildNodes(node) === 1 &&
|
if (dom.childNodes(node).length === 1 &&
|
||||||
getAssoc(node, 'shouldBeEmpty') &&
|
getAssoc(node, 'shouldBeEmpty') &&
|
||||||
dom.innerHTML(node) === ' ' &&
|
dom.innerHTML(node) === ' ' &&
|
||||||
!getAssoc(node, 'unpasted')) {
|
!getAssoc(node, 'unpasted')) {
|
||||||
if (state) {
|
if (state) {
|
||||||
const child = dom.childNode(node, 0);
|
const child = dom.childNodes(node)[0];
|
||||||
_reachPoint(child, 0, state);
|
_reachPoint(child, 0, state);
|
||||||
_reachPoint(child, 1, state);
|
_reachPoint(child, 1, state);
|
||||||
}
|
}
|
||||||
|
@ -483,8 +468,7 @@ const makeContentCollector = (collectStyles, abrowser, apool, className2Author)
|
||||||
// lists do not need to have a type, so before we make a wrong guess
|
// lists do not need to have a type, so before we make a wrong guess
|
||||||
// check if we find a better hint within the node's children
|
// check if we find a better hint within the node's children
|
||||||
if (!rr && !type) {
|
if (!rr && !type) {
|
||||||
for (let i = 0; i < dom.numChildNodes(node); i++) {
|
for (const child of dom.childNodes(node)) {
|
||||||
const child = dom.childNode(node, i);
|
|
||||||
if (dom.tagName(child) !== 'ul') continue;
|
if (dom.tagName(child) !== 'ul') continue;
|
||||||
type = dom.getAttribute(child, 'class');
|
type = dom.getAttribute(child, 'class');
|
||||||
if (type) break;
|
if (type) break;
|
||||||
|
@ -566,9 +550,7 @@ const makeContentCollector = (collectStyles, abrowser, apool, className2Author)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const nc = dom.numChildNodes(node);
|
for (const c of dom.childNodes(node)) {
|
||||||
for (let i = 0; i < nc; i++) {
|
|
||||||
const c = dom.childNode(node, i);
|
|
||||||
cc.collectContent(c, state);
|
cc.collectContent(c, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue