@@ -386,12 +386,14 @@ ZSSEditor.restoreRange = function(){
386386 }
387387} ;
388388
389- ZSSEditor . resetSelectionOnField = function ( fieldId ) {
389+ ZSSEditor . resetSelectionOnField = function ( fieldId , offset ) {
390+ offset = typeof offset !== 'undefined' ? offset : 0 ;
391+
390392 var query = "div#" + fieldId ;
391393 var field = document . querySelector ( query ) ;
392394 var range = document . createRange ( ) ;
393- range . setStart ( field , 0 ) ;
394- range . setEnd ( field , 0 ) ;
395+ range . setStart ( field , offset ) ;
396+ range . setEnd ( field , offset ) ;
395397
396398 var selection = document . getSelection ( ) ;
397399 selection . removeAllRanges ( ) ;
@@ -554,12 +556,6 @@ ZSSEditor.getYCaretInfo = function() {
554556 return this . caretInfo ;
555557} ;
556558
557- // MARK: - Default paragraph separator
558-
559- ZSSEditor . defaultParagraphSeparatorTag = function ( ) {
560- return '<' + this . defaultParagraphSeparator + '>' ;
561- } ;
562-
563559// MARK: - Styles
564560
565561ZSSEditor . setBold = function ( ) {
@@ -638,7 +634,7 @@ ZSSEditor.setUnderline = function() {
638634/**
639635 * @brief Turns blockquote ON or OFF for the current selection.
640636 * @details This method makes sure that the contents of the blockquotes are surrounded by the
641- * defaultParagraphSeparatorTag (by default '<p>'). This ensures parity with the web
637+ * defaultParagraphSeparator tag (by default '<p>'). This ensures parity with the web
642638 * editor.
643639 */
644640ZSSEditor . setBlockquote = function ( ) {
@@ -682,9 +678,9 @@ ZSSEditor.setHeading = function(heading) {
682678 var formatBlock = document . queryCommandValue ( 'formatBlock' ) ;
683679
684680 if ( formatBlock . length > 0 && formatBlock . toLowerCase ( ) == formatTag ) {
685- document . execCommand ( 'formatBlock' , false , this . defaultParagraphSeparatorTag ( ) ) ;
681+ document . execCommand ( 'formatBlock' , false , Util . buildOpeningTag ( this . defaultParagraphSeparator ) ) ;
686682 } else {
687- document . execCommand ( 'formatBlock' , false , '<' + formatTag + '>' ) ;
683+ document . execCommand ( 'formatBlock' , false , Util . buildOpeningTag ( formatTag ) ) ;
688684 }
689685
690686 ZSSEditor . sendEnabledStyles ( ) ;
@@ -695,9 +691,9 @@ ZSSEditor.setParagraph = function() {
695691 var formatBlock = document . queryCommandValue ( 'formatBlock' ) ;
696692
697693 if ( formatBlock . length > 0 && formatBlock . toLowerCase ( ) == formatTag ) {
698- document . execCommand ( 'formatBlock' , false , this . defaultParagraphSeparatorTag ( ) ) ;
694+ document . execCommand ( 'formatBlock' , false , Util . buildOpeningTag ( this . defaultParagraphSeparator ) ) ;
699695 } else {
700- document . execCommand ( 'formatBlock' , false , '<' + formatTag + '>' ) ;
696+ document . execCommand ( 'formatBlock' , false , Util . buildOpeningTag ( formatTag ) ) ;
701697 }
702698
703699 ZSSEditor . sendEnabledStyles ( ) ;
@@ -790,8 +786,8 @@ ZSSEditor.setBackgroundColor = function(color) {
790786 */
791787ZSSEditor . insertHTMLWrappedInParagraphTags = function ( html ) {
792788 var space = '<br>' ;
793- var paragraphOpenTag = '<' + this . defaultParagraphSeparator + '>' ;
794- var paragraphCloseTag = '</' + this . defaultParagraphSeparator + '>' ;
789+ var paragraphOpenTag = Util . buildOpeningTag ( this . defaultParagraphSeparator ) ;
790+ var paragraphCloseTag = Util . buildClosingTag ( this . defaultParagraphSeparator ) ;
795791
796792 if ( this . getFocusedField ( ) . getHTML ( ) . length == 0 ) {
797793 html = paragraphOpenTag + html ;
@@ -816,7 +812,7 @@ ZSSEditor.insertLink = function(url, title) {
816812 var html = '<a href="' + url + '">' + title + "</a>" ;
817813
818814 if ( this . getFocusedField ( ) . getHTML ( ) . length == 0 ) {
819- html = '<' + this . defaultParagraphSeparator + '>' + html ;
815+ html = Util . buildOpeningTag ( this . defaultParagraphSeparator ) + html ;
820816 }
821817
822818 this . insertHTML ( html ) ;
@@ -2785,7 +2781,7 @@ ZSSEditor.joinAdjacentSiblingsBlockquotes = function(node) {
27852781ZSSEditor . joinAdjacentSiblingsOrAncestorBlockquotes = function ( node ) {
27862782
27872783 var currentNode = node ;
2788- var rootNode = this . getFocusedField ( ) . wrappedDomNode ( ) ;
2784+ var rootNode = this . getFocusedField ( ) . getWrappedDomNode ( ) ;
27892785 var joined = false ;
27902786
27912787 while ( currentNode
@@ -3008,7 +3004,7 @@ function ZSSField(wrappedObject) {
30083004 this . multiline = false ;
30093005 this . wrappedObject = wrappedObject ;
30103006
3011- if ( this . wrappedDomNode ( ) . hasAttribute ( 'nostyle' ) ) {
3007+ if ( this . getWrappedDomNode ( ) . hasAttribute ( 'nostyle' ) ) {
30123008 this . hasNoStyle = true ;
30133009 }
30143010
@@ -3080,9 +3076,35 @@ ZSSField.prototype.handleKeyDownEvent = function(e) {
30803076 } else if ( wasEnterPressed && ! this . isMultiline ( ) ) {
30813077 e . preventDefault ( ) ;
30823078 } else if ( this . isMultiline ( ) ) {
3083- this . wrapCaretInParagraphIfNecessary ( ) ;
3079+ // For hardware keyboards, don't do any paragraph handling for non-printable keyCodes
3080+ // https://css-tricks.com/snippets/javascript/javascript-keycodes/
3081+ // This avoids the filler zero-width space character from being inserted and displayed in the content field
3082+ // when special keys are pressed in new posts
3083+ var wasTabPressed = ( e . keyCode == '9' ) ;
3084+ var intKeyCode = parseInt ( e . keyCode , 10 ) ;
3085+ if ( wasTabPressed || ( intKeyCode > 13 && intKeyCode < 46 ) || intKeyCode == 192 ) {
3086+ return ;
3087+ }
3088+
3089+ // This is intended to work around an API19-only bug where paragraph wrapping the first character in a post
3090+ // will display a zero-width space character (from ZSSField.wrapCaretInParagraphIfNecessary)
3091+ // We can drop the if statement wrapping wrapCaretInParagraphIfNecessary() if we find a way to stop using
3092+ // zero-width space characters (e.g., autocorrect issues are fixed and we switch back to p tags)
3093+ var containsParagraphSeparators = this . getWrappedDomNode ( ) . innerHTML . search (
3094+ '<' + ZSSEditor . defaultParagraphSeparator ) > - 1 ;
3095+ if ( nativeState . androidApiLevel != 19 || containsParagraphSeparators ) {
3096+ this . wrapCaretInParagraphIfNecessary ( ) ;
3097+ }
30843098
30853099 if ( wasEnterPressed ) {
3100+ // Wrap the existing text in paragraph tags if necessary (this should only be needed if
3101+ // wrapCaretInParagraphIfNecessary() was skipped earlier (API19))
3102+ var currentHtml = this . getWrappedDomNode ( ) . innerHTML ;
3103+ if ( currentHtml . search ( '<' + ZSSEditor . defaultParagraphSeparator ) == - 1 ) {
3104+ ZSSEditor . focusedField . setHTML ( Util . wrapHTMLInTag ( currentHtml , ZSSEditor . defaultParagraphSeparator ) ) ;
3105+ ZSSEditor . resetSelectionOnField ( this . getWrappedDomNode ( ) . id , 1 ) ;
3106+ }
3107+
30863108 var sel = window . getSelection ( ) ;
30873109 if ( sel . rangeCount < 1 ) {
30883110 return null ;
@@ -3358,7 +3380,7 @@ ZSSField.prototype.disableEditing = function () {
33583380ZSSField . prototype . wrapCaretInParagraphIfNecessary = function ( )
33593381{
33603382 var closerParentNode = ZSSEditor . closerParentNode ( ) ;
3361- var parentNodeShouldBeParagraph = ( closerParentNode == this . wrappedDomNode ( )
3383+ var parentNodeShouldBeParagraph = ( closerParentNode == this . getWrappedDomNode ( )
33623384 || closerParentNode . nodeName == NodeName . BLOCKQUOTE ) ;
33633385
33643386 if ( parentNodeShouldBeParagraph ) {
@@ -3419,7 +3441,14 @@ ZSSField.prototype.getHTML = function() {
34193441ZSSField . prototype . getHTMLForCallback = function ( ) {
34203442 var functionArgument = "function=getHTMLForCallback" ;
34213443 var idArgument = "id=" + this . getNodeId ( ) ;
3422- var contentsArgument = "contents=" + this . getHTML ( ) ;
3444+ var contentsArgument ;
3445+
3446+ if ( this . hasNoStyle ) {
3447+ contentsArgument = "contents=" + this . strippedHTML ( ) ;
3448+ } else {
3449+ contentsArgument = "contents=" + this . getHTML ( ) ;
3450+ }
3451+
34233452 var joinedArguments = functionArgument + defaultCallbackSeparator + idArgument + defaultCallbackSeparator +
34243453 contentsArgument ;
34253454 ZSSEditor . callback ( 'callback-response-string' , joinedArguments ) ;
@@ -3459,6 +3488,6 @@ ZSSField.prototype.setPlaceholderText = function(placeholder) {
34593488
34603489// MARK: - Wrapped Object
34613490
3462- ZSSField . prototype . wrappedDomNode = function ( ) {
3491+ ZSSField . prototype . getWrappedDomNode = function ( ) {
34633492 return this . wrappedObject [ 0 ] ;
34643493} ;
0 commit comments