CsQuery
CsQuery copied to clipboard
DOM Object Annotations
Often when you are querying / manipulating the DOM, you would like to associate data you collect / compute as you traverse the DOM for later consumption. This pull request adds an Annotations collection to the IDomObject interface and DomObject implementation to allow arbitrary annotations (key/value pairs) to be associated with DOM objects.
Thoughts?
I like the idea -- however, my concern with adding this (or anything really) to the DomObject
entity is performance and memory consumption. Adding an object reference is 8 bytes per node, which adds up quickly for large DOMs.
Depending on what kind of information you need to associate with a DOM node, I would suggest a couple alternate approaches. You could use attributes, e.g. CQ.Data
which will only work well with things that can be easily serialized. You could create a collection that's separate from the DOM and contains WeakReferences
to the DOM objects -- I've used this technique to associate information with objects that I don't own before. The downside here is you basically need to garbage collect your table of associated data since you won't know when something gets destroyed. But it's doable.
I expected that might be a concern. What if the annotations only applied to DomElement? There would be much fewer of those per document and they are the most likely target for annotations.
@iSynaptic what about data attributes?