gnash-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gnash-commit] /srv/bzr/gnash/trunk r10552: Implement TextField.replaceS


From: Benjamin Wolsey
Subject: [Gnash-commit] /srv/bzr/gnash/trunk r10552: Implement TextField.replaceSel().
Date: Thu, 22 Jan 2009 19:57:28 +0100
User-agent: Bazaar (1.5)

------------------------------------------------------------
revno: 10552
committer: Benjamin Wolsey <address@hidden>
branch nick: trunk
timestamp: Thu 2009-01-22 19:57:28 +0100
message:
  Implement TextField.replaceSel().
modified:
  libcore/TextField.cpp
  libcore/TextField.h
  testsuite/actionscript.all/TextField.as
    ------------------------------------------------------------
    revno: 10551.1.1
    committer: Benjamin Wolsey <address@hidden>
    branch nick: work
    timestamp: Thu 2009-01-22 17:55:07 +0100
    message:
      Test and implement TextField.replaceSel().
    modified:
      libcore/TextField.cpp
      libcore/TextField.h
      testsuite/actionscript.all/TextField.as
    ------------------------------------------------------------
    revno: 10551.1.2
    committer: Benjamin Wolsey <address@hidden>
    branch nick: work
    timestamp: Thu 2009-01-22 18:22:03 +0100
    message:
      Log AS error.
    modified:
      libcore/TextField.cpp
=== modified file 'libcore/TextField.cpp'
--- a/libcore/TextField.cpp     2009-01-22 14:35:44 +0000
+++ b/libcore/TextField.cpp     2009-01-22 17:22:03 +0000
@@ -125,7 +125,6 @@
     :
     character(parent, id),
     _tag(&def),
-    _text(L""),
     _textDefined(def.hasText()),
     _underlined(false),
     _leading(def.leading()),
@@ -183,7 +182,6 @@
     :
     // the id trick is to fool assertions in character ctor
     character(parent, parent ? 0 : -1),
-    _text(L""),
     _textDefined(false),
     _underlined(false),
     _leading(0),
@@ -381,6 +379,20 @@
 }
 
 void
+TextField::replaceSelection(const std::string& replace)
+{
+
+    const int version = _vm.getSWFVersion();
+    const std::wstring& wstr = utf8::decodeCanonicalString(replace, version);
+    
+    const size_t start = _selection.first;
+    const size_t replaceLength = wstr.size();
+
+    _text.replace(start, _selection.second - start, wstr);
+    _selection = std::make_pair(start + replaceLength, start + replaceLength);
+}
+
+void
 TextField::setSelection(int start, int end)
 {
 
@@ -2637,14 +2649,35 @@
     return as_value();
 }
 
-
+/// TextField.replaceSel(newText)
+//
+/// Replaces the current selection with the new text, setting both
+/// begin and end of the selection to one after the inserted text.
+/// If an empty string is passed, SWF8 erases the selection; SWF7 and below
+/// is a no-op.
+/// If no argument is passed, this is a no-op.
 as_value
 textfield_replaceSel(const fn_call& fn)
 {
     boost::intrusive_ptr<TextField> text = ensureType<TextField>(fn.this_ptr);
-    UNUSED(text);
-
-    LOG_ONCE (log_unimpl("TextField.replaceSel()"));
+
+    if (!fn.nargs) {
+        IF_VERBOSE_ASCODING_ERRORS(
+            std::ostringstream os;
+            fn.dump_args(os);
+            log_aserror("TextField.replaceSel(%s) requires exactly one "
+                "argument", os.str());
+        );
+        return as_value();
+    }
+
+    const std::string& replace = fn.arg(0).to_string();
+
+    /// Do nothing if text is empty and version less than 8.
+    const int version = text->getVM().getSWFVersion();
+    if (version < 8 && replace.empty()) return as_value();
+
+    text->replaceSelection(replace);
 
     return as_value();
 }

=== modified file 'libcore/TextField.h'
--- a/libcore/TextField.h       2008-12-16 12:20:22 +0000
+++ b/libcore/TextField.h       2009-01-22 16:55:07 +0000
@@ -137,6 +137,16 @@
         return _selection;
     }
 
+    /// Replace the current selection with the new text.
+    void replaceSelection(const std::string& replace);
+
+    /// Set the current selection
+    //
+    /// @param start    The index of the beginning of the selection.
+    /// @param end      The index of the end of the selection.
+    //
+    /// If start is greater than end, the values are swapped, ensuring
+    /// end is never less than start.
     void setSelection(int start, int end);
 
        /// We have a "text" member.
@@ -643,6 +653,8 @@
        ///
        rect _bounds;
 
+    /// Represents the selected part of the text. The second element must
+    /// never be less than the first.
     std::pair<size_t, size_t> _selection;
 
 protected:

=== modified file 'testsuite/actionscript.all/TextField.as'
--- a/testsuite/actionscript.all/TextField.as   2009-01-22 14:35:44 +0000
+++ b/testsuite/actionscript.all/TextField.as   2009-01-22 16:55:07 +0000
@@ -956,6 +956,72 @@
 createTextField("tf6", 103, 10, 10, 160);
 check_equals(typeof(tf6), 'undefined');
 
+/// Test TextField.replaceSel
+
+createTextField('repl1', 99, 10, 10, 10, 10);
+Selection.setFocus(repl1);
+check_equals(Selection.getFocus(), '_level0.repl1');
+repl1.text = "Text in a string";
+
+ret = repl1.replaceSel("More ");
+/// Check that the selection start and end indices are adjusted.
+check_equals(Selection.getBeginIndex(), 5);
+check_equals(Selection.getEndIndex(), 5);
+check_equals(repl1.text, "More Text in a string");
+
+ret = repl1.replaceSel("");
+check_equals(Selection.getEndIndex(), 5);
+check_equals(repl1.text, "More Text in a string");
+
+
+Selection.setSelection(0, 1);
+ret = repl1.replaceSel("HU");
+check_equals(ret, undefined);
+check_equals(repl1.text, "HUore Text in a string");
+
+check_equals(Selection.getBeginIndex(), 2);
+check_equals(Selection.getEndIndex(), 2);
+
+Selection.setSelection(2, 5);
+ret = repl1.replaceSel("HUU");
+check_equals(ret, undefined);
+check_equals(repl1.text, "HUHUU Text in a string");
+
+check_equals(Selection.getBeginIndex(), 5);
+check_equals(Selection.getEndIndex(), 5);
+
+Selection.setSelection(10, 13);
+repl1.replaceSel(7);
+check_equals(repl1.text, "HUHUU Text7 a string");
+check_equals(Selection.getBeginIndex(), 11);
+check_equals(Selection.getEndIndex(), 11);
+
+Selection.setSelection(10, 13);
+repl1.replaceSel(new Object());
+check_equals(repl1.text, "HUHUU Text[object Object] string");
+check_equals(Selection.getBeginIndex(), 25);
+check_equals(Selection.getEndIndex(), 25);
+
+Selection.setSelection(1, 20);
+repl1.replaceSel("");
+#if OUTPUT_VERSION < 8
+check_equals(repl1.text, "HUHUU Text[object Object] string");
+check_equals(Selection.getBeginIndex(), 1);
+check_equals(Selection.getEndIndex(), 20);
+#else
+check_equals(repl1.text, "Hject] string");
+check_equals(Selection.getBeginIndex(), 1);
+check_equals(Selection.getEndIndex(), 1);
+#endif
+
+repl1.text = "New text";
+
+Selection.setSelection(2, 5);
+repl1.replaceSel();
+check_equals(repl1.text, "New text");
+check_equals(Selection.getBeginIndex(), 2);
+check_equals(Selection.getEndIndex(), 5);
+
 //------------------------------------------------------------
 // Test properties
 //------------------------------------------------------------
@@ -1004,11 +1070,11 @@
 //------------------------------------------------------------
 
 #if OUTPUT_VERSION == 6
- check_totals(438);
+ check_totals(464);
 #elif OUTPUT_VERSION == 7
- check_totals(441);
+ check_totals(467);
 #elif OUTPUT_VERSION == 8
- check_totals(442);
+ check_totals(468);
 #endif
 
 #endif


reply via email to

[Prev in Thread] Current Thread [Next in Thread]