diff --git a/HISTORY.md b/HISTORY.md index a2e8a75..e33a289 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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) -------------------------- diff --git a/lib/sanitize/transformers/clean_element.rb b/lib/sanitize/transformers/clean_element.rb index 0b1bdf3..7e2be30 100644 --- a/lib/sanitize/transformers/clean_element.rb +++ b/lib/sanitize/transformers/clean_element.rb @@ -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)