Reviewed and landed by Darin Adler.

	- Fixes <http://bugzilla.opendarwin.org/show_bug.cgi?id=3893>

        Test cases added:
	* layout-tests/fast/dom/css-dom-read-2.html: Added.
	* layout-tests/fast/dom/css-dom-read-2-expected.txt: Added.

        * khtml/css/css_computedstyle.cpp:
        (DOM::CSSComputedStyleDeclarationImpl::cssText):
	Implement.

        (DOM::CSSComputedStyleDeclarationImpl::item):
	Return the item name, not its value.

        * khtml/css/css_ruleimpl.cpp:
        (CSSImportRuleImpl::cssText):
        (CSSMediaRuleImpl::cssText):
        (CSSStyleRuleImpl::cssText):
        * khtml/css/css_ruleimpl.h:
	Implement.

        * khtml/css/css_stylesheetimpl.cpp:
        (MediaListImpl::mediaText):
	Don't put a trailing comma after lists.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@9980 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/fast/dom/css-dom-read-2-expected.txt b/LayoutTests/fast/dom/css-dom-read-2-expected.txt
new file mode 100644
index 0000000..97a95df
--- /dev/null
+++ b/LayoutTests/fast/dom/css-dom-read-2-expected.txt
@@ -0,0 +1,50 @@
+This tests different ways of deserializing css stylesheets. The first item below is the style as specified in the document. The second is serialized using the different rule accessors. The third just uses rule.cssText. If the test is successful, all three should be the same.
+@import url("fancyfonts.css") screen;
+@media print {
+  body { font-size: 10pt }
+}
+@media screen {
+  body { font-size: 13px }
+}
+@media screen, print {
+  body { line-height: 1.2 }
+}
+.one {display: block;}
+.two {display: inline;}
+.three {display: list-item; list-style-type: square; margin-left: 3em;}
+.four {display: none; color: red;}
+I {display: block;}
+
+@import url("fancyfonts.css") screen;
+@media print {
+  body { font-size: 10pt; } 
+}
+@media screen {
+  body { font-size: 13px; } 
+}
+@media screen, print {
+  body { line-height: 1.2; } 
+}
+.one { display: block; } 
+.two { display: inline; } 
+.three { display: list-item; list-style-type: square; margin-left: 3em; } 
+.four { display: none; color: red; } 
+i { display: block; } 
+
+@import url("fancyfonts.css") screen;
+@media print { 
+  body { font-size: 10pt; }
+}
+@media screen { 
+  body { font-size: 13px; }
+}
+@media screen, print { 
+  body { line-height: 1.2; }
+}
+.one { display: block; }
+.two { display: inline; }
+.three { display: list-item; list-style-type: square; margin-left: 3em; }
+.four { display: none; color: red; }
+i { display: block; }
+
+
diff --git a/LayoutTests/fast/dom/css-dom-read-2.html b/LayoutTests/fast/dom/css-dom-read-2.html
new file mode 100644
index 0000000..3cee8b9
--- /dev/null
+++ b/LayoutTests/fast/dom/css-dom-read-2.html
@@ -0,0 +1,93 @@
+<html>
+<head>
+<style id="style">@import url("fancyfonts.css") screen;
+@media print {
+  body { font-size: 10pt }
+}
+@media screen {
+  body { font-size: 13px }
+}
+@media screen, print {
+  body { line-height: 1.2 }
+}
+.one {display: block;}
+.two {display: inline;}
+.three {display: list-item; list-style-type: square; margin-left: 3em;}
+.four {display: none; color: red;}
+I {display: block;}
+</style>
+</head>
+<body onload="runTests();">
+<script>
+function dumpRuleList(cssRules)
+{
+	var s = '';
+	
+	var i;
+	for (i = 0; i < cssRules.length; i++) {
+		rule = cssRules.item(i);
+
+		switch (rule.type) {
+		case CSSRule.IMPORT_RULE:
+			s += '@import url("' + rule.href + '") ' + rule.media.mediaText;
+			s += ';\n';
+			break;
+		case CSSRule.STYLE_RULE:		
+			s += rule.selectorText + ' { ';
+			
+			style = rule.style;
+			for (j = 0; j < style.length; j++) {
+				s += style.item(j) + ': ' + style.getPropertyValue(style.item(j))+ '; ';
+			}
+			s += '} \n';
+			break;
+		case CSSRule.MEDIA_RULE:
+			s += '@media ' + rule.media.mediaText + ' {\n';
+			s += '  ' + dumpRuleList(rule.cssRules);
+			s += '}\n';
+			break;
+		case CSSRule.PAGE_RULE:
+			s += '@page ';
+			break;
+		}
+
+	}
+	
+	return s;
+}
+
+function debug(s) {
+	console = document.getElementById('console');
+	li = document.createElement('li');
+	console.appendChild(li);
+	pre = document.createElement('pre');
+	li.appendChild(pre);
+	t = document.createTextNode(s + '\n');
+	pre.appendChild(t);
+}
+
+function runTests() {
+	if (window.layoutTestController)
+		layoutTestController.dumpAsText();
+
+	originalStyle = document.getElementById('style').firstChild.nodeValue;
+	debug(originalStyle);
+
+	styleSheet = document.styleSheets.item(0)
+	s = dumpRuleList(styleSheet.cssRules);
+	debug(s);
+	
+	s = ""
+	for (i = 0; i < styleSheet.cssRules.length; i++) {
+		rule = styleSheet.cssRules.item(i);
+
+		s += rule.cssText + '\n';
+	}
+	debug(s);
+}
+</script>
+This tests different ways of deserializing css stylesheets. The first item below is the style as specified in the document. The second is serialized using the different rule accessors. The third just uses rule.cssText. If the test is successful, all three should be the same.
+<ul id="console">
+</ul>
+</body>
+</html>
diff --git a/WebCore/ChangeLog-2005-08-23 b/WebCore/ChangeLog-2005-08-23
index b285718..cbda244 100644
--- a/WebCore/ChangeLog-2005-08-23
+++ b/WebCore/ChangeLog-2005-08-23
@@ -1,3 +1,31 @@
+2005-07-30  Anders Carlsson  <andersca@mac.com>
+
+        Reviewed and landed by Darin Adler.
+
+	- Fixes <http://bugzilla.opendarwin.org/show_bug.cgi?id=3893>
+	
+        Test cases added: 
+	* layout-tests/fast/dom/css-dom-read-2.html: Added.
+	* layout-tests/fast/dom/css-dom-read-2-expected.txt: Added.
+	
+        * khtml/css/css_computedstyle.cpp:
+        (DOM::CSSComputedStyleDeclarationImpl::cssText):
+	Implement.
+	
+        (DOM::CSSComputedStyleDeclarationImpl::item):
+	Return the item name, not its value.
+	
+        * khtml/css/css_ruleimpl.cpp:
+        (CSSImportRuleImpl::cssText):
+        (CSSMediaRuleImpl::cssText):
+        (CSSStyleRuleImpl::cssText):
+        * khtml/css/css_ruleimpl.h:
+	Implement.
+	
+        * khtml/css/css_stylesheetimpl.cpp:
+        (MediaListImpl::mediaText):
+	Don't put a trailing comma after lists.
+
 2005-07-30  Darin Adler  <darin@apple.com>
 
         - rolled back a test that has been failing since we rolled back the <script/> quirk
diff --git a/WebCore/khtml/css/css_computedstyle.cpp b/WebCore/khtml/css/css_computedstyle.cpp
index 3e7580f..496a31a 100644
--- a/WebCore/khtml/css/css_computedstyle.cpp
+++ b/WebCore/khtml/css/css_computedstyle.cpp
@@ -50,6 +50,8 @@
 using khtml::ShadowData;
 using khtml::StyleDashboardRegion;
 
+extern DOM::DOMString getPropertyName(unsigned short id);
+
 namespace DOM {
 
 // List of all properties we know how to compute, omitting shorthands.
@@ -286,8 +288,18 @@
 
 DOMString CSSComputedStyleDeclarationImpl::cssText() const
 {
-    ERROR("unimplemented");
-    return DOMString();
+    DOMString result;
+    
+    for (unsigned i = 0; i < numComputedProperties; i++) {
+        if (i != 0)
+            result += " ";
+        result += getPropertyName(computedProperties[i]);
+        result += ": ";
+        result += getPropertyValue(computedProperties[i]);
+        result += ";";
+    }
+    
+    return result;
 }
 
 void CSSComputedStyleDeclarationImpl::setCssText(const DOMString &, int &exceptionCode)
@@ -1256,7 +1268,8 @@
 {
     if (i >= numComputedProperties)
         return DOMString();
-    return getPropertyValue(computedProperties[i]);
+    
+    return getPropertyName(computedProperties[i]);
 }
 
 CSSMutableStyleDeclarationImpl *CSSComputedStyleDeclarationImpl::copyInheritableProperties() const
diff --git a/WebCore/khtml/css/css_ruleimpl.cpp b/WebCore/khtml/css/css_ruleimpl.cpp
index 51c0d90..2a955fe 100644
--- a/WebCore/khtml/css/css_ruleimpl.cpp
+++ b/WebCore/khtml/css/css_ruleimpl.cpp
@@ -197,6 +197,21 @@
     }
 }
 
+DOMString CSSImportRuleImpl::cssText() const
+{
+    DOMString result = "@import url(\"";
+    result += m_strHref;
+    result += "\")";
+
+    if (m_lstMedia) {
+        result += " ";
+        result += m_lstMedia->mediaText();
+    }
+    result += ";";
+
+    return result;
+}
+
 // --------------------------------------------------------------------------
 CSSMediaRuleImpl::CSSMediaRuleImpl( StyleBaseImpl *parent, MediaListImpl *mediaList, CSSRuleListImpl *ruleList )
     :   CSSRuleImpl( parent )
@@ -265,6 +280,28 @@
     return m_lstCSSRules->insertRule( newRule, index );
 }
 
+DOMString CSSMediaRuleImpl::cssText() const
+{
+    DOMString result = "@media ";
+    if (m_lstMedia) {
+        result += m_lstMedia->mediaText();
+        result += " ";
+    }
+    result += "{ \n";
+    
+    if (m_lstCSSRules) {
+        unsigned long len = m_lstCSSRules->length();
+        for (unsigned long i = 0; i < len; i++) {
+            result += "  ";
+            result += m_lstCSSRules->item(i)->cssText();
+            result += "\n";
+        }
+    }
+    
+    result += "}";
+    return result;
+}
+
 // ---------------------------------------------------------------------------
 
 CSSRuleListImpl::CSSRuleListImpl(StyleListImpl *lst)
@@ -343,6 +380,17 @@
     // ###
 }
 
+DOMString CSSStyleRuleImpl::cssText() const
+{
+    DOMString result = selectorText();
+    
+    result += " { ";
+    result += m_style->cssText();
+    result += "}";
+    
+    return result;
+}
+
 bool CSSStyleRuleImpl::parseString( const DOMString &/*string*/, bool )
 {
     // ###
diff --git a/WebCore/khtml/css/css_ruleimpl.h b/WebCore/khtml/css/css_ruleimpl.h
index ba8ae5d..c642015 100644
--- a/WebCore/khtml/css/css_ruleimpl.h
+++ b/WebCore/khtml/css/css_ruleimpl.h
@@ -59,7 +59,7 @@
     CSSStyleSheetImpl *parentStyleSheet() const;
     CSSRuleImpl *parentRule() const;
 
-    DOM::DOMString cssText() const;
+    virtual DOMString cssText() const;
     void setCssText(DOM::DOMString str);
     virtual void init() {}
 
@@ -77,6 +77,7 @@
     MAIN_THREAD_ALLOCATED;
 
     virtual bool isCharsetRule() { return true; }
+    virtual DOMString cssText() const;
 
     DOMString encoding() const { return m_encoding; }
     void setEncoding(DOMString _encoding) { m_encoding = _encoding; }
@@ -119,7 +120,8 @@
     CSSStyleSheetImpl *styleSheet() const { return m_styleSheet; }
 
     virtual bool isImportRule() { return true; }
-
+    virtual DOMString cssText() const;
+  
     // from CachedObjectClient
     virtual void setStyleSheet(const DOM::DOMString &url, const DOM::DOMString &sheet);
 
@@ -174,6 +176,7 @@
     void deleteRule ( unsigned long index ) { m_lstCSSRules->deleteRule( index ); }
 
     virtual bool isMediaRule() { return true; }
+    virtual DOMString cssText() const;
 
     /* Not part of the DOM */
     unsigned long append( CSSRuleImpl *rule );
@@ -215,6 +218,7 @@
     CSSMutableStyleDeclarationImpl *style() const { return m_style; }
 
     virtual bool isStyleRule() { return true; }
+    virtual DOMString cssText() const;
 
     DOM::DOMString selectorText() const;
     void setSelectorText(DOM::DOMString str);
diff --git a/WebCore/khtml/css/css_stylesheetimpl.cpp b/WebCore/khtml/css/css_stylesheetimpl.cpp
index 01814f6..9c16534 100644
--- a/WebCore/khtml/css/css_stylesheetimpl.cpp
+++ b/WebCore/khtml/css/css_stylesheetimpl.cpp
@@ -380,10 +380,11 @@
 
 DOM::DOMString MediaListImpl::mediaText() const
 {
-    DOMString text;
-    for ( QValueList<DOMString>::ConstIterator it = m_lstMedia.begin(); it != m_lstMedia.end(); ++it ) {
+    DOMString text = "";
+    for (QValueList<DOMString>::ConstIterator it = m_lstMedia.begin(); it != m_lstMedia.end(); ++it) {
+        if (text.length() > 0)
+            text += ", ";
         text += *it;
-        text += ", ";
     }
     return text;
 }