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


3 comments:

Unknown said...

I realize this probably isn't useful for anyone, but I wanted to write it down somewhere for me.

Anonymous said...

Actually, I'm working with getSelection issue and I did find it useful, so thank you.

Nathan Rosquist said...

Very useful. Thanks for posting your research.