Ejemplo n.º 1
0
    def _getProperties(self):
        if not self.firstTime:
            return
        self.firstTime = False
        elem = self._getElement()
        if not elem or not elem.style:
            return

#        print "---"
#        print "IN", len(elem.innerText)
#        print "TR", len(self.mshtmlIHTMLTextRange.text)
#        print "IN}%s{" % elem.innerText
#        print "TR}%s{" % self.mshtmlIHTMLTextRange.text
        bgColor = None
        color = None
        fontSize = None
        fontName = None
        self.hasCSSFontSize = False
        self.hasCSSFontName = False

        # walk up the ladder and build css font properties
        while (elem != None):
            if not self.hasCSSFontSize and getNoneString(elem.style.fontSize):
                self.hasCSSFontSize = True
            if not self.hasCSSFontName and getNoneString(
                    elem.style.fontFamily):
                self.hasCSSFontName = True

            if not bgColor:
                bgColor = getNoneString(elem.style.backgroundColor)
            if not color:
                color = getNoneString(elem.style.color)
            if not fontName:
                fontName = self.mshtmlControl.queryCommandValue(
                    u"FontName")  #$NON-NLS-1$
            if not fontSize:
                fontSize = getNoneString(elem.style.fontSize)
            if elem.tagName == u"BODY":  #$NON-NLS-1$
                break
            if (color and bgColor and fontSize and fontName):
                break
            elem = elem.parentElement
        # end while
        if color:
            self.cssColor = ZCssColor(color)
        if bgColor:
            self.cssBgColor = ZCssColor(bgColor)
        self.fontInfo = ZCssFontInfo()
        self.fontInfo.setFontName(fontName)
        self.fontInfo.setFontSize(fontSize)
Ejemplo n.º 2
0
    def _getProperties(self):
        if not self.firstTime:
            return
        self.firstTime = False
        elem = self._getElement()
        if not elem or not elem.style:
            return

#        print "---"
#        print "IN", len(elem.innerText)
#        print "TR", len(self.mshtmlIHTMLTextRange.text)
#        print "IN}%s{" % elem.innerText
#        print "TR}%s{" % self.mshtmlIHTMLTextRange.text
        bgColor = None
        color = None
        fontSize = None
        fontName = None
        self.hasCSSFontSize = False
        self.hasCSSFontName = False

        # walk up the ladder and build css font properties
        while (elem != None):
            if not self.hasCSSFontSize and getNoneString(elem.style.fontSize):
                self.hasCSSFontSize = True
            if not self.hasCSSFontName and getNoneString(elem.style.fontFamily):
                self.hasCSSFontName = True

            if not bgColor:
                bgColor = getNoneString( elem.style.backgroundColor )
            if not color:
                color = getNoneString(elem.style.color)
            if not fontName:
                fontName = self.mshtmlControl.queryCommandValue(u"FontName") #$NON-NLS-1$
            if not fontSize:
                fontSize = getNoneString(elem.style.fontSize)
            if elem.tagName == u"BODY": #$NON-NLS-1$
                break
            if (color and bgColor and fontSize and fontName):
                break
            elem = elem.parentElement
        # end while
        if color:
            self.cssColor = ZCssColor(color)
        if bgColor:
            self.cssBgColor = ZCssColor(bgColor)
        self.fontInfo = ZCssFontInfo()
        self.fontInfo.setFontName(fontName)
        self.fontInfo.setFontSize(fontSize)
Ejemplo n.º 3
0
 def getFontInfo(self):
     fontInfo = ZCssFontInfo()
     fontInfo.setFontName(self.getFontName())
     fontInfo.setFontSize(self.getFontSize())
     return fontInfo
Ejemplo n.º 4
0
class ZMshtmlEditControlStyleContext(IZXHTMLEditControlStyleContext):

    def __init__(self, mshtmlControl, mshtmlIHTMLElement, mshtmlIHTMLTextRange):
        self.mshtmlControl = mshtmlControl
        self.mshtmlIHTMLElement = mshtmlIHTMLElement
        self.mshtmlIHTMLTextRange = mshtmlIHTMLTextRange
        self.cssColor = None
        self.cssBgColor = None
        self.fontInfo = None
        self.hasCSSFontSize = False
        self.hasCSSFontName = False
        self.firstTime = True
    # end __init__

    def _getElement(self):
        elem = None
        if self.mshtmlIHTMLTextRange:
            elem = self.mshtmlIHTMLTextRange.parentElement()
        else:
            elem = self.mshtmlIHTMLElement
        return elem
    # end _getElement()

    def _getProperties(self):
        if not self.firstTime:
            return
        self.firstTime = False
        elem = self._getElement()
        if not elem or not elem.style:
            return

#        print "---"
#        print "IN", len(elem.innerText)
#        print "TR", len(self.mshtmlIHTMLTextRange.text)
#        print "IN}%s{" % elem.innerText
#        print "TR}%s{" % self.mshtmlIHTMLTextRange.text
        bgColor = None
        color = None
        fontSize = None
        fontName = None
        self.hasCSSFontSize = False
        self.hasCSSFontName = False

        # walk up the ladder and build css font properties
        while (elem != None):
            if not self.hasCSSFontSize and getNoneString(elem.style.fontSize):
                self.hasCSSFontSize = True
            if not self.hasCSSFontName and getNoneString(elem.style.fontFamily):
                self.hasCSSFontName = True

            if not bgColor:
                bgColor = getNoneString( elem.style.backgroundColor )
            if not color:
                color = getNoneString(elem.style.color)
            if not fontName:
                fontName = self.mshtmlControl.queryCommandValue(u"FontName") #$NON-NLS-1$
            if not fontSize:
                fontSize = getNoneString(elem.style.fontSize)
            if elem.tagName == u"BODY": #$NON-NLS-1$
                break
            if (color and bgColor and fontSize and fontName):
                break
            elem = elem.parentElement
        # end while
        if color:
            self.cssColor = ZCssColor(color)
        if bgColor:
            self.cssBgColor = ZCssColor(bgColor)
        self.fontInfo = ZCssFontInfo()
        self.fontInfo.setFontName(fontName)
        self.fontInfo.setFontSize(fontSize)
    # end _getProperties()

    def applyStyle(self, cssFontInfo, cssColor, cssBgColor):
        elem = self._getElement()
        if not elem or not elem.style or not self.mshtmlIHTMLTextRange:
            return

        removeFont = self.getFontInfo().getFontName() and not cssFontInfo.getFontName()
        removeSize = self.getFontInfo().getFontSize() and not cssFontInfo.getFontSize()
        removeColor = self.getColor() and not cssColor
        removeBackground = self.getBackgroundColor() and not cssBgColor
        handled = True
        if self.mshtmlIHTMLTextRange.text == elem.innerText and elem.tagName != u"BODY" and elem.tagName != u"P": #$NON-NLS-2$ #$NON-NLS-1$
            # apply to currently selected element
            if removeFont:
                # reset font name
                elem.style.fontFamily = u'' #$NON-NLS-1$
            elif cssFontInfo.getFontName() and cssFontInfo.getFontName() != self.getFontInfo().getFontName():
                # set new font size
                elem.style.fontFamily = cssFontInfo.getFontName()

            if removeSize:
                # reset font size
                elem.style.fontSize = u'' #$NON-NLS-1$
            elif cssFontInfo.getFontSize() and cssFontInfo.getFontSize() != self.getFontInfo().getFontSize():
                # set new font size
                elem.style.fontSize = cssFontInfo.getFontSize()

            if removeColor:
                elem.style.color = u"" #$NON-NLS-1$
            elif cssColor and self.getColor() and cssColor.getCssColor() != self.getColor().getCssColor():
                elem.style.color = cssColor.getCssColor()
            elif cssColor:
                elem.style.color = cssColor.getCssColor()

            if removeBackground:
                elem.style.backgroundColor = u"" #$NON-NLS-1$
            elif cssBgColor and self.getBackgroundColor() and cssBgColor.getCssColor() != self.getBackgroundColor().getCssColor():
                elem.style.backgroundColor = cssBgColor.getCssColor()
            elif cssBgColor:
                elem.style.backgroundColor = cssBgColor.getCssColor()
            # Remove SPAN tag if there is no style applied.
            if elem.tagName == u"SPAN" and not ( getNoneString(elem.style.cssText) or getNoneString(elem.className) ): #$NON-NLS-1$
                elem.outerHTML = elem.innerHTML

        elif self.mshtmlIHTMLTextRange.htmlText:
            # apply on the text range
            s = u"" #$NON-NLS-1$
            if elem.outerHTML == self.mshtmlIHTMLTextRange.htmlText:
                s = elem.innerHTML
            else:
                s = self.mshtmlIHTMLTextRange.htmlText

            style = u''    #$NON-NLS-1$
            if cssFontInfo.getFontName() and cssFontInfo.getFontName() != self.getFontInfo().getFontName():
                style = style + u"font-family:" + cssFontInfo.getFontName() + u"; " #$NON-NLS-2$ #$NON-NLS-1$
            if cssFontInfo.getFontSize() and cssFontInfo.getFontSize() != self.getFontInfo().getFontSize():
                style = style + u"font-size:" + cssFontInfo.getFontSize() + u"; " #$NON-NLS-2$ #$NON-NLS-1$
            if cssColor and (not self.getColor() or ( cssColor.getCssColor() != self.getColor().getCssColor() ) ):
                style = style + u"color:" + cssColor.getCssColor() + u"; " #$NON-NLS-2$ #$NON-NLS-1$
            if cssBgColor and (not self.getBackgroundColor() or (cssBgColor.getCssColor() != self.getBackgroundColor().getCssColor()) ):
                style = style + u"background-color:" + cssBgColor.getCssColor() + u"; " #$NON-NLS-2$ #$NON-NLS-1$
            if style:
                html = u'<SPAN style="%s" >%s</SPAN>' % (style, s) #$NON-NLS-1$
                self.mshtmlIHTMLTextRange.pasteHTML(html)
        else:
            handled = False
        if handled:
            self.mshtmlControl._fireContentModified()
    # end _setProperties

    def getFontInfo(self):
        if not self.fontInfo:
            self._getProperties()
        return self.fontInfo
    # end getFontInfo

    def setFontInfo(self, fontInfo):
        self.fontInfo = fontInfo
        self.applyStyle(fontInfo, None, None)
    # end setFontInfo

    def getColor(self):
        if not self.cssColor:
            self._getProperties()
        return self.cssColor
    # end getColor

    def setColor(self, cssColor):
        self.cssColor = cssColor
        self.applyStyle(None, cssColor, None)
    # end setColor

    def getBackgroundColor(self):
        if not self.cssBgColor:
            self._getProperties()
        return self.cssBgColor
    # end getBackgroundColor

    def setBackgroundColor(self, cssColor):
        self.cssBgColor = cssColor
        self.applyStyle(None, None, cssColor)
Ejemplo n.º 5
0
class ZMshtmlEditControlStyleContext(IZXHTMLEditControlStyleContext):
    def __init__(self, mshtmlControl, mshtmlIHTMLElement,
                 mshtmlIHTMLTextRange):
        self.mshtmlControl = mshtmlControl
        self.mshtmlIHTMLElement = mshtmlIHTMLElement
        self.mshtmlIHTMLTextRange = mshtmlIHTMLTextRange
        self.cssColor = None
        self.cssBgColor = None
        self.fontInfo = None
        self.hasCSSFontSize = False
        self.hasCSSFontName = False
        self.firstTime = True

    # end __init__

    def _getElement(self):
        elem = None
        if self.mshtmlIHTMLTextRange:
            elem = self.mshtmlIHTMLTextRange.parentElement()
        else:
            elem = self.mshtmlIHTMLElement
        return elem

    # end _getElement()

    def _getProperties(self):
        if not self.firstTime:
            return
        self.firstTime = False
        elem = self._getElement()
        if not elem or not elem.style:
            return

#        print "---"
#        print "IN", len(elem.innerText)
#        print "TR", len(self.mshtmlIHTMLTextRange.text)
#        print "IN}%s{" % elem.innerText
#        print "TR}%s{" % self.mshtmlIHTMLTextRange.text
        bgColor = None
        color = None
        fontSize = None
        fontName = None
        self.hasCSSFontSize = False
        self.hasCSSFontName = False

        # walk up the ladder and build css font properties
        while (elem != None):
            if not self.hasCSSFontSize and getNoneString(elem.style.fontSize):
                self.hasCSSFontSize = True
            if not self.hasCSSFontName and getNoneString(
                    elem.style.fontFamily):
                self.hasCSSFontName = True

            if not bgColor:
                bgColor = getNoneString(elem.style.backgroundColor)
            if not color:
                color = getNoneString(elem.style.color)
            if not fontName:
                fontName = self.mshtmlControl.queryCommandValue(
                    u"FontName")  #$NON-NLS-1$
            if not fontSize:
                fontSize = getNoneString(elem.style.fontSize)
            if elem.tagName == u"BODY":  #$NON-NLS-1$
                break
            if (color and bgColor and fontSize and fontName):
                break
            elem = elem.parentElement
        # end while
        if color:
            self.cssColor = ZCssColor(color)
        if bgColor:
            self.cssBgColor = ZCssColor(bgColor)
        self.fontInfo = ZCssFontInfo()
        self.fontInfo.setFontName(fontName)
        self.fontInfo.setFontSize(fontSize)

    # end _getProperties()

    def applyStyle(self, cssFontInfo, cssColor, cssBgColor):
        elem = self._getElement()
        if not elem or not elem.style or not self.mshtmlIHTMLTextRange:
            return

        removeFont = self.getFontInfo().getFontName(
        ) and not cssFontInfo.getFontName()
        removeSize = self.getFontInfo().getFontSize(
        ) and not cssFontInfo.getFontSize()
        removeColor = self.getColor() and not cssColor
        removeBackground = self.getBackgroundColor() and not cssBgColor
        handled = True
        if self.mshtmlIHTMLTextRange.text == elem.innerText and elem.tagName != u"BODY" and elem.tagName != u"P":  #$NON-NLS-2$ #$NON-NLS-1$
            # apply to currently selected element
            if removeFont:
                # reset font name
                elem.style.fontFamily = u''  #$NON-NLS-1$
            elif cssFontInfo.getFontName() and cssFontInfo.getFontName(
            ) != self.getFontInfo().getFontName():
                # set new font size
                elem.style.fontFamily = cssFontInfo.getFontName()

            if removeSize:
                # reset font size
                elem.style.fontSize = u''  #$NON-NLS-1$
            elif cssFontInfo.getFontSize() and cssFontInfo.getFontSize(
            ) != self.getFontInfo().getFontSize():
                # set new font size
                elem.style.fontSize = cssFontInfo.getFontSize()

            if removeColor:
                elem.style.color = u""  #$NON-NLS-1$
            elif cssColor and self.getColor(
            ) and cssColor.getCssColor() != self.getColor().getCssColor():
                elem.style.color = cssColor.getCssColor()
            elif cssColor:
                elem.style.color = cssColor.getCssColor()

            if removeBackground:
                elem.style.backgroundColor = u""  #$NON-NLS-1$
            elif cssBgColor and self.getBackgroundColor(
            ) and cssBgColor.getCssColor() != self.getBackgroundColor(
            ).getCssColor():
                elem.style.backgroundColor = cssBgColor.getCssColor()
            elif cssBgColor:
                elem.style.backgroundColor = cssBgColor.getCssColor()
            # Remove SPAN tag if there is no style applied.
            if elem.tagName == u"SPAN" and not (
                    getNoneString(elem.style.cssText)
                    or getNoneString(elem.className)):  #$NON-NLS-1$
                elem.outerHTML = elem.innerHTML

        elif self.mshtmlIHTMLTextRange.htmlText:
            # apply on the text range
            s = u""  #$NON-NLS-1$
            if elem.outerHTML == self.mshtmlIHTMLTextRange.htmlText:
                s = elem.innerHTML
            else:
                s = self.mshtmlIHTMLTextRange.htmlText

            style = u''  #$NON-NLS-1$
            if cssFontInfo.getFontName() and cssFontInfo.getFontName(
            ) != self.getFontInfo().getFontName():
                style = style + u"font-family:" + cssFontInfo.getFontName(
                ) + u"; "  #$NON-NLS-2$ #$NON-NLS-1$
            if cssFontInfo.getFontSize() and cssFontInfo.getFontSize(
            ) != self.getFontInfo().getFontSize():
                style = style + u"font-size:" + cssFontInfo.getFontSize(
                ) + u"; "  #$NON-NLS-2$ #$NON-NLS-1$
            if cssColor and (
                    not self.getColor() or
                (cssColor.getCssColor() != self.getColor().getCssColor())):
                style = style + u"color:" + cssColor.getCssColor(
                ) + u"; "  #$NON-NLS-2$ #$NON-NLS-1$
            if cssBgColor and (not self.getBackgroundColor() or
                               (cssBgColor.getCssColor() !=
                                self.getBackgroundColor().getCssColor())):
                style = style + u"background-color:" + cssBgColor.getCssColor(
                ) + u"; "  #$NON-NLS-2$ #$NON-NLS-1$
            if style:
                html = u'<SPAN style="%s" >%s</SPAN>' % (style, s
                                                         )  #$NON-NLS-1$
                self.mshtmlIHTMLTextRange.pasteHTML(html)
        else:
            handled = False
        if handled:
            self.mshtmlControl._fireContentModified()

    # end _setProperties

    def getFontInfo(self):
        if not self.fontInfo:
            self._getProperties()
        return self.fontInfo

    # end getFontInfo

    def setFontInfo(self, fontInfo):
        self.fontInfo = fontInfo
        self.applyStyle(fontInfo, None, None)

    # end setFontInfo

    def getColor(self):
        if not self.cssColor:
            self._getProperties()
        return self.cssColor

    # end getColor

    def setColor(self, cssColor):
        self.cssColor = cssColor
        self.applyStyle(None, cssColor, None)

    # end setColor

    def getBackgroundColor(self):
        if not self.cssBgColor:
            self._getProperties()
        return self.cssBgColor

    # end getBackgroundColor

    def setBackgroundColor(self, cssColor):
        self.cssBgColor = cssColor
        self.applyStyle(None, None, cssColor)
Ejemplo n.º 6
0
 def getFontInfo(self):
     fontInfo = ZCssFontInfo()
     fontInfo.setFontName( self.getFontName() )
     fontInfo.setFontSize( self.getFontSize())
     return fontInfo