1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Significantly optimize regexp time of fix_textareas!

The leading whitespace match and back reference added significant overhead in
certain cases, sometimes leading to render times taking minutes. In some cases,
the render time dropped from 2+ minutes to 200 milliseconds.

Closes #849
This commit is contained in:
Stan Hu 2015-07-16 09:12:24 -07:00 committed by Norman Clarke
parent c89da381a0
commit bc2b9f186f

View file

@ -273,16 +273,16 @@ module Haml
# @since Haml 4.0.1
# @private
def fix_textareas!(input)
pattern = /([ ]*)<(textarea)([^>]*)>(\n|&#x000A;)(.*?)(<\/\2>)/im
pattern = /<(textarea)([^>]*)>(\n|&#x000A;)(.*?)<\/textarea>/im
input.gsub!(pattern) do |s|
match = pattern.match(s)
content = match[5]
if match[4] == '&#x000A;'
content = match[4]
if match[3] == '&#x000A;'
content.sub!(/\A /, '&#x0020;')
else
content.sub!(/\A[ ]*/, '')
end
"#{match[1]}<#{match[2]}#{match[3]}>\n#{content}</#{match[2]}>"
"<#{match[1]}#{match[2]}>\n#{content}</#{match[1]}>"
end
end