Multiple find/replace problem

I wrote simple script for parsing html source. The tag list for cleaning is a list. The replace value too. In example:

set parse_list to {"</p>", ASCII character 13, "<br />", ASCII character 13, "<p>", null, "<p ", null, "<span ", null}
set source to "<p>sample text</p> sample text2 <br /> sample text..."

As you see, the first item in parse_list is text to find, second “ text to replace.
OK. I found the find/replace handler on macscripter.net. And this script works perfectly:


set n to 1
set w to (count items of parse_list) / 2
repeat w times
	set source to replaceText(item n of parse_list, item (n + 1) of parse_list, source)
	set n to (n + 2)
end repeat

-- handler
on replaceText(find, replace, subject)
	set prevTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to find
	set subject to text items of subject
	set AppleScript's text item delimiters to replace
	set subject to "" & subject
	set AppleScript's text item delimiters to prevTIDs
	return subject as Unicode text -- that's it! handler gubil unicode!
end replaceText

But, for clarity, I want create a new handler. I wrote this:


on wymiataj(subject, lista)
	set w to ((count items of lista) / 2)
	set n to 1
	repeat w times
		set prevTIDs to AppleScript's text item delimiters
		set AppleScript's text item delimiters to (item n of lista)
		set subject to text items of subject
		set AppleScript's text item delimiters to (item (n + 1) of lista)
		set subject to "" & subject
		set AppleScript's text item delimiters to prevTIDs
		return subject as Unicode text
		set n to (n + 2)
	end repeat
end wymiataj

… but “set xxx to wymiataj(source, parse_list)” not working. It replace only first value. Why?

Try something like:

repeat with k from 1 to (count parse_list) -1 by 2
	set source to replaceText(item k of parse_list, item (k + 1) of parse_list, source)
end repeat

-- handler
on replaceText(find, replace, subject)
	set prevTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to find
	set subject to text items of subject
	set AppleScript's text item delimiters to replace
	set subject to "" & subject
	set AppleScript's text item delimiters to prevTIDs
	return subject as Unicode text -- that's it! handler gubil unicode!
end replaceText

OK. This works. Thank you.