Skip to content
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
17 changes: 10 additions & 7 deletions src/soup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,16 @@ let rec leaf_text node =
| [child] -> leaf_text (forget_type child)
| _ -> None

let rec texts node =
match node.values with
| `Text s -> [s]
| `Element {children; _} ->
children |> List.map forget_type |> List.map texts |> List.fold_left (@) []
| `Document {roots; _} ->
roots |> List.map forget_type |> List.map texts |> List.fold_left (@) []
let texts node =
let rec collect acc node =
match node.values with
| `Text s -> s :: acc
| `Element {children; _} ->
List.fold_left (fun acc child -> collect acc (forget_type child)) acc children
| `Document {roots; _} ->
List.fold_left (fun acc root -> collect acc (forget_type root)) acc roots
in
List.rev (collect [] node)

let trimmed_texts node =
texts node
Expand Down
Loading