How can I get access to the label, which is in a listitem?
I tried things like, getAnonymousNodes, childNodes. But none of them
returned any element.
In listbox.xml there are these bindings:
listitem:
<content>
<children>
<xul:listcell xbl:inherits="label,crop,disabled,flexlabel"/>
</children>
</content
<content>
<children>
listcell: (extends #listitem)
<content>
<children>
<xul:label class="listcell-label"
xbl:inherits="value=label,flex=flexlabel,crop,disabled" flex="1"
crop="right"/>
</children>
</content>
Regards
Bullja
The result of the example is:
count child of static checkbox: 2
count child of dynamic checkbox:
The error console is showing this error:
document.getAnonymousNodes(cbDyn) is null
Here the xul-code:
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="main-window"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<label id="result-static" value="count child of static checkbox: " />
<label id="result-dynamic" value="count child of dynamic checkbox: " />
<checkbox id="static-checkbox" label="static checkbox" />
<script type="application/x-javascript">
// Get reference to static checkbox
var cbStat = document.getElementById("static-checkbox");
// Create a new checkbox and add it to the window
var cbDyn = document.createElement('checkbox');
document.getElementById('main-window').appendChild(cbDyn );
cbDyn.setAttribute('label', 'dynamic created checkbox');
// Try to access the anonymous nodes of the static checkbox
var labelResult = document.getElementById('result-static');
labelResult.value += document.getAnonymousNodes(cbStat).length;
// Try to access the anonymous nodes of the static checkbox
// Doesn't work, but why?
var labelStatic = document.getElementById('result-dynamic');
labelStatic.value += document.getAnonymousNodes(cbDyn).length;
</script>
</window>
You're using XBL 1? ;)
Seriously, this is a good example of why XBL2 is needed. With XBL1,
binding attachment happens at two possible points in time:
1) When we create the layout object for a node.
2) When we create the JS wrapper for a node.
The latter is a hack to allow one to sorta work with nodes that have no
layout object for whatever reason (not in the DOM yet, or display:none)
but have some XBL bound to them that attaches methods or some such.
For xul, #1 happens right before onload is fired, after the whole page
has been rendered.
#2 happens only for XUL nodes that are clones of other XUL nodes or for
nodes that have been inserted into a document's content tree.
Now your code is running during parsing, so #1 doesn't apply. And since
you create the node via createElement, at the point when the JS wrapper
is created it's not in a document yet (and hasn't been created via
cloneNode()), so #2 doesn't apply.
Which means that the binding hasn't been attached to the node when you
make the getAnonymousNodes call. So there are no anonymous nodes.
Either creating the checkbox by cloning the existing one or running all
the code in the onload handler (recommended anyway, since that's where
overlay merging has happened and such) makes things work as one would
desire.
And yes, XBL really needs a sane binding mechnism. See above about XBL2.
-Boris