Bug 394853

Summary: Find and replace don't support space on xml file
Product: [Applications] krename Reporter: AndreaT <androide74>
Component: generalAssignee: Heiko Becker <heiko.becker>
Status: CONFIRMED ---    
Severity: minor CC: attila.schler, carstengrohmann, greg, mail, mailinglist, msdobrescu, n-kde, rafael.linux.user
Priority: NOR    
Version First Reported In: 5.0.0   
Target Milestone: ---   
Platform: Ubuntu   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description AndreaT 2018-05-30 09:41:34 UTC
Find/replace support space (example find " " replace with "-") but don't work by importing find/replace setting from xml file (see bottom)


this don't work:

<!DOCTYPE KRename>
<KRename version="1.0"><FindReplaceList rows="2"><Row num="0"><RegularExpression>0</RegularExpression><Find>:</Find><ReplaceWith></ReplaceWith><ProcessTokens>0</ProcessTokens></Row><Row num="1"><RegularExpression>0</RegularExpression><Find> </Find><ReplaceWith> h</ReplaceWith><ProcessTokens>0</ProcessTokens></Row></FindReplaceList></KRename>
Comment 1 Greg Lepore 2018-05-30 15:49:03 UTC
On a probably related note, find and replace doesn't work on spaces, period. Attempted to replace multiple spaces with a single space doesn't work on Tab 4 - Filename->Find and Replace.

Presumably there would be an equivalent regular expression for this, but a plain text search and replace should work.
Comment 2 Attila Schler 2018-06-16 12:18:14 UTC
(In reply to AndreaT from comment #0)
> Find/replace support space (example find " " replace with "-") but don't
> work by importing find/replace setting from xml file (see bottom)
> 
> 
> this don't work:
> 
> <!DOCTYPE KRename>
> <KRename version="1.0"><FindReplaceList rows="2"><Row
> num="0"><RegularExpression>0</RegularExpression><Find>:</Find><ReplaceWith></
> ReplaceWith><ProcessTokens>0</ProcessTokens></Row><Row
> num="1"><RegularExpression>0</RegularExpression><Find> </Find><ReplaceWith>
> h</ReplaceWith><ProcessTokens>0</ProcessTokens></Row></FindReplaceList></
> KRename>

I can observe the same problem on my system.

OpenSUSE Tumbleweed 20180606-261.1
KRename 5.0.0
KDE Frameworks 5.46.0
Qt 5.11.0
Comment 3 Heiko Becker 2018-06-20 15:36:32 UTC
Yep, seems the  xml file is misimported. I'll look into it.
Comment 4 Heiko Becker 2018-06-20 15:39:06 UTC
(In reply to Greg Lepore from comment #1)
> On a probably related note, find and replace doesn't work on spaces, period.
> Attempted to replace multiple spaces with a single space doesn't work on Tab
> 4 - Filename->Find and Replace.
> 
> Presumably there would be an equivalent regular expression for this, but a
> plain text search and replace should work.

I can't reproduce that. And if I interpret 'Find/replace support space (example find " " replace with "-") but don't work by importing find/replace setting from xml file' by the original reporter correctly it does work for him.
Comment 5 Carsten Grohmann 2018-11-27 18:21:31 UTC
I observe this behaviour also. Thereby I did some tests to provide further feedback.

KRename stores the space characters correctly. But after loading the definition file, all tags containing only spaces are not displayed as expected. They are displayed as empty strings. Leading and trailing spaces are displayed as expected.

The spaces in the rows #0 and #2 disappear, but row #1 is fully added to the dialog.

Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE KRename>
<KRename version="1.0">
   <FindReplaceList rows="3">
      <Row num="0">
         <RegularExpression>0</RegularExpression>
         <Find> </Find>
         <ReplaceWith>simple space</ReplaceWith>
         <ProcessTokens>0</ProcessTokens>
      </Row>
      <Row num="1">
         <RegularExpression>0</RegularExpression>
         <Find> x </Find>
         <ReplaceWith>space_x_space</ReplaceWith>
         <ProcessTokens>0</ProcessTokens>
      </Row>
      <Row num="2">
         <RegularExpression>0</RegularExpression>
         <Find>  </Find>
         <ReplaceWith>two spaces</ReplaceWith>
         <ProcessTokens>0</ProcessTokens>
      </Row>
   </FindReplaceList>
</KRename>

There is an old thread "QDomElement's text() for <element> </element> giving me a empty Qstring" from 2012 at http://www.qtcentre.org. Unfortunataly it's inaccessible - directly and via Google Cache.

The thread describes that QDomElement.text() returns an empty Qstring if the element contains only spaces. It should be an old Qt bug. The suggested workaround is to use QXmlSimpleReader.

The described behaviour can fit to the source in ReplaceDialog::slotLoadList(). Maybe you can use the examples and check if RowElement.firstChildElement("Find").text() returns an empty Qstring.

Current versions
 * up-to-date ArchLinux
 * KRename 5.0.0

Regards,
Carsten
Comment 6 Carsten Grohmann 2018-12-08 11:29:15 UTC
I had printed an example from the above mentioned thread and typed it in again. With it the problem can be shown well:

#include <QString>
#include <QDomDocument>
#include <QDomElement>
#include <QXmlInputSource>
#include <QDebug>


int main() {
    QString xmlString("<original>      </original>");
    QDomDocument doc;

    doc.setContent(xmlString);
    QDomElement docElem = doc.documentElement();
    qDebug() << "Without QXmlSimpleReader \t- element contains" << docElem.text() << "\tlength=" << docElem.text().length();
    doc.clear();

    QXmlInputSource source;
    source.setData(xmlString);
    QXmlSimpleReader reader;
    doc.setContent(&source, &reader);
    docElem = doc.documentElement();
    qDebug() << "With QXmlSimpleReader \t- element contains" << docElem.text() << "\tlength=" << docElem.text().length();
}

The program output:
$ ./qxmltest
Without QXmlSimpleReader 	- element contains "" 	length= 0
With QXmlSimpleReader 	- element contains "      " 	length= 6
Comment 7 Justin Zobel 2020-11-24 06:36:19 UTC
*** Bug 410253 has been marked as a duplicate of this bug. ***
Comment 8 Heiko Becker 2021-07-29 15:49:41 UTC
*** Bug 434141 has been marked as a duplicate of this bug. ***
Comment 9 szecsodimlaszlo 2022-03-22 04:30:27 UTC
I have the same problem.

Arch Linux
KDE 5.24.3
Qt 5.15.3 & 6.2.4
krename 5.0.1
Comment 10 Mihai Sorin Dobrescu 2022-10-30 08:39:36 UTC
While I could make it save some spaces to XML, even find by regex with \x0020 instead of spaces, can't make it load and apply a replacement string consisting in space.
So, a common two spaces to one space replacement can not be saved and loaded back from an XML file.

I've also encountered some other characters problems to replace for various brackets characters ('[', ' ]', '{', '}'). For those, I always use \0x expressions, which is difficult.
Comment 11 Mihai Sorin Dobrescu 2022-10-30 08:42:27 UTC
*** Bug 439291 has been marked as a duplicate of this bug. ***