Closed Bug 1001351 Opened 11 years ago Closed 11 years ago

Allow null-safe conversion from nsINode to nsIDOMNode

Categories

(Core :: DOM: Core & HTML, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla32

People

(Reporter: ayg, Assigned: ayg)

References

Details

Attachments

(1 file)

I sometimes convert code from using nsIDOMNode to using nsINode. Often I have to convert from nsINode to nsIDOMNode to call legacy methods. ->AsDOMNode() is nice, but it crashes if the node is null. I would like a method nsIDOMNode* AsDOMNode(nsINode* aNode) { return aNode ? aNode->AsDOMNode() : nullptr; } to ease conversion. Or some comparably convenient mechanism, I don't really care what as long as it's not lengthy to type.
Summary: Allow constructing nsIDOMNode from nsINode → Allow null-safe conversion from nsINode to nsIDOMNode
Attached patch patchSplinter Review
I can use a different technique if you prefer, I don't really care how it's implemented. The callsite I added that uses it is a case where there was a real crash because I forgot to do a null-check before using ->AsDOMNode(), which fortunately was caught by tests before landing.
Attachment #8412495 - Flags: review?(bzbarsky)
I would make the method a static method of nsINode. Polluting global scope with AsDOMNode isn't too nice.
And perhaps it should be GetAsDOMNode(nsINode*) to hint that it may return null.
Then instead of nsCOMPtr<nsIDOMNode> ret = dont_AddRef(AsDOMNode(GetBlockNodeParent(node).take())); you get nsCOMPtr<nsIDOMNode> ret = dont_AddRef(nsINode::GetAsDOMNode(GetBlockNodeParent(node).take())); which I don't like nearly as much. AsDOMNode is already going to be in mozilla::dom, I think that's enough.
Comment on attachment 8412495 [details] [diff] [review] patch I would really like to not make this any longer than it is -- the editor cleanup requires a lot of stuff like + return InsertNode(AsDOMNode(aContent), AsDOMNode(aParent), aPosition); which would be a lot more cumbersome if it were longer. Editor code right now mixes nsINode* and nsIDOMNode* everywhere, and constant conversion is required as I port it to use more nsINode*. I don't think namespace pollution is an issue here, because "AsDOMNode" is a very specific name, which only makes sense for a function, and nobody is going to make a function with name and signature "AsDOMNode(nsINode*)" that isn't exactly this function. That said, this blocks ongoing editor cleanup work, so if making the name longer will get it reviewed faster, I guess I will.
Attachment #8412495 - Flags: review?(bzbarsky) → review?(bugs)
Comment on attachment 8412495 [details] [diff] [review] patch Make it GetAsDOMNode so that it is clear that null may be returned. (And already_AddRefed<nsIDOMNode> GetAsDOMNode(already_AddRefed<nsINode>& aNode); might be useful too.)
Attachment #8412495 - Flags: review?(bugs) → review+
(Aryeh, if this doesn't suit your needs, please feel free to add something specific to editor/.)
Status: NEW → RESOLVED
Closed: 11 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla32
Component: DOM → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: