From ab1060e864f73a50a782f61092298137062e7f89 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Mon, 29 Nov 2021 13:01:04 -0800 Subject: [PATCH] insertBefore, insertAfter --- src/selection/index.js | 4 +++- src/selection/insert.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/selection/index.js b/src/selection/index.js index a593a21..99453e8 100644 --- a/src/selection/index.js +++ b/src/selection/index.js @@ -25,7 +25,7 @@ import selection_html from "./html.js"; import selection_raise from "./raise.js"; import selection_lower from "./lower.js"; import selection_append from "./append.js"; -import selection_insert from "./insert.js"; +import selection_insert, {insertAfter, insertBefore} from "./insert.js"; import selection_remove from "./remove.js"; import selection_clone from "./clone.js"; import selection_datum from "./datum.js"; @@ -79,6 +79,8 @@ Selection.prototype = selection.prototype = { lower: selection_lower, append: selection_append, insert: selection_insert, + insertAfter, + insertBefore, remove: selection_remove, clone: selection_clone, datum: selection_datum, diff --git a/src/selection/insert.js b/src/selection/insert.js index 1733de5..bccdaf3 100644 --- a/src/selection/insert.js +++ b/src/selection/insert.js @@ -5,6 +5,14 @@ function constantNull() { return null; } +function selectThis() { + return this; +} + +function selectNextSibling() { + return this.nextSibling; +} + export default function(name, before) { var create = typeof name === "function" ? name : creator(name), select = before == null ? constantNull : typeof before === "function" ? before : selector(before); @@ -12,3 +20,11 @@ export default function(name, before) { return this.insertBefore(create.apply(this, arguments), select.apply(this, arguments) || null); }); } + +export function insertBefore(name) { + return this.insert(name, selectThis); +} + +export function insertAfter(name) { + return this.insert(name, selectNextSibling); +}