String escaping in Plone python script
I've created a portlet product to tie in an outside chat system. I use a configlet with a WYSIWYGWidget (among others) and portal_properties to hold some properties.
What's giving me an issue is that I am passing those property values to auto_popup.js.pt and having the javascript create a time delayed popup with the contents of the WYSIWYGWidget being the popup's text, but if there is a newline character in the html of the WYSIWYGWidget between tags it causes an error in my javascript. I can fix this simply by going to the portal_properties and manually removing the newline character (which appears as a space in the string field), but that's not the point.
The solution I've been working with is using a python script to translate html from the property field (which is escaped) into standard html & also remove the newline character. The call on the script works perfectly and the script works perfectly in testing but for some reason it won't work when it calls in the specific object from portal_properties.
In the code below I've commented out actual value of the property I'm working with for testing purposes. When run as is in plone the only replace() that goes through is the replace of "welcome" to "hello", but if you use the commented out value the whole thing works.
Any suggestions would be greatly appreciated.
from Products.CMFCore.utils import getToolByName
properties = getToolByName(context, 'portal_properties')
chatMessage = properties.chat_properties.chat_message
# chatMessage = '''<h2 style="text-align: center; ">Welcome</h2>
# <h3 style="text-align: center; ">Would you like to talk to Keith?</h3>'''
chatMes开发者_如何学JAVAsage = chatMessage.replace(">", ">")
chatMessage = chatMessage.replace("<", "<")
chatMessage = chatMessage.replace("&", "&")
chatMessage = chatMessage.replace("> <", "><")
chatMessage = chatMessage.replace('>\n<', '><')
chatMessage = chatMessage.replace('Welcome', 'Hello')
#print chatMessage
return chatMessage
Use urllib.quote() and urllib.unquote() for automatically replacing escape sequences
try:
from Products.PythonScripts.standard import url_unquote
chatMessage = url_unquote(properties.chat_properties.chat_message)
精彩评论