Wednesday, January 18, 2012

window.getSelection()

This post will focus on test cases for the javascript (well, DOM) function window.getSelection().

Test case:  <DIV id="edit" contenteditable>One Two Three Four</DIV>
1.

  1.  Select "Two".  
    1. window.getSelection().anchorNode.nodeValue="One Two Three Four"
    2. window.getSelection().baseNode.nodeValue="One Two Three Four"
    3. window.getSelection().extentNode.nodeValue="One Two Three Four"
    4. window.getSelection().focusNode.nodeValue="One Two Three Four"
    5. window.getSelection().anchorOffset=4
    6. window.getSelection().baseOffset=4
    7. window.getSelection().extentOffset=7
    8. window.getSelection().focusOffset=7
  2. With "Two" Selected, run document.execCommand('bold').
    1. window.getSelection().anchorNode.nodeValue="Two"
    2. window.getSelection().baseNode.nodeValue="One "
    3. window.getSelection().extentNode.nodeValue="Two"
    4. window.getSelection().focusNode.nodeValue="Two"
    5. window.getSelection().anchorOffset=0
    6. window.getSelection().baseOffset=4
    7. window.getSelection().extentOffset=3
    8. window.getSelection().focusOffset=3
  3. Select "Three".
    1. window.getSelection().anchorNode.nodeValue=" Three Four"
    2. window.getSelection().baseNode.nodeValue=" Three Four"
    3. window.getSelection().extentNode.nodeValue=" Three Four"
    4. window.getSelection().focusNode.nodeValue=" Three Four"
    5. window.getSelection().anchorOffset=1
    6. window.getSelection().baseOffset=1
    7. window.getSelection().extentOffset=6
    8. window.getSelection().focusOffset=6
  4. Select "Four" and run document.execCommand('bold').
    1. window.getSelection().anchorNode.nodeValue="Four"
    2. window.getSelection().baseNode.nodeValue=" Three "
    3. window.getSelection().extentNode.nodeValue=" Four "
    4. window.getSelection().focusNode.nodeValue=" Four "
    5. window.getSelection().anchorOffset=0
    6. window.getSelection().baseOffset=7
    7. window.getSelection().extentOffset=4
    8. window.getSelection().focusOffset=4
  5. Position cursor immediately after "Four" and press enter. Then Select "Four"
    1. window.getSelection().anchorNode.nodeValue="Four"
    2. window.getSelection().baseNode.nodeValue=" Three "
    3. window.getSelection().extentNode.nodeValue=" Four "
    4. window.getSelection().focusNode.nodeValue=" Four "
    5. window.getSelection().anchorOffset=0
    6. window.getSelection().baseOffset=7
    7. window.getSelection().extentOffset=4
    8. window.getSelection().focusOffset=4


Javascript execCommand()

I am writing a program (don't want to say what yet) that is largely reinventing many wheels in the programming world.  I know I am making my life harder than it needs to be, but I am gaining the following:

1. I am learning way more than I would have other wise.
2. I know exactly how every part of my program works, and why it works the way it does.
3. I don't have to pay anyone for anything.
4. I avoid situations like what I encountered tonight.

One of the components of my program is a rich text editor.  I did my research, and basically gave up to download a pre-built editor.  I started in on implementing it, when I discovered the cost to use it was going to be $300ish a year.  Given the budgetary concerns listed in point 3 I went back to the idea of doing it myself.  What I discovered is that all the tutorials I could find were making things way harder than they needed to be.  I started over from scratch and very quickly made a working prototype.  Is it perfect? No.  Do I still need to work through a lot of issues I wouldn't have had to otherwise? Yes.  Would I still be sitting in a corner crying if I hadn't? Yes.

Of interesting note is the fact that one of the top results for execCommand returned a javascript reference for IE 4.

Anyway,  here is some useful reference I've compiled for execCommand() in javascript that doesn't seem to be put in one place in a simple manor anywhere else.  Hope it saves someone the hours it would have saved me.  These are all valid for Chrome 16.  Your mileage will vary with other browsers.

The phrase "current selection" can refer to any length of selection, even 0.  In other words it can also mean current cursor (caret) position.

All references below assume the use of a <DIV> tag with contenteditable set such as:
<DIV id="imeditable" contenteditable></DIV>

All references below to using the default UI need to be updated as they don't work in Chrome. Bah.

The method itself:

execCommand(String aCommandName, Boolean aShowDefaultUI, String aValueArgument)

-returns true on success, false on failure

Commands:

BackColor - Sets the background color of the current selection.

document.execCommand('BackColor', false, '#333333')

Bold - Toggles the current selection between bold and non-bold OR Toggles bold/non-bold for future typing at cursor position.  *note: depending on implementation, the former case will continue in bold for future typing if it is not toggled off, or if the cursor is placed back on a bold section or immediately adjacent.

document.execCommand('Bold')

CreateLink - Inserts a hyperlink on the current selection, or displays a dialog box enabling the user to specify a URL to insert as a hyperlink on the current selection.

document.execCommand('CreateLink', false, 'http://www.example.com')


document.execCommand('CreateLink', true)     //displays user dialog

Delete - Deletes current selection.

document.execCommand('Delete')

FontName - Sets the font family for the current selection OR future typing

document.execCommand('FontName',false,'font name')

FontSize - Sets the font size for the current selection OR future typing

document.execCommand('FontSize',false,'font size')

ForeColor - Sets the foreground (i.e. font) color of the current selection

document.execCommand('ForeColor',false,'#333333')

FormatBlock - Sets the current block format tag. Possible Values: "address", "div", "h1", "h2", "h3", "h4", "h5", "h6", "listing", "p", "pre", "xmp","li", "dt", "dd", "p"

document.execCommand('FormatBlock',false,'pre')

Indent - Increases the indent of the selected text by one indent increment

document.execCommand('Indent')

InsertHorizontalRule - Inserts a Horizontal Rule

document.execCommand('insertHorizontalRule','id for new hr')

InsertImage - Inserts an Image

document.execCommand('InsertImage',true)


document.execCommand('InsertImage',false,'path/filename/image')

InsertOrderedList - Toggles the text selection between an ordered list and a normal block

document.execCommand('InsertOrderedList',false,'id for new ol')

InsertParagraph - Inserts a paragraph break

document.execCommand('InsertParagraph',false,'id for new p')


InsertUnorderedList - Toggles the text selection between an unordered list and a normal block

document.execCommand('InsertUnorderedList',false,'id for new ul')


Italic - Toggles the current selection between italic and non-italic

document.execCommand('Italic')

JustifyCenter - Aligns the current selection to the center

document.execCommand('JustifyCenter')


JustifyFull - Aligns the current selection to be fully justified

document.execCommand('JustifyFull')

JustifyNone - Aligns the current selection to the nothing

document.execCommand('JustifyNone')

JustifyLeft - Aligns the current selection to the left

document.execCommand('JustifyLeft')

JustifyRight - Aligns the current selection to the center

document.execCommand('JustifyRight')

Outdent - Decreases the indentation of the current selection by one indentation increment (i.e. the opposite of indent)

document.execCommand('Outdent')

Print - Opens the print dialog

document.execCommand('Print', true)

Redo - Steps forward one step in the undo history

document.execCommand('Redo')

RemoveFormat - Removes the formatting tags from the current selection

document.execCommand('RemoveFormat')

SelectAll - Selects All

document.execCommand('SelectAll')

Strikethrough - Draws a line through the middle of the current selection

document.execCommand('StrikeThrough')

Subscript - Lowers the current selection to the subscript position

document.execCommand('Subscript')

Superscript - Raises the current selection to the superscript position

document.execCommand('Superscript')

Underline - Toggles the current selection between underlined and not underlined

document.execCommand('Underline')

Undo - Undo the previous action

document.execCommand('Undo')

Unlink - Removes any hyperlink from the current selection

document.execCommand('Unlink')

Unselect - Clears the current selection

document.execCommand('Unselect')



Useful Reference:
Rich-Text Editing in Mozilla
https://developer.mozilla.org/en/Rich-Text_Editing_in_Mozilla

MSDN Command Identifiers
http://msdn.microsoft.com/en-us/library/ms533049(v=vs.85).aspx

WHATWG Dev guide for execCommand
http://developers.whatwg.org/dnd.html#execCommand

HTML Editing APIs
http://dvcs.w3.org/hg/editing/raw-file/tip/editing.html


Welcome Back

I have been wanting to return to blogging for quite some time but have been putting it off as I am in the middle of writing some software that will include blogging capabilities.  However, today it occurred to me that it really is rather silly to not use this blog for at least some of my content given the level of integration with Picasa and G+ that it has.  So here I am.  Hopefully this won't be the only post on here for the next year!

Thursday, January 04, 2007

Eat Alphabet Soup

Thoughts from vacation:

I love My Chemical Romance and +44. Thanks Deb!
I'm just a man,
I'm not a hero
Just a boy
I have to take a break every now and then and remind my self that I am not a failure.

Driving to Phoenix from Visalia isn't that bad if you stop in LA for three days on the way.
Spending 5 hours at Borders and Barnes and Noble the week before your Birthday when everything on your gift list is from those two stores is pure torture.
Claim Jumper makes the best free food.
Uninstalling a domain controller without first removing the computer accounts from it can mess things up.
Carting a 42 inch TV 1600 miles is worth it.
Sam and Melissa have the best cozy apartment.
I miss Swimming.
I had one of the best Birthdays ever.
My plan worked... I ended up with only Border's gift certificates; none to barnes and noble.
I got tons of awesome presents.

Thank you every body who put us up and took the time out of their busy schedules to spend time with us.
Mom and Dad
Rob and Candace
Gma and Gpa
John and Linda
Mary and Tim
Dan and Jennifer
Mike
Kevin White
Jen and Todd
Sharon
Sam and Melissa (special thanks to them for sacraficing their living room to us for 5 days!)

Love you all!



Wednesday, December 20, 2006

I did it

Well, I transfered over to the new non-beta version of blogger today (since today is the official non-beta version release day). So far I can't really see any reason to have done so other than I can log in with my gmail account instead of my blogger account. Oh, and there are labels at the end of my posts now.
The one major feature that could be useful I have no use for: the ability to restrict access to select readers. In other news, Jen should be dancing a happy dance about now.

Edit: Because I have a custom blog layout I don't have a place to view all labels. Oh well. Guess it is time to make a new template.

Thursday, December 07, 2006

Life and stuff

Life has been crazy. I've been working a ton trying to get a new web server up and going. I spent 3 or 4 weeks trying to get it going. you can see it at www.fresnounified.org.

We are doing Christmas in LA and New years in Phoenix. Should be exciting. I need to decide what I want to do for my birthday.

Oh, and I have finals next week. I'll be studying all weekend (aside from picking up something in bakersfield from Deb's parents).

Wednesday, November 22, 2006

$200

God is good! It turns out that we are only going to have to pay $200 for the $3000 worth of car repairs we have! The car is getting a brand new transmission and a brand new radio, both with a new 3 year warrenty. They gave us a good-will warrenty because the transmission really shouldn't go out after only 3.5 years. So we just have to pay a deductable of $200.

Yay!

Monday, November 20, 2006

Yes I like books

http://www.jakemandell.com/tonedeaf/
Check that out. It is a tool for tone-deafness. I just scored 63% after a long day of work when I wasn't really paying attention. I am going to take it again tomorrow when I am a little less tired.

Friday, November 17, 2006

Christmas and Birthday List

BOOKS:

Halo 2 Novel

Halo 1 Triolgy

Halo 2 Graphic Novel (low on my list)

TOYS:
xbox 360 wireless headset

xbox 360 Halo faceplate

xbox 360 HD-DVD player

Gears of War (Game)


Dish HD-DVR

DVD Collections:
Futurama Vol. 3

Futurama Vol. 4

Family Guy Volume 4

RING:
Low Dome TrewTungsten™ Inlaid with Platinum

More BOOKS:
(in order of preference, with complete cycles being prefered)

Artifacts Cycle

1. The Brothers War by Jeff Grubb (1998, ISBN 0-7869-1170-0)
2. Planeswalker by Lynn Abbey (1998, ISBN 0-7869-1182-4)
3. Time Streams by J Robert King (1999, ISBN 0-7869-1344-4)
4. Bloodlines by Loren L Coleman (1999, ISBN 0-7869-1380-0)

Ice Age

1. The Gathering Dark' by Jeff Grubb (1999, ISBN 0-7869-1357-6)
2. Eternal Ice by Jeff Grubb (2000, ISBN 0-7869-1562-5)
3. The Shattered Alliance by Jeff Grubb (2000, ISBN 0-7869-1403-3)

Invasion

1. Invasion by J Robert King (2000, ISBN 0-7869-1438-6)
2. Planeshift by J Robert King (2001, ISBN 0-7869-1802-0)
3. Apocalypse by J Robert King (2001, ISBN 0-7869-1880-2)

Novels

1. Arena by William R. Forstchen (1994, ISBN 0-06-105424-0)
2. Whispering Woods by Clayton Emery (1995, ISBN 0-06-105418-6)
3. Shattered Chains by Clayton Emery (1995, ISBN 0-06-105419-4)
4. The Cursed Land by Teri McLaren (1995, ISBN 0-06-105016-4)
5. The Prodigal Sorcerer by Mark C Sumner (1995, ISBN 0-06-105476-3)
6. Final Sacrifice by Clayton Emery (1995, ISBN 0-7522-0217-0)
7. Ashes of the Sun by Hanovi Braddock (1996, ISBN 0-06-105649-9)
8. Song of Time by Teri McLaren (199, ISBN 0-06-105622-7)
9. And Peace Shall Sleep by Sonia Orin Lyris (1996, ISBN 0-06-105619-7)
10. Dark Legacy by Robert E Vardeman (1996, ISBN 0-06-105697-9)
11. The Thran by J Robert King (1999, ISBN 0-7869-1600-1)

Legends Cycle

1. Johan by Clayton Emery (2001, ISBN 0-7869-1803-9)
2. Jedit by Clayton Emery (2001, ISBN 0-7869-1907-8)
3. Hazezon by Clayton Emery (2002, ISBN 0-7869-2792-5)

Legends Cycle 2

1. Assassin's Blade by Scott McGough (2002, ISBN 0-7869-2830-1)
2. Emperor's Fist by Scott McGough (2003, ISBN 0-7869-2935-9)
3. Champion's Trial by Scott McGough (2003, ISBN 0-7869-3015-2)

Masquerade

1. Mercadian Masques by Francis Lebaron (1999, ISBN 0-7869-1188-3)
2. Nemesis by Paul B. Thompson (2000, ISBN 0-7869-1188-3)
3. Prophecy by Vance Moore (2000, ISBN 0-7869-1570-6)

Mirrodin Cycle

1. The Moons of Mirrodin by Will McDermott (2003, ISBN 0-7869-2995-2)
2. The Darksteel Eye by Jess Lebow (2004, ISBN 0-7869-3140-X)
3. The Fifth Dawn by Cory Herndon (2004, ISBN 0-7869-3205-8)

Odyssey Cycle

1. Odyssey by Vance Moore (2001, ISBN 0-7869-1900-0)
2. Chainer's Torment by Scott McGough (2002, ISBN 0-7869-2696-1)
3. Judgment by Will McDermott (2002, ISBN 0-7869-2743-7)

Onslaught Cycle

1. Onslaught by J Robert King (2002, ISBN 0-7869-2801-8)
2. Legions by J Robert King (2003, ISBN 0-7869-2914-6)
3. Scourge by J Robert King (2003, ISBN 0-7869-2956-1)

Ravnica Cycle

1. Ravnica by Cory J. Herndon (2005, ISBN 0-7869-3792-0)
2. Guildpact by Cory J. Herndon (2006, ISBN 0-7869-3989-3)
3. Dissension by Cory J. Herndon (2006, ISBN 0-7869-4001-8)

Anthologies

1. Tapestries by Kathy Ice (1995, ISBN 0-06-105308-2)
2. Distant Planes by Kathy Ice (1995, ISBN 0-06-105765-7)
3. Rath and Storm by Peter Archer (1998, ISBN 0-7869-1175-1)
4. The Colors of Magic by Jess Lebow (1999, ISBN 0-7869-1323-1)
5. Myths of Magic by Jess Lebow (2000, ISBN 0-7869-1529-3)
6. Dragons of Magic by J. Robert King (2001, ISBN 0-7869-1872-1)
7. The Sectrets of Magic by J. Robert King (2002, ISBN 0-7869-2710-0)
8. Monsters of Magic by J. Robert King (2003, ISBN 0-7869-2983-9)

Kamigawa

1. Outlaw: Champions of Kamigawa by Scott McGough (2004, ISBN 0-7869-3357-7)
2. Heretic: Betrayers of Kamigawa by Scott McGough (2005, ISBN 0-7869-3575-8)
3. Guardian: Saviors of Kamigawa by Scott McGough (2005, ISBN 0-7869-3786-6)