amplesdk icon indicating copy to clipboard operation
amplesdk copied to clipboard

Attribute names starting on 'on' don't work

Open tedbeer opened this issue 12 years ago • 16 comments

Attribute names starting on 'on' don't work. Example (@on is not readable):

<div on="XXX"></div>

In normal HTML documents they work.

tedbeer avatar May 03 '13 15:05 tedbeer

Do you mean you cannot set/get attributes starting "on" ? What are the use cases for such feature?

ilinsky avatar May 03 '13 18:05 ilinsky

The use case is simple - natural attribute names. I wanted to have attributes "on", "off" but I can't. I can't have attributes online, one, onion, onyx etc or any other starting from these words.

tedbeer avatar May 03 '13 20:05 tedbeer

Ok, suppose you create an attribute onyyy, do you expect the value of this attribute to be treated as event handler and be evaluated if an event with name "yyy" propagates through the element with such attribute?

ilinsky avatar May 04 '13 09:05 ilinsky

Why not to copy browsers behavior? "Standard" events can be handled via inline handlers but not custom events.

 <div onmousemove="console.log(888)" onxxx="console.log(999)"></div>

@onmousemove handles 'mousemove' events but @onxxx does not handle 'xxx' event. And both @onmousemove and @onxxx are accessible via .getAttribute() method.

tedbeer avatar May 04 '13 23:05 tedbeer

In Ample SDK we have generic support for multiple XML technologies and also let creating custom ones. The list of "Standard" events does not cover all languages, and building specific language technology into the core of the framework doesn't sound as viable solution.

I see this behaviour as a nice generic approach to inline handlers in attributes working consistently across all XML languages? Suggested by you list of attributes: online, one, onion, onyx and on, sounds like a list of values for attributes with other names, except for maybe one - "online", but even here an attribute "state" with values online/offline could be of a good alternative.

What do you think?

ilinsky avatar May 06 '13 09:05 ilinsky

You don't realize that an attribute name can't even start from any of this words. I wanted to create attributes 'onLabel', 'offLabel' but I can't. Ok, let's skip "standard" events. But why I can't read/set attributes via .getAttribute()/.setAttribute() ? It's possible in browser to read value like

 element.getAttribute('onmousemove')

tedbeer avatar May 06 '13 10:05 tedbeer

Yes, this is correct: by design all attribute names starting "on" have been reserved for "inline event handlers". As for suggested names, it is better to name them "labelOn" and "labelOff". The reason for hiding on-attributes from setAttribute/getAttribute was problem with ordering/finding handlers on the node. This can now be changed, however any attribute starting "on" would still throw handler compilation error if its content is not valid JavaScript.

ilinsky avatar May 06 '13 11:05 ilinsky

Can't you use lazy evaluation? Until event fires event handler is not created? That's how it works in FF/Chrome.

tedbeer avatar May 06 '13 12:05 tedbeer

Good idea for optimization. I will look if this is possible. Still, suppose it is lazy evaluated, and event passes through with matching name, should the attribute be then treated as handler? Isn't this going to be source of unexpected problems?

ilinsky avatar May 06 '13 12:05 ilinsky

Ed, can you open separate request for lazy evaluation?

ilinsky avatar May 06 '13 12:05 ilinsky

All attributes including inline handlers should be treated as attributes. But some of them may have additional meaning.

tedbeer avatar May 06 '13 12:05 tedbeer

Ok, who and how should make sense of 'some with additional meaning'? Suggestions?

ilinsky avatar May 06 '13 12:05 ilinsky

At this moment attributes starting 'on-' can be treated in 2 ways. But I agree - without lazy evaluating it will be hard to solve the issue.

tedbeer avatar May 06 '13 12:05 tedbeer

Question is still open. Suppose we have lazy evaluation in place. Now there is an attribute "once" specified on an element, and an event passes this node with name "ce". Shall the handler be instantiated now and invoked? If not, who and where would have to have provided instructions otherwise.

ilinsky avatar May 06 '13 12:05 ilinsky

When @once attribute is created and special meaning is detected null event handler is created to handle 'ce' events. When 'ce' event is fired null event handler is replaced by actual event handler and invoked. At this moment syntax error exception can be thrown if attribute value is invalid JS. When @once attribute is removed related event handler also is removed.

tedbeer avatar May 06 '13 13:05 tedbeer

Who and where do you think should provide "special meaning" to attributes?

ilinsky avatar May 06 '13 13:05 ilinsky