Permalink
Browse files

moving spellcheck plugin to git hub

  • Loading branch information...
0 parents commit 3905f57cfd2ae303e027359fe5a918a5f06096a0 @brandonaaron committed Oct 16, 2008
Showing with 190 additions and 0 deletions.
  1. +10 −0 README.markdown
  2. +111 −0 jquery.spellcheck.js
  3. +20 −0 spellcheck.php
  4. BIN spellcheck.png
  5. +49 −0 test.html
@@ -0,0 +1,10 @@
+# jQuery Spellcheck Plugin
+
+A jQuery plugin that adds spellcheck support to inputs.
+
+
+## License
+
+The batch plugin is dual licensed *(just like jQuery)* under the [MIT](http://www.opensource.org/licenses/mit-license.php) and [GPL](http://www.opensource.org/licenses/gpl-license.php) licenses.
+
+Copyright (c) 2007 [Brandon Aaron](http://brandonaaron.net)
@@ -0,0 +1,111 @@
+/*! Copyright (c) 2008 Brandon Aaron (http://brandonaaron.net)
+ * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
+ * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
+ *
+ * Version 0.2-pre
+ */
+(function($){
+
+/**
+ * Creates an instance of a SpellChecker for each matched element.
+ * The SpellChecker has several configurable options.
+ * - lang: the 2 letter language code, defaults to en for english
+ * - events: a space seperated string of events to use, default is 'keypress blur paste'
+ * - autocheck: number of milliseconds to check spelling after a key event, default is 750.
+ * - url: url of the spellcheck service on your server, default is spellcheck.php
+ * - ignorecaps: 1 to ignore words with all caps, 0 to check them
+ * - ignoredigits: 1 to ignore digits, 0 to check them
+ */
+$.fn.spellcheck = function(options) {
+ return this.each(function() {
+ var $this = $(this);
+ if ( !$this.is('[type=password]') && !$(this).data('spellchecker') )
+ $(this).data('spellchecker', new $.SpellChecker(this, options));
+ });
+};
+
+/**
+ * Forces a spell check on an element that has an instance of SpellChecker.
+ */
+$.fn.checkspelling = function() {
+ return this.each(function() {
+ var spellchecker = $(this).data('spellchecker');
+ spellchecker && spellchecker.checkSpelling();
+ });
+};
+
+
+$.SpellChecker = function(element, options) {
+ this.$element = $(element);
+ this.options = $.extend({
+ lang: 'en',
+ autocheck: 750,
+ events: 'keypress blur paste',
+ url: 'spellcheck.php',
+ ignorecaps: 1,
+ ignoredigits: 1
+ }, options);
+ this.bindEvents();
+};
+
+$.SpellChecker.prototype = {
+ bindEvents: function() {
+ if ( !this.options.events ) return;
+ var self = this, timeout;
+ this.$element.bind(this.options.events, function(event) {
+ if ( /^key[press|up|down]/.test(event.type) ) {
+ if ( timeout ) clearTimeout(timeout);
+ timeout = setTimeout(function() { self.checkSpelling(); }, self.options.autocheck);
+ } else
+ self.checkSpelling();
+ });
+ },
+
+ checkSpelling: function() {
+ var prevText = this.text, text = this.$element.val(), self = this;
+ if ( prevText === text ) return;
+ this.text = this.$element.val();
+ $.get(this.options.url, $.extend({ text: this.text }, this.options), function(r) { self.parseResults(r); });
+ },
+
+ parseResults: function(results) {
+ var self = this;
+ this.results = [];
+ $(results).find('c').each(function() {
+ var $this = $(this),
+ offset = $this.attr('o'),
+ length = $this.attr('l');
+ self.results.push({
+ word: self.text.substr(offset, length),
+ suggestions: $this.text().split(/\s/)
+ });
+ });
+ this.displayResults();
+ },
+
+ displayResults: function() {
+ $('#spellcheckresults').remove();
+ if ( !this.results.length ) return;
+ var $container = $('<div id="spellcheckresults"></div>').appendTo('body'),
+ dl = [], self = this, offset = this.$element.offset(), height = this.$element[0].offsetHeight, i, k;
+ for ( i=0; i<this.results.length; i++ ) {
+ var result = this.results[i], suggestions = result.suggestions;
+ dl.push('<dl><dt>'+result.word+'</dt>');
+ for ( k=0; k<suggestions.length; k++ )
+ dl.push('<dd>'+suggestions[k]+'</dd>');
+ dl.push('<dd class="ignore">ignore</dd></dl>');
+ }
+ $container.append(dl.join('')).find('dd').bind('click', function(event) {
+ var $this = $(this), $parent = $this.parent();
+ if ( !$this.is('.ignore') )
+ self.$element.val( self.$element.val().replace( $parent.find('dt').text(), $this.text() ) );
+ $parent.remove();
+ if ( $('#spellcheckresults').is(':empty') )
+ $('#spellcheckresults').remove();
+ this.blur();
+ }).end().css({ top: offset.top + height, left: offset.left });
+ }
+
+};
+
+})(jQuery);
@@ -0,0 +1,20 @@
+<?php
+header("Content-Type: text/xml; charset=utf-8");
+$url="https://www.google.com/tbproxy/spell?lang=" . $_GET['lang'];
+$text = urldecode($_GET['text']);
+
+$body = '<?xml version="1.0" encoding="utf-8" ?>';
+$body .= '<spellrequest textalreadyclipped="0" ignoredups="1" ignoredigits="' . $_GET['ignoredigits'] . '" ignoreallcaps="' . $_GET['ignorecaps'] . '">';
+$body .= '<text>' . $text . '</text>';
+$body .= '</spellrequest>';
+
+$ch = curl_init();
+curl_setopt($ch, CURLOPT_URL, $url);
+curl_setopt ($ch, CURLOPT_POST, 1);
+curl_setopt ($ch, CURLOPT_POSTFIELDS, $body);
+curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
+$contents = curl_exec ($ch);
+curl_close ($ch);
+
+print $contents;
+?>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,49 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
+ "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+ <head>
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
+ <title>test</title>
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript" charset="utf-8"></script>
+ <script src="jquery.spellcheck.js" type="text/javascript" charset="utf-8"></script>
+ <script type="text/javascript" charset="utf-8">
+ jQuery(document).ready(function($) {
+ // $.get("test.php", { lang: 'en', text: "testng spelcheckr testng"}, function(r) { console.log(r); });
+ $('#test1').spellcheck({ events: null });
+ $('#test2, #test3').spellcheck();
+ $('#test4').spellcheck({ events: 'keyup' });
+ $('a.checkspelling').bind('click', function(event) {
+ $(this.hash).checkspelling();
+ this.blur();
+ return false;
+ });
+ });
+ </script>
+ <style type="text/css" media="screen">
+ body { font-size: small; font-family:"lucida Grande", "lucida sans unicode", "myriad pro", arial, helvetica, sans-serif; }
+ input, textarea { width: 250px; }
+ textarea { height: 100px; }
+ #spellcheckresults { position: absolute; background: #fff; margin: 0; border: 1px solid #000; padding: 5px; }
+ #spellcheckresults dl { margin: 0; padding: 0; }
+ #spellcheckresults dl dt { float: left; clear: left; margin: 3px 0; padding: 0; font-weight: bold; }
+ #spellcheckresults dl dd { float: left; margin: 3px 10px; padding: 0; cursor: pointer; color: green; }
+ #spellcheckresults dl dd.ignore { color: red; font-style: italic; }
+
+ a.checkspelling { }
+ a.checkspelling img { margin: 0 5px; border: 0; }
+ </style>
+ </head>
+ <body>
+ <form action="#" method="get">
+ <fieldset>
+ <legend>Testing Spellchecker</legend>
+ <label for="test1">Test: </label><input type="text" name="test1" id="test1" value="Testng ths spelcheckr out."><a href="#test1" class="checkspelling"><img src="spellcheck.png"></a><br>
+ <label for="test2">Test: </label><input type="text" name="test2" id="test2"><br>
+ <label for="test3">Test: </label><input type="password" name="test3" id="test3"><br>
+ <label for="test4">Test: </label><br>
+ <textarea name="test4" id="test4"></textarea>
+ </legend>
+ </fieldset>
+ </form>
+ </body>
+</html>

0 comments on commit 3905f57

Please sign in to comment.