2

I want to know what is the suitable replacement for this line.

this.__LZtextclip.text

I am using this to get the string present in the text node. This works fine in Openlaszlo 3.3 but in 4.9 and 5.0 it's giving a problem

I tried updating it to

this.sprite.__LZtextclip.text

And i am getting an error:

79: Error: Access of possibly undefined property __LZtextclip through a reference with static type LzSprite, in line: Debug.write(this.sprite.__LZtextclip.text);

Any idea why this problem is happening?

karthick
  • 11,998
  • 6
  • 56
  • 88
  • "__LZtextclip" is the reference to the normal Flash-Textfield used in the SWF8 runtime. The SWF9+ and DHTML runtime have a different implementation. Try to avoid any calls to private properties marked with a double underscore '__' in LZX, if possible. – raju-bitter Sep 04 '12 at 16:01

2 Answers2

2

If you are trying to access the text content of a text field, why don't you just access the attribute text?

<canvas> 

    <text name="sample" id="gRead" />

    <handler name="oninit">
      gRead.setAttribute('text',"HI");
      Debug.info(gRead.text);
    </handler>

</canvas>

In OpenLaszlo 3.3 there is method getText(), which gives you the same value. Accessing mx.textfield in your code does not work for the DHTML runtime.

Edit: Added information regarding the stripping of HTML tags
The Flash Textfield class flash.text.Textfield provides an API to enable HTML tag content in a Textfield instance. There are two different properties, one called text, the other one htmlText. If you want to directly access the Flash Textfield object of an lz.text instance, it's a property of the display object of the lz.text instance:

// Flash Textfield instance
gRead.getDisplayObject().textfield
// Pure text content
gRead.getDisplayObject().textfield.text
// Formatted text
gRead.getDisplayObject().textfield.htmlText

You should be aware of the fact that Flash automatically adds HTML format to any textstring you set as content. When you do

gRead.setAttribute('text',"HI");

the textfield.htmlText value is

<P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="11" COLOR="#000000" LETTERSPACING="0" KERNING="1">HI</FONT></P> 

For the DHTML runtime, the text content is added as the innerHTML of a <div> tag, and there is no standardized API to retrieve the pure text content of a DOM structure for a tag with content. You could write your own function to extract the text content, or use JavaScript functions from existing frameworks - like the jQuery text() function - to achieve the same result for the DHTML runtime.

I guess the reason is that Laszlo started using the Dojo based rich text editor for text input with HTML formatting since OpenLaszlo 4.0 or 4.1.

The best approach to have consistent behavior across runtimes when stripping tags is to do the conversion on the server-side. That's especially needed if you wan to have consistent whitespace treatment in multiline text, since there differences in how browsers treat whitespace. The question how to best strip tags from strings in JavaScript has been answered before on Stackoverflow, e.g. JavaScript: How to strip HTML tags from string?

Here is a cross-runtime example which works in DHTML with Firefox, Chrome, and it should work with IE9+:

<canvas> 

    <text name="sample" id="gRead" />

    <handler name="oninit"><![CDATA[
      gRead.setAttribute("text", 'Hello <b>World</b> <a href="http://www.openlaszlo.org">OL</a>');
      Debug.info("gRead.text=" + gRead.text);
      if ($dhtml) {
        Debug.info(gRead.getDisplayObject().textContent);
      } else {
        Debug.info(gRead.getDisplayObject().textfield.text);
      }
    ]]></handler>

</canvas>
Community
  • 1
  • 1
raju-bitter
  • 8,906
  • 4
  • 42
  • 53
  • In the above code if i change it to. gRead.setAttribute('text'."Hello World!

    "); then gRead.text will return the text with html tags. But by using the __LZtextclip i'll get the text that doesn't have these html markup. So that's why i was trying to use the sprite. Is there any alternative that will work everywhere?
    – karthick Sep 05 '12 at 07:54
  • Do you need that functionality for both runtimes, or for SWF only? – raju-bitter Sep 05 '12 at 08:44
  • Yes i need for both the environment. I know that the sorite things works in as3 so i can create a check for that. But in DHTML i am not sure i was thinking of using JavaScript. Is there anything in built in laszlo itself? – karthick Sep 05 '12 at 10:05
  • 1
    Just remember that the textContent property is not available in all browsers. Here is more information on textContent support http://stackoverflow.com/questions/1359469/innertext-works-in-ie-but-not-in-firefox – raju-bitter Sep 05 '12 at 10:14
1

I found what is the problem. The problem is that i have to declare a variable and have to refer the property from that.

<canvas> 
<library>
    <text name="sample" id="gRead">         
        <method name="getTextFrom">
            Debug.write("this.text" , this.sprite);
            var mx = this.sprite;           
            Debug.write("this.text" , mx.textfield.text);

        </method>
    </text>

</library>
<handler name="oninit">
    gRead.setAttribute('text',"HI");
    gRead.getTextFrom();

</handler>
</canvas>
karthick
  • 11,998
  • 6
  • 56
  • 88