Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to "element" methods to fix errors #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions next-previous-element.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
"use strict";

// Find the next element in an in-order traversal of a tree of nodes.
function getNextElement(element) {
export function getNextElement(element) {
if (!element) {
return null;
}

// The next element is either this one's first child...
var nextelement = null;
if (element.firstChild) {
nextElement = element.firstChild;
var nextElement = null;
if (element.firstElementChild) {
nextElement = element.firstElementChild;
}

// ...or the next sibling...
else if (element.nextSibling) {
nextElement = element.nextSibling;
else if (element.nextElementSibling) {
nextElement = element.nextElementSibling;
}

// ...or the next sibling for the first ancestor that has one.
Expand All @@ -18,8 +23,8 @@ function getNextElement(element) {
while (true) {
if (current.parentElement) {
var parentElement = current.parentElement;
if (parentElement.nextSibling) {
nextElement = parentElement.nextSibling;
if (parentElement.nextElementSibling) {
nextElement = parentElement.nextElementSibling;
break;
}
else {
Expand All @@ -36,18 +41,22 @@ function getNextElement(element) {
}

// Find the previous element in an in-order traversal of a tree of nodes.
function getPreviousElement( element ) {
export function getPreviousElement(element) {

if (!element) {
return null;
}

// The previous element is either this one's previous sibling
var previouselement = null;
if (element.previousSibling) {
previousElement = element.previousSibling;
var previousElement = null;
if (element.previousElementSibling) {
previousElement = element.previousElementSibling;
}

// ...or its parent
else {
previousElement = element.parentElement;;
previousElement = element.parentElement;
}

return previousElement;
}
}