XSLImportRule.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * This file is part of the XSL implementation.
  3. *
  4. * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public License
  17. * along with this library; see the file COPYING.LIB. If not, write to
  18. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. * Boston, MA 02110-1301, USA.
  20. */
  21. #include "config.h"
  22. #include "XSLImportRule.h"
  23. #if ENABLE(XSLT)
  24. #include "CachedXSLStyleSheet.h"
  25. #include "CachedResourceLoader.h"
  26. #include "CachedResourceRequest.h"
  27. #include "Document.h"
  28. #include "XSLStyleSheet.h"
  29. namespace WebCore {
  30. XSLImportRule::XSLImportRule(XSLStyleSheet* parent, const String& href)
  31. : m_parentStyleSheet(parent)
  32. , m_strHref(href)
  33. , m_cachedSheet(0)
  34. , m_loading(false)
  35. {
  36. }
  37. XSLImportRule::~XSLImportRule()
  38. {
  39. if (m_styleSheet)
  40. m_styleSheet->setParentStyleSheet(0);
  41. if (m_cachedSheet)
  42. m_cachedSheet->removeClient(this);
  43. }
  44. void XSLImportRule::setXSLStyleSheet(const String& href, const KURL& baseURL, const String& sheet)
  45. {
  46. if (m_styleSheet)
  47. m_styleSheet->setParentStyleSheet(0);
  48. m_styleSheet = XSLStyleSheet::create(this, href, baseURL);
  49. XSLStyleSheet* parent = parentStyleSheet();
  50. if (parent)
  51. m_styleSheet->setParentStyleSheet(parent);
  52. m_styleSheet->parseString(sheet);
  53. m_loading = false;
  54. if (parent)
  55. parent->checkLoaded();
  56. }
  57. bool XSLImportRule::isLoading()
  58. {
  59. return (m_loading || (m_styleSheet && m_styleSheet->isLoading()));
  60. }
  61. void XSLImportRule::loadSheet()
  62. {
  63. CachedResourceLoader* cachedResourceLoader = 0;
  64. XSLStyleSheet* rootSheet = parentStyleSheet();
  65. if (rootSheet) {
  66. while (XSLStyleSheet* parentSheet = rootSheet->parentStyleSheet())
  67. rootSheet = parentSheet;
  68. }
  69. if (rootSheet)
  70. cachedResourceLoader = rootSheet->cachedResourceLoader();
  71. String absHref = m_strHref;
  72. XSLStyleSheet* parentSheet = parentStyleSheet();
  73. if (!parentSheet->baseURL().isNull())
  74. // use parent styleheet's URL as the base URL
  75. absHref = KURL(parentSheet->baseURL(), m_strHref).string();
  76. // Check for a cycle in our import chain. If we encounter a stylesheet
  77. // in our parent chain with the same URL, then just bail.
  78. for (XSLStyleSheet* parentSheet = parentStyleSheet(); parentSheet; parentSheet = parentSheet->parentStyleSheet()) {
  79. if (absHref == parentSheet->baseURL().string())
  80. return;
  81. }
  82. CachedResourceRequest request(ResourceRequest(cachedResourceLoader->document()->completeURL(absHref)));
  83. m_cachedSheet = cachedResourceLoader->requestXSLStyleSheet(request);
  84. if (m_cachedSheet) {
  85. m_cachedSheet->addClient(this);
  86. // If the imported sheet is in the cache, then setXSLStyleSheet gets called,
  87. // and the sheet even gets parsed (via parseString). In this case we have
  88. // loaded (even if our subresources haven't), so if we have a stylesheet after
  89. // checking the cache, then we've clearly loaded.
  90. if (!m_styleSheet)
  91. m_loading = true;
  92. }
  93. }
  94. } // namespace WebCore
  95. #endif // ENABLE(XSLT)