Commits

Walter Dörwald committed 2afee5f

Bildgrößenberechnung wird jetzt in asString() gemacht.

Das hat den Vorteil, daß man schnelleren Code erzeugen kann.

Vorher:
e = html.div(html.img(...),gurk.hurz()).asHTML().asString()

Jetzt:
e = html.div(html.img(...),gurk.hurz().asHTML()).asString()

  • Participants
  • Parent commits a296c06

Comments (0)

Files changed (2)

File xist/html.py

 	attrHandlers = xsc.appendDict(attrs,{ "src" : xsc.URLAttr , "alt" : xsc.TextAttr , "longdesc" : xsc.TextAttr , "width" : xsc.TextAttr , "height" : xsc.TextAttr , "usemap" : xsc.TextAttr , "ismap" : xsc.TextAttr })
 	attrHandlers = xsc.appendDict(attrHandlers,{ "name" : xsc.TextAttr , "border" : xsc.TextAttr , "align" : xsc.TextAttr , "hspace" : xsc.TextAttr , "vspace" : xsc.TextAttr , "lowsrc" : xsc.URLAttr }) # deprecated
 
-	def asHTML(self):
-		e = self.clone()
-		e.addImageSizeAttributes("src")
-
-		return e
+	def asString(self,XHTML = None):
+		return self._asStringWithImageSize(XHTML,"src","width","height")
 
 	def asPlainString(self):
 		if self.hasAttr("alt"):
 				v.append([nest,self.endloc,elementno,self._str(brackets = 1,slash = -1,ansi = ansi)])
 		return v
 
-	def asString(self,XHTML = None):
+	def _asStringWithImageSize(self,XHTML = None,imgattr = None,widthattr = None,heightattr = None):
+		"""
+		<par noindent>generates a string representing the element and adds width and height attributes in the process
+		The URL for the image is fetched for the attribute named <argref>imgattr</argref>.
+		If the attributes are already there, they are taken as a formatting template with the size
+		passed in as a dictionary with the keys <code>"width"</code> and <code>"height"</code>,
+		i.e. you could make your image twice as wide with <code>width="2*%(width)d"</code>.</par>
+
+		<par>If <argref>imgattr</argref> is <code>None</code> no image size attribute generation
+		will be done.</par>
+		"""
+
 		v = []
 		v.append("<")
 		v.append(self.elementname) # requires that the element is registered via registerElement()
+		if imgattr is not None:
+			size = self[imgattr].asHTML().ImageSize()
+			sizedict = { "width": size[0], "height": size[1] }
+		else:
+			size = None
 		for attr in self.attrs.keys():
 			v.append(' ')
 			v.append(attr)
 			value = self[attr]
 			if len(value):
 				v.append('="')
-				v.append(value.asString(XHTML))
+				if size is not None and attr==widthattr:
+					try:
+						v.append(str(eval(self[widthattr].asPlainString() % sizedict)))
+					except:
+						raise ImageSizeFormatError(self,widthattr)
+				elif size is not None and attr==heightattr:
+					try:
+						v.append(str(eval(self[heightattr].asPlainString() % sizedict)))
+					except:
+						raise ImageSizeFormatError(self,heightattr)
+				else:
+					v.append(value.asString(XHTML))
+				v.append('"')
+		if size is not None:
+			if not self.hasAttr(widthattr):
+				v.append(' ')
+				v.append(widthattr)
+				v.append('="')
+				v.append(str(size[0]))
+				v.append('"')
+		if not self.hasAttr(heightattr):
+				v.append(' ')
+				v.append(heightattr)
+				v.append('="')
+				v.append(str(size[1]))
 				v.append('"')
 		if len(self):
 			if self.empty:
 
 		return string.join(v,"")
 
+	def asString(self,XHTML = None):
+		return self._asStringWithImageSize(XHTML)
+
 	def __getitem__(self,index):
 		"""
 		returns an attribute or one of the content nodes depending on whether
 				v.append(strQuote(ansi = ansi))
 		return string.join(v,"")
 
-	def addImageSizeAttributes(self,imgattr,widthattr = "width",heightattr = "height"):
-		"""
-		add width and height attributes to the element for the image that can be found in the attribute
-		<argref>imgattr</argref>. If the attributes are already there, they are taken as a formatting
-		template with the size passed in as a dictionary with the keys <code>"width"</code> and <code>"height"</code>,
-		i.e. you could make your image twice as wide with <code>width="%(width)d*2"</code>.
-		"""
-
-		if self.hasAttr(imgattr):
-			size = self[imgattr].ImageSize()
-			sizedict = { "width": size[0], "height": size[1] }
-			if size is not None: # the size was retrieved so we can use it
-				if self.hasAttr(widthattr):
-					try:
-						self[widthattr] = eval(self[widthattr].asPlainString() % sizedict)
-					except:
-						raise ImageSizeFormatError(self,widthattr)
-				else:
-					self[widthattr] = size[0]
-				if self.hasAttr(heightattr):
-					try:
-						self[heightattr] = eval(self[heightattr].asPlainString() % sizedict)
-					except:
-						raise ImageSizeFormatError(self,heightattr)
-				else:
-					self[heightattr] = size[1]
-
 	def compact(self):
 		node = self.__class__(self.content.compact())
 		for attr in self.attrs.keys():