Bug 147189. Part 2: Don't hide "significant" whitespace nodes.

r=bzbarsky sr=hewitt
This commit is contained in:
caillon%returnzero.com 2006-05-17 02:37:41 +00:00
Родитель a883c6c290
Коммит 451c27c8e2
6 изменённых файлов: 89 добавлений и 45 удалений

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

@ -20,6 +20,7 @@
*
* Contributor(s):
* Joe Hewitt <hewitt@netscape.com> (original author)
* Christopher A. Aillon <christopher@aillon.com>
*
*
* Alternatively, the contents of this file may be used under the terms of
@ -40,6 +41,7 @@
interface nsISimpleEnumerator;
interface nsISupportsArray;
interface nsIDOMCharacterData;
interface nsIDOMElement;
interface nsIDOMDocument;
interface nsIDOMCSSStyleRule;
@ -51,6 +53,9 @@ interface inIDOMUtils : nsISupports
nsISupportsArray getStyleRules(in nsIDOMElement aElement);
unsigned long getRuleWeight(in nsIDOMCSSStyleRule aRule);
unsigned long getRuleLine(in nsIDOMCSSStyleRule aRule);
// DOM Node utilities
boolean isIgnorableWhitespace(in nsIDOMCharacterData aDataNode);
// XBL utilities
nsISimpleEnumerator getBindingURLs(in nsIDOMElement aElement);

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

@ -1,4 +1,3 @@
/* */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
@ -21,6 +20,7 @@
*
* Contributor(s):
* Joe Hewitt <hewitt@netscape.com> (original author)
* Christopher A. Aillon <christopher@aillon.com>
*
*
* Alternatively, the contents of this file may be used under the terms of
@ -45,6 +45,8 @@
#include "nsIDOMElement.h"
#include "nsIDocument.h"
#include "nsIDOMDocument.h"
#include "nsIDOMCharacterData.h"
#include "nsITextContent.h"
#include "nsEnumeratorUtils.h"
#include "nsIXBLBinding.h"
#include "nsIStyleContext.h"
@ -91,8 +93,6 @@ inDOMUtils::GetStyleContextForContent(nsIContent* aContent,
return mCSSUtils->GetStyleContextForFrame(frame, aStyleContext);
}
NS_ASSERTION(aContent->IsContentOfType(nsIContent::eELEMENT),
"We need to deal with non-elements too? This code needs fixing");
// No frame. Do this the hard way.
// Get the parent style context
nsCOMPtr<nsIStyleContext> parentContext;
@ -108,8 +108,65 @@ inDOMUtils::GetStyleContextForContent(nsIContent* aContent,
nsCOMPtr<nsIPresContext> presContext;
aPresShell->GetPresContext(getter_AddRefs(presContext));
NS_ENSURE_TRUE(presContext, NS_ERROR_UNEXPECTED);
return presContext->ResolveStyleContextFor(aContent, parentContext, aStyleContext);
if (aContent->IsContentOfType(nsIContent::eELEMENT)) {
return presContext->ResolveStyleContextFor(aContent, parentContext,
aStyleContext);
}
return presContext->ResolveStyleContextForNonElement(parentContext,
aStyleContext);
}
NS_IMETHODIMP
inDOMUtils::IsIgnorableWhitespace(nsIDOMCharacterData *aDataNode,
PRBool *aReturn)
{
NS_PRECONDITION(aDataNode, "Must have a character data node");
NS_PRECONDITION(aReturn, "Must have an out parameter");
*aReturn = PR_FALSE;
nsCOMPtr<nsITextContent> textContent = do_QueryInterface(aDataNode);
NS_ASSERTION(textContent, "Does not implement nsITextContent!");
PRBool whiteSpaceOnly = PR_FALSE;
textContent->IsOnlyWhitespace(&whiteSpaceOnly);
if (!whiteSpaceOnly) {
return NS_OK;
}
// Okay. We have only white space. Let's check the white-space
// property now and make sure that this isn't preformatted text...
nsCOMPtr<nsIDOMWindowInternal> win = inLayoutUtils::GetWindowFor(aDataNode);
if (!win) {
// Hmm. Things are screwy if we have no window...
NS_ERROR("No window!");
return NS_OK;
}
nsCOMPtr<nsIPresShell> presShell = inLayoutUtils::GetPresShellFor(win);
NS_ASSERTION(presShell, "No pres shell!");
nsCOMPtr<nsIStyleContext> styleContext;
GetStyleContextForContent(textContent, presShell,
getter_AddRefs(styleContext));
if (styleContext) {
const nsStyleText* text = nsnull;
::GetStyleData(styleContext, &text);
NS_ASSERTION(text, "Could not get a style struct!");
*aReturn = !text->WhiteSpaceIsSignificant();
}
else {
// No style context. Let's just assume the default value of
// white-space: normal, which we can safely ignore.
*aReturn = PR_TRUE;
}
return NS_OK;
}
NS_IMETHODIMP
@ -128,8 +185,8 @@ inDOMUtils::GetStyleRules(nsIDOMElement *aElement, nsISupportsArray **_retval)
nsCOMPtr<nsIContent> content(do_QueryInterface(aElement));
nsCOMPtr<nsIStyleContext> styleContext;
nsresult rv = GetStyleContextForContent(content, shell,
getter_AddRefs(styleContext));
GetStyleContextForContent(content, shell,
getter_AddRefs(styleContext));
if (!styleContext) {
NS_ERROR("no StyleContext");
return NS_ERROR_UNEXPECTED;

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

@ -37,6 +37,7 @@
* ***** END LICENSE BLOCK ***** */
#include "inDOMView.h"
#include "inIDOMUtils.h"
#include "inLayoutUtils.h"
@ -45,12 +46,14 @@
#include "nsISupportsArray.h"
#include "nsIDOMNode.h"
#include "nsIDOMNodeList.h"
#include "nsIDOMCharacterData.h"
#include "nsIDOMAttr.h"
#include "nsIDOMDocument.h"
#include "nsIDOMNamedNodeMap.h"
#include "nsIDOMMutationEvent.h"
#include "nsIBindingManager.h"
#include "nsIDocument.h"
#include "nsIServiceManager.h"
////////////////////////////////////////////////////////////////////////
// inDOMViewNode
@ -1256,6 +1259,12 @@ inDOMView::AppendKidsToArray(nsIDOMNodeList* aKids, nsISupportsArray* aArray)
nsCOMPtr<nsIDOMNode> kid;
PRUint16 nodeType = 0;
PRBool filtered = PR_FALSE;
// Try and get DOM Utils in case we don't have one yet.
if (mShowWhitespaceNodes && !mDOMUtils) {
mDOMUtils = do_CreateInstance("@mozilla.org/inspector/dom-utils;1");
}
for (PRUint32 i = 0; i < l; ++i) {
aKids->Item(i, getter_AddRefs(kid));
kid->GetNodeType(&nodeType);
@ -1263,40 +1272,12 @@ inDOMView::AppendKidsToArray(nsIDOMNodeList* aKids, nsISupportsArray* aArray)
if (filtered) {
if ((nodeType == nsIDOMNode::TEXT_NODE ||
nodeType == nsIDOMNode::COMMENT_NODE) &&
!mShowWhitespaceNodes) {
// Check to see if this node contains only whitespace.
nsAutoString nodeValue;
kid->GetNodeValue(nodeValue);
nsAString::const_iterator start, end;
nodeValue.BeginReading(start);
nodeValue.EndReading(end);
PRUint32 size;
PRBool foundNonWhitespace = PR_FALSE;
for (; start != end; start.advance(size)) {
const PRUnichar *buf = start.get();
size = start.size_forward();
for (PRUint32 j = 0; j < size; ++j, ++buf) {
if (!nsCRT::IsAsciiSpace(*buf)) {
// Great, we found a non-whitspace char.
// Let's mark that, and break out.
foundNonWhitespace = PR_TRUE;
break;
}
}
// Another loop we need to break out of. That is,
// if we've already found what we're looking for.
if (foundNonWhitespace) {
break;
}
}
// Okay, we tried, but it seems everything here is whitespace.
// The user doesn't want to see it so suck it up and move on.
if (!foundNonWhitespace) {
!mShowWhitespaceNodes && mDOMUtils) {
nsCOMPtr<nsIDOMCharacterData> data = do_QueryInterface(kid);
NS_ASSERTION(data, "Does not implement nsIDOMCharacterData!");
PRBool ignore;
mDOMUtils->IsIgnorableWhitespace(data, &ignore);
if (ignore) {
continue;
}
}

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

@ -40,6 +40,7 @@
#define __inDOMView_h__
#include "inIDOMView.h"
#include "inIDOMUtils.h"
#include "nsITreeView.h"
#include "nsITreeSelection.h"
@ -133,6 +134,7 @@ protected:
nsCOMPtr<nsITreeBoxObject> mTree;
nsCOMPtr<nsITreeSelection> mSelection;
nsCOMPtr<inIDOMUtils> mDOMUtils;
PRPackedBool mShowAnonymous;
PRPackedBool mShowSubDocuments;

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

@ -52,17 +52,16 @@
#include "nsIViewManager.h"
#include "nsIWidget.h"
#include "nsIPresContext.h"
#include "nsIStyleContext.h"
#include "nsXULAtoms.h"
#include "nsHTMLAtoms.h"
///////////////////////////////////////////////////////////////////////////////
nsIDOMWindowInternal*
inLayoutUtils::GetWindowFor(nsIDOMElement* aElement)
inLayoutUtils::GetWindowFor(nsIDOMNode* aNode)
{
nsCOMPtr<nsIDOMDocument> doc1;
aElement->GetOwnerDocument(getter_AddRefs(doc1));
aNode->GetOwnerDocument(getter_AddRefs(doc1));
return GetWindowFor(doc1);
}

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

@ -52,7 +52,7 @@
class inLayoutUtils
{
public:
static nsIDOMWindowInternal* GetWindowFor(nsIDOMElement* aElement);
static nsIDOMWindowInternal* GetWindowFor(nsIDOMNode* aNode);
static nsIDOMWindowInternal* GetWindowFor(nsIDOMDocument* aDoc);
static nsIPresShell* GetPresShellFor(nsISupports* aThing);
static nsIFrame* GetFrameFor(nsIDOMElement* aElement, nsIPresShell* aShell);