Skip to content

Commit

Permalink
Don't try to reparent the children of deleted nodes.
Browse files Browse the repository at this point in the history
This triggers a memory leak in Nokogiri.

Fixes #129.
  • Loading branch information
rgrove committed Feb 22, 2015
1 parent bfaf14a commit 42f13db
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 11 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
Sanitize History
================================================================================

Version 3.1.2 (git)
-------------------

* Fixed: Deleting a node in a custom transformer could trigger a memory leak
in Nokogiri if that node's children were later reparented, which the built-in
CleanElement transformer did by default. The CleanElement transformer is now
careful not to reparent the children of deleted nodes. [#129]

[129]:https://github.com/rgrove/sanitize/issues/129


Version 3.1.1 (2015-02-04)
--------------------------

Expand Down
9 changes: 7 additions & 2 deletions lib/sanitize/transformers/clean_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ def call(env)

name = env[:node_name]

# Delete any element that isn't in the config whitelist.
unless @elements.include?(name)
# Delete any element that isn't in the config whitelist, unless the node has
# already been deleted from the document.
#
# It's important that we not try to reparent the children of a node that has
# already been deleted, since that seems to trigger a memory leak in
# Nokogiri.
unless @elements.include?(name) || node.parent.nil?
# Elements like br, div, p, etc. need to be replaced with whitespace in
# order to preserve readability.
if @whitespace_elements.include?(name)
Expand Down

0 comments on commit 42f13db

Please sign in to comment.