Changeset 31230 in webkit


Ignore:
Timestamp:
Mar 22, 2008 2:49:58 AM (16 years ago)
Author:
eric@webkit.org
Message:

Reviewed by mjs.

Fix createElementNS to throw exceptions for invalid qualified names
Fixes Acid3 sub-test 23
http://bugs.webkit.org/show_bug.cgi?id=16833

Tests: fast/dom/Document/createAttributeNS-namespace-err.html

fast/dom/Document/createElementNS-namespace-err.html

  • dom/Document.cpp: (WebCore::Document::createElement): (WebCore::hasNamespaceError): (WebCore::Document::createElementNS): (WebCore::Document::createAttributeNS):
  • dom/Document.idl:
Location:
trunk
Files:
7 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r31228 r31230  
     12008-03-22  Eric Seidel  <eric@webkit.org>
     2
     3        Reviewed by mjs.
     4
     5        Fix createElementNS to throw exceptions for invalid qualified names
     6        Fixes Acid3 sub-test 23
     7        http://bugs.webkit.org/show_bug.cgi?id=16833
     8
     9        * dom/xhtml/level3/core/documentsetstricterrorchecking02-expected.txt:
     10        * fast/dom/Document/createAttributeNS-namespace-err-expected.txt: Added.
     11        * fast/dom/Document/createAttributeNS-namespace-err.html: Copied from LayoutTests/fast/dom/Element/dimension-properties-unrendered.html.
     12        * fast/dom/Document/createElementNS-namespace-err-expected.txt: Added.
     13        * fast/dom/Document/createElementNS-namespace-err.html: Copied from LayoutTests/fast/dom/EntityReference/readonly-exceptions.html.
     14        * fast/dom/Document/resources/TEMPLATE.html: Copied from LayoutTests/fast/dom/Element/resources/TEMPLATE.html.
     15        * fast/dom/Document/resources/createAttributeNS-namespace-err.js: Added.
     16        * fast/dom/Document/resources/createElementNS-namespace-err.js: Added.
     17
    1182008-03-21  Cameron Zwarich  <cwzwarich@uwaterloo.ca>
    219
  • trunk/LayoutTests/dom/xhtml/level3/core/documentsetstricterrorchecking02-expected.txt

    r30052 r31230  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/documentsetstricterrorchecking02
    2 Status  failure
    3 Message NAMESPACE_ERR_documentsetstricterrorchecking02: assertTrue failed
     2Status  Success
  • trunk/WebCore/ChangeLog

    r31228 r31230  
     12008-03-22  Eric Seidel  <eric@webkit.org>
     2
     3        Reviewed by mjs.
     4
     5        Fix createElementNS to throw exceptions for invalid qualified names
     6        Fixes Acid3 sub-test 23
     7        http://bugs.webkit.org/show_bug.cgi?id=16833
     8
     9        Tests: fast/dom/Document/createAttributeNS-namespace-err.html
     10               fast/dom/Document/createElementNS-namespace-err.html
     11
     12        * dom/Document.cpp:
     13        (WebCore::Document::createElement):
     14        (WebCore::hasNamespaceError):
     15        (WebCore::Document::createElementNS):
     16        (WebCore::Document::createAttributeNS):
     17        * dom/Document.idl:
     18
    1192008-03-21  Cameron Zwarich  <cwzwarich@uwaterloo.ca>
    220
  • trunk/WebCore/dom/Document.cpp

    r31160 r31230  
    108108#include "WheelEvent.h"
    109109#include "XMLHttpRequest.h"
     110#include "XMLNames.h"
    110111#include "XMLTokenizer.h"
    111112#include "kjs_binding.h"
     
    515516PassRefPtr<Element> Document::createElement(const String &name, ExceptionCode& ec)
    516517{
    517     if (m_isXHTML) {
    518         if (!isValidName(name)) {
    519             ec = INVALID_CHARACTER_ERR;
    520             return 0;
    521         }
    522 
    523         return HTMLElementFactory::createHTMLElement(AtomicString(name), this, 0, false);
    524     } else
    525         return createElementNS(nullAtom, name, ec);
     518    if (!isValidName(name)) {
     519        ec = INVALID_CHARACTER_ERR;
     520        return 0;
     521    }
     522
     523    if (m_isXHTML)
     524        return HTMLElementFactory::createHTMLElement(name, this, 0, false);
     525
     526    return createElement(QualifiedName(nullAtom, name, nullAtom), false, ec);
    526527}
    527528
     
    717718}
    718719
     720static bool hasNamespaceError(const QualifiedName& qName)
     721{
     722    static const AtomicString xmlnsNamespaceURI = "http://www.w3.org/2000/xmlns/";
     723    static const AtomicString xmlns = "xmlns";
     724    static const AtomicString xml = "xml";
     725
     726    // These checks are from DOM Core Level 2, createElementNS
     727    // http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-DocCrElNS
     728    if (qName.prefix() == emptyAtom) // createElementNS(null, ":div")
     729        return true;
     730    if (qName.localName().isEmpty()) // createElementNS(null, ""), createElementNS(null, null), createElementNS()
     731        return true;
     732    if (!qName.prefix().isEmpty() && qName.namespaceURI().isNull()) // createElementNS(null, "html:div")
     733        return true;
     734    if (qName.prefix() == xml && qName.namespaceURI() != XMLNames::xmlNamespaceURI) // createElementNS("http://www.example.com", "xml:lang")
     735        return true;
     736
     737    // Required by DOM Level 3 Core and unspecified by DOM Level 2 Core:
     738    // http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-DocCrElNS
     739    // createElementNS("http://www.w3.org/2000/xmlns/", "foo:bar"), createElementNS(null, "xmlns:bar")
     740    if ((qName.prefix() == xmlns && qName.namespaceURI() != xmlnsNamespaceURI) || (qName.prefix() != xmlns && qName.namespaceURI() == xmlnsNamespaceURI))
     741        return true;
     742
     743    return false;
     744}
     745
    719746// FIXME: This should really be in a possible ElementFactory class
    720747PassRefPtr<Element> Document::createElement(const QualifiedName& qName, bool createdByParser, ExceptionCode& ec)
     
    733760        e = new Element(qName, document());
    734761   
     762    // FIXME: The element factories should be fixed to not ignore qName.prefix()
     763    // Instead they should pass the entire qName into element creation so we don't
     764    // need to manually set the prefix after creation.
     765    // Then this code can become ASSERT(qName == e.qname());
     766    // and Document::createElement can stop taking ExceptionCode& as well.
    735767    if (e && !qName.prefix().isNull()) {
    736768        ec = 0;
     
    738770        if (ec)
    739771            return 0;
    740     }   
     772    }
    741773   
    742774    return e.release();
    743775}
    744776
    745 PassRefPtr<Element> Document::createElementNS(const String &_namespaceURI, const String &qualifiedName, ExceptionCode& ec)
     777PassRefPtr<Element> Document::createElementNS(const String& namespaceURI, const String& qualifiedName, ExceptionCode& ec)
    746778{
    747779    String prefix, localName;
     
    751783    }
    752784
    753     RefPtr<Element> e;
    754     QualifiedName qName = QualifiedName(AtomicString(prefix), AtomicString(localName), AtomicString(_namespaceURI));
    755    
     785    QualifiedName qName(prefix, localName, namespaceURI);
     786    if (hasNamespaceError(qName)) {
     787        ec = NAMESPACE_ERR;
     788        return 0;
     789    }
     790
    756791    return createElement(qName, false, ec);
    757792}
     
    27852820}
    27862821
    2787 bool Document::parseQualifiedName(const String &qualifiedName, String &prefix, String &localName)
     2822bool Document::parseQualifiedName(const String& qualifiedName, String& prefix, String& localName)
    27882823{
    27892824    unsigned length = qualifiedName.length();
     
    34903525}
    34913526
    3492 PassRefPtr<Attr> Document::createAttributeNS(const String &namespaceURI, const String &qualifiedName, ExceptionCode& ec)
    3493 {
    3494     if (qualifiedName.isNull()) {
     3527PassRefPtr<Attr> Document::createAttributeNS(const String& namespaceURI, const String& qualifiedName, ExceptionCode& ec)
     3528{
     3529    String prefix, localName;
     3530    if (!parseQualifiedName(qualifiedName, prefix, localName)) {
     3531        ec = INVALID_CHARACTER_ERR;
     3532        return 0;
     3533    }
     3534
     3535    QualifiedName qName(prefix, localName, namespaceURI);
     3536    if (hasNamespaceError(qName)) {
    34953537        ec = NAMESPACE_ERR;
    34963538        return 0;
    34973539    }
    34983540
    3499     String localName = qualifiedName;
    3500     String prefix;
    3501     int colonpos;
    3502     if ((colonpos = qualifiedName.find(':')) >= 0) {
    3503         prefix = qualifiedName.substring(0, colonpos);
    3504         localName = qualifiedName.substring(colonpos + 1);
    3505     }
    3506 
    3507     if (!isValidName(localName)) {
    3508         ec = INVALID_CHARACTER_ERR;
     3541    // Spec: DOM Level 2 Core: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-DocCrAttrNS
     3542    if (qName.localName() == "xmlns" && qName.namespaceURI() != "http://www.w3.org/2000/xmlns/") {
     3543        ec = NAMESPACE_ERR;
    35093544        return 0;
    35103545    }
    3511    
     3546
    35123547    // FIXME: Assume this is a mapped attribute, since createAttribute isn't namespace-aware.  There's no harm to XML
    35133548    // documents if we're wrong.
    3514     return new Attr(0, this, new MappedAttribute(QualifiedName(prefix, localName, namespaceURI), StringImpl::empty()));
     3549    return new Attr(0, this, new MappedAttribute(qName, StringImpl::empty()));
    35153550}
    35163551
  • trunk/WebCore/dom/Document.idl

    r30515 r31230  
    3434        readonly attribute Element documentElement;
    3535
    36         Element            createElement(in DOMString tagName)
     36        Element            createElement(in [ConvertNullToNullString] DOMString tagName)
    3737            raises (DOMException);
    3838        DocumentFragment   createDocumentFragment();
     
    5656            raises (DOMException);
    5757        [OldStyleObjC] Element createElementNS(in [ConvertNullToNullString] DOMString namespaceURI,
    58                                                in DOMString qualifiedName)
     58                                               in [ConvertNullToNullString] DOMString qualifiedName)
    5959            raises (DOMException);
    6060        [OldStyleObjC] Attr createAttributeNS(in [ConvertNullToNullString] DOMString namespaceURI,
    61                                               in DOMString qualifiedName)
     61                                              in [ConvertNullToNullString] DOMString qualifiedName)
    6262            raises (DOMException);
    6363        [OldStyleObjC] NodeList getElementsByTagNameNS(in [ConvertNullToNullString] DOMString namespaceURI,
Note: See TracChangeset for help on using the changeset viewer.