jquery-replace-text
Recursively replace text in an HTML structure.
This plugin is a slight improvement over
Ben Alman's replaceText
plugin; its distinction
lies in the fact that it will walk recursively through all text nodes below a given node and apply the
replacement. The code as such is quite short, viz:
#----------------------------------------------------------------------------------------------------------- = -> # http://refactormycode.com/codes/341-jquery-all-descendent-text-nodes-within-a-node R = @each -> fn = argumentscallee $ @ contentseach -> if @nodeType == 3 Rpush @ else fnapply $ @ return $ R #----------------------------------------------------------------------------------------------------------- = to_be_removed = if is_raw then null else @text_nodeseach if new_value = @nodeValuereplace matcherreplacement isnt @nodeValue if not is_raw and '<' in new_value $ @ before new_value to_be_removedpush @ else @nodeValue = new_value $ to_be_removed remove if not is_raw and to_be_removedlength > 0
As a bonus, you get a snazzy node.text_nodes()
method for free!
Installation
Use npm
:
npm install jquery-replace-text