[haiku-commits] haiku: hrev44326 - src/kits/interface

  • From: korli@xxxxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Thu, 12 Jul 2012 23:58:41 +0200 (CEST)

hrev44326 adds 1 changeset to branch 'master'
old head: 5e4a2efb479d3b3957f89fd3d318996541f00aeb
new head: 639ac47052e9c01a2a00f38e5a09bfe3bd53dc2c

----------------------------------------------------------------------------

639ac47: BTextView should not accept disallowed chars being pasted.
  
  * Patch from #6885: This filters out any disallowed characters from text
    being pasted or dropped in. If the resulting filtered text is zero
    characters long, it beeps. Works with styled text too.
  * Coding style: variables renaming by korli.

                                    [ Hamish Morrison <hamish@xxxxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev44326
Commit:      639ac47052e9c01a2a00f38e5a09bfe3bd53dc2c
URL:         http://cgit.haiku-os.org/haiku/commit/?id=639ac47
Author:      Hamish Morrison <hamish@xxxxxxxxxxx>
Date:        Thu Jul 12 21:52:25 2012 UTC
Committer:   Jérôme Duval <jerome.duval@xxxxxxxxx>
Commit-Date: Thu Jul 12 21:56:36 2012 UTC

Ticket:      https://dev.haiku-os.org/ticket/6885

----------------------------------------------------------------------------

1 file changed, 75 insertions(+), 16 deletions(-)
src/kits/interface/TextView.cpp |   91 +++++++++++++++++++++++++++++------

----------------------------------------------------------------------------

diff --git a/src/kits/interface/TextView.cpp b/src/kits/interface/TextView.cpp
index 16934da..d46414d 100644
--- a/src/kits/interface/TextView.cpp
+++ b/src/kits/interface/TextView.cpp
@@ -1432,28 +1432,36 @@ BTextView::Paste(BClipboard *clipboard)
        BMessage *clip = clipboard->Data();
        if (clip != NULL) {
                const char *text = NULL;
-               ssize_t len = 0;
+               ssize_t length = 0;
 
                if (clip->FindData("text/plain", B_MIME_TYPE,
-                               (const void **)&text, &len) == B_OK) {
+                               (const void **)&text, &length) == B_OK) {
                        text_run_array *runArray = NULL;
-                       ssize_t runLen = 0;
+                       ssize_t runLength = 0;
 
                        if (fStylable) {
                                
clip->FindData("application/x-vnd.Be-text_run_array",
-                                       B_MIME_TYPE, (const void **)&runArray, 
&runLen);
+                                       B_MIME_TYPE, (const void **)&runArray, 
&runLength);
+                       }
+
+                       _FilterDisallowedChars((char*)text, length, runArray);
+
+                       if (length < 1) {
+                               beep();
+                               clipboard->Unlock();
+                               return;
                        }
 
                        if (fUndo) {
                                delete fUndo;
-                               fUndo = new PasteUndoBuffer(this, text, len, 
runArray,
-                                       runLen);
+                               fUndo = new PasteUndoBuffer(this, text, length, 
runArray,
+                                       runLength);
                        }
 
                        if (fSelStart != fSelEnd)
                                Delete();
 
-                       Insert(text, len, runArray);
+                       Insert(text, length, runArray);
                        ScrollToOffset(fSelEnd);
                }
        }
@@ -4774,30 +4782,38 @@ BTextView::_MessageDropped(BMessage *inMessage, BPoint 
where, BPoint offset)
                        return true;
        }
 
-       ssize_t dataLen = 0;
+       ssize_t dataLength = 0;
        const char *text = NULL;
        entry_ref ref;
        if (inMessage->FindData("text/plain", B_MIME_TYPE, (const void **)&text,
-                       &dataLen) == B_OK) {
+                       &dataLength) == B_OK) {
                text_run_array *runArray = NULL;
-               ssize_t runLen = 0;
-               if (fStylable)
+               ssize_t runLength = 0;
+               if (fStylable) {
                        
inMessage->FindData("application/x-vnd.Be-text_run_array",
-                               B_MIME_TYPE, (const void **)&runArray, &runLen);
+                               B_MIME_TYPE, (const void **)&runArray, 
&runLength);
+               }
+
+               _FilterDisallowedChars((char*)text, dataLength, runArray);
+
+               if (dataLength < 1) {
+                       beep();
+                       return true;
+               }
 
                if (fUndo) {
                        delete fUndo;
-                       fUndo = new DropUndoBuffer(this, text, dataLen, 
runArray,
-                               runLen, dropOffset, internalDrop);
+                       fUndo = new DropUndoBuffer(this, text, dataLength, 
runArray,
+                               runLength, dropOffset, internalDrop);
                }
 
                if (internalDrop) {
                        if (dropOffset > fSelEnd)
-                               dropOffset -= dataLen;
+                               dropOffset -= dataLength;
                        Delete();
                }
 
-               Insert(dropOffset, text, dataLen, runArray);
+               Insert(dropOffset, text, dataLength, runArray);
        }
 
        return true;
@@ -5582,6 +5598,49 @@ BTextView::_ShowContextMenu(BPoint where)
        menu->Go(where, true, true,     true);
 }
 
+
+void
+BTextView::_FilterDisallowedChars(char* text, int32& length,
+       text_run_array* runArray)
+{
+       if (!fDisallowedChars)
+               return;
+
+       if (fDisallowedChars->IsEmpty() || !text)
+               return;
+
+       int32 stringIndex = 0;
+       if (runArray) {
+               int32 remNext = 0;
+
+               for (int i = 0; i < runArray->count; i++) {
+                       runArray->runs[i].offset -= remNext;
+                       while (stringIndex < runArray->runs[i].offset
+                               && stringIndex < length) {
+                               if (fDisallowedChars->HasItem(
+                                       reinterpret_cast<void 
*>(text[stringIndex]))) {
+                                       memmove(text + stringIndex, text + 
stringIndex + 1,
+                                               length - stringIndex - 1);
+                                       length--;
+                                       runArray->runs[i].offset--;
+                                       remNext++;
+                               } else
+                                       stringIndex++;
+                       }
+               }
+       }
+
+       while (stringIndex < length) {
+               if (fDisallowedChars->HasItem(
+                       reinterpret_cast<void *>(text[stringIndex]))) {
+                       memmove(text + stringIndex, text + stringIndex + 1,
+                               length - stringIndex - 1);
+                       length--;
+               } else
+                       stringIndex++;
+       }
+}
+
 // #pragma mark - BTextView::TextTrackState
 
 


Other related posts: