Changeset 35744 in webkit


Ignore:
Timestamp:
Aug 13, 2008 7:22:35 PM (16 years ago)
Author:
Nikolas Zimmermann
Message:

Reviewed by Eric.
Fixes: https://bugs.webkit.org/show_bug.cgi?id=20372

Refactor HTMLScriptElement's code into a common base class: ScriptElement.
SVGScriptElement will be converted to use ScriptElement in a follow-up patch.

This resolves code duplications and allows us to completly replace the old
SVGScriptElement (which doesn't use CachedScript, no dynamic injected scripts etc..)

As ScriptElement, doesn't actually inherit from Element, we may want to rename
it, though StyleElement uses the same naming convention, so I left it as is for now.
Eventually we'll rename both files in future.

No functional changes yet, as SVGScriptElement doesn't yet use the new base class.

Location:
trunk/WebCore
Files:
2 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r35743 r35744  
     12008-08-13  Nikolas Zimmermann  <zimmermann@kde.org>
     2
     3        Reviewed by Eric.
     4
     5        Fixes: https://bugs.webkit.org/show_bug.cgi?id=20372
     6
     7        Refactor HTMLScriptElement's code into a common base class: ScriptElement.
     8        SVGScriptElement will be converted to use ScriptElement in a follow-up patch.
     9
     10        This resolves code duplications and allows us to completly replace the old
     11        SVGScriptElement (which doesn't use CachedScript, no dynamic injected scripts etc..)
     12
     13        As ScriptElement, doesn't actually inherit from Element, we may want to rename
     14        it, though StyleElement uses the same naming convention, so I left it as is for now.
     15        Eventually we'll rename both files in future.
     16
     17        No functional changes yet, as SVGScriptElement doesn't yet use the new base class.
     18
     19        * WebCore.pro: Add new ScriptElement.cpp to build.
     20        * WebCore.vcproj/WebCore.vcproj: Ditto.
     21        * WebCore.xcodeproj/project.pbxproj: Ditto.
     22        * WebCoreSources.bkl: Dutto.
     23        * dom/ScriptElement.cpp: Added. 1:1 based on HTMLScriptElement
     24        (WebCore::ScriptElement::insertedIntoDocument):
     25        (WebCore::ScriptElement::removedFromDocument):
     26        (WebCore::ScriptElement::childrenChanged):
     27        (WebCore::ScriptElement::finishParsingChildren):
     28        (WebCore::ScriptElement::handleSourceAttribute):
     29        (WebCore::isSupportedJavaScriptLanguage):
     30        (WebCore::ScriptElementData::ScriptElementData):
     31        (WebCore::ScriptElementData::~ScriptElementData):
     32        (WebCore::ScriptElementData::requestScript):
     33        (WebCore::ScriptElementData::evaluateScript):
     34        (WebCore::ScriptElementData::stopLoadRequest):
     35        (WebCore::ScriptElementData::notifyFinished):
     36        (WebCore::ScriptElementData::ignoresLoadRequest):
     37        (WebCore::ScriptElementData::shouldExecuteAsJavaScript):
     38        (WebCore::ScriptElementData::scriptCharset):
     39        (WebCore::ScriptElementData::scriptContent):
     40        * dom/ScriptElement.h: Added.
     41        (WebCore::ScriptElement::ScriptElement):
     42        (WebCore::ScriptElement::~ScriptElement):
     43        (WebCore::ScriptElementData::element):
     44        (WebCore::ScriptElementData::createdByParser):
     45        (WebCore::ScriptElementData::setCreatedByParser):
     46        * dom/XMLTokenizer.cpp:
     47        (WebCore::isScriptElement):
     48        (WebCore::castToScriptElement):
     49        (WebCore::eventuallyMarkAsParserCreated):
     50        (WebCore::XMLTokenizer::startElementNs):
     51        (WebCore::XMLTokenizer::endElementNs):
     52        (WebCore::createXHTMLParserErrorHeader):
     53        (WebCore::XMLTokenizer::insertErrorMessageBlock):
     54        * html/HTMLScriptElement.cpp: Refactored code, pushed most code down to ScriptElement.
     55        (WebCore::HTMLScriptElement::HTMLScriptElement):
     56        (WebCore::HTMLScriptElement::~HTMLScriptElement):
     57        (WebCore::HTMLScriptElement::isURLAttribute):
     58        (WebCore::HTMLScriptElement::setCreatedByParser):
     59        (WebCore::HTMLScriptElement::shouldExecuteAsJavaScript):
     60        (WebCore::HTMLScriptElement::childrenChanged):
     61        (WebCore::HTMLScriptElement::parseMappedAttribute):
     62        (WebCore::HTMLScriptElement::finishParsingChildren):
     63        (WebCore::HTMLScriptElement::insertedIntoDocument):
     64        (WebCore::HTMLScriptElement::removedFromDocument):
     65        (WebCore::HTMLScriptElement::text):
     66        (WebCore::HTMLScriptElement::setText):
     67        (WebCore::HTMLScriptElement::setHtmlFor):
     68        (WebCore::HTMLScriptElement::setEvent):
     69        (WebCore::HTMLScriptElement::charset):
     70        (WebCore::HTMLScriptElement::src):
     71        (WebCore::HTMLScriptElement::type):
     72        (WebCore::HTMLScriptElement::scriptCharset):
     73        (WebCore::HTMLScriptElement::scriptContent):
     74        (WebCore::HTMLScriptElement::sourceAttributeValue):
     75        (WebCore::HTMLScriptElement::charsetAttributeValue):
     76        (WebCore::HTMLScriptElement::typeAttributeValue):
     77        (WebCore::HTMLScriptElement::languageAttributeValue):
     78        (WebCore::HTMLScriptElement::dispatchLoadEvent):
     79        (WebCore::HTMLScriptElement::dispatchErrorEvent):
     80        * html/HTMLScriptElement.h:
     81        * svg/SVGScriptElement.cpp: Inherit from ScriptElement, don't actually use it yet.
     82        * svg/SVGScriptElement.cpp:
     83        (WebCore::SVGScriptElement::SVGScriptElement):
     84        (WebCore::SVGScriptElement::scriptContent):
     85        (WebCore::SVGScriptElement::sourceAttributeValue):
     86        (WebCore::SVGScriptElement::charsetAttributeValue):
     87        (WebCore::SVGScriptElement::typeAttributeValue):
     88        (WebCore::SVGScriptElement::languageAttributeValue):
     89        (WebCore::SVGScriptElement::dispatchLoadEvent):
     90        (WebCore::SVGScriptElement::dispatchErrorEvent):
     91        * svg/SVGScriptElement.h:
     92        (WebCore::SVGScriptElement::setCreatedByParser):
     93
    1942008-08-13  Eric Seidel  <eric@webkit.org>
    295
  • trunk/WebCore/WebCore.pro

    r35732 r35744  
    537537    dom/Range.cpp \
    538538    dom/RegisteredEventListener.cpp \
     539    dom/ScriptElement.cpp \
    539540    dom/SelectorNodeList.cpp \
    540541    dom/StaticNodeList.cpp \
  • trunk/WebCore/WebCore.vcproj/WebCore.vcproj

    r35732 r35744  
    1042910429                        </File>
    1043010430                        <File
     10431                                RelativePath="..\dom\ScriptElement.cpp"
     10432                                >
     10433                                <FileConfiguration
     10434                                        Name="Release_PGO|Win32"
     10435                                        >
     10436                                        <Tool
     10437                                                Name="VCCLCompilerTool"
     10438                                                WholeProgramOptimization="true"
     10439                                        />
     10440                                </FileConfiguration>
     10441                        </File>
     10442                        <File
     10443                                RelativePath="..\dom\ScriptElement.h"
     10444                                >
     10445                        </File>
     10446                        <File
    1043110447                                RelativePath="..\dom\SelectorNodeList.cpp"
    1043210448                                >
  • trunk/WebCore/WebCore.xcodeproj/project.pbxproj

    r35732 r35744  
    4040                06E81ED70AB5D5E900C87837 /* LocalCurrentGraphicsContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 06E81ED60AB5D5E900C87837 /* LocalCurrentGraphicsContext.h */; };
    4141                06E81EEC0AB5DA9700C87837 /* LocalCurrentGraphicsContext.mm in Sources */ = {isa = PBXBuildFile; fileRef = 06E81EEB0AB5DA9700C87837 /* LocalCurrentGraphicsContext.mm */; };
     42                08A484770E5272C500C3FE76 /* ScriptElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 08A484750E5272C500C3FE76 /* ScriptElement.cpp */; };
     43                08A484780E5272C500C3FE76 /* ScriptElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 08A484760E5272C500C3FE76 /* ScriptElement.h */; };
    4244                08E4FE460E2BD41400F4CAE0 /* JSSVGLengthCustom.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 08E4FE450E2BD41400F4CAE0 /* JSSVGLengthCustom.cpp */; };
    4345                0A4844990CA44CB200B7BD48 /* SoftLinking.h in Headers */ = {isa = PBXBuildFile; fileRef = 0A4844980CA44CB200B7BD48 /* SoftLinking.h */; settings = {ATTRIBUTES = (Private, ); }; };
     
    45444546                06E81ED60AB5D5E900C87837 /* LocalCurrentGraphicsContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LocalCurrentGraphicsContext.h; sourceTree = "<group>"; };
    45454547                06E81EEB0AB5DA9700C87837 /* LocalCurrentGraphicsContext.mm */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.objcpp; path = LocalCurrentGraphicsContext.mm; sourceTree = "<group>"; };
     4548                08A484750E5272C500C3FE76 /* ScriptElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ScriptElement.cpp; sourceTree = "<group>"; };
     4549                08A484760E5272C500C3FE76 /* ScriptElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScriptElement.h; sourceTree = "<group>"; };
    45464550                08E4FE450E2BD41400F4CAE0 /* JSSVGLengthCustom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSSVGLengthCustom.cpp; sourceTree = "<group>"; };
    45474551                0A4844980CA44CB200B7BD48 /* SoftLinking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SoftLinking.h; sourceTree = "<group>"; };
     
    1333213336                                85031B350A44EFC700F992E0 /* RegisteredEventListener.cpp */,
    1333313337                                85031B360A44EFC700F992E0 /* RegisteredEventListener.h */,
     13338                                08A484750E5272C500C3FE76 /* ScriptElement.cpp */,
     13339                                08A484760E5272C500C3FE76 /* ScriptElement.h */,
    1333413340                                BC7FA6800D1F167900DB22A9 /* SelectorNodeList.cpp */,
    1333513341                                BC7FA67F0D1F167900DB22A9 /* SelectorNodeList.h */,
     
    1539515401                                31C0FF4C0E4CEFDD007D6FE5 /* DOMWebKitTransitionEvent.h in Headers */,
    1539615402                                31C0FF4E0E4CEFDD007D6FE5 /* DOMWebKitTransitionEventInternal.h in Headers */,
     15403                                08A484780E5272C500C3FE76 /* ScriptElement.h in Headers */,
    1539715404                                A8FA6E5D0E4CFDED00D5CF49 /* Pattern.h in Headers */,
    1539815405                        );
     
    1717917186                                31C0FF4A0E4CEFDD007D6FE5 /* DOMWebKitAnimationEvent.mm in Sources */,
    1718017187                                31C0FF4D0E4CEFDD007D6FE5 /* DOMWebKitTransitionEvent.mm in Sources */,
     17188                                08A484770E5272C500C3FE76 /* ScriptElement.cpp in Sources */,
    1718117189                                A8FA6E5E0E4CFDED00D5CF49 /* Pattern.cpp in Sources */,
    1718217190                                A80A38FE0E50CC8200A25EBC /* PatternCG.cpp in Sources */,
  • trunk/WebCore/WebCoreSources.bkl

    r35732 r35744  
    407407        dom/Range.cpp
    408408        dom/RegisteredEventListener.cpp
     409        dom/ScriptElement.cpp
    409410        dom/SelectorNodeList.cpp
    410411        dom/StaticNodeList.cpp
  • trunk/WebCore/dom/XMLTokenizer.cpp

    r35172 r35744  
    4343#include "HTMLTokenizer.h"
    4444#include "ScriptController.h"
     45#include "ScriptElement.h"
    4546#include "ProcessingInstruction.h"
    4647#include "ResourceError.h"
     
    6667#if ENABLE(SVG)
    6768#include "SVGNames.h"
     69#include "SVGScriptElement.h"
    6870#include "SVGStyleElement.h"
    69 #include "XLinkNames.h"
    7071#endif
    7172
     
    7576
    7677using namespace EventNames;
    77 using namespace HTMLNames;
    7878
    7979const int maxErrors = 25;
     
    543543
    544544// --------------------------------
     545
     546inline bool isScriptElement(Element* element)
     547{
     548    return element->hasTagName(HTMLNames::scriptTag)
     549#if ENABLE(SVG)
     550        || element->hasTagName(SVGNames::scriptTag)
     551#endif
     552        ;
     553}
     554
     555inline ScriptElement* castToScriptElement(Element* element)
     556{
     557    ASSERT(isScriptElement(element));
     558
     559    if (element->hasTagName(HTMLNames::scriptTag))
     560        return static_cast<HTMLScriptElement*>(element);
     561
     562#if ENABLE(SVG)
     563    if (element->hasTagName(SVGNames::scriptTag))
     564        return static_cast<SVGScriptElement*>(element);
     565#endif
     566
     567    ASSERT_NOT_REACHED();
     568    return 0;
     569}
    545570
    546571XMLTokenizer::XMLTokenizer(Document* _doc, FrameView* _view)
     
    811836}
    812837
     838static void eventuallyMarkAsParserCreated(Element* element)
     839{
     840    if (element->hasTagName(HTMLNames::scriptTag))
     841        static_cast<HTMLScriptElement*>(element)->setCreatedByParser(true);
     842#if ENABLE(SVG)
     843    else if (element->hasTagName(SVGNames::scriptTag))
     844        static_cast<SVGScriptElement*>(element)->setCreatedByParser(true);
     845#endif
     846    else if (element->hasTagName(HTMLNames::styleTag))
     847        static_cast<HTMLStyleElement*>(element)->setCreatedByParser(true);
     848#if ENABLE(SVG)
     849    else if (element->hasTagName(SVGNames::styleTag))
     850        static_cast<SVGStyleElement*>(element)->setCreatedByParser(true);
     851#endif
     852    else if (element->hasTagName(HTMLNames::linkTag))
     853        static_cast<HTMLLinkElement*>(element)->setCreatedByParser(true);
     854}
     855
    813856void XMLTokenizer::startElementNs(const xmlChar* xmlLocalName, const xmlChar* xmlPrefix, const xmlChar* xmlURI, int nb_namespaces,
    814857                                  const xmlChar** libxmlNamespaces, int nb_attributes, int nb_defaulted, const xmlChar** libxmlAttributes)
     
    866909
    867910    newElement->beginParsingChildren();
    868 
    869     if (newElement->hasTagName(scriptTag))
    870         static_cast<HTMLScriptElement*>(newElement.get())->setCreatedByParser(true);
    871     else if (newElement->hasTagName(HTMLNames::styleTag))
    872         static_cast<HTMLStyleElement*>(newElement.get())->setCreatedByParser(true);
    873 #if ENABLE(SVG)
    874     else if (newElement->hasTagName(SVGNames::styleTag))
    875         static_cast<SVGStyleElement*>(newElement.get())->setCreatedByParser(true);
    876 #endif
    877     else if (newElement->hasTagName(HTMLNames::linkTag))
    878         static_cast<HTMLLinkElement*>(newElement.get())->setCreatedByParser(true);
    879 
    880     if (newElement->hasTagName(HTMLNames::scriptTag)
    881 #if ENABLE(SVG)
    882         || newElement->hasTagName(SVGNames::scriptTag)
    883 #endif
    884         )
     911    eventuallyMarkAsParserCreated(newElement.get());
     912
     913    if (isScriptElement(newElement.get()))
    885914        m_scriptStartLine = lineNumber();
    886    
     915
    887916    if (!m_currentNode->addChild(newElement.get())) {
    888917        stopParsing();
    889918        return;
    890919    }
    891    
     920
    892921    setCurrentNode(newElement.get());
    893922    if (m_view && !newElement->attached())
     
    912941   
    913942    // don't load external scripts for standalone documents (for now)
    914     if (n->isElementNode() && m_view && (static_cast<Element*>(n)->hasTagName(scriptTag)
    915 #if ENABLE(SVG)
    916                                          || static_cast<Element*>(n)->hasTagName(SVGNames::scriptTag)
    917 #endif
    918                                          )) {
    919 
    920                                          
     943    if (n->isElementNode() && m_view && isScriptElement(static_cast<Element*>(n))) {
    921944        ASSERT(!m_pendingScript);
    922        
    923945        m_requestingScript = true;
    924        
    925         Element* scriptElement = static_cast<Element*>(n);       
    926         String scriptHref;
    927        
    928         if (static_cast<Element*>(n)->hasTagName(scriptTag))
    929             scriptHref = scriptElement->getAttribute(srcAttr);
    930 #if ENABLE(SVG)
    931         else if (static_cast<Element*>(n)->hasTagName(SVGNames::scriptTag))
    932             scriptHref = scriptElement->getAttribute(XLinkNames::hrefAttr);
    933 #endif
    934        
     946
     947        Element* element = static_cast<Element*>(n);
     948        ScriptElement* scriptElement = castToScriptElement(element);
     949
     950        String scriptHref = scriptElement->sourceAttributeValue();
    935951        if (!scriptHref.isEmpty()) {
    936952            // we have a src attribute
    937             const AtomicString& charset = scriptElement->getAttribute(charsetAttr);
    938             if ((m_pendingScript = m_doc->docLoader()->requestScript(scriptHref, charset))) {
    939                 m_scriptElement = scriptElement;
     953            String scriptCharset = scriptElement->charsetAttributeValue();
     954            if ((m_pendingScript = m_doc->docLoader()->requestScript(scriptHref, scriptCharset))) {
     955                m_scriptElement = element;
    940956                m_pendingScript->addClient(this);
    941                    
     957
    942958                // m_pendingScript will be 0 if script was already loaded and ref() executed it
    943959                if (m_pendingScript)
     
    947963
    948964        } else {
    949             String scriptCode = "";
    950             for (Node* child = scriptElement->firstChild(); child; child = child->nextSibling()) {
    951                 if (child->isTextNode() || child->nodeType() == Node::CDATA_SECTION_NODE)
    952                     scriptCode += static_cast<CharacterData*>(child)->data();
    953             }
     965            String scriptCode = scriptElement->scriptContent();
    954966            m_view->frame()->loader()->executeScript(m_doc->url().string(), m_scriptStartLine, scriptCode);
    955967        }
    956        
     968
    957969        m_requestingScript = false;
    958970    }
     
    14321444{
    14331445    ExceptionCode ec = 0;
    1434     RefPtr<Element> reportElement = doc->createElementNS(xhtmlNamespaceURI, "parsererror", ec);
    1435     reportElement->setAttribute(styleAttr, "display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black");
    1436    
    1437     RefPtr<Element> h3 = doc->createElementNS(xhtmlNamespaceURI, "h3", ec);
     1446    RefPtr<Element> reportElement = doc->createElementNS(HTMLNames::xhtmlNamespaceURI, "parsererror", ec);
     1447    reportElement->setAttribute(HTMLNames::styleAttr, "display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black");
     1448   
     1449    RefPtr<Element> h3 = doc->createElementNS(HTMLNames::xhtmlNamespaceURI, "h3", ec);
    14381450    reportElement->appendChild(h3.get(), ec);
    14391451    h3->appendChild(doc->createTextNode("This page contains the following errors:"), ec);
    14401452   
    1441     RefPtr<Element> fixed = doc->createElementNS(xhtmlNamespaceURI, "div", ec);
     1453    RefPtr<Element> fixed = doc->createElementNS(HTMLNames::xhtmlNamespaceURI, "div", ec);
    14421454    reportElement->appendChild(fixed.get(), ec);
    1443     fixed->setAttribute(styleAttr, "font-family:monospace;font-size:12px");
     1455    fixed->setAttribute(HTMLNames::styleAttr, "font-family:monospace;font-size:12px");
    14441456    fixed->appendChild(doc->createTextNode(errorMessages), ec);
    14451457   
    1446     h3 = doc->createElementNS(xhtmlNamespaceURI, "h3", ec);
     1458    h3 = doc->createElementNS(HTMLNames::xhtmlNamespaceURI, "h3", ec);
    14471459    reportElement->appendChild(h3.get(), ec);
    14481460    h3->appendChild(doc->createTextNode("Below is a rendering of the page up to the first error."), ec);
     
    14661478    Node* documentElement = doc->documentElement();
    14671479    if (!documentElement) {
    1468         RefPtr<Node> rootElement = doc->createElementNS(xhtmlNamespaceURI, "html", ec);
     1480        RefPtr<Node> rootElement = doc->createElementNS(HTMLNames::xhtmlNamespaceURI, "html", ec);
    14691481        doc->appendChild(rootElement, ec);
    1470         RefPtr<Node> body = doc->createElementNS(xhtmlNamespaceURI, "body", ec);
     1482        RefPtr<Node> body = doc->createElementNS(HTMLNames::xhtmlNamespaceURI, "body", ec);
    14711483        rootElement->appendChild(body, ec);
    14721484        documentElement = body.get();
     
    14771489        // wrap the erroneous SVG document in an xhtml document and render
    14781490        // the combined document with error messages.
    1479         RefPtr<Node> rootElement = doc->createElementNS(xhtmlNamespaceURI, "html", ec);
    1480         RefPtr<Node> body = doc->createElementNS(xhtmlNamespaceURI, "body", ec);
     1491        RefPtr<Node> rootElement = doc->createElementNS(HTMLNames::xhtmlNamespaceURI, "html", ec);
     1492        RefPtr<Node> body = doc->createElementNS(HTMLNames::xhtmlNamespaceURI, "body", ec);
    14811493        rootElement->appendChild(body, ec);
    14821494        body->appendChild(documentElement, ec);
     
    14901502#if ENABLE(XSLT)
    14911503    if (doc->transformSourceDocument()) {
    1492         RefPtr<Element> par = doc->createElementNS(xhtmlNamespaceURI, "p", ec);
     1504        RefPtr<Element> par = doc->createElementNS(HTMLNames::xhtmlNamespaceURI, "p", ec);
    14931505        reportElement->appendChild(par, ec);
    1494         par->setAttribute(styleAttr, "white-space: normal");
     1506        par->setAttribute(HTMLNames::styleAttr, "white-space: normal");
    14951507        par->appendChild(doc->createTextNode("This document was created as the result of an XSL transformation. The line and column numbers given are from the transformed result."), ec);
    14961508    }
     
    19131925    }
    19141926
    1915     if (newElement->hasTagName(scriptTag))
    1916         static_cast<HTMLScriptElement*>(newElement.get())->setCreatedByParser(true);
    1917 
    1918     if (newElement->hasTagName(HTMLNames::scriptTag)
    1919 #if ENABLE(SVG)
    1920         || newElement->hasTagName(SVGNames::scriptTag)
    1921 #endif
    1922         )
     1927    eventuallyMarkAsParserCreated(newElement.get());
     1928
     1929    if (isScriptElement(newElement.get()))
    19231930        m_scriptStartLine = lineNumber();
    19241931
     
    19381945
    19391946    Node* n = m_currentNode;
    1940 
    1941     // skip end of dummy element
    1942 //     if (m_parsingFragment & n->nodeType() == Node::DOCUMENT_FRAGMENT_NODE)
    1943 //         return;
    1944    
    19451947    RefPtr<Node> parent = n->parentNode();
    19461948    n->finishParsingChildren();
    19471949
    19481950    // don't load external scripts for standalone documents (for now)
    1949     if (n->isElementNode() && m_view && (static_cast<Element*>(n)->hasTagName(scriptTag)
    1950 #if ENABLE(SVG)
    1951                                          || static_cast<Element*>(n)->hasTagName(SVGNames::scriptTag)
    1952 #endif
    1953                                          )) {
    1954 
    1955 
     1951    if (n->isElementNode() && m_view && isScriptElement(static_cast<Element*>(n))) {
    19561952        ASSERT(!m_pendingScript);
    1957 
    19581953        m_requestingScript = true;
    19591954
    1960         Element* scriptElement = static_cast<Element*>(n);
    1961         String scriptHref;
    1962 
    1963         if (static_cast<Element*>(n)->hasTagName(scriptTag))
    1964             scriptHref = scriptElement->getAttribute(srcAttr);
    1965 #if ENABLE(SVG)
    1966         else if (static_cast<Element*>(n)->hasTagName(SVGNames::scriptTag))
    1967             scriptHref = scriptElement->getAttribute(XLinkNames::hrefAttr);
    1968 #endif
     1955        Element* element = static_cast<Element*>(n);
     1956        ScriptElement* scriptElement = castToScriptElement(element);
     1957
     1958        String scriptHref = scriptElement->sourceAttributeValue();
    19691959        if (!scriptHref.isEmpty()) {
    19701960            // we have a src attribute
    1971             const AtomicString& charset = scriptElement->getAttribute(charsetAttr);
    1972             if ((m_pendingScript = m_doc->docLoader()->requestScript(scriptHref, charset))) {
    1973                 m_scriptElement = scriptElement;
     1961            String scriptCharset = scriptElement->charsetAttributeValue();
     1962            if ((m_pendingScript = m_doc->docLoader()->requestScript(scriptHref, scriptCharset))) {
     1963                m_scriptElement = element;
    19741964                m_pendingScript->addClient(this);
    19751965
     
    19811971
    19821972        } else {
    1983             String scriptCode = "";
    1984             for (Node* child = scriptElement->firstChild(); child; child = child->nextSibling()) {
    1985                 if (child->isTextNode() || child->nodeType() == Node::CDATA_SECTION_NODE)
    1986                     scriptCode += static_cast<CharacterData*>(child)->data();
    1987             }
     1973            String scriptCode = scriptElement->scriptContent();
    19881974            m_view->frame()->loader()->executeScript(m_doc->url().string(), m_scriptStartLine, scriptCode);
    19891975        }
     1976
    19901977        m_requestingScript = false;
    19911978    }
  • trunk/WebCore/html/HTMLScriptElement.cpp

    r34589 r35744  
    2424#include "HTMLScriptElement.h"
    2525
    26 #include "CachedScript.h"
    27 #include "DocLoader.h"
    2826#include "Document.h"
    2927#include "EventNames.h"
    30 #include "Frame.h"
    31 #include "FrameLoader.h"
    3228#include "HTMLNames.h"
    33 #include "ScriptController.h"
    34 #include "MIMETypeRegistry.h"
    3529#include "Text.h"
    3630
     
    4236HTMLScriptElement::HTMLScriptElement(Document* doc)
    4337    : HTMLElement(scriptTag, doc)
    44     , m_cachedScript(0)
    45     , m_createdByParser(false)
    46     , m_evaluated(false)
     38    , m_data(this, this)
    4739{
    4840}
     
    5042HTMLScriptElement::~HTMLScriptElement()
    5143{
    52     if (m_cachedScript)
    53         m_cachedScript->removeClient(this);
    5444}
    5545
    5646bool HTMLScriptElement::isURLAttribute(Attribute* attr) const
    5747{
    58     return attr->name() == srcAttr;
     48    return attr->name() == sourceAttributeValue();
     49}
     50
     51void HTMLScriptElement::setCreatedByParser(bool createdByParser)
     52{
     53    m_data.setCreatedByParser(createdByParser);
     54}
     55
     56bool HTMLScriptElement::shouldExecuteAsJavaScript() const
     57{
     58    return m_data.shouldExecuteAsJavaScript();
    5959}
    6060
    6161void HTMLScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
    6262{
    63     // If a node is inserted as a child of the script element
    64     // and the script element has been inserted in the document
    65     // we evaluate the script.
    66     if (!m_createdByParser && inDocument() && firstChild())
    67         evaluateScript(document()->url().string(), text());
     63    ScriptElement::childrenChanged(m_data);
    6864    HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
    6965}
     
    7268{
    7369    const QualifiedName& attrName = attr->name();
    74     if (attrName == srcAttr) {
    75         if (m_evaluated || m_cachedScript || m_createdByParser || !inDocument())
    76             return;
    77 
    78         // FIXME: Evaluate scripts in viewless documents.
    79         // See http://bugs.webkit.org/show_bug.cgi?id=5727
    80         if (!document()->frame())
    81             return;
    82    
    83         const AtomicString& url = attr->value();
    84         if (!url.isEmpty()) {
    85             m_cachedScript = document()->docLoader()->requestScript(url, scriptCharset());
    86             if (m_cachedScript)
    87                 m_cachedScript->addClient(this);
    88             else
    89                 dispatchHTMLEvent(errorEvent, true, false);
    90         }
    91     } else if (attrName == onloadAttr)
     70
     71    if (attrName == srcAttr)
     72        handleSourceAttribute(m_data, attr->value());
     73    else if (attrName == onloadAttr)
    9274        setHTMLEventListener(loadEvent, attr);
    9375    else
     
    9779void HTMLScriptElement::finishParsingChildren()
    9880{
    99     // The parser just reached </script>. If we have no src and no text,
    100     // allow dynamic loading later.
    101     if (getAttribute(srcAttr).isEmpty() && text().isEmpty())
    102         setCreatedByParser(false);
     81    ScriptElement::finishParsingChildren(m_data, sourceAttributeValue());
    10382    HTMLElement::finishParsingChildren();
    10483}
     
    10786{
    10887    HTMLElement::insertedIntoDocument();
    109 
    110     ASSERT(!m_cachedScript);
    111 
    112     if (m_createdByParser)
    113         return;
    114    
    115     // FIXME: Eventually we'd like to evaluate scripts which are inserted into a
    116     // viewless document but this'll do for now.
    117     // See http://bugs.webkit.org/show_bug.cgi?id=5727
    118     if (!document()->frame())
    119         return;
    120    
    121     const AtomicString& url = getAttribute(srcAttr);
    122     if (!url.isEmpty()) {
    123         m_cachedScript = document()->docLoader()->requestScript(url, scriptCharset());
    124         if (m_cachedScript)
    125             m_cachedScript->addClient(this);
    126         else
    127             dispatchHTMLEvent(errorEvent, true, false);
     88    ScriptElement::insertedIntoDocument(m_data, sourceAttributeValue());
     89}
     90
     91void HTMLScriptElement::removedFromDocument()
     92{
     93    HTMLElement::removedFromDocument();
     94    ScriptElement::removedFromDocument(m_data);
     95}
     96
     97String HTMLScriptElement::text() const
     98{
     99    return m_data.scriptContent();
     100}
     101
     102void HTMLScriptElement::setText(const String &value)
     103{
     104    ExceptionCode ec = 0;
     105    int numChildren = childNodeCount();
     106
     107    if (numChildren == 1 && firstChild()->isTextNode()) {
     108        static_cast<Text*>(firstChild())->setData(value, ec);
    128109        return;
    129110    }
    130111
    131     // If there's an empty script node, we shouldn't evaluate the script
    132     // because if a script is inserted afterwards (by setting text or innerText)
    133     // it should be evaluated, and evaluateScript only evaluates a script once.
    134     String scriptString = text();   
    135     if (!scriptString.isEmpty())
    136         evaluateScript(document()->url().string(), scriptString);
    137 }
    138 
    139 void HTMLScriptElement::removedFromDocument()
    140 {
    141     HTMLElement::removedFromDocument();
    142 
    143     if (m_cachedScript) {
    144         m_cachedScript->removeClient(this);
    145         m_cachedScript = 0;
    146     }
    147 }
    148 
    149 void HTMLScriptElement::notifyFinished(CachedResource* o)
    150 {
    151     CachedScript* cs = static_cast<CachedScript*>(o);
    152 
    153     ASSERT(cs == m_cachedScript);
    154 
    155     // Evaluating the script could lead to a garbage collection which
    156     // can delete the script element so we need to protect it.
    157     RefPtr<HTMLScriptElement> protect(this);
    158    
    159     if (cs->errorOccurred())
    160         dispatchHTMLEvent(errorEvent, true, false);
    161     else {
    162         evaluateScript(cs->url(), cs->script());
    163         dispatchHTMLEvent(loadEvent, false, false);
    164     }
    165 
    166     // script evaluation may have dereffed it already
    167     if (m_cachedScript) {
    168         m_cachedScript->removeClient(this);
    169         m_cachedScript = 0;
    170     }
    171 }
    172 
    173 bool HTMLScriptElement::shouldExecuteAsJavaScript()
    174 {
    175     /*
    176          Mozilla 1.8 accepts javascript1.0 - javascript1.7, but WinIE 7 accepts only javascript1.1 - javascript1.3.
    177          Mozilla 1.8 and WinIE 7 both accept javascript and livescript.
    178          WinIE 7 accepts ecmascript and jscript, but Mozilla 1.8 doesn't.
    179          Neither Mozilla 1.8 nor WinIE 7 accept leading or trailing whitespace.
    180          We want to accept all the values that either of these browsers accept, but not other values.
    181      */
    182     static const AtomicString validLanguages[] = {
    183         "javascript",
    184         "javascript1.0",
    185         "javascript1.1",
    186         "javascript1.2",
    187         "javascript1.3",
    188         "javascript1.4",
    189         "javascript1.5",
    190         "javascript1.6",
    191         "javascript1.7",
    192         "livescript",
    193         "ecmascript",
    194         "jscript"
    195     };
    196     static const unsigned validLanguagesCount = sizeof(validLanguages) / sizeof(validLanguages[0]);
    197 
    198     const AtomicString& type = getAttribute(typeAttr);
    199     if (!type.isEmpty()) {
    200         String lowerType = type.string().stripWhiteSpace().lower();
    201         if (MIMETypeRegistry::isSupportedJavaScriptMIMEType(lowerType))
    202             return true;
    203 
    204         return false;
    205     }
    206 
    207     const AtomicString& language = getAttribute(languageAttr);
    208     if (!language.isEmpty()) {
    209         String lowerLanguage = language.string().lower();
    210         for (unsigned i = 0; i < validLanguagesCount; ++i)
    211             if (lowerLanguage == validLanguages[i])
    212                 return true;
    213 
    214         return false;
    215     }
    216 
    217     // No type or language is specified, so we assume the script to be JavaScript
    218     return true;
    219 }
    220 
    221 void HTMLScriptElement::evaluateScript(const String& url, const String& script)
    222 {
    223     if (m_evaluated)
    224         return;
    225    
    226     if (!shouldExecuteAsJavaScript())
    227         return;
    228    
    229     Frame* frame = document()->frame();
    230     if (frame) {
    231         if (frame->script()->isEnabled()) {
    232             m_evaluated = true;
    233             // FIXME: This starting line number will be incorrect for evaluation triggered
    234             // from insertedIntoDocument or childrenChanged.
    235             frame->script()->evaluate(url, 1, script);
    236             Document::updateDocumentsRendering();
    237         }
    238     }
    239 }
    240 
    241 String HTMLScriptElement::text() const
    242 {
    243     Vector<UChar> val;
    244     Text* firstTextNode = 0;
    245     bool foundMultipleTextNodes = false;
    246    
    247     for (Node* n = firstChild(); n; n = n->nextSibling()) {
    248         if (n->isTextNode()) {
    249             if (foundMultipleTextNodes)
    250                 append(val, static_cast<Text*>(n)->data());
    251             else if (firstTextNode) {
    252                 append(val, firstTextNode->data());
    253                 append(val, static_cast<Text*>(n)->data());
    254                 foundMultipleTextNodes = true;
    255             } else {
    256                 firstTextNode = static_cast<Text*>(n);
    257             }
    258         }
    259     }
    260        
    261     if (firstTextNode && !foundMultipleTextNodes)
    262         return firstTextNode->data();
    263    
    264     return String::adopt(val);
    265 }
    266 
    267 void HTMLScriptElement::setText(const String &value)
    268 {
    269     ExceptionCode ec = 0;
    270     int numChildren = childNodeCount();
    271    
    272     if (numChildren == 1 && firstChild()->isTextNode()) {
    273         static_cast<Text *>(firstChild())->setData(value, ec);
    274         return;
    275     }
    276    
    277112    if (numChildren > 0)
    278113        removeChildren();
    279    
     114
    280115    appendChild(document()->createTextNode(value.impl()), ec);
    281116}
     
    287122}
    288123
    289 void HTMLScriptElement::setHtmlFor(const String& /*value*/)
     124void HTMLScriptElement::setHtmlFor(const String&)
    290125{
    291126    // DOM Level 1 says: reserved for future use.
     
    298133}
    299134
    300 void HTMLScriptElement::setEvent(const String& /*value*/)
     135void HTMLScriptElement::setEvent(const String&)
    301136{
    302137    // DOM Level 1 says: reserved for future use.
     
    305140String HTMLScriptElement::charset() const
    306141{
    307     return getAttribute(charsetAttr);
     142    return charsetAttributeValue();
    308143}
    309144
     
    325160KURL HTMLScriptElement::src() const
    326161{
    327     return document()->completeURL(getAttribute(srcAttr));
     162    return document()->completeURL(sourceAttributeValue());
    328163}
    329164
     
    335170String HTMLScriptElement::type() const
    336171{
    337     return getAttribute(typeAttr);
     172    return typeAttributeValue();
    338173}
    339174
     
    345180String HTMLScriptElement::scriptCharset() const
    346181{
    347     // First we try to get encoding from charset attribute.
    348     String charset = getAttribute(charsetAttr).string().stripWhiteSpace();
    349     // If charset has not been declared in script tag, fall back
    350     // to frame encoding.
    351     if (charset.isEmpty()) {
    352         if (Frame* frame = document()->frame())
    353             charset = frame->loader()->encoding();
    354     }
    355     return charset;
     182    return m_data.scriptCharset();
     183}
     184
     185String HTMLScriptElement::scriptContent() const
     186{
     187    return m_data.scriptContent();
    356188}
    357189
     
    361193}
    362194
    363 }
     195String HTMLScriptElement::sourceAttributeValue() const
     196{
     197    return getAttribute(srcAttr).string();
     198}
     199
     200String HTMLScriptElement::charsetAttributeValue() const
     201{
     202    return getAttribute(charsetAttr).string();;
     203}
     204
     205String HTMLScriptElement::typeAttributeValue() const
     206{
     207    return getAttribute(typeAttr).string();;
     208}
     209
     210String HTMLScriptElement::languageAttributeValue() const
     211{
     212    return getAttribute(languageAttr).string();
     213}
     214
     215void HTMLScriptElement::dispatchLoadEvent()
     216{
     217    dispatchHTMLEvent(loadEvent, false, false);
     218}
     219
     220void HTMLScriptElement::dispatchErrorEvent()
     221{
     222    dispatchHTMLEvent(errorEvent, true, false);
     223}
     224
     225}
  • trunk/WebCore/html/HTMLScriptElement.h

    r31357 r35744  
    33 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
    44 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
     5 * Copyright (C) 2008 Nikolas Zimmermann <zimmermann@kde.org>
    56 *
    67 * This library is free software; you can redistribute it and/or
     
    2425#define HTMLScriptElement_h
    2526
    26 #include "CachedResourceClient.h"
     27#include "ScriptElement.h"
    2728#include "HTMLElement.h"
    2829
    2930namespace WebCore {
    3031
    31 class CachedScript;
    32 
    33 class HTMLScriptElement : public HTMLElement, public CachedResourceClient {
     32class HTMLScriptElement : public HTMLElement
     33                        , public ScriptElement {
    3434public:
    3535    HTMLScriptElement(Document*);
    3636    ~HTMLScriptElement();
     37
     38    void setCreatedByParser(bool);
     39    bool shouldExecuteAsJavaScript() const;
     40    virtual String scriptContent() const;
    3741
    3842    virtual HTMLTagStatus endTagRequirement() const { return TagStatusRequired; }
     
    4347    virtual void insertedIntoDocument();
    4448    virtual void removedFromDocument();
    45     virtual void notifyFinished(CachedResource*);
    46 
    4749    virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0);
    4850
    4951    virtual bool isURLAttribute(Attribute*) const;
    50 
    51     void setCreatedByParser(bool createdByParser) { m_createdByParser = createdByParser; }
    5252    virtual void finishParsingChildren();
    53 
    54     bool shouldExecuteAsJavaScript();
    55     void evaluateScript(const String& url, const String& script);
    5653
    5754    String text() const;
     
    8178    virtual void getSubresourceAttributeStrings(Vector<String>&) const;
    8279
     80protected:
     81    virtual String sourceAttributeValue() const;
     82    virtual String charsetAttributeValue() const;
     83    virtual String typeAttributeValue() const;
     84    virtual String languageAttributeValue() const;
     85
     86    virtual void dispatchLoadEvent();
     87    virtual void dispatchErrorEvent();
     88
    8389private:
    84     CachedScript* m_cachedScript;
    85     bool m_createdByParser;
    86     bool m_evaluated;
     90    ScriptElementData m_data;
    8791};
    8892
  • trunk/WebCore/svg/SVGScriptElement.cpp

    r35248 r35744  
    3333    , SVGURIReference()
    3434    , SVGExternalResourcesRequired()
     35    , m_data(this, this)
    3536{
    3637}
     
    4849{
    4950    m_type = type;
     51}
     52
     53String SVGScriptElement::scriptContent() const
     54{
     55    return m_data.scriptContent();
    5056}
    5157
     
    6975}
    7076
     77String SVGScriptElement::sourceAttributeValue() const
     78{
     79    return href();
     80}
     81
     82String SVGScriptElement::charsetAttributeValue() const
     83{
     84    return String();
     85}
     86
     87String SVGScriptElement::typeAttributeValue() const
     88{
     89    return type();
     90}
     91
     92String SVGScriptElement::languageAttributeValue() const
     93{
     94    return String();
     95}
     96
     97void SVGScriptElement::dispatchLoadEvent()
     98{
     99    // TODO!
     100    // dispatchSVGEvent(loadEvent, false, false);
     101}
     102
     103void SVGScriptElement::dispatchErrorEvent()
     104{
     105    // TODO!
     106    // dispatchSVGEvent(errorEvent, true, false);
     107}
    71108}
    72109
  • trunk/WebCore/svg/SVGScriptElement.h

    r35248 r35744  
    2525
    2626#if ENABLE(SVG)
     27#include "ScriptElement.h"
    2728#include "SVGElement.h"
    2829#include "SVGURIReference.h"
     
    3132namespace WebCore {
    3233
    33     class SVGScriptElement : public SVGElement,
    34                              public SVGURIReference,
    35                              public SVGExternalResourcesRequired {
     34    class SVGScriptElement : public SVGElement
     35                           , public SVGURIReference
     36                           , public SVGExternalResourcesRequired
     37                           , public ScriptElement {
    3638    public:
    3739        SVGScriptElement(const QualifiedName&, Document*);
    3840        virtual ~SVGScriptElement();
     41
     42        void setCreatedByParser(bool) { }
     43        virtual String scriptContent() const;
    3944
    4045        String type() const;
     
    4752        virtual const SVGElement* contextElement() const { return this; }
    4853
     54        virtual String sourceAttributeValue() const;
     55        virtual String charsetAttributeValue() const;
     56        virtual String typeAttributeValue() const;
     57        virtual String languageAttributeValue() const;
     58
     59        virtual void dispatchLoadEvent();
     60        virtual void dispatchErrorEvent();
     61
    4962    private:
     63        ScriptElementData m_data;
    5064        String m_type;
    5165    };
Note: See TracChangeset for help on using the changeset viewer.