Skip to content

Instantly share code, notes, and snippets.

@annevk
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save annevk/e9e61801fcfb251389ef to your computer and use it in GitHub Desktop.
Save annevk/e9e61801fcfb251389ef to your computer and use it in GitHub Desktop.
Imperative Distribution

After Ryosuke, Travis, Wilson, William, and I discussed the problem for an hour over a burrito, William came up with this approach. All hail William.

var shadow = host.createShadowRoot({
mode: "closed",
distribute: (distributionList, insertionList) => {
for(var i = 0; i < distributionList.length; i++) {
for(var ii = 0; ii < insertionList.length; ii++) {
var select = insertionList[ii].getAttribute("select")
if(select != null && distributionList[i].matches(select)) {
insertionList[ii].add(distrubtionList[i])
} else if(select == null) {
insertionList[ii].add(distrubtionList[i])
}
}
}
}
})
host.shadowRoot.distribute()
// mutation observer code that invokes distribute() on childList changes
callback DistributionCallback = void (sequence<(Text or Element)>, sequence<HTMLContentElement>);
enum ShadowRootMode { "open", "closed" };
dictionary ShadowRootInit {
require ShadowRootMode mode;
require DistributionCallback distribute;
};
partial interface Element {
ShadowRoot createShadowRoot(ShadowRootInit options);
};
partial interface ShadowRoot {
void distribute(); // invoke the callback, recursively if there's nesting
};
interface HTMLContentElement : HTMLElement {
void add((Text or Element) node);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment