浏览器不可知的C ++ DOM接口 [英] Browser agnostic C++ DOM interface

查看:145
本文介绍了浏览器不可知的C ++ DOM接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中针对浏览器的 http://msdn.microsoft .com / en-us / library / aa752279(VS.85).aspx),Mozilla有 XPCOM ,基于 Gecko DOM 等。

When programming in C++ against the browser's DOM each engine has a different set of interfaces, IE has the COM based [MSHTML](http://msdn.microsoft.com/en-us/library/aa752279(VS.85).aspx), Mozilla has the XPCOM based Gecko DOM etc.

是否有一个通用的API,它具有适配器适用于主要浏览器(和版本)?

Is there a common API that has adapters for major browsers (and versions)?

作为澄清,一个用C ++编写的桌面应用程序,它与浏览器交互,目前我们有单独的代码库支持IE和Mozilla,我试图减少重复的逻辑,并允许添加新的浏览器更少的努力。

As a clarification, the application in question is a desktop application written in C++ which interacts with browsers, currently we have separate code bases for support of IE and Mozilla and I'm trying to reduce duplications of logic and allow adding new browsers with less effort.

一个具体的例子是获取HTML元素的innerHTML属性。

A concrete example can be getting the innerHTML property of an HTML element.

// Firefox
nsAutoString html;
nsCOMPtr<nsIDOMNSHTMLElement> elem = do_QueryInterface(obj);
if (elem)
    elem->GetInnerHTML(html); 

// IE
CComBSTR html;
MSHTML::IHTMLElementPtr elem = obj;
if (elem) 
     elem->get_innerHTML(&html);


推荐答案

您最好的打赌似乎是定义自己的界面并写入不同的适配器。在你的C ++代码不会使用所有DOM遍历功能的可能性,你的界面将只需要定义一组紧凑的遍历函数。

Your best bet seems to be to define your own interface and write different adapters. In the likeliness that your C++ code won't be employing all DOM traversing capabilities, your interface will only have to define a compact set of traversal functions.

定义您自己的界面,您可以轻松地为您的单元测试编写一个模拟适配器。

As a bonus of defining your own interface, you can easily write a mock adapter for your unit testing.

这篇关于浏览器不可知的C ++ DOM接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆