コード例 #1
0
ファイル: nsTextEditRules.cpp プロジェクト: amyvmiwei/firefox
NS_IMETHODIMP
nsTextEditRules::Init(nsPlaintextEditor *aEditor, PRUint32 aFlags)
{
  if (!aEditor) { return NS_ERROR_NULL_POINTER; }

  mEditor = aEditor;  // we hold a non-refcounted reference back to our editor
  // call SetFlags only aftet mEditor has been initialized!
  SetFlags(aFlags);
  nsCOMPtr<nsISelection> selection;
  mEditor->GetSelection(getter_AddRefs(selection));
  NS_ASSERTION(selection, "editor cannot get selection");

  // Cache our body node, if available.
  nsIDOMNode *body = mEditor->GetRoot();

  // Put in a magic br if needed. This method handles null selection,
  // which should never happen anyway
  nsresult res = CreateBogusNodeIfNeeded(selection);
  if (NS_FAILED(res)) return res;

  if (mFlags & nsIPlaintextEditor::eEditorPlaintextMask)
  {
    // ensure trailing br node
    res = CreateTrailingBRIfNeeded();
    if (NS_FAILED(res)) return res;
  }

  if (body)
  {
    // create a range that is the entire body contents
    nsCOMPtr<nsIDOMRange> wholeDoc =
      do_CreateInstance("@mozilla.org/content/range;1");
    if (!wholeDoc) return NS_ERROR_NULL_POINTER;
    wholeDoc->SetStart(body,0);
    nsCOMPtr<nsIDOMNodeList> list;
    res = body->GetChildNodes(getter_AddRefs(list));
    if (NS_FAILED(res)) return res;
    if (!list) return NS_ERROR_FAILURE;

    PRUint32 listCount;
    res = list->GetLength(&listCount);
    if (NS_FAILED(res)) return res;

    res = wholeDoc->SetEnd(body, listCount);
    if (NS_FAILED(res)) return res;

    // replace newlines in that range with breaks
    res = ReplaceNewlines(wholeDoc);
  }

  PRBool deleteBidiImmediately = PR_FALSE;
  nsCOMPtr<nsIPrefBranch> prefBranch =
    do_GetService(NS_PREFSERVICE_CONTRACTID, &res);
  if (NS_SUCCEEDED(res))
    prefBranch->GetBoolPref("bidi.edit.delete_immediately",
                            &deleteBidiImmediately);
  mDeleteBidiImmediately = deleteBidiImmediately;

  return res;
}
コード例 #2
0
ファイル: nsTextEditRules.cpp プロジェクト: lofter2011/Icefox
NS_IMETHODIMP
nsTextEditRules::Init(nsPlaintextEditor *aEditor)
{
    if (!aEditor) {
        return NS_ERROR_NULL_POINTER;
    }

    mEditor = aEditor;  // we hold a non-refcounted reference back to our editor
    nsCOMPtr<nsISelection> selection;
    mEditor->GetSelection(getter_AddRefs(selection));
    NS_ASSERTION(selection, "editor cannot get selection");

    // Cache our body node, if available.
    nsIDOMNode *body = mEditor->GetRoot();

    // Put in a magic br if needed. This method handles null selection,
    // which should never happen anyway
    nsresult res = CreateBogusNodeIfNeeded(selection);
    NS_ENSURE_SUCCESS(res, res);

    // If the selection hasn't been set up yet, set it up collapsed to the end of
    // our editable content.
    PRInt32 rangeCount;
    res = selection->GetRangeCount(&rangeCount);
    NS_ENSURE_SUCCESS(res, res);
    if (!rangeCount) {
        res = mEditor->EndOfDocument();
        NS_ENSURE_SUCCESS(res, res);
    }

    if (IsPlaintextEditor())
    {
        // ensure trailing br node
        res = CreateTrailingBRIfNeeded();
        NS_ENSURE_SUCCESS(res, res);
    }

    if (body)
    {
        // create a range that is the entire body contents
        nsCOMPtr<nsIDOMRange> wholeDoc =
            do_CreateInstance("@mozilla.org/content/range;1");
        NS_ENSURE_TRUE(wholeDoc, NS_ERROR_NULL_POINTER);
        wholeDoc->SetStart(body,0);
        nsCOMPtr<nsIDOMNodeList> list;
        res = body->GetChildNodes(getter_AddRefs(list));
        NS_ENSURE_SUCCESS(res, res);
        NS_ENSURE_TRUE(list, NS_ERROR_FAILURE);

        PRUint32 listCount;
        res = list->GetLength(&listCount);
        NS_ENSURE_SUCCESS(res, res);

        res = wholeDoc->SetEnd(body, listCount);
        NS_ENSURE_SUCCESS(res, res);

        // replace newlines in that range with breaks
        res = ReplaceNewlines(wholeDoc);
    }

    PRBool deleteBidiImmediately = PR_FALSE;
    nsCOMPtr<nsIPrefBranch> prefBranch =
        do_GetService(NS_PREFSERVICE_CONTRACTID, &res);
    if (NS_SUCCEEDED(res))
        prefBranch->GetBoolPref("bidi.edit.delete_immediately",
                                &deleteBidiImmediately);
    mDeleteBidiImmediately = deleteBidiImmediately;

    return res;
}