CsQuery icon indicating copy to clipboard operation
CsQuery copied to clipboard

DOM Object Annotations

Open iSynaptic opened this issue 10 years ago • 3 comments

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?

iSynaptic avatar May 01 '14 20:05 iSynaptic

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.

jamietre avatar May 02 '14 14:05 jamietre

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 avatar May 06 '14 18:05 iSynaptic

@iSynaptic what about data attributes?

benjamingr avatar May 11 '14 13:05 benjamingr