Skip to content

Commit

Permalink
cleanup: some unnecessary uses of childNodes
Browse files Browse the repository at this point in the history
a pointless microoptimization informed only by a reading of Blink source of questionable accuracy

but the resulting code is also cleaner
  • Loading branch information
charmander committed Dec 19, 2024
1 parent 5d6628c commit 0894102
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions assets/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
return false;
}

var empty = containerNode => {
var child;

while (child = containerNode.firstChild) {
containerNode.removeChild(child);
}
};

var hasModifierKeys = e =>
e.ctrlKey || e.shiftKey || e.altKey || e.metaKey;

Expand Down Expand Up @@ -396,7 +404,7 @@
}

if (!isBody && allowedTags.indexOf(node.nodeName.toLowerCase()) === -1) {
while (node.childNodes.length) {
while (node.hasChildNodes()) {
node.parentNode.insertBefore(node.firstChild, node);
}

Expand Down Expand Up @@ -633,19 +641,15 @@
weasylMarkdown(fragment);
defang(fragment, true);

while (fragment.childNodes.length) {
while (fragment.hasChildNodes()) {
container.appendChild(fragment.firstChild);
}
}

function updateMarkdownPreview(input) {
if (markedLoadState === 2) {
var preview = input.nextSibling;

while (preview.childNodes.length) {
preview.removeChild(preview.firstChild);
}

empty(preview);
renderMarkdown(input.value, preview);
} else {
loadMarked();
Expand Down Expand Up @@ -837,10 +841,9 @@
target.textContent = 'Reply';
target.removeEventListener('click', cancelReply);

if (children.childNodes.length === 1) {
children.removeChild(newListItem);
if (!children.hasChildNodes()) {
children.parentNode.removeChild(children);
} else {
children.removeChild(newListItem);
}

target.focus();
Expand Down

0 comments on commit 0894102

Please sign in to comment.