| 1 |
|
| 2 |
|
| 3 |
use libc::{c_char, c_void}; |
| 4 |
use std::cell::RefCell; |
| 5 |
use std::collections::{HashMap, HashSet}; |
| 6 |
use std::error::Error; |
| 7 |
use std::ffi::{CStr, CString}; |
| 8 |
use std::hash::{Hash, Hasher}; |
| 9 |
use std::ptr; |
| 10 |
use std::rc::Rc; |
| 11 |
use std::str; |
| 12 |
|
| 13 |
use crate::bindings::*; |
| 14 |
use crate::c_helpers::*; |
| 15 |
use crate::tree::namespace::Namespace; |
| 16 |
use crate::tree::nodetype::NodeType; |
| 17 |
use crate::tree::{Document, DocumentRef, DocumentWeak}; |
| 18 |
use crate::xpath::Context; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#[deprecated(note = "no longer used; node_ptr_mut now relies on RefCell::try_borrow_mut")] |
| 26 |
pub static mut NODE_RC_MAX_GUARD: usize = 2; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#[deprecated(note = "no-op: node_ptr_mut now relies on RefCell::try_borrow_mut")] |
| 32 |
pub fn set_node_rc_guard(_value: usize) {} |
| 33 |
|
| 34 |
type NodeRef = Rc<RefCell<_Node>>; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
ng |
| 42 |
|
| 43 |
ts |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
to |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 56 |
enum Linkage { |
| 57 |
Linked, |
| 58 |
Unlinked, |
| 59 |
RustOwned, |
| 60 |
} |
| 61 |
|
| 62 |
#[derive(Debug)] |
| 63 |
struct _Node { |
| 64 |
|
| 65 |
node_ptr: xmlNodePtr, |
| 66 |
|
| 67 |
document: DocumentWeak, |
| 68 |
t. |
| 69 |
linkage: Linkage, |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
#[derive(Clone, Debug)] |
| 74 |
pub struct Node(NodeRef); |
| 75 |
|
| 76 |
impl Hash for Node { |
| 77 |
|
| 78 |
fn hash<H: Hasher>(&self, state: &mut H) { |
| 79 |
self.node_ptr().hash(state); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
impl PartialEq for Node { |
| 84 |
|
| 85 |
fn eq(&self, other: &Node) -> bool { |
| 86 |
std::ptr::eq(self.node_ptr(), other.node_ptr()) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
impl Eq for Node {} |
| 91 |
|
| 92 |
impl Drop for _Node { |
| 93 |
|
| 94 |
er |
| 95 |
s: |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
ts |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
a |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
fn drop(&mut self) { |
| 124 |
let node_ptr = self.node_ptr; |
| 125 |
if node_ptr.is_null() { |
| 126 |
return; |
| 127 |
} |
| 128 |
match self.linkage { |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
Linkage::Linked => {} |
| 133 |
|
| 134 |
ll |
| 135 |
|
| 136 |
Linkage::Unlinked => { |
| 137 |
let still_doc_owned = unsafe { |
| 138 |
let n: *const crate::bindings::_xmlNode = node_ptr as *const _; |
| 139 |
!(*n).doc.is_null() |
| 140 |
}; |
| 141 |
if !still_doc_owned { |
| 142 |
unsafe { xmlFreeNode(node_ptr) }; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
Linkage::RustOwned => unsafe { xmlFreeNode(node_ptr) }, |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
impl Node { |
| 152 |
|
| 153 |
pub fn new(name: &str, ns: Option<Namespace>, doc: &Document) -> Result<Self, ()> { |
| 154 |
|
| 155 |
|
| 156 |
let c_name = CString::new(name).unwrap(); |
| 157 |
let ns_ptr = match ns { |
| 158 |
None => ptr::null_mut(), |
| 159 |
Some(ns) => ns.ns_ptr(), |
| 160 |
}; |
| 161 |
unsafe { |
| 162 |
let node = xmlNewDocRawNode( |
| 163 |
doc.doc_ptr(), |
| 164 |
ns_ptr, |
| 165 |
c_name.as_bytes().as_ptr(), |
| 166 |
ptr::null(), |
| 167 |
); |
| 168 |
if node.is_null() { |
| 169 |
Err(()) |
| 170 |
} else { |
| 171 |
Ok(Node::wrap_new(node, &doc.0)) |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
pub fn node_ptr(&self) -> xmlNodePtr { |
| 178 |
self.0.borrow().node_ptr |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
a |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
pub fn node_ptr_mut(&mut self) -> Result<xmlNodePtr, String> { |
| 203 |
match self.0.try_borrow_mut() { |
| 204 |
Ok(inner) => Ok(inner.node_ptr), |
| 205 |
|
| 206 |
|
| 207 |
Err(_) => { |
| 208 |
Err("Can not mutably reference a Node that is already actively borrowed".to_string()) |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
fn _wrap(node_ptr: xmlNodePtr, unlinked: bool, document: &DocumentRef) -> Node { |
| 215 |
|
| 216 |
if let Some(node) = document.borrow().get_node(node_ptr) { |
| 217 |
return node.clone(); |
| 218 |
} |
| 219 |
|
| 220 |
let node = _Node { |
| 221 |
node_ptr, |
| 222 |
document: Rc::downgrade(document), |
| 223 |
linkage: if unlinked { |
| 224 |
Linkage::Unlinked |
| 225 |
} else { |
| 226 |
Linkage::Linked |
| 227 |
}, |
| 228 |
}; |
| 229 |
let wrapped_node = Node(Rc::new(RefCell::new(node))); |
| 230 |
document |
| 231 |
.borrow_mut() |
| 232 |
.insert_node(node_ptr, wrapped_node.clone()); |
| 233 |
wrapped_node |
| 234 |
} |
| 235 |
|
| 236 |
pub(crate) fn wrap(node_ptr: xmlNodePtr, document: &DocumentRef) -> Node { |
| 237 |
Node::_wrap(node_ptr, false, document) |
| 238 |
} |
| 239 |
|
| 240 |
pub(crate) fn wrap_new(node_ptr: xmlNodePtr, document: &DocumentRef) -> Node { |
| 241 |
Node::_wrap(node_ptr, true, document) |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
pub fn new_text(content: &str, doc: &Document) -> Result<Self, ()> { |
| 246 |
|
| 247 |
let c_content = CString::new(content).unwrap(); |
| 248 |
unsafe { |
| 249 |
let node = xmlNewDocText(doc.doc_ptr(), c_content.as_bytes().as_ptr()); |
| 250 |
if node.is_null() { |
| 251 |
Err(()) |
| 252 |
} else { |
| 253 |
Ok(Node::wrap_new(node, &doc.0)) |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
or |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
pub fn new_comment(content: &str, doc: &Document) -> Result<Self, ()> { |
| 265 |
let c_content = CString::new(content).map_err(|_| ())?; |
| 266 |
unsafe { |
| 267 |
let node = xmlNewDocComment(doc.doc_ptr(), c_content.as_bytes().as_ptr()); |
| 268 |
if node.is_null() { |
| 269 |
Err(()) |
| 270 |
} else { |
| 271 |
Ok(Node::wrap_new(node, &doc.0)) |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
pub fn mock(doc: &Document) -> Self { |
| 277 |
Node::new("mock", None, doc).unwrap() |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
pub fn null() -> Self { |
| 282 |
Node(Rc::new(RefCell::new(_Node { |
| 283 |
node_ptr: ptr::null_mut(), |
| 284 |
document: Rc::downgrade(&Document::null_ref()), |
| 285 |
linkage: Linkage::Unlinked, |
| 286 |
}))) |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
pub fn to_hashable(&self) -> usize { |
| 291 |
self.node_ptr() as usize |
| 292 |
} |
| 293 |
|
| 294 |
pub(crate) fn get_docref(&self) -> DocumentWeak { |
| 295 |
self.0.borrow().document.clone() |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
pub fn get_next_sibling(&self) -> Option<Node> { |
| 300 |
let ptr = xmlNextSibling(self.node_ptr()); |
| 301 |
self.ptr_as_option(ptr) |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
pub fn get_prev_sibling(&self) -> Option<Node> { |
| 306 |
let ptr = xmlPrevSibling(self.node_ptr()); |
| 307 |
self.ptr_as_option(ptr) |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
pub fn get_first_child(&self) -> Option<Node> { |
| 312 |
let ptr = xmlGetFirstChild(self.node_ptr()); |
| 313 |
self.ptr_as_option(ptr) |
| 314 |
} |
| 315 |
|
| 316 |
pub fn get_last_child(&self) -> Option<Node> { |
| 317 |
let ptr = unsafe { xmlGetLastChild(self.node_ptr()) }; |
| 318 |
self.ptr_as_option(ptr) |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
pub fn get_next_element_sibling(&self) -> Option<Node> { |
| 323 |
match self.get_next_sibling() { |
| 324 |
None => None, |
| 325 |
Some(child) => { |
| 326 |
let mut current_node = child; |
| 327 |
while !current_node.is_element_node() { |
| 328 |
match current_node.get_next_sibling() { Some(sibling) => { |
| 329 |
current_node = sibling; |
| 330 |
} _ => { |
| 331 |
break; |
| 332 |
}} |
| 333 |
} |
| 334 |
if current_node.is_element_node() { |
| 335 |
Some(current_node) |
| 336 |
} else { |
| 337 |
None |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
pub fn get_prev_element_sibling(&self) -> Option<Node> { |
| 345 |
match self.get_prev_sibling() { |
| 346 |
None => None, |
| 347 |
Some(child) => { |
| 348 |
let mut current_node = child; |
| 349 |
while !current_node.is_element_node() { |
| 350 |
match current_node.get_prev_sibling() { Some(sibling) => { |
| 351 |
current_node = sibling; |
| 352 |
} _ => { |
| 353 |
break; |
| 354 |
}} |
| 355 |
} |
| 356 |
if current_node.is_element_node() { |
| 357 |
Some(current_node) |
| 358 |
} else { |
| 359 |
None |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
pub fn get_first_element_child(&self) -> Option<Node> { |
| 367 |
match self.get_first_child() { |
| 368 |
None => None, |
| 369 |
Some(child) => { |
| 370 |
let mut current_node = child; |
| 371 |
while !current_node.is_element_node() { |
| 372 |
match current_node.get_next_sibling() { Some(sibling) => { |
| 373 |
current_node = sibling; |
| 374 |
} _ => { |
| 375 |
break; |
| 376 |
}} |
| 377 |
} |
| 378 |
if current_node.is_element_node() { |
| 379 |
Some(current_node) |
| 380 |
} else { |
| 381 |
None |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
pub fn get_last_element_child(&self) -> Option<Node> { |
| 389 |
match self.get_last_child() { |
| 390 |
None => None, |
| 391 |
Some(child) => { |
| 392 |
let mut current_node = child; |
| 393 |
while !current_node.is_element_node() { |
| 394 |
match current_node.get_prev_sibling() { Some(sibling) => { |
| 395 |
current_node = sibling; |
| 396 |
} _ => { |
| 397 |
break; |
| 398 |
}} |
| 399 |
} |
| 400 |
if current_node.is_element_node() { |
| 401 |
Some(current_node) |
| 402 |
} else { |
| 403 |
None |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
pub fn get_child_nodes(&self) -> Vec<Node> { |
| 411 |
let mut children = Vec::new(); |
| 412 |
if let Some(first_child) = self.get_first_child() { |
| 413 |
children.push(first_child); |
| 414 |
while let Some(sibling) = children.last().unwrap().get_next_sibling() { |
| 415 |
children.push(sibling) |
| 416 |
} |
| 417 |
} |
| 418 |
children |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
pub fn get_child_elements(&self) -> Vec<Node> { |
| 423 |
self |
| 424 |
.get_child_nodes() |
| 425 |
.into_iter() |
| 426 |
.filter(|n| n.get_type() == Some(NodeType::ElementNode)) |
| 427 |
.collect::<Vec<Node>>() |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
pub fn get_parent(&self) -> Option<Node> { |
| 432 |
let ptr = xmlGetParent(self.node_ptr()); |
| 433 |
self.ptr_as_option(ptr) |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
pub fn get_type(&self) -> Option<NodeType> { |
| 438 |
NodeType::from_int(xmlGetNodeType(self.node_ptr())) |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
pub fn add_prev_sibling( |
| 443 |
&mut self, |
| 444 |
new_sibling: &mut Node, |
| 445 |
) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 446 |
new_sibling.set_linked(); |
| 447 |
unsafe { |
| 448 |
if xmlAddPrevSibling(self.node_ptr_mut()?, new_sibling.node_ptr_mut()?).is_null() { |
| 449 |
Err(From::from("add_prev_sibling returned NULL")) |
| 450 |
} else { |
| 451 |
Ok(()) |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
pub fn add_next_sibling( |
| 458 |
&mut self, |
| 459 |
new_sibling: &mut Node, |
| 460 |
) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 461 |
new_sibling.set_linked(); |
| 462 |
unsafe { |
| 463 |
if xmlAddNextSibling(self.node_ptr_mut()?, new_sibling.node_ptr_mut()?).is_null() { |
| 464 |
Err(From::from("add_next_sibling returned NULL")) |
| 465 |
} else { |
| 466 |
Ok(()) |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
pub fn is_text_node(&self) -> bool { |
| 473 |
self.get_type() == Some(NodeType::TextNode) |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
pub fn is_element_node(&self) -> bool { |
| 478 |
self.get_type() == Some(NodeType::ElementNode) |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
pub fn is_null(&self) -> bool { |
| 483 |
self.node_ptr().is_null() |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
pub fn get_name(&self) -> String { |
| 488 |
let name_ptr = xmlNodeGetName(self.node_ptr()); |
| 489 |
if name_ptr.is_null() { |
| 490 |
return String::new(); |
| 491 |
} |
| 492 |
let c_string = unsafe { CStr::from_ptr(name_ptr) }; |
| 493 |
c_string.to_string_lossy().into_owned() |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
pub fn set_name(&mut self, name: &str) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 498 |
let c_name = CString::new(name).unwrap(); |
| 499 |
unsafe { xmlNodeSetName(self.node_ptr_mut()?, c_name.as_bytes().as_ptr()) } |
| 500 |
Ok(()) |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
pub fn get_content(&self) -> String { |
| 506 |
let content_ptr = unsafe { xmlNodeGetContent(self.node_ptr()) }; |
| 507 |
if content_ptr.is_null() { |
| 508 |
|
| 509 |
return String::new(); |
| 510 |
} |
| 511 |
let c_string = unsafe { CStr::from_ptr(content_ptr as *const c_char) }; |
| 512 |
let rust_utf8 = c_string.to_string_lossy().into_owned(); |
| 513 |
bindgenFree(content_ptr as *mut c_void); |
| 514 |
rust_utf8 |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
pub fn set_content(&mut self, content: &str) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 519 |
let c_content = CString::new(content).unwrap(); |
| 520 |
unsafe { xmlNodeSetContent(self.node_ptr_mut()?, c_content.as_bytes().as_ptr()); } |
| 521 |
Ok(()) |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
pub fn get_property(&self, name: &str) -> Option<String> { |
| 526 |
let c_name = CString::new(name).unwrap(); |
| 527 |
let value_ptr = unsafe { xmlGetProp(self.node_ptr(), c_name.as_bytes().as_ptr()) }; |
| 528 |
if value_ptr.is_null() { |
| 529 |
return None; |
| 530 |
} |
| 531 |
let c_value_string = unsafe { CStr::from_ptr(value_ptr as *const c_char) }; |
| 532 |
let prop_str = c_value_string.to_string_lossy().into_owned(); |
| 533 |
bindgenFree(value_ptr as *mut c_void); |
| 534 |
Some(prop_str) |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
pub fn get_property_ns(&self, name: &str, ns: &str) -> Option<String> { |
| 539 |
let c_name = CString::new(name).unwrap(); |
| 540 |
let c_ns = CString::new(ns).unwrap(); |
| 541 |
let value_ptr = unsafe { |
| 542 |
xmlGetNsProp( |
| 543 |
self.node_ptr(), |
| 544 |
c_name.as_bytes().as_ptr(), |
| 545 |
c_ns.as_bytes().as_ptr(), |
| 546 |
) |
| 547 |
}; |
| 548 |
if value_ptr.is_null() { |
| 549 |
return None; |
| 550 |
} |
| 551 |
let c_value_string = unsafe { CStr::from_ptr(value_ptr as *const c_char) }; |
| 552 |
let prop_str = c_value_string.to_string_lossy().into_owned(); |
| 553 |
bindgenFree(value_ptr as *mut c_void); |
| 554 |
Some(prop_str) |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
pub fn get_property_no_ns(&self, name: &str) -> Option<String> { |
| 559 |
let c_name = CString::new(name).unwrap(); |
| 560 |
let value_ptr = unsafe { xmlGetNoNsProp(self.node_ptr(), c_name.as_bytes().as_ptr()) }; |
| 561 |
if value_ptr.is_null() { |
| 562 |
return None; |
| 563 |
} |
| 564 |
let c_value_string = unsafe { CStr::from_ptr(value_ptr as *const c_char) }; |
| 565 |
let prop_str = c_value_string.to_string_lossy().into_owned(); |
| 566 |
bindgenFree(value_ptr as *mut c_void); |
| 567 |
Some(prop_str) |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
pub fn get_property_node(&self, name: &str) -> Option<Node> { |
| 572 |
let c_name = CString::new(name).unwrap(); |
| 573 |
unsafe { |
| 574 |
let attr_node = xmlHasProp(self.node_ptr(), c_name.as_bytes().as_ptr()); |
| 575 |
self.ptr_as_option(attr_node as xmlNodePtr) |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
pub fn get_property_node_ns(&self, name: &str, ns: &str) -> Option<Node> { |
| 581 |
let c_name = CString::new(name).unwrap(); |
| 582 |
let c_ns = CString::new(ns).unwrap(); |
| 583 |
let attr_node = unsafe { |
| 584 |
xmlHasNsProp( |
| 585 |
self.node_ptr(), |
| 586 |
c_name.as_bytes().as_ptr(), |
| 587 |
c_ns.as_bytes().as_ptr(), |
| 588 |
) |
| 589 |
}; |
| 590 |
self.ptr_as_option(attr_node as xmlNodePtr) |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
pub fn get_property_node_no_ns(&self, name: &str) -> Option<Node> { |
| 595 |
let c_name = CString::new(name).unwrap(); |
| 596 |
let attr_node = |
| 597 |
unsafe { xmlHasNsProp(self.node_ptr(), c_name.as_bytes().as_ptr(), ptr::null()) }; |
| 598 |
self.ptr_as_option(attr_node as xmlNodePtr) |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
pub fn has_property(&self, name: &str) -> bool { |
| 603 |
let c_name = CString::new(name).unwrap(); |
| 604 |
let value_ptr = unsafe { xmlHasProp(self.node_ptr(), c_name.as_bytes().as_ptr()) }; |
| 605 |
!value_ptr.is_null() |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
pub fn has_property_ns(&self, name: &str, ns: &str) -> bool { |
| 610 |
let c_name = CString::new(name).unwrap(); |
| 611 |
let c_ns = CString::new(ns).unwrap(); |
| 612 |
let value_ptr = unsafe { |
| 613 |
xmlHasNsProp( |
| 614 |
self.node_ptr(), |
| 615 |
c_name.as_bytes().as_ptr(), |
| 616 |
c_ns.as_bytes().as_ptr(), |
| 617 |
) |
| 618 |
}; |
| 619 |
!value_ptr.is_null() |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
pub fn has_property_no_ns(&self, name: &str) -> bool { |
| 624 |
let c_name = CString::new(name).unwrap(); |
| 625 |
let value_ptr = |
| 626 |
unsafe { xmlHasNsProp(self.node_ptr(), c_name.as_bytes().as_ptr(), ptr::null()) }; |
| 627 |
!value_ptr.is_null() |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
pub fn has_attribute(&self, name: &str) -> bool { |
| 632 |
self.has_property(name) |
| 633 |
} |
| 634 |
|
| 635 |
pub fn has_attribute_ns(&self, name: &str, ns: &str) -> bool { |
| 636 |
self.has_property_ns(name, ns) |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
pub fn has_attribute_no_ns(&self, name: &str) -> bool { |
| 641 |
self.has_property_no_ns(name) |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
pub fn set_property( |
| 646 |
&mut self, |
| 647 |
name: &str, |
| 648 |
value: &str, |
| 649 |
) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 650 |
let c_name = CString::new(name).unwrap(); |
| 651 |
let c_value = CString::new(value).unwrap(); |
| 652 |
unsafe { |
| 653 |
xmlSetProp( |
| 654 |
self.node_ptr_mut()?, |
| 655 |
c_name.as_bytes().as_ptr(), |
| 656 |
c_value.as_bytes().as_ptr(), |
| 657 |
) |
| 658 |
}; |
| 659 |
Ok(()) |
| 660 |
} |
| 661 |
|
| 662 |
pub fn set_property_ns( |
| 663 |
&mut self, |
| 664 |
name: &str, |
| 665 |
value: &str, |
| 666 |
ns: &Namespace, |
| 667 |
) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 668 |
let c_name = CString::new(name).unwrap(); |
| 669 |
let c_value = CString::new(value).unwrap(); |
| 670 |
unsafe { |
| 671 |
xmlSetNsProp( |
| 672 |
self.node_ptr_mut()?, |
| 673 |
ns.ns_ptr(), |
| 674 |
c_name.as_bytes().as_ptr(), |
| 675 |
c_value.as_bytes().as_ptr(), |
| 676 |
) |
| 677 |
}; |
| 678 |
Ok(()) |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
pub fn remove_property(&mut self, name: &str) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 683 |
let c_name = CString::new(name).unwrap(); |
| 684 |
unsafe { |
| 685 |
let attr_node = xmlHasProp(self.node_ptr_mut()?, c_name.as_bytes().as_ptr()); |
| 686 |
if !attr_node.is_null() { |
| 687 |
let remove_prop_status = xmlRemoveProp(attr_node); |
| 688 |
if remove_prop_status == 0 { |
| 689 |
Ok(()) |
| 690 |
} else { |
| 691 |
|
| 692 |
Err(From::from(format!( |
| 693 |
"libxml2 failed to remove property with status: {remove_prop_status:?}"))) |
| 694 |
} |
| 695 |
} else { |
| 696 |
|
| 697 |
Ok(()) |
| 698 |
} |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
pub fn remove_property_ns( |
| 704 |
&mut self, |
| 705 |
name: &str, |
| 706 |
ns: &str, |
| 707 |
) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 708 |
let c_name = CString::new(name).unwrap(); |
| 709 |
let c_ns = CString::new(ns).unwrap(); |
| 710 |
unsafe { |
| 711 |
let attr_node = xmlHasNsProp( |
| 712 |
self.node_ptr_mut()?, |
| 713 |
c_name.as_bytes().as_ptr(), |
| 714 |
c_ns.as_bytes().as_ptr(), |
| 715 |
); |
| 716 |
if !attr_node.is_null() { |
| 717 |
let remove_prop_status = xmlRemoveProp(attr_node); |
| 718 |
if remove_prop_status == 0 { |
| 719 |
Ok(()) |
| 720 |
} else { |
| 721 |
|
| 722 |
Err(From::from(format!( |
| 723 |
"libxml2 failed to remove property with status: {remove_prop_status:?}"))) |
| 724 |
} |
| 725 |
} else { |
| 726 |
|
| 727 |
Ok(()) |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
pub fn remove_property_no_ns(&mut self, name: &str) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 734 |
let c_name = CString::new(name).unwrap(); |
| 735 |
let attr_node = unsafe { |
| 736 |
xmlHasNsProp( |
| 737 |
self.node_ptr_mut()?, |
| 738 |
c_name.as_bytes().as_ptr(), |
| 739 |
ptr::null(), |
| 740 |
) |
| 741 |
}; |
| 742 |
if !attr_node.is_null() { |
| 743 |
let remove_prop_status = unsafe { xmlRemoveProp(attr_node) }; |
| 744 |
if remove_prop_status == 0 { |
| 745 |
Ok(()) |
| 746 |
} else { |
| 747 |
|
| 748 |
Err(From::from(format!( |
| 749 |
"libxml2 failed to remove property with status: {remove_prop_status:?}"))) |
| 750 |
} |
| 751 |
} else { |
| 752 |
|
| 753 |
Ok(()) |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
pub fn get_attribute(&self, name: &str) -> Option<String> { |
| 759 |
self.get_property(name) |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
pub fn get_attribute_ns(&self, name: &str, ns: &str) -> Option<String> { |
| 764 |
self.get_property_ns(name, ns) |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
pub fn get_attribute_no_ns(&self, name: &str) -> Option<String> { |
| 769 |
self.get_property_no_ns(name) |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
pub fn get_attribute_node(&self, name: &str) -> Option<Node> { |
| 774 |
self.get_property_node(name) |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
pub fn get_attribute_node_ns(&self, name: &str, ns: &str) -> Option<Node> { |
| 779 |
self.get_property_node_ns(name, ns) |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
pub fn get_attribute_node_no_ns(&self, name: &str) -> Option<Node> { |
| 784 |
self.get_property_node_no_ns(name) |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
pub fn set_attribute( |
| 789 |
&mut self, |
| 790 |
name: &str, |
| 791 |
value: &str, |
| 792 |
) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 793 |
self.set_property(name, value) |
| 794 |
} |
| 795 |
|
| 796 |
pub fn set_attribute_ns( |
| 797 |
&mut self, |
| 798 |
name: &str, |
| 799 |
value: &str, |
| 800 |
ns: &Namespace, |
| 801 |
) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 802 |
self.set_property_ns(name, value, ns) |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
pub fn remove_attribute(&mut self, name: &str) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 807 |
self.remove_property(name) |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
pub fn remove_attribute_ns( |
| 812 |
&mut self, |
| 813 |
name: &str, |
| 814 |
ns: &str, |
| 815 |
) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 816 |
self.remove_property_ns(name, ns) |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
pub fn remove_attribute_no_ns(&mut self, name: &str) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 821 |
self.remove_property_no_ns(name) |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
pub fn get_properties(&self) -> HashMap<String, String> { |
| 826 |
let mut attributes = HashMap::new(); |
| 827 |
|
| 828 |
let mut current_prop = xmlGetFirstProperty(self.node_ptr()); |
| 829 |
while !current_prop.is_null() { |
| 830 |
let name_ptr = xmlAttrName(current_prop); |
| 831 |
let c_name_string = unsafe { CStr::from_ptr(name_ptr) }; |
| 832 |
let name = c_name_string.to_string_lossy().into_owned(); |
| 833 |
let value = self.get_property(&name).unwrap_or_default(); |
| 834 |
attributes.insert(name, value); |
| 835 |
current_prop = xmlNextPropertySibling(current_prop); |
| 836 |
} |
| 837 |
|
| 838 |
attributes |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
pub fn get_properties_ns(&self) -> HashMap<(String, Option<Namespace>), String> { |
| 843 |
let mut attributes = HashMap::new(); |
| 844 |
|
| 845 |
let mut current_prop = xmlGetFirstProperty(self.node_ptr()); |
| 846 |
while !current_prop.is_null() { |
| 847 |
let name_ptr = xmlAttrName(current_prop); |
| 848 |
let c_name_string = unsafe { CStr::from_ptr(name_ptr) }; |
| 849 |
let name = c_name_string.to_string_lossy().into_owned(); |
| 850 |
let ns_ptr = xmlAttrNs(current_prop); |
| 851 |
if ns_ptr.is_null() { |
| 852 |
let value = self.get_property_no_ns(&name).unwrap_or_default(); |
| 853 |
attributes.insert((name, None), value); |
| 854 |
} else { |
| 855 |
let ns = Namespace { ns_ptr }; |
| 856 |
let value = self |
| 857 |
.get_property_ns(&name, &ns.get_href()) |
| 858 |
.unwrap_or_default(); |
| 859 |
attributes.insert((name, Some(ns)), value); |
| 860 |
} |
| 861 |
current_prop = xmlNextPropertySibling(current_prop); |
| 862 |
} |
| 863 |
|
| 864 |
attributes |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
pub fn get_attributes(&self) -> HashMap<String, String> { |
| 869 |
self.get_properties() |
| 870 |
} |
| 871 |
|
| 872 |
|
| 873 |
pub fn get_attributes_ns(&self) -> HashMap<(String, Option<Namespace>), String> { |
| 874 |
self.get_properties_ns() |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
pub fn get_namespace(&self) -> Option<Namespace> { |
| 879 |
let ns_ptr = xmlNodeNs(self.node_ptr()); |
| 880 |
if ns_ptr.is_null() { |
| 881 |
None |
| 882 |
} else { |
| 883 |
Some(Namespace { ns_ptr }) |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
|
| 888 |
pub fn get_namespaces(&self, doc: &Document) -> Vec<Namespace> { |
| 889 |
let list_ptr_raw = unsafe { xmlGetNsList(doc.doc_ptr(), self.node_ptr()) }; |
| 890 |
if list_ptr_raw.is_null() { |
| 891 |
Vec::new() |
| 892 |
} else { |
| 893 |
let mut namespaces = Vec::new(); |
| 894 |
let mut ptr_iter = list_ptr_raw as *mut xmlNsPtr; |
| 895 |
unsafe { |
| 896 |
while !ptr_iter.is_null() && !(*ptr_iter).is_null() { |
| 897 |
namespaces.push(Namespace { ns_ptr: *ptr_iter }); |
| 898 |
ptr_iter = ptr_iter.add(1); |
| 899 |
} |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
} |
| 913 |
namespaces |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
pub fn get_namespace_declarations(&self) -> Vec<Namespace> { |
| 919 |
if self.get_type() != Some(NodeType::ElementNode) { |
| 920 |
|
| 921 |
return Vec::new(); |
| 922 |
} |
| 923 |
let mut namespaces = Vec::new(); |
| 924 |
let mut ns_ptr = xmlNodeNsDeclarations(self.node_ptr()); |
| 925 |
while !ns_ptr.is_null() { |
| 926 |
if !xmlNsPrefix(ns_ptr).is_null() || !xmlNsHref(ns_ptr).is_null() { |
| 927 |
namespaces.push(Namespace { ns_ptr }); |
| 928 |
} |
| 929 |
ns_ptr = xmlNextNsSibling(ns_ptr); |
| 930 |
} |
| 931 |
namespaces |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
pub fn set_namespace( |
| 936 |
&mut self, |
| 937 |
namespace: &Namespace, |
| 938 |
) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 939 |
unsafe { |
| 940 |
xmlSetNs(self.node_ptr_mut()?, namespace.ns_ptr()); |
| 941 |
} |
| 942 |
Ok(()) |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
pub fn lookup_namespace_prefix(&self, href: &str) -> Option<String> { |
| 947 |
if href.is_empty() { |
| 948 |
return None; |
| 949 |
} |
| 950 |
let c_href = CString::new(href).unwrap(); |
| 951 |
unsafe { |
| 952 |
let ptr_mut = self.node_ptr(); |
| 953 |
let ns_ptr = xmlSearchNsByHref(xmlGetDoc(ptr_mut), ptr_mut, c_href.as_bytes().as_ptr()); |
| 954 |
if !ns_ptr.is_null() { |
| 955 |
let ns = Namespace { ns_ptr }; |
| 956 |
let ns_prefix = ns.get_prefix(); |
| 957 |
Some(ns_prefix) |
| 958 |
} else { |
| 959 |
None |
| 960 |
} |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
pub fn lookup_namespace_uri(&self, prefix: &str) -> Option<String> { |
| 966 |
if prefix.is_empty() { |
| 967 |
return None; |
| 968 |
} |
| 969 |
let c_prefix = CString::new(prefix).unwrap(); |
| 970 |
unsafe { |
| 971 |
let ns_ptr = xmlSearchNs( |
| 972 |
xmlGetDoc(self.node_ptr()), |
| 973 |
self.node_ptr(), |
| 974 |
c_prefix.as_bytes().as_ptr(), |
| 975 |
); |
| 976 |
if !ns_ptr.is_null() { |
| 977 |
let ns = Namespace { ns_ptr }; |
| 978 |
let ns_prefix = ns.get_href(); |
| 979 |
if !ns_prefix.is_empty() { |
| 980 |
Some(ns_prefix) |
| 981 |
} else { |
| 982 |
None |
| 983 |
} |
| 984 |
} else { |
| 985 |
None |
| 986 |
} |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
pub fn recursively_remove_namespaces(&mut self) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 993 |
xmlNodeRecursivelyRemoveNs(self.node_ptr_mut()?); |
| 994 |
Ok(()) |
| 995 |
} |
| 996 |
|
| 997 |
|
| 998 |
pub fn get_class_names(&self) -> HashSet<String> { |
| 999 |
let mut set = HashSet::new(); |
| 1000 |
if let Some(value) = self.get_property("class") { |
| 1001 |
for n in value.split(' ') { |
| 1002 |
set.insert(n.to_owned()); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
set |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
pub fn add_child(&mut self, child: &mut Node) -> Result<(), String> { |
| 1010 |
child.set_linked(); |
| 1011 |
unsafe { |
| 1012 |
let new_child_ptr = xmlAddChild(self.node_ptr_mut()?, child.node_ptr_mut()?); |
| 1013 |
if new_child_ptr.is_null() { |
| 1014 |
Err("add_child encountered NULL pointer".to_string()) |
| 1015 |
} else { |
| 1016 |
Ok(()) |
| 1017 |
} |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
|
| 1022 |
pub fn new_child( |
| 1023 |
&mut self, |
| 1024 |
ns: Option<Namespace>, |
| 1025 |
name: &str, |
| 1026 |
) -> Result<Node, Box<dyn Error + Send + Sync>> { |
| 1027 |
let c_name = CString::new(name).unwrap(); |
| 1028 |
let ns_ptr = match ns { |
| 1029 |
None => ptr::null_mut(), |
| 1030 |
Some(mut ns) => ns.ns_ptr_mut(), |
| 1031 |
}; |
| 1032 |
unsafe { |
| 1033 |
let new_ptr = xmlNewChild( |
| 1034 |
self.node_ptr_mut()?, |
| 1035 |
ns_ptr, |
| 1036 |
c_name.as_bytes().as_ptr(), |
| 1037 |
ptr::null(), |
| 1038 |
); |
| 1039 |
Ok(Node::wrap(new_ptr, &self.get_docref().upgrade().unwrap())) |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
pub fn add_text_child( |
| 1045 |
&mut self, |
| 1046 |
ns: Option<Namespace>, |
| 1047 |
name: &str, |
| 1048 |
content: &str, |
| 1049 |
) -> Result<Node, Box<dyn Error + Send + Sync>> { |
| 1050 |
let c_name = CString::new(name).unwrap(); |
| 1051 |
let c_content = CString::new(content).unwrap(); |
| 1052 |
let ns_ptr = match ns { |
| 1053 |
None => ptr::null_mut(), |
| 1054 |
Some(mut ns) => ns.ns_ptr_mut(), |
| 1055 |
}; |
| 1056 |
unsafe { |
| 1057 |
let new_ptr = xmlNewTextChild( |
| 1058 |
self.node_ptr_mut()?, |
| 1059 |
ns_ptr, |
| 1060 |
c_name.as_bytes().as_ptr(), |
| 1061 |
c_content.as_bytes().as_ptr(), |
| 1062 |
); |
| 1063 |
Ok(Node::wrap(new_ptr, &self.get_docref().upgrade().unwrap())) |
| 1064 |
} |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
pub fn append_text(&mut self, content: &str) -> Result<(), Box<dyn Error + Send + Sync>> { |
| 1069 |
let c_len = content.len() as i32; |
| 1070 |
if c_len > 0 { |
| 1071 |
let c_content = CString::new(content).unwrap(); |
| 1072 |
unsafe { |
| 1073 |
xmlNodeAddContentLen(self.node_ptr_mut()?, c_content.as_bytes().as_ptr(), c_len); |
| 1074 |
} |
| 1075 |
} |
| 1076 |
Ok(()) |
| 1077 |
} |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
is |
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
pub fn unlink_node(&mut self) { |
| 1104 |
let node_type = self.get_type(); |
| 1105 |
if node_type != Some(NodeType::DocumentNode) |
| 1106 |
&& node_type != Some(NodeType::DocumentFragNode) |
| 1107 |
&& !self.is_unlinked() |
| 1108 |
{ |
| 1109 |
|
| 1110 |
self.set_unlinked(); |
| 1111 |
unsafe { |
| 1112 |
xmlUnlinkNode(self.node_ptr()); |
| 1113 |
} |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
pub fn unlink(&mut self) { |
| 1118 |
self.unlink_node() |
| 1119 |
} |
| 1120 |
|
| 1121 |
pub fn unbind_node(&mut self) { |
| 1122 |
self.unlink_node() |
| 1123 |
} |
| 1124 |
|
| 1125 |
pub fn unbind(&mut self) { |
| 1126 |
self.unlink_node() |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
th |
| 1132 |
|
| 1133 |
pub fn is_unlinked(&self) -> bool { |
| 1134 |
!matches!(self.0.borrow().linkage, Linkage::Linked) |
| 1135 |
} |
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
— |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
pub fn set_rust_owned(&self) { |
| 1165 |
let mut inner = self.0.borrow_mut(); |
| 1166 |
debug_assert!( |
| 1167 |
!matches!(inner.linkage, Linkage::Linked), |
| 1168 |
"set_rust_owned called on a Linked node — call unlink_node first" |
| 1169 |
); |
| 1170 |
inner.linkage = Linkage::RustOwned; |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
pub fn is_rust_owned(&self) -> bool { |
| 1176 |
matches!(self.0.borrow().linkage, Linkage::RustOwned) |
| 1177 |
} |
| 1178 |
|
| 1179 |
fn ptr_as_option(&self, node_ptr: xmlNodePtr) -> Option<Node> { |
| 1180 |
if node_ptr.is_null() { |
| 1181 |
None |
| 1182 |
} else { |
| 1183 |
let doc_ref = self.get_docref().upgrade().unwrap(); |
| 1184 |
let new_node = Node::wrap(node_ptr, &doc_ref); |
| 1185 |
Some(new_node) |
| 1186 |
} |
| 1187 |
} |
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
pub(crate) fn set_linked(&self) { |
| 1200 |
let mut inner = self.0.borrow_mut(); |
| 1201 |
debug_assert!( |
| 1202 |
!matches!(inner.linkage, Linkage::RustOwned), |
| 1203 |
"set_linked called on a RustOwned node — re-linking would set up a double free; \ |
| 1204 |
drop the wrapper before re-attaching, or do not call set_rust_owned" |
| 1205 |
); |
| 1206 |
if matches!(inner.linkage, Linkage::Unlinked) { |
| 1207 |
inner.linkage = Linkage::Linked; |
| 1208 |
} |
| 1209 |
} |
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
". |
| 1215 |
pub(crate) fn set_unlinked(&self) { |
| 1216 |
{ |
| 1217 |
let mut inner = self.0.borrow_mut(); |
| 1218 |
if matches!(inner.linkage, Linkage::Linked) { |
| 1219 |
inner.linkage = Linkage::Unlinked; |
| 1220 |
} |
| 1221 |
} |
| 1222 |
self |
| 1223 |
.get_docref() |
| 1224 |
.upgrade() |
| 1225 |
.unwrap() |
| 1226 |
.borrow_mut() |
| 1227 |
.forget_node(self.node_ptr()); |
| 1228 |
} |
| 1229 |
|
| 1230 |
|
| 1231 |
pub fn findnodes(&self, xpath: &str) -> Result<Vec<Node>, ()> { |
| 1232 |
let mut context = Context::from_node(self)?; |
| 1233 |
context.findnodes(xpath, Some(self)) |
| 1234 |
} |
| 1235 |
|
| 1236 |
|
| 1237 |
pub fn at_xpath(&self, path: &str, ns_binlings: &[(&str, &str)]) -> Result<Option<Node>, ()> { |
| 1238 |
let mut context = Context::from_node(self)?; |
| 1239 |
for (prefix, href) in ns_binlings { |
| 1240 |
context.register_namespace(prefix, href)?; |
| 1241 |
} |
| 1242 |
let nodes = context.findnodes(path, Some(self))?; |
| 1243 |
|
| 1244 |
Ok(nodes.first().cloned()) |
| 1245 |
} |
| 1246 |
|
| 1247 |
|
| 1248 |
pub fn ancestors(&self) -> Vec<Node> { |
| 1249 |
let node_ptr = self.node_ptr(); |
| 1250 |
|
| 1251 |
let mut res = Vec::new(); |
| 1252 |
|
| 1253 |
let ancestor_ptrs = node_ancestors(node_ptr); |
| 1254 |
|
| 1255 |
for ptr in ancestor_ptrs { |
| 1256 |
if let Some(node) = self.ptr_as_option(ptr) { |
| 1257 |
res.push(node) |
| 1258 |
} |
| 1259 |
} |
| 1260 |
|
| 1261 |
res |
| 1262 |
} |
| 1263 |
|
| 1264 |
|
| 1265 |
pub fn findvalues(&self, xpath: &str) -> Result<Vec<String>, ()> { |
| 1266 |
let mut context = Context::from_node(self)?; |
| 1267 |
context.findvalues(xpath, Some(self)) |
| 1268 |
} |
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
pub fn replace_child_node( |
| 1273 |
&mut self, |
| 1274 |
mut new: Node, |
| 1275 |
mut old: Node, |
| 1276 |
) -> Result<Node, Box<dyn Error + Send + Sync>> { |
| 1277 |
|
| 1278 |
if new == old || self == &new { |
| 1279 |
|
| 1280 |
Ok(old) |
| 1281 |
} else if self.get_type() == Some(NodeType::ElementNode) { |
| 1282 |
match old.get_parent() { Some(old_parent) => { |
| 1283 |
if &old_parent == self { |
| 1284 |
|
| 1285 |
new.unlink(); |
| 1286 |
|
| 1287 |
old.add_next_sibling(&mut new)?; |
| 1288 |
old.unlink(); |
| 1289 |
Ok(old) |
| 1290 |
} else { |
| 1291 |
Err(From::from(format!( |
| 1292 |
"Old node was not a child of {:?} parent. Registered parent is {:?} instead.", |
| 1293 |
self.get_name(), |
| 1294 |
old_parent.get_name() |
| 1295 |
))) |
| 1296 |
} |
| 1297 |
} _ => { |
| 1298 |
Err(From::from(format!( |
| 1299 |
"Old node was not a child of {:?} parent. No registered parent exists.", |
| 1300 |
self.get_name() |
| 1301 |
))) |
| 1302 |
}} |
| 1303 |
} else { |
| 1304 |
Err(From::from( |
| 1305 |
"Can only call replace_child_node an a NodeType::Element type parent.", |
| 1306 |
)) |
| 1307 |
} |
| 1308 |
} |
| 1309 |
} |
| 1310 |
|
| 1311 |
fn node_ancestors(node_ptr: xmlNodePtr) -> Vec<xmlNodePtr> { |
| 1312 |
if node_ptr.is_null() { |
| 1313 |
return Vec::new(); |
| 1314 |
} |
| 1315 |
|
| 1316 |
let mut parent_ptr = xmlGetParent(node_ptr); |
| 1317 |
|
| 1318 |
if parent_ptr.is_null() { |
| 1319 |
Vec::new() |
| 1320 |
} else { |
| 1321 |
let mut parents = vec![parent_ptr]; |
| 1322 |
|
| 1323 |
while !xmlGetParent(parent_ptr).is_null() { |
| 1324 |
parent_ptr = xmlGetParent(parent_ptr); |
| 1325 |
parents.push(parent_ptr); |
| 1326 |
} |
| 1327 |
|
| 1328 |
parents |
| 1329 |
} |
| 1330 |
} |
| 1331 |
|
| 1332 |
mod c14n; |