[javascriptlint-commit] SF.net SVN: javascriptlint:[279] trunk/javascriptlint
Status: Beta
Brought to you by:
matthiasmiller
From: <mat...@us...> - 2009-10-22 05:09:32
|
Revision: 279 http://javascriptlint.svn.sourceforge.net/javascriptlint/?rev=279&view=rev Author: matthiasmiller Date: 2009-10-22 05:09:12 +0000 (Thu, 22 Oct 2009) Log Message: ----------- htmlparse should not have JavaScript-specific logic. Modified Paths: -------------- trunk/javascriptlint/htmlparse.py trunk/javascriptlint/jsl.py trunk/javascriptlint/lint.py Modified: trunk/javascriptlint/htmlparse.py =================================================================== --- trunk/javascriptlint/htmlparse.py 2009-10-13 01:29:52 UTC (rev 278) +++ trunk/javascriptlint/htmlparse.py 2009-10-22 05:09:12 UTC (rev 279) @@ -2,88 +2,50 @@ import HTMLParser import unittest -import jsparse - class _Parser(HTMLParser.HTMLParser): def __init__(self): HTMLParser.HTMLParser.__init__(self) - self._node_positions = jsparse.NodePositions('') - self._script = None - self._scripts = [] + self._tags = [] def handle_starttag(self, tag, attributes): - if tag.lower() == 'script' and not self._script: - offset = self._getoffset() + len(self.get_starttag_text()) - self._script = self._script or { - 'start': offset, - 'attributes': attributes - } + if tag.lower() == 'script': + attr = dict(attributes) + self._tags.append({ + 'type': 'start', + 'lineno': self.lineno, + 'offset': self.offset, + 'len': len(self.get_starttag_text()), + 'attr': attr + }) def handle_endtag(self, tag): - if tag.lower() == 'script' and self._script: - start = self._script['start'] - end = self._getoffset() - script = self.rawdata[start:end] - if jsparse.is_compilable_unit(script): - attr = dict(self._script['attributes']) - self._scripts.append({ - 'script': script, - 'startoffset': start, - 'endoffset': end, - 'startpos': self._node_positions.from_offset(start), - 'endpos': self._node_positions.from_offset(end), - 'src': attr.get('src'), - 'type': attr.get('type') - }) - self._script = None + if tag.lower() == 'script': + self._tags.append({ + 'type': 'end', + 'lineno': self.lineno, + 'offset': self.offset, + }) - def feed(self, data): - self._node_positions = jsparse.NodePositions(self.rawdata + data) - HTMLParser.HTMLParser.feed(self, data) - def unknown_decl(self, data): # Ignore unknown declarations instead of raising an exception. pass - def getscripts(self): - return self._scripts + def gettags(self): + return self._tags - def _getnodepos(self): - line, col = self.getpos() - return jsparse.NodePos(line - 1, col) - - def _getoffset(self): - return self._node_positions.to_offset(self._getnodepos()) - -def findscripts(s): +def findscripttags(s): + """ Note that the lineno is 1-based. + """ parser = _Parser() parser.feed(s) parser.close() - return parser.getscripts() + return parser.gettags() class TestHTMLParse(unittest.TestCase): - def testFindScript(self): - html = """ -<html><body> -<script src=test.js></script> -hi&b -a<script><!-- -var s = '<script></script>'; ---></script> -ok& -..</script> -ok& -</body> -</html> -""" - scripts = [x['script'] for x in findscripts(html)] - self.assertEquals(scripts, [ - "", - "<!--\nvar s = '<script></script>';\n-->" - ]) def testConditionalComments(self): html = """ <!--[if IE]>This is Internet Explorer.<![endif]--> <![if !IE]>This is not Internet Explorer<![endif]> """ - findscripts(html) + findscripttags(html) + Modified: trunk/javascriptlint/jsl.py =================================================================== --- trunk/javascriptlint/jsl.py 2009-10-13 01:29:52 UTC (rev 278) +++ trunk/javascriptlint/jsl.py 2009-10-22 05:09:12 UTC (rev 279) @@ -104,7 +104,7 @@ if options.unittest: suite = unittest.TestSuite(); - for module in [conf, htmlparse, jsparse, util]: + for module in [conf, htmlparse, jsparse, lint, util]: suite.addTest(unittest.findTestCases(module)) runner = unittest.TextTestRunner(verbosity=options.verbosity) Modified: trunk/javascriptlint/lint.py =================================================================== --- trunk/javascriptlint/lint.py 2009-10-13 01:29:52 UTC (rev 278) +++ trunk/javascriptlint/lint.py 2009-10-22 05:09:12 UTC (rev 279) @@ -8,6 +8,7 @@ import jsparse import visitation import warnings +import unittest import util from spidermonkey import tok, op @@ -213,6 +214,45 @@ if global_: return global_ +def _findhtmlscripts(contents): + starttag = None + nodepos = jsparse.NodePositions(contents) + for tag in htmlparse.findscripttags(contents): + if tag['type'] == 'start': + # Ignore nested start tags. + if not starttag: + starttag = tag + src = tag['attr'].get('src') + if src: + yield { + 'type': 'external', + 'src': src + } + elif tag['type'] == 'end': + if not starttag: + continue + + # htmlparse returns 1-based line numbers. Calculate the + # position of the script's contents. + tagpos = jsparse.NodePos(starttag['lineno']-1, starttag['offset']) + tagoffset = nodepos.to_offset(tagpos) + startoffset = tagoffset + starttag['len'] + startpos = nodepos.from_offset(startoffset) + endpos = jsparse.NodePos(tag['lineno']-1, tag['offset']) + endoffset = nodepos.to_offset(endpos) + script = contents[startoffset:endoffset] + + if jsparse.is_compilable_unit(script): + starttag = None + if script.strip(): + yield { + 'type': 'inline', + 'pos': startpos, + 'contents': script, + } + else: + assert False, 'Invalid internal tag type %s' % tag['type'] + def lint_files(paths, lint_error, conf=conf.Conf()): def lint_file(path, kind): def import_script(import_path): @@ -235,12 +275,15 @@ if kind == 'js': script_parts.append((None, contents)) elif kind == 'html': - for script in htmlparse.findscripts(contents): - if script['src']: + for script in _findhtmlscripts(contents): + if script['type'] == 'external': other = import_script(script['src']) lint_cache[normpath].importscript(other) - if script['script'].strip(): - script_parts.append((script['startpos'], script['script'])) + elif script['type'] == 'inline': + script_parts.append((script['pos'], script['contents'])) + else: + assert False, 'Invalid internal script type %s' % \ + script['type'] else: assert False, 'Unsupported file kind: %s' % kind @@ -521,3 +564,24 @@ visitor(node) +class TestLint(unittest.TestCase): + def testFindScript(self): + html = """ +<html><body> +<script src=test.js></script> +hi&b +a<script><!-- +var s = '<script></script>'; +--></script> +ok& +..</script> +ok& +</body> +</html> +""" + scripts = [(x.get('src'), x.get('contents')) + for x in _findhtmlscripts(html)] + self.assertEquals(scripts, [ + ('test.js', None), + (None, "<!--\nvar s = '<script></script>';\n-->") + ]) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |