Using a customConverter to remove font formatting from rich text

<xp:inputRichText id="inputRichText1" value="#{doc.body}" readonly="true">
	<xp:this.converter>
		<xp:customConverter>
			<xp:this.getAsObject><![CDATA[#{javascript:value}]]></xp:this.getAsObject>
			<xp:this.getAsString><![CDATA[#{javascript:
				try{
					var html=value.toString();
					html=html.replace(/<(FONT|font)([ ]([a-zA-Z]+)=(\"|')[^\"\\']+(\"|'))*[^>]+>/g,"")
					html=html.replace(/(<\/FONT>|<\/font>)/g,"")
				       return html;
				}catch(e){
				       print (e);
				}}]]>
			</xp:this.getAsString>
		</xp:customConverter>
	</xp:this.converter>
</xp:inputRichText>





This example of a customConverter uses regular expression to remove all occurences of HTML font tags from a rich text field. This can be used to control the formatting of rich text when rendered in the browser.

The code can easily be changed to modify other parts of the HTML formatting.

XPages
Per Henrik Lausten
March 27, 2012 8:57 AM
Rating
281

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.



No comments yetLogin first to comment...