зеркало из https://github.com/mozilla/pjs.git
Landing support for the 'innerHTML' property on HTML elements in mozilla, fixes part of bug 9201.
This commit is contained in:
Родитель
8c03eca95a
Коммит
6ff3742e4c
|
@ -28,6 +28,8 @@
|
|||
#include "nsICSSStyleRule.h"
|
||||
#include "nsICSSDeclaration.h"
|
||||
#include "nsIDocument.h"
|
||||
#include "nsIDocumentEncoder.h"
|
||||
#include "nsIDOMDocumentFragment.h"
|
||||
#include "nsIDOMAttr.h"
|
||||
#include "nsIDOMEventReceiver.h"
|
||||
#include "nsIDOMNamedNodeMap.h"
|
||||
|
@ -84,6 +86,12 @@
|
|||
#include "nsIDOMHTMLFormElement.h"
|
||||
#include "nsILanguageAtomService.h"
|
||||
|
||||
#include "nsIParser.h"
|
||||
#include "nsParserCIID.h"
|
||||
#include "nsIHTMLContentSink.h"
|
||||
#include "nsHTMLContentSinkStream.h"
|
||||
#include "nsXIFDTD.h"
|
||||
|
||||
// XXX todo: add in missing out-of-memory checks
|
||||
NS_DEFINE_IID(kIDOMHTMLElementIID, NS_IDOMHTMLELEMENT_IID);
|
||||
|
||||
|
@ -781,6 +789,79 @@ nsGenericHTMLElement::GetOffsetParent(nsIDOMElement** aOffsetParent)
|
|||
return res;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsGenericHTMLElement::GetInnerHTML(nsString& aInnerHTML)
|
||||
{
|
||||
nsCOMPtr<nsIDOMNode> thisNode(do_QueryInterface(mContent));
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
// Frist we create XIF for the children of this node...
|
||||
nsAutoString buf;
|
||||
nsXIFConverter xifc(buf);
|
||||
|
||||
PRUint32 i, count = 0;
|
||||
nsCOMPtr<nsIDOMNodeList> childNodes;
|
||||
thisNode->GetChildNodes(getter_AddRefs(childNodes));
|
||||
if (childNodes)
|
||||
childNodes->GetLength(&count);
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
nsCOMPtr<nsIDOMNode> child;
|
||||
childNodes->Item(i, getter_AddRefs(child));
|
||||
rv = mDocument->ToXIF(xifc, child);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
}
|
||||
|
||||
// And then we use the parser and sinks to create the HTML.
|
||||
|
||||
static NS_DEFINE_IID(kCParserCID, NS_PARSER_IID);
|
||||
nsCOMPtr<nsIParser> parser = do_CreateInstance(kCParserCID, &rv);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
nsCOMPtr<nsIHTMLContentSink> sink;
|
||||
rv = NS_New_HTML_ContentSinkStream(getter_AddRefs(sink), &aInnerHTML,
|
||||
nsIDocumentEncoder::OutputNoDoctype);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
parser->SetContentSink(sink);
|
||||
nsCOMPtr<nsIDTD> dtd;
|
||||
|
||||
rv = NS_NewXIFDTD(getter_AddRefs(dtd));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
parser->RegisterDTD(dtd);
|
||||
parser->Parse(buf, 0, NS_ConvertASCIItoUCS2("text/xif"), PR_FALSE, PR_TRUE);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsGenericHTMLElement::SetInnerHTML(const nsString& aInnerHTML)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
nsRange *range = new nsRange;
|
||||
NS_ENSURE_TRUE(range, NS_ERROR_OUT_OF_MEMORY);
|
||||
NS_ADDREF(range);
|
||||
|
||||
nsCOMPtr<nsIDOMNode> thisNode(do_QueryInterface(mContent));
|
||||
rv = range->SelectNodeContents(thisNode);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
rv = range->DeleteContents();
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
nsCOMPtr<nsIDOMDocumentFragment> df;
|
||||
|
||||
rv = range->CreateContextualFragment(aInnerHTML, getter_AddRefs(df));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
NS_RELEASE(range);
|
||||
|
||||
nsCOMPtr<nsIDOMNode> tmpNode;
|
||||
return thisNode->AppendChild(df, getter_AddRefs(tmpNode));
|
||||
}
|
||||
|
||||
static nsIHTMLStyleSheet* GetAttrStyleSheet(nsIDocument* aDocument)
|
||||
{
|
||||
nsIHTMLStyleSheet* sheet = nsnull;
|
||||
|
|
|
@ -98,6 +98,8 @@ public:
|
|||
nsresult GetOffsetWidth(PRInt32* aOffsetWidth);
|
||||
nsresult GetOffsetHeight(PRInt32* aOffsetHeight);
|
||||
nsresult GetOffsetParent(nsIDOMElement** aOffsetParent);
|
||||
nsresult GetInnerHTML(nsString& aInnerHTML);
|
||||
nsresult SetInnerHTML(const nsString& aInnerHTML);
|
||||
nsresult GetOffsetRect(nsRect& aRect,
|
||||
nsIAtom* aOffsetParentTag,
|
||||
nsIContent** aOffsetParent);
|
||||
|
@ -539,6 +541,12 @@ public:
|
|||
} \
|
||||
NS_IMETHOD GetOffsetParent(nsIDOMElement** aOffsetParent) { \
|
||||
return _g.GetOffsetParent(aOffsetParent); \
|
||||
} \
|
||||
NS_IMETHOD GetInnerHTML(nsString& aInnerHTML) { \
|
||||
return _g.GetInnerHTML(aInnerHTML); \
|
||||
} \
|
||||
NS_IMETHOD SetInnerHTML(const nsString& aInnerHTML) { \
|
||||
return _g.SetInnerHTML(aInnerHTML); \
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -66,6 +66,9 @@ public:
|
|||
NS_IMETHOD GetOffsetHeight(PRInt32* aOffsetHeight)=0;
|
||||
|
||||
NS_IMETHOD GetOffsetParent(nsIDOMElement** aOffsetParent)=0;
|
||||
|
||||
NS_IMETHOD GetInnerHTML(nsString& aInnerHTML)=0;
|
||||
NS_IMETHOD SetInnerHTML(const nsString& aInnerHTML)=0;
|
||||
};
|
||||
|
||||
|
||||
|
@ -86,6 +89,8 @@ public:
|
|||
NS_IMETHOD GetOffsetWidth(PRInt32* aOffsetWidth); \
|
||||
NS_IMETHOD GetOffsetHeight(PRInt32* aOffsetHeight); \
|
||||
NS_IMETHOD GetOffsetParent(nsIDOMElement** aOffsetParent); \
|
||||
NS_IMETHOD GetInnerHTML(nsString& aInnerHTML); \
|
||||
NS_IMETHOD SetInnerHTML(const nsString& aInnerHTML); \
|
||||
|
||||
|
||||
|
||||
|
@ -106,6 +111,8 @@ public:
|
|||
NS_IMETHOD GetOffsetWidth(PRInt32* aOffsetWidth) { return _to GetOffsetWidth(aOffsetWidth); } \
|
||||
NS_IMETHOD GetOffsetHeight(PRInt32* aOffsetHeight) { return _to GetOffsetHeight(aOffsetHeight); } \
|
||||
NS_IMETHOD GetOffsetParent(nsIDOMElement** aOffsetParent) { return _to GetOffsetParent(aOffsetParent); } \
|
||||
NS_IMETHOD GetInnerHTML(nsString& aInnerHTML) { return _to GetInnerHTML(aInnerHTML); } \
|
||||
NS_IMETHOD SetInnerHTML(const nsString& aInnerHTML) { return _to SetInnerHTML(aInnerHTML); } \
|
||||
|
||||
|
||||
extern "C" NS_DOM nsresult NS_InitHTMLElementClass(nsIScriptContext *aContext, void **aPrototype);
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
interface HTMLElement : Element {
|
||||
/* IID: { 0xa6cf9085, 0x15b3, 0x11d2, \
|
||||
{ 0x93, 0x2e, 0x00, 0x80, 0x5f, 0x8a, 0xdd, 0x32 } } */
|
||||
|
||||
attribute DOMString id;
|
||||
attribute DOMString title;
|
||||
attribute DOMString lang;
|
||||
attribute DOMString dir;
|
||||
attribute DOMString className;
|
||||
readonly attribute CSSStyleDeclaration style;
|
||||
|
||||
/* Temporary till we find a place for them */
|
||||
readonly attribute int offsetTop;
|
||||
readonly attribute int offsetLeft;
|
||||
readonly attribute int offsetWidth;
|
||||
readonly attribute int offsetHeight;
|
||||
readonly attribute Element offsetParent;
|
||||
};
|
|
@ -358,6 +358,7 @@ enum nsDOMProp {
|
|||
NS_DOM_PROP_HTMLELEMENT_CLASSNAME,
|
||||
NS_DOM_PROP_HTMLELEMENT_DIR,
|
||||
NS_DOM_PROP_HTMLELEMENT_ID,
|
||||
NS_DOM_PROP_HTMLELEMENT_INNERHTML,
|
||||
NS_DOM_PROP_HTMLELEMENT_LANG,
|
||||
NS_DOM_PROP_HTMLELEMENT_OFFSETHEIGHT,
|
||||
NS_DOM_PROP_HTMLELEMENT_OFFSETLEFT,
|
||||
|
|
|
@ -357,6 +357,7 @@
|
|||
"htmlelement.classname", \
|
||||
"htmlelement.dir", \
|
||||
"htmlelement.id", \
|
||||
"htmlelement.innerhtml", \
|
||||
"htmlelement.lang", \
|
||||
"htmlelement.offsetheight", \
|
||||
"htmlelement.offsetleft", \
|
||||
|
|
|
@ -60,7 +60,8 @@ enum HTMLElement_slots {
|
|||
HTMLELEMENT_OFFSETLEFT = -8,
|
||||
HTMLELEMENT_OFFSETWIDTH = -9,
|
||||
HTMLELEMENT_OFFSETHEIGHT = -10,
|
||||
HTMLELEMENT_OFFSETPARENT = -11
|
||||
HTMLELEMENT_OFFSETPARENT = -11,
|
||||
HTMLELEMENT_INNERHTML = -12
|
||||
};
|
||||
|
||||
/***********************************************************************/
|
||||
|
@ -217,6 +218,18 @@ GetHTMLElementProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
|
|||
}
|
||||
break;
|
||||
}
|
||||
case HTMLELEMENT_INNERHTML:
|
||||
{
|
||||
rv = secMan->CheckScriptAccess(cx, obj, NS_DOM_PROP_HTMLELEMENT_INNERHTML, PR_FALSE);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
nsAutoString prop;
|
||||
rv = a->GetInnerHTML(prop);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
nsJSUtils::nsConvertStringToJSVal(prop, cx, vp);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return nsJSUtils::nsCallJSScriptObjectGetProperty(a, cx, obj, id, vp);
|
||||
}
|
||||
|
@ -310,6 +323,18 @@ SetHTMLElementProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
|
|||
}
|
||||
break;
|
||||
}
|
||||
case HTMLELEMENT_INNERHTML:
|
||||
{
|
||||
rv = secMan->CheckScriptAccess(cx, obj, NS_DOM_PROP_HTMLELEMENT_INNERHTML, PR_TRUE);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
nsAutoString prop;
|
||||
nsJSUtils::nsConvertJSValToString(prop, cx, *vp);
|
||||
|
||||
rv = a->SetInnerHTML(prop);
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return nsJSUtils::nsCallJSScriptObjectSetProperty(a, cx, obj, id, vp);
|
||||
}
|
||||
|
@ -390,6 +415,7 @@ static JSPropertySpec HTMLElementProperties[] =
|
|||
{"offsetWidth", HTMLELEMENT_OFFSETWIDTH, JSPROP_ENUMERATE | JSPROP_READONLY},
|
||||
{"offsetHeight", HTMLELEMENT_OFFSETHEIGHT, JSPROP_ENUMERATE | JSPROP_READONLY},
|
||||
{"offsetParent", HTMLELEMENT_OFFSETPARENT, JSPROP_ENUMERATE | JSPROP_READONLY},
|
||||
{"innerHTML", HTMLELEMENT_INNERHTML, JSPROP_ENUMERATE},
|
||||
{0}
|
||||
};
|
||||
|
||||
|
|
|
@ -28,6 +28,8 @@
|
|||
#include "nsICSSStyleRule.h"
|
||||
#include "nsICSSDeclaration.h"
|
||||
#include "nsIDocument.h"
|
||||
#include "nsIDocumentEncoder.h"
|
||||
#include "nsIDOMDocumentFragment.h"
|
||||
#include "nsIDOMAttr.h"
|
||||
#include "nsIDOMEventReceiver.h"
|
||||
#include "nsIDOMNamedNodeMap.h"
|
||||
|
@ -84,6 +86,12 @@
|
|||
#include "nsIDOMHTMLFormElement.h"
|
||||
#include "nsILanguageAtomService.h"
|
||||
|
||||
#include "nsIParser.h"
|
||||
#include "nsParserCIID.h"
|
||||
#include "nsIHTMLContentSink.h"
|
||||
#include "nsHTMLContentSinkStream.h"
|
||||
#include "nsXIFDTD.h"
|
||||
|
||||
// XXX todo: add in missing out-of-memory checks
|
||||
NS_DEFINE_IID(kIDOMHTMLElementIID, NS_IDOMHTMLELEMENT_IID);
|
||||
|
||||
|
@ -781,6 +789,79 @@ nsGenericHTMLElement::GetOffsetParent(nsIDOMElement** aOffsetParent)
|
|||
return res;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsGenericHTMLElement::GetInnerHTML(nsString& aInnerHTML)
|
||||
{
|
||||
nsCOMPtr<nsIDOMNode> thisNode(do_QueryInterface(mContent));
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
// Frist we create XIF for the children of this node...
|
||||
nsAutoString buf;
|
||||
nsXIFConverter xifc(buf);
|
||||
|
||||
PRUint32 i, count = 0;
|
||||
nsCOMPtr<nsIDOMNodeList> childNodes;
|
||||
thisNode->GetChildNodes(getter_AddRefs(childNodes));
|
||||
if (childNodes)
|
||||
childNodes->GetLength(&count);
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
nsCOMPtr<nsIDOMNode> child;
|
||||
childNodes->Item(i, getter_AddRefs(child));
|
||||
rv = mDocument->ToXIF(xifc, child);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
}
|
||||
|
||||
// And then we use the parser and sinks to create the HTML.
|
||||
|
||||
static NS_DEFINE_IID(kCParserCID, NS_PARSER_IID);
|
||||
nsCOMPtr<nsIParser> parser = do_CreateInstance(kCParserCID, &rv);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
nsCOMPtr<nsIHTMLContentSink> sink;
|
||||
rv = NS_New_HTML_ContentSinkStream(getter_AddRefs(sink), &aInnerHTML,
|
||||
nsIDocumentEncoder::OutputNoDoctype);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
parser->SetContentSink(sink);
|
||||
nsCOMPtr<nsIDTD> dtd;
|
||||
|
||||
rv = NS_NewXIFDTD(getter_AddRefs(dtd));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
parser->RegisterDTD(dtd);
|
||||
parser->Parse(buf, 0, NS_ConvertASCIItoUCS2("text/xif"), PR_FALSE, PR_TRUE);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsGenericHTMLElement::SetInnerHTML(const nsString& aInnerHTML)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
nsRange *range = new nsRange;
|
||||
NS_ENSURE_TRUE(range, NS_ERROR_OUT_OF_MEMORY);
|
||||
NS_ADDREF(range);
|
||||
|
||||
nsCOMPtr<nsIDOMNode> thisNode(do_QueryInterface(mContent));
|
||||
rv = range->SelectNodeContents(thisNode);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
rv = range->DeleteContents();
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
nsCOMPtr<nsIDOMDocumentFragment> df;
|
||||
|
||||
rv = range->CreateContextualFragment(aInnerHTML, getter_AddRefs(df));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
NS_RELEASE(range);
|
||||
|
||||
nsCOMPtr<nsIDOMNode> tmpNode;
|
||||
return thisNode->AppendChild(df, getter_AddRefs(tmpNode));
|
||||
}
|
||||
|
||||
static nsIHTMLStyleSheet* GetAttrStyleSheet(nsIDocument* aDocument)
|
||||
{
|
||||
nsIHTMLStyleSheet* sheet = nsnull;
|
||||
|
|
|
@ -98,6 +98,8 @@ public:
|
|||
nsresult GetOffsetWidth(PRInt32* aOffsetWidth);
|
||||
nsresult GetOffsetHeight(PRInt32* aOffsetHeight);
|
||||
nsresult GetOffsetParent(nsIDOMElement** aOffsetParent);
|
||||
nsresult GetInnerHTML(nsString& aInnerHTML);
|
||||
nsresult SetInnerHTML(const nsString& aInnerHTML);
|
||||
nsresult GetOffsetRect(nsRect& aRect,
|
||||
nsIAtom* aOffsetParentTag,
|
||||
nsIContent** aOffsetParent);
|
||||
|
@ -539,6 +541,12 @@ public:
|
|||
} \
|
||||
NS_IMETHOD GetOffsetParent(nsIDOMElement** aOffsetParent) { \
|
||||
return _g.GetOffsetParent(aOffsetParent); \
|
||||
} \
|
||||
NS_IMETHOD GetInnerHTML(nsString& aInnerHTML) { \
|
||||
return _g.GetInnerHTML(aInnerHTML); \
|
||||
} \
|
||||
NS_IMETHOD SetInnerHTML(const nsString& aInnerHTML) { \
|
||||
return _g.SetInnerHTML(aInnerHTML); \
|
||||
}
|
||||
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче