Closed Bug 1010760 Opened 10 years ago Closed 10 years ago

Add nsINode::IsText, AsText like for Element

Categories

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

enhancement
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla34

People

(Reporter: ayg, Assigned: ayg)

Details

Attachments

(1 file, 1 obsolete file)

It's better to call AsElement than cast to Element, because it asserts that it actually is an element.  Likewise it would be good to have for Text and nsIContent.  IsText() is a lot easier to type and read than IsNodeOfType(nsINode::eTEXT).
We have IsContent and AsContent
Summary: Add nsINode::IsText, AsText, IsContent, AsContent like for Element → Add nsINode::IsText, AsText like for Element
Attached patch Patch (obsolete) — Splinter Review
Attachment #8471546 - Flags: review?(bugs)
Comment on attachment 8471546 [details] [diff] [review]
Patch

IsContent and IsElement are non-virtual and inlined.
It would be odd if IsText() behaved significantly slower.
We need a bool flag.
Attachment #8471546 - Flags: review?(bugs) → review-
Do you actually want me to add a bool flag?  I don't imagine there would be enough callers to justify it.  If not, what would you prefer instead?  The code pattern we have right now is

  if (node->IsNodeOfType(nsINode::eTEXT)) {
    nsCOMPtr<Text> textNode = static_cast<Text>(node);
    // . . .

which is bad because a) it's long to type, and b) the static_cast has no safety check in debug builds.  If you would prefer, I could add just AsText() and not IsText(), which would solve (b), although the asymmetry in the API would be confusing.
Flags: needinfo?(bugs)
I want IsFoo methods to have same performance characteristics, so bool flag would be needed.

Would GetAsText() work? That hints that it may return null, and implementation could be then
something like.
Text* GetAsText()
{
  return IsNodeOfType(nsINode::eTEXT) ? static_cast<Text*>(this) : nullptr;
}
Flags: needinfo?(bugs)
No longer blocks: 1010761
Attached patch Patch v2Splinter Review
Attachment #8471546 - Attachment is obsolete: true
Attachment #8472311 - Flags: review?(bugs)
Attachment #8472311 - Flags: review?(bugs) → review+
https://hg.mozilla.org/mozilla-central/rev/1ebef29c93f6
Status: NEW → RESOLVED
Closed: 10 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla34
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: