def setUp(self, request, node, data): FormFillerWidget.setUp(self, request, node, data) for k, f in self.model.err.items(): en = self.errorNodes[k] tn = self.inputNodes[k] en.setAttribute('class', 'formError') tn.setAttribute('class', 'formInputError') en.childNodes[:]=[] # gurfle, CLEAR IT NOW!@# if isinstance(f, failure.Failure): f = f.getErrorMessage() lmx(en).text(str(f))
def setUp(self, request, node, data): FormFillerWidget.setUp(self, request, node, data) for k, f in self.model.err.items(): en = self.errorNodes[k] tn = self.inputNodes[k] en.setAttribute('class', 'formError') tn.setAttribute('class', 'formInputError') en.childNodes[:] = [] # gurfle, CLEAR IT NOW!@# if isinstance(f, failure.Failure): f = f.getErrorMessage() lmx(en).text(str(f))
def scrub(self, node, filterCIDLinks=True): """ Remove all potentially harmful elements from the node and return a wrapper node. For reasons (perhaps dubious) of performance, this mutates its input. """ if node.nodeName == 'html': filler = body = lmx().div(_class="message-html") for c in node.childNodes: if c.nodeName == 'head': for hc in c.childNodes: if hc.nodeName == 'title': body.div(_class="message-title").text(domhelpers.gatherTextNodes(hc)) break elif c.nodeName == 'body': filler = body.div(_class='message-body') break else: filler = body = lmx().div(_class="message-nohtml") for e in self.iternode(node): if getattr(e, 'clean', False): # If I have manually exploded this node, just forget about it. continue ennl = e.nodeName.lower() if filterCIDLinks and self._filterCIDLink(e): # we could replace these with a marker element, like we do # with dangerous tags, but i'm not sure there is a reason to e.parentNode.removeChild(e) if ennl in self._goodHtml: handler = getattr(self, '_handle_' + ennl, None) if handler is not None: e = handler(e) newAttributes = {} oldAttributes = e.attributes e.attributes = newAttributes goodAttributes = self._goodHtml[ennl] + self._alwaysSafeAttributes for attr in goodAttributes: if attr in oldAttributes: newAttributes[attr] = oldAttributes[attr] else: e.attributes.clear() e.setTagName("div") e.setAttribute("class", "message-html-unknown") e.setAttribute("style", "display: none") div = Element('div') div.setAttribute('class', 'message-html-unknown-tag') div.appendChild(Text("Untrusted %s tag" % (ennl, ))) e.childNodes.insert(0, div) filler.node.appendChild(node) return body.node
def setUp(self, request, node, data): # node = widgets.Widget.generateDOM(self,request,node) lmn = lmx(node) if not node.hasAttribute('action'): lmn['action'] = (request.prepath + request.postpath)[-1] if not node.hasAttribute("method"): lmn['method'] = 'post' lmn['enctype'] = 'multipart/form-data' self.errorNodes = errorNodes = {} # name: nodes which trap errors self.inputNodes = inputNodes = {} for errorNode in domhelpers.findElementsWithAttribute( node, 'errorFor'): errorNodes[errorNode.getAttribute('errorFor')] = errorNode argz = {} # list to figure out which nodes are in the template already and which aren't hasSubmit = 0 argList = self.model.fmethod.getArgs() for arg in argList: if isinstance(arg, formmethod.Submit): hasSubmit = 1 argz[arg.name] = arg inNodes = domhelpers.findElements( node, lambda n: n.tagName.lower() in ('textarea', 'select', 'input', 'div')) for inNode in inNodes: t = inNode.getAttribute("type") if t and t.lower() == "submit": hasSubmit = 1 if not inNode.hasAttribute("name"): continue nName = inNode.getAttribute("name") if argz.has_key(nName): #send an empty content shell - we just want the node inputNodes[nName] = self.convergeInput(request, lmx(), argz[nName], inNode) inNode.parentNode.replaceChild(inputNodes[nName], inNode) del argz[nName] # TODO: # * some arg types should only have a single node (text, string, etc) # * some should have multiple nodes (choice, checkgroup) # * some have a bunch of ancillary nodes that are possible values (menu, radiogroup) # these should all be taken into account when walking through the template if argz: shell = self.createShell(request, node, data) # create inputs, in the same order they were passed to us: for remArg in [arg for arg in argList if argz.has_key(arg.name)]: inputNode, errorNode = self.createInput(request, shell, remArg) errorNodes[remArg.name] = errorNode inputNodes[remArg.name] = inputNode if not hasSubmit: lmn.input(type="submit")
def setUp(self, request, node, data): # node = widgets.Widget.generateDOM(self,request,node) lmn = lmx(node) if not node.hasAttribute('action'): lmn['action'] = (request.prepath+request.postpath)[-1] if not node.hasAttribute("method"): lmn['method'] = 'post' lmn['enctype'] = 'multipart/form-data' self.errorNodes = errorNodes = {} # name: nodes which trap errors self.inputNodes = inputNodes = {} for errorNode in domhelpers.findElementsWithAttribute(node, 'errorFor'): errorNodes[errorNode.getAttribute('errorFor')] = errorNode argz={} # list to figure out which nodes are in the template already and which aren't hasSubmit = 0 argList = self.model.fmethod.getArgs() for arg in argList: if isinstance(arg, formmethod.Submit): hasSubmit = 1 argz[arg.name] = arg inNodes = domhelpers.findElements( node, lambda n: n.tagName.lower() in ('textarea', 'select', 'input', 'div')) for inNode in inNodes: t = inNode.getAttribute("type") if t and t.lower() == "submit": hasSubmit = 1 if not inNode.hasAttribute("name"): continue nName = inNode.getAttribute("name") if argz.has_key(nName): #send an empty content shell - we just want the node inputNodes[nName] = self.convergeInput(request, lmx(), argz[nName], inNode) inNode.parentNode.replaceChild(inputNodes[nName], inNode) del argz[nName] # TODO: # * some arg types should only have a single node (text, string, etc) # * some should have multiple nodes (choice, checkgroup) # * some have a bunch of ancillary nodes that are possible values (menu, radiogroup) # these should all be taken into account when walking through the template if argz: shell = self.createShell(request, node, data) # create inputs, in the same order they were passed to us: for remArg in [arg for arg in argList if argz.has_key(arg.name)]: inputNode, errorNode = self.createInput(request, shell, remArg) errorNodes[remArg.name] = errorNode inputNodes[remArg.name] = inputNode if not hasSubmit: lmn.input(type="submit")
def wvupdate_thumbnail(self, request, node, data): a = microdom.lmx(node) a["href"] = data if os.path.isdir(os.path.join(self.directory, data)): a.text(data) else: a.img(src=(data + "/preview"), width="200", height="200").text(data)
def testUnicodeTolerance(self): import struct s = '<foo><bar><baz /></bar></foo>' j =(u'<?xml version="1.0" encoding="UCS-2" ?>\r\n<JAPANESE>\r\n' u'<TITLE>\u5c02\u9580\u5bb6\u30ea\u30b9\u30c8 </TITLE></JAPANESE>') j2=('\xff\xfe<\x00?\x00x\x00m\x00l\x00 \x00v\x00e\x00r\x00s\x00i\x00o' '\x00n\x00=\x00"\x001\x00.\x000\x00"\x00 \x00e\x00n\x00c\x00o\x00d' '\x00i\x00n\x00g\x00=\x00"\x00U\x00C\x00S\x00-\x002\x00"\x00 \x00?' '\x00>\x00\r\x00\n\x00<\x00J\x00A\x00P\x00A\x00N\x00E\x00S\x00E' '\x00>\x00\r\x00\n\x00<\x00T\x00I\x00T\x00L\x00E\x00>\x00\x02\\' '\x80\x95\xb6[\xea0\xb90\xc80 \x00<\x00/\x00T\x00I\x00T\x00L\x00E' '\x00>\x00<\x00/\x00J\x00A\x00P\x00A\x00N\x00E\x00S\x00E\x00>\x00') def reverseBytes(s): fmt = str(len(s) / 2) + 'H' return struct.pack('<' + fmt, *struct.unpack('>' + fmt, s)) urd = microdom.parseString(reverseBytes(s.encode('UTF-16'))) ud = microdom.parseString(s.encode('UTF-16')) sd = microdom.parseString(s) self.assert_(ud.isEqualToDocument(sd)) self.assert_(ud.isEqualToDocument(urd)) ud = microdom.parseString(j) urd = microdom.parseString(reverseBytes(j2)) sd = microdom.parseString(j2) self.assert_(ud.isEqualToDocument(sd)) self.assert_(ud.isEqualToDocument(urd)) j3=microdom.parseString(u'<foo/>') hdr='<?xml version="1.0"?>' div=microdom.lmx().text(u'\u221a', raw=1).node de=j3.documentElement de.appendChild(div) de.appendChild(j3.createComment(u'\u221a')) self.assertEquals(j3.toxml(), hdr+ u'<foo><div>\u221a</div><!--\u221a--></foo>'.encode('utf8'))
def spanify(self, node): node.attributes = {} node.childNodes = [] node.endTagName = node.nodeName = node.tagName = 'span' lnew = lmx(node).span() lnew.node.clean = True return lnew
def wvupdate_thumbnail(self, request, node, data): size = request.args.get('thumbnailSize', ('200', ))[0] a = microdom.lmx(node) a['href'] = data if os.path.isdir(os.path.join(self.directory, data)): a.text(data) else: a.img(src=(data + '/preview'), width=size, height=size).text(data)
def wvupdate_thumbnail(self, request, node, data): a = microdom.lmx(node) a['href'] = data if os.path.isdir(os.path.join(self.directory, data)): a.text(data) else: a.img(src=(data + '/preview'), width='200', height='200').text(data)
def wvupdate_thumbnail(self, request, node, data): size = request.args.get('thumbnailSize',('200',))[0] a = microdom.lmx(node) a['href'] = data if os.path.isdir(os.path.join(self.directory,data)): a.text(data) else: a.img(src=(data+'/preview'),width=size,height=size).text(data)
def save(self, xmlfile=None): """Save current state as XML @param xmlfile file name (optional)""" xmlfile = xmlfile or self.xmlfile root = microdom.lmx(self.root) for k,v in self.items(): # if v is None: v='' self.write_element(root, k, v) save_xml(root.node, xmlfile)
def wvupdate_thumbnail(self, request, node, data): prefs = request.getSession(IPreferences) size = getattr(prefs, 'size', '200') a = microdom.lmx(node) a['href'] = data if os.path.isdir(os.path.join(self.directory, data)): a.text(data) else: a.img(src=(data + '/preview'), width=size, height=size).text(data)
def wvupdate_thumbnail(self, request, node, data): prefs = request.getSession(IPreferences) size = getattr(prefs, 'size','200') a = microdom.lmx(node) a['href'] = data if os.path.isdir(os.path.join(self.directory,data)): a.text(data) else: a.img(src=(data+'/preview'),width=size,height=size).text(data)
def save(self, xmlfile=None): """Save current state as XML @param xmlfile file name (optional)""" xmlfile = xmlfile or self.xmlfile root = microdom.lmx(self.root) for k,v in self.items(): # if v is None: v='' log.msg("save(): write element: key = %s, value = %s" % (k,v)); self.write_element(root, k, v) save_xml(root.node, xmlfile)
def testLMX(self): n = microdom.Element("p") lmx = microdom.lmx(n) lmx.text("foo") b = lmx.b(a="c") b.foo()["z"] = "foo" b.foo() b.add("bar", c="y") s = '<p>foo<b a="c"><foo z="foo"></foo><foo></foo><bar c="y"></bar></b></p>' self.assertEquals(s, n.toxml())
def setUp(self, request, node, data): lmn = lmx(node) if not node.hasAttribute('action'): lmn['action'] = (request.prepath+request.postpath)[-1] if not node.hasAttribute("method"): lmn['method'] = 'post' lmn['enctype'] = 'multipart/form-data' self.errorNodes = errorNodes = {} # name: nodes which trap errors self.inputNodes = inputNodes = {} for errorNode in domhelpers.findElementsWithAttribute(node, 'errorFor'): errorNodes[errorNode.getAttribute('errorFor')] = errorNode argz={} hasSubmit = 0 argList = self.model.fmethod.getArgs() for arg in argList: if isinstance(arg, formmethod.Submit): hasSubmit = 1 argz[arg.name] = arg inNodes = domhelpers.findElements( node, lambda n: n.tagName.lower() in ('textarea', 'select', 'input', 'div')) for inNode in inNodes: t = inNode.getAttribute("type") if t and t.lower() == "submit": hasSubmit = 1 if not inNode.hasAttribute("name"): continue nName = inNode.getAttribute("name") if argz.has_key(nName): inputNodes[nName] = self.convergeInput(request, lmx(), argz[nName], inNode) inNode.parentNode.replaceChild(inputNodes[nName], inNode) del argz[nName] if argz: shell = self.createShell(request, node, data) for remArg in [arg for arg in argList if argz.has_key(arg.name)]: inputNode, errorNode = self.createInput(request, shell, remArg) errorNodes[remArg.name] = errorNode inputNodes[remArg.name] = inputNode if not hasSubmit: lmn.input(type="submit")
def test_LMX(self): n = microdom.Element("p") lmx = microdom.lmx(n) lmx.text("foo") b = lmx.b(a="c") b.foo()["z"] = "foo" b.foo() b.add("bar", c="y") s = '<p>foo<b a="c"><foo z="foo"></foo><foo></foo><bar c="y"></bar></b></p>' self.assertEqual(s, n.toxml())
def testUnicodeTolerance(self): import struct s = "<foo><bar><baz /></bar></foo>" j = ( u'<?xml version="1.0" encoding="UCS-2" ?>\r\n<JAPANESE>\r\n' u"<TITLE>\u5c02\u9580\u5bb6\u30ea\u30b9\u30c8 </TITLE></JAPANESE>" ) j2 = ( "\xff\xfe<\x00?\x00x\x00m\x00l\x00 \x00v\x00e\x00r\x00s\x00i\x00o" '\x00n\x00=\x00"\x001\x00.\x000\x00"\x00 \x00e\x00n\x00c\x00o\x00d' '\x00i\x00n\x00g\x00=\x00"\x00U\x00C\x00S\x00-\x002\x00"\x00 \x00?' "\x00>\x00\r\x00\n\x00<\x00J\x00A\x00P\x00A\x00N\x00E\x00S\x00E" "\x00>\x00\r\x00\n\x00<\x00T\x00I\x00T\x00L\x00E\x00>\x00\x02\\" "\x80\x95\xb6[\xea0\xb90\xc80 \x00<\x00/\x00T\x00I\x00T\x00L\x00E" "\x00>\x00<\x00/\x00J\x00A\x00P\x00A\x00N\x00E\x00S\x00E\x00>\x00" ) def reverseBytes(s): fmt = str(len(s) // 2) + "H" return struct.pack("<" + fmt, *struct.unpack(">" + fmt, s)) urd = microdom.parseString(reverseBytes(s.encode("UTF-16"))) ud = microdom.parseString(s.encode("UTF-16")) sd = microdom.parseString(s) self.assert_(ud.isEqualToDocument(sd)) self.assert_(ud.isEqualToDocument(urd)) ud = microdom.parseString(j) urd = microdom.parseString(reverseBytes(j2)) sd = microdom.parseString(j2) self.assert_(ud.isEqualToDocument(sd)) self.assert_(ud.isEqualToDocument(urd)) # test that raw text still gets encoded # test that comments get encoded j3 = microdom.parseString(u"<foo/>") hdr = '<?xml version="1.0"?>' div = microdom.lmx().text(u"\u221a", raw=1).node de = j3.documentElement de.appendChild(div) de.appendChild(j3.createComment(u"\u221a")) self.assertEqual(j3.toxml(), hdr + u"<foo><div>\u221a</div><!--\u221a--></foo>".encode("utf8"))
def test_unicodeTolerance(self): import struct s = "<foo><bar><baz /></bar></foo>" j = ('<?xml version="1.0" encoding="UCS-2" ?>\r\n<JAPANESE>\r\n' "<TITLE>\u5c02\u9580\u5bb6\u30ea\u30b9\u30c8 </TITLE></JAPANESE>") j2 = ( b"\xff\xfe<\x00?\x00x\x00m\x00l\x00 \x00v\x00e\x00r\x00s\x00i\x00o" b'\x00n\x00=\x00"\x001\x00.\x000\x00"\x00 \x00e\x00n\x00c\x00o\x00d' b'\x00i\x00n\x00g\x00=\x00"\x00U\x00C\x00S\x00-\x002\x00"\x00 \x00?' b"\x00>\x00\r\x00\n\x00<\x00J\x00A\x00P\x00A\x00N\x00E\x00S\x00E" b"\x00>\x00\r\x00\n\x00<\x00T\x00I\x00T\x00L\x00E\x00>\x00\x02\\" b"\x80\x95\xb6[\xea0\xb90\xc80 \x00<\x00/\x00T\x00I\x00T\x00L\x00E" b"\x00>\x00<\x00/\x00J\x00A\x00P\x00A\x00N\x00E\x00S\x00E\x00>\x00" ) def reverseBytes(s): fmt = str(len(s) // 2) + "H" return struct.pack("<" + fmt, *struct.unpack(">" + fmt, s)) urd = microdom.parseString(reverseBytes(s.encode("UTF-16"))) ud = microdom.parseString(s.encode("UTF-16")) sd = microdom.parseString(s) self.assertTrue(ud.isEqualToDocument(sd)) self.assertTrue(ud.isEqualToDocument(urd)) ud = microdom.parseString(j) urd = microdom.parseString(reverseBytes(j2)) sd = microdom.parseString(j2) self.assertTrue(ud.isEqualToDocument(sd)) self.assertTrue(ud.isEqualToDocument(urd)) # test that raw text still gets encoded # test that comments get encoded j3 = microdom.parseString("<foo/>") hdr = '<?xml version="1.0"?>' div = microdom.lmx().text("\u221a", raw=1).node de = j3.documentElement de.appendChild(div) de.appendChild(j3.createComment("\u221a")) self.assertEqual(j3.toxml(), (hdr + "<foo><div>\u221a</div><!--\u221a--></foo>"))
def testUnicodeTolerance(self): import struct s = '<foo><bar><baz /></bar></foo>' j = ( u'<?xml version="1.0" encoding="UCS-2" ?>\r\n<JAPANESE>\r\n' u'<TITLE>\u5c02\u9580\u5bb6\u30ea\u30b9\u30c8 </TITLE></JAPANESE>') j2 = ( '\xff\xfe<\x00?\x00x\x00m\x00l\x00 \x00v\x00e\x00r\x00s\x00i\x00o' '\x00n\x00=\x00"\x001\x00.\x000\x00"\x00 \x00e\x00n\x00c\x00o\x00d' '\x00i\x00n\x00g\x00=\x00"\x00U\x00C\x00S\x00-\x002\x00"\x00 \x00?' '\x00>\x00\r\x00\n\x00<\x00J\x00A\x00P\x00A\x00N\x00E\x00S\x00E' '\x00>\x00\r\x00\n\x00<\x00T\x00I\x00T\x00L\x00E\x00>\x00\x02\\' '\x80\x95\xb6[\xea0\xb90\xc80 \x00<\x00/\x00T\x00I\x00T\x00L\x00E' '\x00>\x00<\x00/\x00J\x00A\x00P\x00A\x00N\x00E\x00S\x00E\x00>\x00') def reverseBytes(s): fmt = str(len(s) / 2) + 'H' return struct.pack('<' + fmt, *struct.unpack('>' + fmt, s)) urd = microdom.parseString(reverseBytes(s.encode('UTF-16'))) ud = microdom.parseString(s.encode('UTF-16')) sd = microdom.parseString(s) self.assert_(ud.isEqualToDocument(sd)) self.assert_(ud.isEqualToDocument(urd)) ud = microdom.parseString(j) urd = microdom.parseString(reverseBytes(j2)) sd = microdom.parseString(j2) self.assert_(ud.isEqualToDocument(sd)) self.assert_(ud.isEqualToDocument(urd)) # test that raw text still gets encoded # test that comments get encoded j3 = microdom.parseString(u'<foo/>') hdr = '<?xml version="1.0"?>' div = microdom.lmx().text(u'\u221a', raw=1).node de = j3.documentElement de.appendChild(div) de.appendChild(j3.createComment(u'\u221a')) self.assertEquals( j3.toxml(), hdr + u'<foo><div>\u221a</div><!--\u221a--></foo>'.encode('utf8'))
def createShell(self, request, node, data): """Create a `shell' node that will hold the additional form elements, if one is required. """ return lmx(node).table(border="0")
def wvupdate_loginform(self, request, widget, model): microdom.lmx(widget.node).form(action=guard.INIT_PERSPECTIVE, model="form")
def save(self,config_file=None): """Saves to XML config file""" config_file = config_file or self.config_file tree = microdom.lmx('stage') tree['name'] = self.name tree['id'] = self.ID tree['created'] = self.created if self.active: tree['active'] = 'active' if self.splash_message != self.__class__.splash_message: splash = tree.add('splash') splash.text(self.splash_message) #Aaron 1Aug 08 Save the Prop and Backdrop BG colour if self.backgroundPropBgColour != self.__class__.backgroundPropBgColour: bgPropBgColour = tree.add('bgPropBgColour') bgPropBgColour.text(self.backgroundPropBgColour) #Aaron 12 Nov 08 Save the Debug screen Value if self.debugMessages != self.__class__.debugMessages: showDebugScreen = tree.add('showDebugScreen') showDebugScreen.text(self.debugMessages) for x in self.get_avatar_list(): # log.msg("Voices: %s %s " %(x.voice,x.media.voice)) tree.add(self.avatars.typename, media=x.media.file, showname=x.show_name, name=x.name, voice=(x.voice or x.media.voice or '') ) log.msg("avatar list in save method: %s" % self.avatars.typename) # NOTE one day, save player permissions. for x in self.get_prop_list(): tree.add(self.props.typename, media=x.media.file, name=x.name) for x in self.get_backdrop_list(): tree.add(self.backdrops.typename, media=x.media.file, name=x.name) log.msg('stage.save() - adding audio to xml') for x in self.get_audio_list(): log.msg('stage.save() - audio item: %s' %(x)) tree.add(self.audios.typename, media=x.media.file, name=x.name, type=x.media.medium) #Shaun Narayan (02/14/10) - Write all new values to XML. access_string = '' for x in self.access_level_one: access_string += x+',' #Heath Behrens 10/08/2011 - Added so that items can be separated and the last , is removed access_string = access_string.rstrip(',') one = tree.add('access_one') one.text(access_string) access_string = '' for x in self.access_level_two: access_string += x+',' #Heath Behrens 10/08/2011 - Added so that items can be separated and the last , is removed access_string = access_string.rstrip(',') two = tree.add('access_two') two.text(access_string) access_string = '' for x in self.access_level_three: access_string += x+',' #Heath Behrens 10/08/2011 - Added so that items can be separated and the last , is removed access_string = access_string.rstrip(',') three = tree.add('access_three') three.text(access_string) nodeChatBgColour = tree.add('chatBgColour') nodeChatBgColour.text(self.chatBgColour) nodeToolsBgColour = tree.add('toolsBgColour') nodeToolsBgColour.text(self.toolsBgColour) nodePageBgColour = tree.add('pageBgColour') nodePageBgColour.text(self.pageBgColour) save_xml(tree.node, config_file) del tree