Querying strings with spaces


#1

EDIT: I created a new branch-topic to change focus a little here after feedback here and further research.

I have this issue with querying a string with spaces in it.

For example I can type in the following in a text field:

'hi love      all these spaces!'

The string will get saved fine in Mongo (with the spaces), but when I do a collection_name.find({}), the string becomes:

‘hi love all these spaces!’

Removing the spaces!

What am I doing wrong?

EDIT:
Since I’m displaying this string in a Blaze tag like so {{{message}}} to preserve the HTML formatting, maybe it makes sense to save off the string to Mongo with &nbsp when I encounter more than one space in a row?

NOTE:
The strings are just stored in a field inside a document like everything else – nothing special about it. I’m using a Purifier to gather the HTML and save off to Mongo first, then I retrieve the string back and display it in a div.


#2
  • Do you try to store strings as documents in a collection?
  • Maybe the string has line breaks?

#3

Are you sure you’re not just seeing normal HTML behaviour? If you display your string between <pre></pre> tags does it display correctly?

I can even do that here:

this      has    lots     of      spaces

#4

Thanks @Sanjo

The strings are just stored in a field inside a document like everything else – nothing special about it. And line breaks would create a new line – right? This just removes the extra space. I’m thinking I can place a &nbsp when I encounter more than one space in a row (I’m using a Purifier to gather the HTML and save off to Mongo first, then I retrieve the string back and display it).


#5

@aadams it’s what @robfallows says. HTML trims all consecutive whitespaces (newline, space etc) an displays them as a single space.

If you want to preserve it, you need to either use the pre tag, or style the enclosing tag with white-space: pre-line which preserves spaces as well as line breaks.


#6

I wish it did. It doesn’t display the spaces and the formatting changes a lot.

With a Editor input like this (with white spaces):

And code that looks likes:

// inside template event

var editorContent = instance.find('[id^=ql-editor-]').innerHTML;
var filteredMsg = UniHTML.purify(editorContent);


// now I save this off to Mongo…

// have a tracker on the publish so after the insert the subscription gets refreshed and the helper is called again
// inside template helper

  user_messages: function() {
    var user_id = Template.instance().user_id;
    var records = messages_detail.find({
      user_id: user_id
    }, {
      sort: {
        created: -1
      }
    });
    // here records.fetch()[some_index].message won't have the extra spacing.
    return records;
  }

// template html

{{#each user_messages}}
   <div class="contents regular">
      {{{message}}}
   </div>
{{/each}}

The results look like this:

Where their should be more than one space between wow and very.

if I add a <pre> tag to it sorta like so:

var editorContent = instance.find('[id^=ql-editor-]').innerHTML;
var withPreTag = '<pre>' + editorContent + '</pre>'
var filteredMsg = UniHTML.purify(withPreTag);

The message gets displayed in a grey box still without the spaces:

EDIT:
I thought the important part was that the Purifier IS PRESERVING the whitespace? In the mongo user_messages collection message field the string.

If I go to the Browser console, and using Mini Mongo, typing in:

messages_detail.find().fetch()[the_proper_index]

I get a record like so:

<div class="ql-multi-cursor"/>
<span class="cursor hidden"><span class="cursor-flag"><span class="cursor-name" style="background-color: rgba(255, 153, 51, 0.901961);">Gandalf</span></span></span>
<div class="ql-editor authorship ql-content"/>
<span class="author-test">wow very cool!</span>
<div class="ql-paste-manager"/>
<br/>

A string without the spaces.

But again, if I go into meter shell (the server side) and type in the same thing:

messages_detail.find().fetch()[the_proper_index]

I get a record like so:

<div class="ql-multi-cursor"/>
<span class="cursor hidden"><span class="cursor-flag"><span class="cursor-name" style="background-color: rgba(255, 153, 51, 0.901961);">Gandalf</span></span></span>
<div class="ql-editor authorship ql-content"/>
<span class="author-test">wow   very cool!</span>
<div class="ql-paste-manager"/>
<br/>

A string with the spaces.

Is this an important fact somehow?


#7

Hmm, the purifier doc quotes:

By default, HTML Purifier may remove some of your spacing or indentation. Turn on CollectErrors or experimental features in order to fully preserve whitespace.