mirror of
https://github.com/codex-team/editor.js
synced 2026-03-17 08:05:47 +01:00
improve getDeepest node method one more time
This commit is contained in:
parent
01a1b9676f
commit
fb3e6fd6ee
3 changed files with 69 additions and 15 deletions
|
|
@ -543,18 +543,39 @@ var Dom = function () {
|
|||
var atLast = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
||||
|
||||
|
||||
if (node && node.nodeType === Node.ELEMENT_NODE && node.firstChild) {
|
||||
/**
|
||||
* Current function have two directions:
|
||||
* - starts from first child and every time gets first or nextSibling in special cases
|
||||
* - starts from last child and gets last or previousSibling
|
||||
* @type {string}
|
||||
*/
|
||||
var child = atLast ? 'lastChild' : 'firstChild',
|
||||
sibling = atLast ? 'previousSibling' : 'nextSibling';
|
||||
|
||||
var nodeChild = atLast ? node.lastChild : node.firstChild;
|
||||
if (node && node.nodeType === Node.ELEMENT_NODE && node[child]) {
|
||||
|
||||
if (nodeChild.tagName === 'BR') {
|
||||
var nodeChild = atLast ? node[child] : node[child];
|
||||
|
||||
if (nodeChild.nextSibling) {
|
||||
/**
|
||||
* special case when child is single tag that can't contain any content
|
||||
*/
|
||||
if (Dom.singleTags.includes(nodeChild.tagName)) {
|
||||
|
||||
nodeChild = nodeChild.nextSibling;
|
||||
} else if (!nodeChild.nextSibling && nodeChild.parentNode.nextSibling) {
|
||||
/**
|
||||
* 1) We need to check the next sibling. If it is Node Element then continue searching for deepest
|
||||
* from sibling
|
||||
*
|
||||
* 2) If single tag's next sibling is null, then go back to parent and check his sibling
|
||||
* In case of Node Element continue searching
|
||||
*
|
||||
* 3) If none of conditions above happened return parent Node Element
|
||||
*/
|
||||
if (nodeChild[sibling]) {
|
||||
|
||||
nodeChild = nodeChild.parentNode.nextSibling;
|
||||
nodeChild = nodeChild[sibling];
|
||||
} else if (nodeChild.parentNode[sibling]) {
|
||||
|
||||
nodeChild = nodeChild.parentNode[sibling];
|
||||
} else {
|
||||
|
||||
return nodeChild.parentNode;
|
||||
|
|
@ -694,6 +715,12 @@ var Dom = function () {
|
|||
return _this.isNodeEmpty(leaf);
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: 'singleTags',
|
||||
get: function get() {
|
||||
|
||||
return ['BR', 'HR', 'IMG'];
|
||||
}
|
||||
}]);
|
||||
|
||||
return Dom;
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -3,6 +3,12 @@
|
|||
*/
|
||||
export default class Dom {
|
||||
|
||||
static get singleTags() {
|
||||
|
||||
return ['BR', 'HR', 'IMG'];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper for making Elements with classname and attributes
|
||||
*
|
||||
|
|
@ -109,19 +115,40 @@ export default class Dom {
|
|||
*/
|
||||
static getDeepestNode(node, atLast = false) {
|
||||
|
||||
if (node && node.nodeType === Node.ELEMENT_NODE && node.firstChild) {
|
||||
/**
|
||||
* Current function have two directions:
|
||||
* - starts from first child and every time gets first or nextSibling in special cases
|
||||
* - starts from last child and gets last or previousSibling
|
||||
* @type {string}
|
||||
*/
|
||||
let child = atLast ? 'lastChild' : 'firstChild',
|
||||
sibling = atLast ? 'previousSibling' : 'nextSibling';
|
||||
|
||||
let nodeChild = atLast ? node.lastChild : node.firstChild;
|
||||
if (node && node.nodeType === Node.ELEMENT_NODE && node[child]) {
|
||||
|
||||
if (nodeChild.tagName === 'BR') {
|
||||
let nodeChild = atLast ? node[child] : node[child];
|
||||
|
||||
if (nodeChild.nextSibling) {
|
||||
/**
|
||||
* special case when child is single tag that can't contain any content
|
||||
*/
|
||||
if (Dom.singleTags.includes(nodeChild.tagName)) {
|
||||
|
||||
nodeChild = nodeChild.nextSibling;
|
||||
/**
|
||||
* 1) We need to check the next sibling. If it is Node Element then continue searching for deepest
|
||||
* from sibling
|
||||
*
|
||||
* 2) If single tag's next sibling is null, then go back to parent and check his sibling
|
||||
* In case of Node Element continue searching
|
||||
*
|
||||
* 3) If none of conditions above happened return parent Node Element
|
||||
*/
|
||||
if (nodeChild[sibling]) {
|
||||
|
||||
} else if (!nodeChild.nextSibling && nodeChild.parentNode.nextSibling) {
|
||||
nodeChild = nodeChild[sibling];
|
||||
|
||||
nodeChild = nodeChild.parentNode.nextSibling;
|
||||
} else if (nodeChild.parentNode[sibling]) {
|
||||
|
||||
nodeChild = nodeChild.parentNode[sibling];
|
||||
|
||||
} else {
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue