=== added file 'examples/ajaxspellchecker.py' --- examples/ajaxspellchecker.py 1970-01-01 00:00:00 +0000 +++ examples/ajaxspellchecker.py 2007-03-15 01:49:58 +0000 @@ -0,0 +1,63 @@ + +""" Playing with the popular online spellchecking demo at http://www.broken-notebook.com/spell_checker/ """ + +import sys +sys.path.append("..") + +import pykhtml + +def startSpellCheck(browser): + document = browser.document + # check if the javascript stuff is finished -- when it is, + # an anchor with a class name of "check_spelling" will + # exist. + generator = document.getElementsByClass("check_spelling", "a") + anchors = list(generator) + if not anchors: + # if not, try again in 0.2 seconds + pykhtml.timer(0.2, pykhtml.partial(startSpellCheck, browser)) + return + # okay, everything is good to go -- insert the text into + # the box + textBox = document.getElementById("spell_checker1") + textBox.value = "Here we have twow intentionally misplt words" + # and then click the spellcheck link which will do some + # AJAXy stuff + checkSpellingAnchor = anchors[0] + checkSpellingAnchor.click() + seeSpellCheckResponse(browser) + +def seeSpellCheckResponse(browser): + document = browser.document + # check if spellchecking is done (if the textarea has been + # replaced with a div with a class of edit_box) + generator = document.getElementsByClass("edit_box", "div") + divs = list(generator) + if not divs: + # if not, try again in 0.2 seconds + pykhtml.timer(0.2, pykhtml.partial(seeSpellCheckResponse, browser)) + return + spellCheckedContainer = divs[0] + # go through each child in the div + for child in spellCheckedContainer: + if isinstance(child, pykhtml.dom.Text): + # if it's a text node it's a collection of + # correctly spelt words + print "correct:", child.value + else: + # otherwise it's a that is highlighting + # an incorrect word + print "incorrect:", child.text + pykhtml.stopEventLoop() + +def main(): + browser = pykhtml.Browser() + # the browser is passed as a parameter to `login` + # when it is called (when the page has loaded) + browser.load("http://www.broken-notebook.com/spell_checker/", startSpellCheck) + # kick things off + pykhtml.startEventLoop() + +if __name__ == "__main__": + main() +