views:

3621

answers:

4

let's say that I have an XML file containing this :

<description><![CDATA[

   <h2>lorem ipsum</h2>
   <p>some text</p>

]]></description>

that I want to get and parse in ActionScript 2 as HTML text, and setting some CSS before displaying it. Problem is, Flash takes those whitespaces (line feed and tab) and display it as it is.

<some whitespace here>
       lorem ipsum
       some text

where the output I want is

lorem ipsum
some text

I know that I could remove the whitespaces directly from the XML file (the Flash developer at my workplace also suggests this. I guess that he doesn't have any idea on how to do this [sigh]). But by doing this, it would be difficult to read the section in the XML file, especially when lots of tags are involved and that makes editing more difficult.

So now, I'm looking for a way to strip those whitespaces in ActionScript. I've tried to use PHP's str_replace equivalent (got it from here). But what should I use as a needle (string to search) ? (I've tried to put in "\t" and "\r", don't seem to be able to detect those whitespaces).

edit :

now that I've tried to throw in newline as a needle, it works (meaning that newline successfully got stripped).

mystring = str_replace(newline, '', mystring);

But, newlines only got stripped once, meaning that in every consecutive newlines, (eg. a newline followed by another newline) only one newline can be stripped away.

Now, I don't see that this as a problem in the str_replace function, since every consecutive character other than newline get stripped away just fine.

Pretty much confused about how stuff like this is handled in ActionScript. :-s

edit 2:

I've tried str_replace -ing everything I know of, \n, \r, \t, newline, and tab (by pressing tab key). Replacing \n, \r, and \t seem to have no effect whatsoever.

I know that by successfully doing this, my content can never have real line breaks. That's exactly my intention. I could format the XML the way I want without Flash displaying any of the formatting stuff. :)

+1  A: 

its been a while since I've tinkered with AS2.

someXML = new XML();
someXML.ignoreWhite = true;

if you wanted to str_replace try '\n'

gltovar
Nope. XML.ignoreWhite ignores whitespace between tags; it won't affect whitespace inside a CDATA container.
fenomas
yup, it's just like fenomas said. And putting \n doesn't seem to have any effect either.
andyk
and, uh, sorry for the late response gltovar.
andyk
+2  A: 

Several ways to approach this. Perhaps the simplest answer is, in one sense your Flash developer is probably right, and you should move your whitespace outside of the CDATA container. The reason being, many people (me at least) tend to assume that everything inside a CDATA is "real data", as opposed to markup. On the other hand, whitespace outside a CDATA is normally assumed to be irrelevant, so data like this:

<description>

<![CDATA[<h2>lorem ipsum</h2>
<p>some text</p>]]>

</description>

would be easier to understand and to work with. (The flash developer can use the XML.ignoreWhite property to ignore the whitespace outside the CDATA.)

With that said, if you're editing the XML by hand, then I can see why it would be easier to use the formatting you describe. However, if the extra whitespace is inside the CDATA, then it will inevitable be included in the String data you extract, so your only option is to grab the content of the CDATA and remove the whitespace afterwards.

Then your question reduces to "how do I strip leading/trailing whitespace from a String in AS2?". And unfortunately, since AS2 doesn't support RegEx there's no simple way to do this. I think your best option would be to parse through from the beginning and end to find the first/last non-white character. Something along these lines (untested pseudocode):

myString = stuffFromXML;
whitespace = " " + "\t" + "\n" + "\r" + newline;
start = 0;
end = myString.length;
while ( testString( myString.substr(start,1), whitespace ) ) { start++; }
while ( testString( myString.substr(end-1,1), whitespace ) ) { end--; }
trimmedString = myString.substring( start, end );

function testString( needle, haystack ) {
    return ( haystack.indexOf( needle ) > -1 );
}

Hope that helps!

Edit: I notice that in your example you'd also need to remove tabs and whitespace within your text data. This would be tricky, unless you can guarantee that your data will never include "real" tabs in addition to the ones for formatting. No matter what you do with the CDATA tags, it would probably be wiser not to insert extraneous formatting inside your real content and then remove it programmatically afterward. That's just making your own life difficult.

Second edit: As for what character to remove to get rid of newlines, it depends partially on what characters are actually in the XML to begin with (which probably depends on what OS is running where the file is generated), and partially on what character the client machine (that's showing the flash) considers a newline. Lots of gory details here. In practice though, if you remove \r, \n, and \r\n, that usually does the trick. That's why I added both \r and \n to the "whitespace" string in my example code.

fenomas
what's `newline` ? Is it some predefined variables in AS2 ? (Sorry for the late response to your answer)
andyk
I just found out that I could strip the tabs away by putting <real tab> as a needle to search. (not \t) Now that just left me with the newline. I agree that I should probably left the formatting stuff, but there must be some kind of way.. hmm...
andyk
Yes, "newline" is a predefined variable in AS2. I'll add a note to the answer about what character to use for newline.
fenomas
I'll try using 'newline' after holiday and give an update afterward. Happy new year!
andyk
a quick update : newline works, but .. (see my question above)
andyk
Hard to guess, but have you tried removing newline and \n and \r, as in my example? That is, use str_replace on one after the other. Also, keep in mind that this approach will mean that your content can never contain "real" (i.e. intentional) line breaks.
fenomas
sorry for the late update. I've tried using 4 str_replace, (for \n, \r, newline, and real tab), \n and \r seems to have no effect whatsoever. End results is the same. (can't replace consecutive newlines). I'll update this to the question.
andyk
A: 

Is there a reason that you are using cdata? Admittedly I have no idea what the best practice for this sort of this is, but I tend to leave them out and just have the HTML sit there inside the node. var foo = node.childnodes.join("") parses it out just fine and I never seem to come across these whitespace problems.

Andrew
whenever you want to put HTML or something else with lots of '<' or '>', it's better to wrap them inside CDATA tag. If not, just a little of non-valid HTML would break down your entire XML.
andyk
still, it is nice to know this one though. Thanks.
andyk
A: 

I'm reading this over and over again, and if I'm interpreting you right, all you want to know how to do is strip certain characters (tabs and newlines) from a string in AS2, right? I cannot believe no one has given you the simple one line answer yet:

myString = myString.split("\n").join("");

That's it. Repeat that for \r, \n, and \t and all newlines and tabs will be gone. If you want it as an easy function, then do this:

function stripWhiteSpace(str: String) : String
{
    return str.split("\r").join("").split("\n").join("").split("\t").join("");
}

That function won't modify your old string, it will return a new one without \r, \n, or \t. To actually modify the old string use that function like this:

myString = stripWhiteSpace(myString);
Bryan Grezeszak