遍历 nsIDOMNodeList

发布于 2024-12-10 01:37:56 字数 885 浏览 0 评论 0原文

我正在为 Firefox 开发一个 C++ 组件,并试图将我的注意力集中在 XPCOM 及其所有部分上。这真的很令人困惑,所以我正在尝试解决它,但我正在尝试浏览一个页面并获取其所有链接。我试图弄清楚所有对象的作用。因此,如果我有这个接口:

interface nsIPageSummary : nsISupports {
    boolean saveSummary(in nsIDOMDocument document, 
                        out unsigned long numLinks,
                        out unsigned long numImages);
};

在 IDL 中定义,我的 C++ 代码中的方法将如下所示:

SaveSummary(nsIDOMDocument* inDoc, PRBool* outSuccess)
{
    *outSuccess = PR_FALSE;
    nsCOMPtr<nsIDOMNodeList> nodeList;
    inDoc->GetElementsByTagName(NS_LITERAL_STRING("A"), getter_AddRefs(nodeList));
}

我知道 C++ 方法需要更多参数来与接口中定义的参数相匹配,但我不明白所有输入是如何进行的还有效。就实际列表而言,我是否正确理解该

inDoc->GetElementsByTagName(NS_LITERAL_STRING("A"), getter_AddRefs(nodeList));

行将 inDoc 中的所有“A”标签放入 nodeList 中?我只需要遍历 nodeList 即可获取所有这些?

I'm working on a C++ component for Firefox and I'm trying to wrap my mind around XPCOM and all of its parts. It's really confusing so I'm trying to work through it but I'm trying to walk through a page and get all its links. I'm trying to figure out what all the objects do. So if I have this interface:

interface nsIPageSummary : nsISupports {
    boolean saveSummary(in nsIDOMDocument document, 
                        out unsigned long numLinks,
                        out unsigned long numImages);
};

defined in the IDL, the method in my C++ code would look like:

SaveSummary(nsIDOMDocument* inDoc, PRBool* outSuccess)
{
    *outSuccess = PR_FALSE;
    nsCOMPtr<nsIDOMNodeList> nodeList;
    inDoc->GetElementsByTagName(NS_LITERAL_STRING("A"), getter_AddRefs(nodeList));
}

I know the C++ method needs more parameters to match up with the one defined in the interface but I don't understand how all the typing works yet. In terms of the actual list, am I right in understanding that the

inDoc->GetElementsByTagName(NS_LITERAL_STRING("A"), getter_AddRefs(nodeList));

line puts all the "A" tags from the inDoc into the nodeList? And I would just have to walk through nodeList to get them all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可遇━不可求 2024-12-17 01:37:56

您使用 xpidl 编译接口定义以获得 C++ 头文件 - 您可以检查该文件以了解接口定义是如何转换的。您的方法定义实际上应该如下所示:

nsresult SaveSummary(nsIDOMDocument* inDoc, PRUint32* outNumLinks, PRUint32* outNumImages, PRBool* outSuccess)

我建议在接口定义中也使用类型 PRUint32 而不是不明确的 unsigned long 。我还建议去掉返回值:XPCOM 方法总是返回 nsresult,它是 NS_OK 或错误代码,这足以指示成功或失败。从 JavaScript 调用时,错误代码会自动转换为异常。最后,我建议使用小写标签名称:虽然在 HTML 中(不区分大小写)并不重要,但在 XHTML 文档中仅接受小写标签名称。所以你的接口定义应该是这样的:

interface nsIPageSummary : nsISupports {
    void saveSummary(in nsIDOMDocument document, 
                     out PRUint32 numLinks,
                     out PRUint32 numImages);
};

以及相应的实现:

nsresult
SaveSummary(nsIDOMDocument* inDoc, PRUint32* outNumLinks, PRUint32* outNumImages)
{
  nsresult rv;

  nsCOMPtr<nsIDOMNodeList> nodeList;
  rv = inDoc->GetElementsByTagName(NS_LITERAL_STRING("a"), getter_AddRefs(nodeList));
  if (NS_FAILED(rv))
    return rv;

  rv = nodeList->GetLength(outNumLinks);
  if (NS_FAILED(rv))
    return rv;

  rv = inDoc->GetElementsByTagName(NS_LITERAL_STRING("img"), getter_AddRefs(nodeList));
  if (NS_FAILED(rv))
    return rv;

  rv = nodeList->GetLength(outNumImages);
  if (NS_FAILED(rv))
    return rv;

  return NS_OK;
}

You compile your interface definition using xpidl to get a C++ header file - you can check that file to see how interface definitions are translated. Your method definition should actually look like thit:

nsresult SaveSummary(nsIDOMDocument* inDoc, PRUint32* outNumLinks, PRUint32* outNumImages, PRBool* outSuccess)

I would suggest to use the type PRUint32 instead of the ambiguous unsigned long in the interface definition as well. I would also suggest getting rid of the return value: XPCOM methods always return nsresult which is either NS_OK or an error code, this is enough to indicate success or failure. An error code is automatically translated into an exception when called from JavaScript. And finally I would recommend using lower-case tag names: while it won't matter in HTML (case-insensitive), in an XHTML document only lower-case tag names are accepted. So your interface definition should look like this:

interface nsIPageSummary : nsISupports {
    void saveSummary(in nsIDOMDocument document, 
                     out PRUint32 numLinks,
                     out PRUint32 numImages);
};

And the corresponding implementation:

nsresult
SaveSummary(nsIDOMDocument* inDoc, PRUint32* outNumLinks, PRUint32* outNumImages)
{
  nsresult rv;

  nsCOMPtr<nsIDOMNodeList> nodeList;
  rv = inDoc->GetElementsByTagName(NS_LITERAL_STRING("a"), getter_AddRefs(nodeList));
  if (NS_FAILED(rv))
    return rv;

  rv = nodeList->GetLength(outNumLinks);
  if (NS_FAILED(rv))
    return rv;

  rv = inDoc->GetElementsByTagName(NS_LITERAL_STRING("img"), getter_AddRefs(nodeList));
  if (NS_FAILED(rv))
    return rv;

  rv = nodeList->GetLength(outNumImages);
  if (NS_FAILED(rv))
    return rv;

  return NS_OK;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文