Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: Sir Osis of Liver - kesäkuu 23, 2014, 01:42:58 AP

Otsikko: Square brackets in <textarea>
Kirjoitti: Sir Osis of Liver - kesäkuu 23, 2014, 01:42:58 AP
 

Trying to put a url wrapped in img tags in a textarea so it can be selected, copied, and pasted into a post.  Sounds simple, doesn't it?

Here's the code -



<button type="button" onclick="embedURL()">Select</button>
<textarea id="embed" rows="1" onclick="this.focus();this.select()" readonly="readonly" style="overflow: hidden; vertical-align: 10%; width: 500px;">[img]http://www.domain.com/images/' . $uploaded_name . '[/img]</textarea>



Here's what displays in the textarea -



     [img]http://www.domain.com/images/



... but if you select and copy, you get this -



     [img]http://www.domain.com/images/image.jpg[/img]



It's all there and copies correctly, but doesn't show $uploaded_name or closing tag.  Tried escaping one or both brackets, and using ascii codes (&#91, &#93), no luck.  Something to do with php array code, I'd guess.  Any way around this?


Otsikko: Re: Square brackets in <textarea>
Kirjoitti: live627 - kesäkuu 23, 2014, 10:38:19 AP
Need moar code. Two lines says jack diddly. This ain't Twitter.
Otsikko: Re: Square brackets in <textarea>
Kirjoitti: Sir Osis of Liver - kesäkuu 23, 2014, 02:32:21 IP
Here's the function -



function embedURL() {
    document.getElementById("embed").select();
}



... and here's the bit that calls it -



<div style="margin: 10px 0 10px 0;">
<button type="button" onclick="embedURL()">Select</button>
<textarea id="embed" rows="1" onclick="this.focus();this.select()" readonly="readonly" style="overflow: hidden; vertical-align: 10%; width: 500px;">http://www.domain.com/images/' . $uploaded_name . '</textarea>
</div>
Select and copy into your post.';



The purpose is to give the member the string to add to their post to embed the image.  The square brackets cut off the displayed text before $uploaded_name.  Select works, and it copies correctly from <textarea>, but doesn't display completely in the box.
Otsikko: Re: Square brackets in <textarea>
Kirjoitti: Arantor - kesäkuu 23, 2014, 07:35:40 IP
Is this inside SMF or not? In which case there are functions available for injecting text into a textarea ;)
Otsikko: Re: Square brackets in <textarea>
Kirjoitti: Sir Osis of Liver - kesäkuu 23, 2014, 10:37:59 IP
No, it's a file uploader that launches in a popup from main menu.  Everything works, including select and copy, the tagged string is complete, but it's truncated in the textarea if there's a a pair of square brackets in the string.  Makes no difference if the brackets contain text or are empty, it loses the php variable and anything following.
Otsikko: Re: Square brackets in <textarea>
Kirjoitti: Sir Osis of Liver - kesäkuu 23, 2014, 11:26:19 IP
Ok, here's the workaround -



To display the image in a post, wrap it in <span style="font-weight: bold;">[img][/img]</span> tags, like this -
</br />';

$emb_text = "[img]http://www.thekrashsite.com/ferrarum/uploads/" . $uploaded_name . "[/img]";

echo '
<div style="margin: 10px 0 10px 0;">
<button type="button" onclick="embedURL()">Select</button>
<textarea id="embed" rows="1" onclick="this.focus();this.select()" readonly="readonly" style="overflow: hidden; vertical-align: 10%; width: 500px;">', $emb_text ,'</textarea>
</div>
Select and copy into your post.';



This displays the complete string in the box, selects and copies correctly.

But why doesn't it work the other way?
Otsikko: Re: Square brackets in <textarea>
Kirjoitti: Angelina Belle - kesäkuu 26, 2014, 07:36:14 IP
What does the html actually look like in the first case? Something which isn't there surely cannot be copied and pasted out of the Textarea.  So if you can select and copy it, then, surely, it is there.

The biggest difference I see is the use of . Instead of ,   But surely that would not make a difference, would it?
Otsikko: Re: Square brackets in <textarea>
Kirjoitti: live627 - kesäkuu 26, 2014, 08:18:05 IP
Lainaa
But why doesn't it work the other way?
You leave me guessing. Was that in a PHP block? I have a feeling not...
Otsikko: Re: Square brackets in <textarea>
Kirjoitti: Sir Osis of Liver - kesäkuu 27, 2014, 02:08:33 IP
Turns out it's a bug in FF (what a surprise!) that they've been aware of since at least 2009.  The text wasn't being truncated, it wrapped to a second line that wasn't visible because rows="1".  FF ignores white-space: nowrap in <textarea>.  Fixed it by adding wrap="off".  It's not proper html, but it werks gud, and doesn't bother other browsers.

This is how it ended up:



<button type="button" onclick="embedURL()">Select</button>
<textarea id="embed" rows="1" onclick="this.focus();this.select()" readonly="readonly" style="overflow: hidden; vertical-align: 10%; width: 450px; white-space: nowrap;" wrap="off">', $emb_text ,'</textarea>


Otsikko: Re: Square brackets in <textarea>
Kirjoitti: Angelina Belle - kesäkuu 27, 2014, 02:16:42 IP
Congratulations!  Marked solved.