Skip to content

Commit

Permalink
chore: refactor duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
shaveko committed Aug 31, 2023
1 parent 52d8b24 commit 6e8fdba
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions views/js/qtiCreator/widgets/static/helpers/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,31 @@ define(function () {
node.textContent &&
decodeURIComponent(node.textContent).includes(text);

const hasAutogeneratedCodeStartComment = el => {
const hasAutogeneratedCodeCommentSibling = (el, siblingDirection) => {
let commentFound = false;
const previousNode = el.previousSibling;
const siblingNode = el[siblingDirection];
const commentsByDirection = {
previousSibling: autogeneratedCodeStartCommentText,
nextSibling: autogeneratedCodeEndCommentText
};

if (!previousNode) {
if (!siblingNode) {
return false;
}

if (previousNode.textContent && previousNode.textContent.trim().length === 0) {
commentFound = hasAutogeneratedCodeStartComment(previousNode);
if (siblingNode.textContent && siblingNode.textContent.trim().length === 0) {
commentFound = hasAutogeneratedCodeCommentSibling(siblingNode, siblingDirection);
} else {
//ckeditor wraps comments and URIEncodes it
//so that is the reason to use decodeURIComponent
commentFound = isCommentNode(previousNode, autogeneratedCodeStartCommentText);
commentFound = isCommentNode(siblingNode, commentsByDirection[siblingDirection]);
}
return commentFound;
};

const hasAutogeneratedCodeEndComment = el => {
let commentFound = false;
const nextNode = el.nextSibling;

if (!nextNode) {
return false;
}

if (nextNode.textContent && nextNode.textContent.trim().length === 0) {
commentFound = hasAutogeneratedCodeEndComment(nextNode);
} else {
//ckeditor wraps comments and URIEncodes it
//so that is the reason to use decodeURIComponent
commentFound = isCommentNode(nextNode, autogeneratedCodeEndCommentText);
}
return commentFound;
};
const isWrapped = $el => hasAutogeneratedCodeStartComment($el[0]) && hasAutogeneratedCodeEndComment($el[0]);
const isWrapped = $el =>
hasAutogeneratedCodeCommentSibling($el[0], 'previousSibling') &&
hasAutogeneratedCodeCommentSibling($el[0], 'nextSibling');

return {
wrapWithAutogeneratedComment: $el => {
Expand Down

0 comments on commit 6e8fdba

Please sign in to comment.