Bug 1476906 - Change nsIHTMLEditor::ReplaceHeadContentsWithHTML() to a non-virtual method of HTMLEditor r=m_kato

nsIHTMLEditor::ReplaceHeadContentsWithHTML() is used only by HTMLEditor
internally. So, it shouldn't be an nsIHTMLEditor's method.  This patch
changes it as a non-virtual method of HTMLEditor and rename it to
ReplaceHeadContentsWithSourceWithTransaction() for consistency with other
methods.

MozReview-Commit-ID: GT3maKEbZuP

--HG--
extra : rebase_source : 2dc8c682f0dd82d3fc9032fbf5195de121975c32
This commit is contained in:
Masayuki Nakano 2018-07-19 21:31:19 +09:00
Родитель 2dbbfa9843
Коммит 905e9bda47
3 изменённых файлов: 49 добавлений и 35 удалений

Просмотреть файл

@ -1214,14 +1214,9 @@ HTMLEditor::CollapseSelectionToDeepestNonTableFirstChild(Selection* aSelection,
selection->Collapse(node, 0);
}
/**
* This is mostly like InsertHTMLWithCharsetAndContext, but we can't use that
* because it is selection-based and the rules code won't let us edit under the
* <head> node
*/
NS_IMETHODIMP
HTMLEditor::ReplaceHeadContentsWithHTML(const nsAString& aSourceToInsert)
nsresult
HTMLEditor::ReplaceHeadContentsWithSourceWithTransaction(
const nsAString& aSourceToInsert)
{
// don't do any post processing, rules get confused
AutoTopLevelEditSubActionNotifier
@ -1244,55 +1239,63 @@ HTMLEditor::ReplaceHeadContentsWithHTML(const nsAString& aSourceToInsert)
RefPtr<nsContentList> nodeList =
document->GetElementsByTagName(NS_LITERAL_STRING("head"));
NS_ENSURE_TRUE(nodeList, NS_ERROR_NULL_POINTER);
if (NS_WARN_IF(!nodeList)) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIContent> headNode = nodeList->Item(0);
NS_ENSURE_TRUE(headNode, NS_ERROR_NULL_POINTER);
if (NS_WARN_IF(!headNode)) {
return NS_ERROR_FAILURE;
}
// First, make sure there are no return chars in the source. Bad things
// happen if you insert returns (instead of dom newlines, \n) into an editor
// document.
nsAutoString inputString (aSourceToInsert); // hope this does copy-on-write
nsAutoString inputString(aSourceToInsert);
// Windows linebreaks: Map CRLF to LF:
inputString.ReplaceSubstring(u"\r\n", u"\n");
inputString.ReplaceSubstring(NS_LITERAL_STRING("\r\n"),
NS_LITERAL_STRING("\n"));
// Mac linebreaks: Map any remaining CR to LF:
inputString.ReplaceSubstring(u"\r", u"\n");
inputString.ReplaceSubstring(NS_LITERAL_STRING("\r"),
NS_LITERAL_STRING("\n"));
AutoPlaceholderBatch beginBatching(this);
// Get the first range in the selection, for context:
RefPtr<nsRange> range = selection->GetRangeAt(0);
NS_ENSURE_TRUE(range, NS_ERROR_NULL_POINTER);
if (NS_WARN_IF(!range)) {
return NS_ERROR_FAILURE;
}
ErrorResult err;
RefPtr<DocumentFragment> docfrag =
RefPtr<DocumentFragment> documentFragment =
range->CreateContextualFragment(inputString, err);
// XXXX BUG 50965: This is not returning the text between <title>...</title>
// Special code is needed in JS to handle title anyway, so it doesn't matter!
if (err.Failed()) {
#ifdef DEBUG
printf("Couldn't create contextual fragment: error was %X\n",
err.ErrorCodeAsInt());
#endif
return err.StealNSResult();
}
NS_ENSURE_TRUE(docfrag, NS_ERROR_NULL_POINTER);
if (NS_WARN_IF(!documentFragment)) {
return NS_ERROR_FAILURE;
}
// First delete all children in head
while (nsCOMPtr<nsIContent> child = headNode->GetFirstChild()) {
nsresult rv = DeleteNodeWithTransaction(*child);
NS_ENSURE_SUCCESS(rv, rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
// Now insert the new nodes
int32_t offsetOfNewNode = 0;
// Loop over the contents of the fragment and move into the document
while (nsCOMPtr<nsIContent> child = docfrag->GetFirstChild()) {
while (nsCOMPtr<nsIContent> child = documentFragment->GetFirstChild()) {
nsresult rv =
InsertNodeWithTransaction(*child,
EditorRawDOMPoint(headNode, offsetOfNewNode++));
@ -1362,13 +1365,15 @@ HTMLEditor::RebuildDocumentFromSource(const nsAString& aSourceString)
if (foundhead) {
if (foundclosehead) {
nsresult rv =
ReplaceHeadContentsWithHTML(Substring(beginhead, beginclosehead));
ReplaceHeadContentsWithSourceWithTransaction(
Substring(beginhead, beginclosehead));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
} else if (foundbody) {
nsresult rv =
ReplaceHeadContentsWithHTML(Substring(beginhead, beginbody));
ReplaceHeadContentsWithSourceWithTransaction(
Substring(beginhead, beginbody));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
@ -1376,7 +1381,9 @@ HTMLEditor::RebuildDocumentFromSource(const nsAString& aSourceString)
// XXX Without recourse to some parser/content sink/docshell hackery we
// don't really know where the head ends and the body begins so we assume
// that there is no body
nsresult rv = ReplaceHeadContentsWithHTML(Substring(beginhead, endtotal));
nsresult rv =
ReplaceHeadContentsWithSourceWithTransaction(
Substring(beginhead, endtotal));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
@ -1387,14 +1394,15 @@ HTMLEditor::RebuildDocumentFromSource(const nsAString& aSourceString)
NS_NAMED_LITERAL_STRING(head, "<head>");
if (foundclosehead) {
nsresult rv =
ReplaceHeadContentsWithHTML(head + Substring(begintotal,
beginclosehead));
ReplaceHeadContentsWithSourceWithTransaction(
head + Substring(begintotal, beginclosehead));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
} else if (foundbody) {
nsresult rv = ReplaceHeadContentsWithHTML(head + Substring(begintotal,
beginbody));
nsresult rv =
ReplaceHeadContentsWithSourceWithTransaction(
head + Substring(begintotal, beginbody));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
@ -1402,7 +1410,7 @@ HTMLEditor::RebuildDocumentFromSource(const nsAString& aSourceString)
// XXX Without recourse to some parser/content sink/docshell hackery we
// don't really know where the head ends and the body begins so we assume
// that there is no head
nsresult rv = ReplaceHeadContentsWithHTML(head);
nsresult rv = ReplaceHeadContentsWithSourceWithTransaction(head);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}

Просмотреть файл

@ -783,6 +783,17 @@ protected: // Shouldn't be used by friend classes
nsresult LoadHTML(const nsAString& aInputString);
/**
* ReplaceHeadContentsWithSourceWithTransaction() replaces all children of
* <head> element with given source code. This is undoable.
*
* @param aSourceToInsert HTML source fragment to replace the children
* of <head> element.
*/
nsresult
ReplaceHeadContentsWithSourceWithTransaction(
const nsAString& aSourceToInsert);
nsresult GetCSSBackgroundColorState(bool* aMixed, nsAString& aOutColor,
bool aBlockLevel);
nsresult GetHTMLBackgroundColorState(bool* aMixed, nsAString& outColor);

Просмотреть файл

@ -347,11 +347,6 @@ interface nsIHTMLEditor : nsISupports
*/
nsISupports getSelectedElement(in AString aTagName);
/**
* Replace all children of <HEAD> with string of HTML source
*/
void replaceHeadContentsWithHTML(in AString aSourceToInsert);
/**
* Return a new element with default attribute values
*