anchor.js 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. module( 'plugins.anchor' );
  2. test( '插入锚点后切换源码', function() {
  3. var editor = te.obj[0];
  4. var range = te.obj[1];
  5. var body = editor.body;
  6. stop();
  7. var br = baidu.editor.browser.ie ? '&nbsp;' : '<br />'; //1.2版本,ie中‘’-〉'&nbsp;'
  8. setTimeout( function() {
  9. editor.setContent( '<p>' + br + '</p>' );
  10. range.setStart( body.firstChild, 0 ).collapse( 1 ).select();
  11. editor.execCommand( 'anchor', 'hello' );
  12. ua.checkHTMLSameStyle( '<img anchorname="hello" class="anchorclass">' + br, editor.document, body.firstChild, '检查锚点html' ); //1.2版本后,在img前有的不可见字符没有删去,这里改成之比较img内的内容
  13. ok(body.getElementsByTagName('img')[0].attributes['anchorname'].nodeValue=="hello"&&body.getElementsByTagName('img')[0].attributes['class'].nodeValue=="anchorclass",'检查锚点');
  14. editor.execCommand( 'source' ); /*切到源码模式下会有一个超时*/
  15. setTimeout( function() {
  16. var tas = editor.iframe.parentNode.getElementsByTagName( 'textarea' );
  17. if(ua.browser.webkit){
  18. // ok( editor.iframe.nextSibling.textContent.indexOf( '<a name="hello"' ) !=-1, '查看是否转换成功' );
  19. }
  20. else{
  21. ok( tas[0].value.indexOf( '<a name="hello"' ) != -1 || tas[0].value.indexOf( '<a anchorname="1"' ) != -1, '查看是否转换成功' );
  22. }
  23. /*没办法比,看上去一样,但是一个42个字符,一个48个字符
  24. * ok((tas[0].value=='<p><a name="hello" anchorname="1"></a></p>')||(tas[0].value=='<p><a anchorname="1" name="hello"></a></p>'),'检查源码');*/
  25. editor.execCommand( 'source' );
  26. ua.checkHTMLSameStyle( '<img anchorname="hello" class="anchorclass">' + br, editor.document, body.firstChild, '检查锚点html' );
  27. setTimeout( function() {
  28. start();
  29. }, 500 );
  30. }, 200);
  31. }, 20 );
  32. } );
  33. test( '在源码模式设置超链接的name属性,切换到编辑器模式检查超链接是否变为锚点', function() {
  34. var editor = te.obj[0];
  35. var body = editor.body;
  36. stop();
  37. setTimeout(function(){
  38. editor.setContent( '' );
  39. setTimeout( function() {
  40. editor.execCommand( 'source' );
  41. setTimeout( function() {
  42. var ta = editor.iframe.parentNode.getElementsByTagName( 'textarea' )[0];
  43. ta.value = '<p><a name="source" anchorname="1"></a></p>'; /*这种情况认为是锚点*/
  44. setTimeout( function() {
  45. editor.execCommand( 'source' );
  46. ua.checkHTMLSameStyle( '<img anchorname="source" class="anchorclass">', editor.document, body.firstChild, '检查锚点html' );
  47. start();
  48. }, 100 );
  49. }, 100 );
  50. }, 100 );
  51. },100);
  52. } );
  53. test( '在源码模式设置超链接没有name属性,切换到编辑器模式检查超链接不变为锚点', function() {
  54. var editor = te.obj[0];
  55. editor.setContent( '' );
  56. var body = editor.body;
  57. stop();
  58. setTimeout( function() {
  59. editor.execCommand( 'source' );
  60. setTimeout( function() {
  61. var ta = editor.iframe.parentNode.getElementsByTagName( 'textarea' )[0];
  62. ta.value = '<p><a name="source" href="www.baidu.com">你好</a></p>';
  63. setTimeout( function() {
  64. editor.execCommand( 'source' );
  65. ua.manualDeleteFillData(editor.body);
  66. // equal( body.firstChild.firstChild.tagName.toLowerCase(), 'a', 'a标签不会转化' );
  67. equal( body.firstChild.lastChild.tagName.toLowerCase(), 'a', 'a标签不会转化' ); //兼容opera
  68. start();
  69. }, 50 );
  70. }, 10 );
  71. }, 20 );
  72. } );
  73. test( '已存在锚点', function() {
  74. var editor = te.obj[0];
  75. var range = te.obj[1];
  76. var body = editor.body;
  77. var br = baidu.editor.browser.ie ? '&nbsp;' : '<br />';
  78. editor.setContent( '<p><img anchorname="1" class="anchorclass"/></p>' );
  79. range.selectNode(body.firstChild).select();
  80. editor.execCommand( 'anchor', 'hello' );
  81. var name=body.firstChild.firstChild.getAttribute('anchorname');
  82. equal(name, 'hello', '更改name');
  83. editor.setContent( '<p><img anchorname="1" class="anchorclass"/></p>' );
  84. range.selectNode(body.firstChild).select();
  85. editor.execCommand( 'anchor');
  86. equal(ua.getChildHTML(editor.body),'<p></p>','去掉锚点');
  87. } );