Skip to content

Commit 8127d31

Browse files
authored
Merge pull request #37 from rust-scraper/prevent-self-id-in-bare-modifiers
Prevent usage of self ID in modifiers working on bare ID
2 parents 78c6bd8 + 4e030f7 commit 8127d31

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
lines changed

src/lib.rs

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
550550
///
551551
/// Panics if `new_child_id` is not valid.
552552
pub fn append_id(&mut self, new_child_id: NodeId) -> NodeMut<T> {
553+
assert_ne!(
554+
self.id(),
555+
new_child_id,
556+
"Cannot append node as a child to itself"
557+
);
558+
553559
let last_child_id = self.node().children.map(|(_, id)| id);
554560
{
555561
let mut new_child = self.tree.get_mut(new_child_id).unwrap();
@@ -565,11 +571,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
565571
}
566572

567573
{
568-
if let Some((first_child_id, _)) = self.node().children {
569-
self.node().children = Some((first_child_id, new_child_id));
570-
} else {
571-
self.node().children = Some((new_child_id, new_child_id));
572-
}
574+
self.node().children = match self.node().children {
575+
Some((first_child_id, _)) => Some((first_child_id, new_child_id)),
576+
None => Some((new_child_id, new_child_id)),
577+
};
573578
}
574579

575580
unsafe { self.tree.get_unchecked_mut(new_child_id) }
@@ -581,6 +586,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
581586
///
582587
/// Panics if `new_child_id` is not valid.
583588
pub fn prepend_id(&mut self, new_child_id: NodeId) -> NodeMut<T> {
589+
assert_ne!(
590+
self.id(),
591+
new_child_id,
592+
"Cannot prepend node as a child to itself"
593+
);
594+
584595
let first_child_id = self.node().children.map(|(id, _)| id);
585596
{
586597
let mut new_child = self.tree.get_mut(new_child_id).unwrap();
@@ -596,11 +607,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
596607
}
597608

598609
{
599-
if let Some((_, last_child_id)) = self.node().children {
600-
self.node().children = Some((new_child_id, last_child_id));
601-
} else {
602-
self.node().children = Some((new_child_id, new_child_id));
603-
}
610+
self.node().children = match self.node().children {
611+
Some((_, last_child_id)) => Some((new_child_id, last_child_id)),
612+
None => Some((new_child_id, new_child_id)),
613+
};
604614
}
605615

606616
unsafe { self.tree.get_unchecked_mut(new_child_id) }
@@ -613,6 +623,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
613623
/// - Panics if `new_sibling_id` is not valid.
614624
/// - Panics if this node is an orphan.
615625
pub fn insert_id_before(&mut self, new_sibling_id: NodeId) -> NodeMut<T> {
626+
assert_ne!(
627+
self.id(),
628+
new_sibling_id,
629+
"Cannot insert node as a sibling of itself"
630+
);
631+
616632
let parent_id = self.node().parent.unwrap();
617633
let prev_sibling_id = self.node().prev_sibling;
618634

@@ -650,6 +666,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
650666
/// - Panics if `new_sibling_id` is not valid.
651667
/// - Panics if this node is an orphan.
652668
pub fn insert_id_after(&mut self, new_sibling_id: NodeId) -> NodeMut<T> {
669+
assert_ne!(
670+
self.id(),
671+
new_sibling_id,
672+
"Cannot insert node as a sibling of itself"
673+
);
674+
653675
let parent_id = self.node().parent.unwrap();
654676
let next_sibling_id = self.node().next_sibling;
655677

@@ -686,6 +708,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
686708
///
687709
/// Panics if `from_id` is not valid.
688710
pub fn reparent_from_id_append(&mut self, from_id: NodeId) {
711+
assert_ne!(
712+
self.id(),
713+
from_id,
714+
"Cannot reparent node's children to itself"
715+
);
716+
689717
let new_child_ids = {
690718
let mut from = self.tree.get_mut(from_id).unwrap();
691719
match from.node().children.take() {
@@ -719,6 +747,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
719747
///
720748
/// Panics if `from_id` is not valid.
721749
pub fn reparent_from_id_prepend(&mut self, from_id: NodeId) {
750+
assert_ne!(
751+
self.id(),
752+
from_id,
753+
"Cannot reparent node's children to itself"
754+
);
755+
722756
let new_child_ids = {
723757
let mut from = self.tree.get_mut(from_id).unwrap();
724758
match from.node().children.take() {

tests/node_mut.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,34 @@ fn detach() {
311311
assert!(c.next_sibling().is_none());
312312
}
313313

314+
#[test]
315+
#[should_panic(expected = "Cannot append node as a child to itself")]
316+
fn append_id_itself() {
317+
let mut tree = tree! {
318+
'a' => {
319+
'b' => { 'c', 'd' },
320+
'e' => { 'f', 'g' },
321+
}
322+
};
323+
let mut a = tree.root_mut();
324+
let mut e = a.last_child().unwrap();
325+
e.append_id(e.id());
326+
}
327+
328+
#[test]
329+
#[should_panic(expected = "Cannot prepend node as a child to itself")]
330+
fn prepend_id_itself() {
331+
let mut tree = tree! {
332+
'a' => {
333+
'b' => { 'c', 'd' },
334+
'e' => { 'f', 'g' },
335+
}
336+
};
337+
let mut a = tree.root_mut();
338+
let mut e = a.last_child().unwrap();
339+
e.prepend_id(e.id());
340+
}
341+
314342
#[test]
315343
fn reparent_from_id_append() {
316344
let mut tree = tree! {

0 commit comments

Comments
 (0)