jamietre / CsQuery

CsQuery is a complete CSS selector engine, HTML parser, and jQuery port for C# and .NET 4.
Other
1.16k stars 250 forks source link

DOM Object Annotations #154

Closed iSynaptic closed 1 year ago

iSynaptic commented 10 years ago

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?

jamietre commented 10 years ago

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.

iSynaptic commented 10 years ago

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.

benjamingr commented 10 years ago

@iSynaptic what about data attributes?