WebCore:

2008-04-02  Brady Eidson  <beidson@apple.com>

        Reviewed by Mitz Pettel

        <rdar://problem/5838347> and http://bugs.webkit.org/show_bug.cgi?id=11839
        Webarchive fails to save CSS files in @import statements

        * css/CSSStyleSheet.cpp:
        (WebCore::CSSStyleSheet::addSubresourceURLStrings): Recursively add the URL each @import rule under the current style sheet.
        * css/CSSStyleSheet.h:
        * css/StyleSheet.h:
        (WebCore::StyleSheet::addSubresourceURLStrings):

        * html/HTMLLinkElement.cpp:
        (WebCore::HTMLLinkElement::getSubresourceAttributeStrings): Add the linked URL as well as all @import
          rules rooted at the linked stylesheet.

        * html/HTMLStyleElement.cpp:
        (WebCore::HTMLStyleElement::getSubresourceAttributeStrings): Walk all @import rules rooted at this
          stylesheet to add to the list.
        * html/HTMLStyleElement.h:

LayoutTests:

2008-04-02  Brady Eidson  <beidson@apple.com>

        Written by David Kilzer, tweaked by Brady, Reviewed by Mitz Pettel

        - test for http://bugs.webkit.org/show_bug.cgi?id=11839
          Webarchive fails to save CSS files in @import statements

        The idea is to <link> to a CSS file which @imports another CSS file,
        and also @import a CSS file inside a <style> element, which also @imports another CSS file

        Then make sure all 4 of the css files are in the resulting webarchive

        * webarchive/resources/test-css-import-recurse.css: Added.
        * webarchive/resources/test-css-import.css: Added.
        * webarchive/resources/test-css-link-recurse.css: Added.
        * webarchive/resources/test-css-link.css: Added.
        * webarchive/test-css-import-expected.txt: Added.
        * webarchive/test-css-import.html: Added.



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@31578 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/WebCore/css/CSSStyleSheet.cpp b/WebCore/css/CSSStyleSheet.cpp
index acd0955..1facad6 100644
--- a/WebCore/css/CSSStyleSheet.cpp
+++ b/WebCore/css/CSSStyleSheet.cpp
@@ -203,4 +203,25 @@
         documentToUpdate->updateStyleSelector();
 }
 
+void CSSStyleSheet::addSubresourceURLStrings(HashSet<String>& urls, const String& base) const
+{        
+    OwnPtr<CSSRuleList> ruleList(const_cast<CSSStyleSheet*>(this)->cssRules());
+    
+    // Add the URLs for each child import rule, and recurse for the stylesheet belonging to each of those rules.
+    for (unsigned i = 0; i < ruleList->length(); ++i) {
+        CSSRule* rule = ruleList->item(i);
+        if (rule->type() != CSSRule::IMPORT_RULE)
+            continue;
+
+        CSSImportRule* importRule = static_cast<CSSImportRule*>(rule);
+        CSSStyleSheet* ruleSheet = importRule->styleSheet();
+        if (!ruleSheet)
+            continue;
+
+        KURL fullURL(KURL(base), importRule->href());
+        urls.add(fullURL.string());
+        ruleSheet->addSubresourceURLStrings(urls, fullURL.string());
+    }
+}
+
 }