IntroductionThis is a generic JS Class, that allows you to translate(parse) the content of text nodes, and replace them for the new content.
You only need to specify the parsing function, and the starting (root) node.
All the text nodes inside it, will get parsed.
ModesThe class supports synchronous and asynchronous parsing.
The parsing function will receive the original text as first argument.
If you can parse the text right away, then just return it from the parsing function.
If it needs to be delayed (f.e: AJAX request), then the set the attribute 'sync' of the instance, to false. By doing this, you will get a second argument in the parsing function, which will be a function that you need to call, passing it the parsed text.
Returned dataThe parsed data you return can be:
a stringThis is the standard, a string replacing the old one.
nothingnew If you don't return data, or the same string, it will just get skipped.
a nodenew You can return an html node.
an arraynew You can return an array with nodes and/or strings.
FilteringnewOptionally, you can pass the translator, a filtering function. This way you can exclude elements (and their descendants) from the parsing.
To use it, pass the function as second argument to the constructor.
It will receive the (element) node, and it must return true or false.
How to useCheck the demo to see both modes and filtering in action.
You need to call the method 'traverse' of the instance, passing it the root node.
Remember to call the method after the html document is parsed, so you can access all the nodes in it.
- Translator 1.0.1 Zip(all files and docs)
- Translator 1.0.1 Source(to learn or test)
- Translator 1.0.1 Minified(recommended)