Skip to content

Commit

Permalink
Trim non-breaking spaces with xml_text(trim = TRUE)
Browse files Browse the repository at this point in the history
Fixes #151
  • Loading branch information
jimhester committed Dec 6, 2016
1 parent 8bcd6be commit 13ec091
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# xml2 1.0.0.9000

* Trim non-breaking spaces in `xml_text(trim = TRUE)` (#151).

* Allow setting non-character attributes (values are coerced to characters). (@sjp, #117, #122).

* Fixed return value in call to vapply in xml_integer.xml_nodeset. (@ddiez, #146, #147).
Expand Down
5 changes: 3 additions & 2 deletions src/xml2_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ void node_set_name(XPtrNode node, std::string value) {
CharacterVector node_text(XPtrNode node, bool trim) {
std::string text = Xml2String(xmlNodeGetContent(node.checked_get())).asStdString();

if (trim)
boost::algorithm::trim(text);
if (trim) {
boost::algorithm::trim_if(text, (! boost::algorithm::is_print()) || boost::algorithm::is_space());
}

return asCharacterVector(text.c_str());
}
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-xml_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ test_that("xml_text works properly with xml_nodeset objects", {
c("This is some text. This is some nested text.", "This is some nested text."))
})

test_that("xml_text trims whitespace if requested", {
x <- read_xml("<p> Some text </p>")
test_that("xml_text trims whitespace if requested, including non-breaking spaces", {
x <- read_html("<p> Some text &nbsp;</p>")
expect_identical(xml_text(x),
" Some text ")
" Some text  ")

expect_identical(xml_text(x, trim = TRUE),
"Some text")
Expand Down

0 comments on commit 13ec091

Please sign in to comment.