#!/usr/bin/python
"""
$Id: minimizemarkup.py,v 1.2 2006/06/12 10:11:45 dom Exp $

The MinimizeMarkupTestCase implements the validator.testcase.TestCase
interface for the tests related to the following BP:
*  Use terse, efficient markup.


License
-------
Copyright (c) 2006 World Wide Web Consortium, (Massachusetts
Institute of Technology, European Research Consortium for Informatics
and Mathematics, Keio University). All Rights Reserved. This work is
distributed under the W3C Software License [1] in the hope that it
will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[1] http://www.w3.org/Consortium/Legal/copyright-software

"""



from  validator import testcase

# Tests regarding the MINIMIZE BP
class MinimizeMarkupTestCase(testcase.XMLBasedTestCase):
    BpId = ["MINIMIZE"]
    def startDocument(self):
        self._whiteSpaceMatters = 0
        self._textContent = ""


    def startElement(self,name,attrs):
        # pre script and style are the elements identified as preserve
        # in the XHTML DTD
        if name in ["pre","script","style"] or attrs.has_key("xml:space") and attrs["xml:space"]=="preserve" or self._whiteSpaceMatters>0:
            # we increment the counter each time we're in this context
            # to keep track of nested elements
            self._whiteSpaceMatters = self._whiteSpaceMatters + 1


    def endElement(self,name):
        if self._whiteSpaceMatters>0:
            self._whiteSpaceMatters = self._whiteSpaceMatters - 1

        
    def characters(self,content):
        if self._whiteSpaceMatters==0:
            self._textContent = self._textContent + content

        

        
    def endDocument(self):
        import re
        characterCount = len(self._textContent)
        whiteSpaceCount = 0
        whitespace_regexp = re.compile(u"\s+",re.UNICODE)
        iterator = whitespace_regexp.finditer(self._textContent)
        for match in iterator:
            whiteSpaceCount = whiteSpaceCount + len(match.group()) - 1
        if characterCount:
            if whiteSpaceCount:
                # @@@ should there be a bottom proportion to send a warning?
                self.addObservation(Observation("MI1",testcase.Location(self.uri),{"proportion": whiteSpaceCount * 100 / characterCount,"whitespace":whiteSpaceCount }))
            else:
                self.addObservation(Observation("MI2",testcase.Location(self.uri)))                

                
# -------------------------
# Unit tests for this module
import unittest

from validator.testcase import TestResults, Observation, Location, LineColumnLocation
class Tests(unittest.TestCase):
    def _test(self,inp,res):
        a = MinimizeMarkupTestCase()
        self.assertEqual(a.run(inp),res)
    
    def testBase(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/base.xhtml"
        res = TestResults(
            [Observation('MI1',Location(inp),{'proportion': 12, 'whitespace': 8})]
            )
        self._test(inp,res)

    def testNoWhitespace(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/noblanks.xhtml"
        res = TestResults(
            [Observation("MI2",Location(inp))]
            )
        self._test(inp,res)

    
def _test():
    unittest.main()

if __name__ == '__main__':
    _test()



