aantron / lambdasoup

Functional HTML scraping and rewriting with CSS in OCaml
https://aantron.github.io/lambdasoup
MIT License
380 stars 31 forks source link

Trying to insert a node before a child of the root fails #46

Open dmbaturin opened 2 years ago

dmbaturin commented 2 years ago

Soup.insert_before only works if the node is a child of an element node, but it fails when the node is at the root of the element tree.

utop # #require "lambdasoup";;

utop # open Soup.Infix;;

utop # let s = Soup.parse "<p>test element</p>" ;;

utop # Soup.insert_before (s $ "p") (Soup.create_text "test stuff") ;;
Exception: Failure "Soup.insert_before: target node has no parent".
aantron commented 1 month ago

I think in order to solve this, the root element node s would have to be replaced by a soup node that has s as a child. Then the new node can be inserted before that child.