Commit 570425a3 authored by Andrea Iacovitti's avatar Andrea Iacovitti

Introduce DOMString::trimSpaces(), useful for removing space characters from url string.

parent 9ba9651c
......@@ -1383,9 +1383,9 @@ CSSImageValueImpl::CSSImageValueImpl(const DOMString &url, StyleBaseImpl* style)
: CSSPrimitiveValueImpl(url, CSSPrimitiveValue::CSS_URI)
{
m_image = 0;
const QString imgUrl = url.string().trimmed();
const DOMString imgUrl = url.trimSpaces();
if (!imgUrl.isEmpty()) {
m_fullImageUrl = KUrl(style->baseURL(), imgUrl).url();
m_fullImageUrl = KUrl(style->baseURL(), imgUrl.string()).url();
} else {
m_fullImageUrl.clear();
}
......
......@@ -2309,8 +2309,8 @@ bool CSSParser::parseFontFaceSrc()
while ((val = valueList->current())) {
CSSFontFaceSrcValueImpl* parsedValue = 0;
if (val->unit == CSSPrimitiveValue::CSS_URI && !expectComma && styleElement) {
const QString uri = qString(val->string).trimmed();
parsedValue = new CSSFontFaceSrcValueImpl(DOMString(KUrl(styleElement->baseURL(), uri).url()), false /*local*/);
const DOMString uri = domString(val->string).trimSpaces();
parsedValue = new CSSFontFaceSrcValueImpl(DOMString(KUrl(styleElement->baseURL(), uri.string()).url()), false /*local*/);
uriValue = parsedValue;
allowFormat = true;
expectComma = true;
......
......@@ -290,6 +290,38 @@ bool DOMString::startsWith(const DOMString& str) const
return impl->startsWith(str.implementation());
}
static inline bool isSpaceCharacter(const ushort &c)
{
// http://dev.w3.org/html5/spec-LC/common-microsyntaxes.html#space-character
return ((c < 0x0021) &&
(c == 0x0020 || c == 0x0009 || c == 0x000A || c == 0x000C || c == 0x000D));
}
DOMString DOMString::trimSpaces() const
{
if (!impl || !impl->l) {
return *this;
}
const QChar *s = impl->s;
unsigned int start = 0;
unsigned int end = impl->l - 1;
while ((start <= end) && isSpaceCharacter(s[start].unicode())) {
++start;
}
if (start > end) {
return DOMString("");
}
while (end && isSpaceCharacter(s[end].unicode())) {
--end;
}
return new DOMStringImpl(s + start, end - start + 1);
}
// ------------------------------------------------------------------------
bool DOM::strcasecmp( const DOMString &as, const DOMString &bs )
......
......@@ -124,6 +124,13 @@ public:
bool endsWith(const DOMString& str) const;
bool startsWith(const DOMString& str) const;
/**
* Returns a string with Space Characters removed from the start and the end.
* Space Characters as defined in
* http://dev.w3.org/html5/spec-LC/common-microsyntaxes.html#space-character
*/
DOMString trimSpaces() const;
/**
* @internal get a handle to the imlementation of the DOMString
* Use at own risk!!!
......
......@@ -231,8 +231,8 @@ void HTMLFrameElement::setScrolling( const DOMString &value )
DOMString HTMLFrameElement::src() const
{
if(!impl) return DOMString();
const QString s = ((ElementImpl *)impl)->getAttribute(ATTR_SRC).string().trimmed();
return !s.isNull() ? impl->document()->completeURL(s) : DOMString();
const DOMString s = ((ElementImpl *)impl)->getAttribute(ATTR_SRC).trimSpaces();
return !s.isNull() ? impl->document()->completeURL(s.string()) : s;
}
void HTMLFrameElement::setSrc( const DOMString &value )
......@@ -367,8 +367,8 @@ void HTMLIFrameElement::setScrolling( const DOMString &value )
DOMString HTMLIFrameElement::src() const
{
if(!impl) return DOMString();
const QString s = ((ElementImpl *)impl)->getAttribute(ATTR_SRC).string().trimmed();
return !s.isNull() ? impl->document()->completeURL(s) : DOMString();
const DOMString s = ((ElementImpl *)impl)->getAttribute(ATTR_SRC).trimSpaces();
return !s.isNull() ? impl->document()->completeURL(s.string()) : s;
}
void HTMLIFrameElement::setSrc( const DOMString &value )
......
......@@ -100,7 +100,7 @@ DOMString HTMLDocument::referrer() const
DOMString HTMLDocument::completeURL(const DOMString& str) const
{
if(!impl) return str;
return ((HTMLDocumentImpl *)impl)->completeURL(str.string().trimmed());
return ((HTMLDocumentImpl *)impl)->completeURL(str.trimSpaces().string());
}
DOMString HTMLDocument::domain() const
......
......@@ -504,8 +504,8 @@ void HTMLInputElement::setSize( long value )
DOMString HTMLInputElement::src() const
{
if(!impl) return DOMString();
const QString s = static_cast<ElementImpl*>(impl)->getAttribute(ATTR_SRC).string().trimmed();
return !s.isNull() ? impl->document()->completeURL(s) : DOMString();
const DOMString s = static_cast<ElementImpl*>(impl)->getAttribute(ATTR_SRC).trimSpaces();
return !s.isNull() ? impl->document()->completeURL(s.string()) : s;
}
void HTMLInputElement::setSrc( const DOMString &value )
......
......@@ -58,8 +58,8 @@ HTMLBaseElement::~HTMLBaseElement()
DOMString HTMLBaseElement::href() const
{
if(!impl) return DOMString();
const QString href = static_cast<ElementImpl*>(impl)->getAttribute(ATTR_HREF).string().trimmed();
return !href.isNull() ? impl->document()->completeURL(href) : DOMString();
const DOMString href = static_cast<ElementImpl*>(impl)->getAttribute(ATTR_HREF).trimSpaces();
return !href.isNull() ? impl->document()->completeURL(href.string()) : href;
}
void HTMLBaseElement::setHref( const DOMString &value )
......@@ -134,8 +134,8 @@ void HTMLLinkElement::setCharset( const DOMString &value )
DOMString HTMLLinkElement::href() const
{
if(!impl) return DOMString();
const QString href = static_cast<ElementImpl*>(impl)->getAttribute(ATTR_HREF).string().trimmed();
return !href.isNull() ? impl->document()->completeURL(href) : DOMString();
const DOMString href = static_cast<ElementImpl*>(impl)->getAttribute(ATTR_HREF).trimSpaces();
return !href.isNull() ? impl->document()->completeURL(href.string()) : href;
}
void HTMLLinkElement::setHref( const DOMString &value )
......@@ -379,8 +379,8 @@ void HTMLScriptElement::setDefer( bool _defer )
DOMString HTMLScriptElement::src() const
{
if(!impl) return DOMString();
const QString s = ((ElementImpl *)impl)->getAttribute(ATTR_SRC).string().trimmed();
return !s.isNull() ? impl->document()->completeURL(s) : DOMString();
const DOMString s = ((ElementImpl *)impl)->getAttribute(ATTR_SRC).trimSpaces();
return !s.isNull() ? impl->document()->completeURL(s.string()) : s;
}
void HTMLScriptElement::setSrc( const DOMString &value )
......
......@@ -95,8 +95,8 @@ void HTMLAreaElement::setCoords( const DOMString &value )
DOMString HTMLAreaElement::href() const
{
if(!impl) return DOMString();
const QString href = static_cast<ElementImpl*>(impl)->getAttribute(ATTR_HREF).string().trimmed();
return !href.isNull() ? impl->document()->completeURL(href) : DOMString();
const DOMString href = static_cast<ElementImpl*>(impl)->getAttribute(ATTR_HREF).trimSpaces();
return !href.isNull() ? impl->document()->completeURL(href.string()) : href;
}
void HTMLAreaElement::setHref( const DOMString &value )
......@@ -302,8 +302,8 @@ void HTMLImageElement::setLongDesc( const DOMString &value )
DOMString HTMLImageElement::src() const
{
if(!impl) return DOMString();
const QString s = ((ElementImpl *)impl)->getAttribute(ATTR_SRC).string().trimmed();
return !s.isNull() ? impl->document()->completeURL(s) : DOMString();
const DOMString s = ((ElementImpl *)impl)->getAttribute(ATTR_SRC).trimSpaces();
return !s.isNull() ? impl->document()->completeURL(s.string()) : s;
}
void HTMLImageElement::setSrc( const DOMString &value )
......
......@@ -95,8 +95,8 @@ void HTMLAnchorElement::setCoords( const DOMString &value )
DOMString HTMLAnchorElement::href() const
{
if(!impl) return DOMString();
const QString href = static_cast<ElementImpl*>(impl)->getAttribute(ATTR_HREF).string().trimmed();
return !href.isNull() ? impl->document()->completeURL(href) : DOMString();
const DOMString href = static_cast<ElementImpl*>(impl)->getAttribute(ATTR_HREF).trimSpaces();
return !href.isNull() ? impl->document()->completeURL(href.string()) : href;
}
void HTMLAnchorElement::setHref( const DOMString &value )
......
......@@ -1587,8 +1587,8 @@ QHash<int, const HTMLElement::BoundPropInfo*>* HTMLElement::boundPropInfo()
QString KJS::HTMLElement::getURLArg(unsigned id) const
{
const QString rel = impl()->getAttribute(id).string().trimmed();
return !rel.isNull() ? impl()->document()->completeURL(rel) : rel;
const DOMString rel = impl()->getAttribute(id).trimSpaces();
return !rel.isNull() ? impl()->document()->completeURL(rel.string()) : QString();
}
DOM::HTMLElementImpl *toHTMLElement(JSValue *val) {
......
......@@ -906,7 +906,8 @@ JSValue *XMLHttpRequestProtoFunc::callAsFunction(ExecState *exec, JSObject *this
return throwError(exec, SyntaxError, "Not enough arguments");
QString method = args[0]->toString(exec).qstring();
KUrl url = KUrl(request->doc->completeURL(args[1]->toString(exec).qstring()));
DOMString urlArg = args[1]->toString(exec).domString().trimSpaces();
KUrl url = KUrl(request->doc->completeURL(urlArg.string()));
bool async = true;
if (args.size() >= 3) {
......
......@@ -71,7 +71,7 @@ void HTMLBodyElementImpl::parseAttribute(AttributeImpl *attr)
case ATTR_BACKGROUND:
{
QString url = attr->val()->string().trimmed();
QString url = attr->value().trimSpaces().string();
if (!url.isEmpty()) {
url = document()->completeURL( url );
addCSSProperty(CSS_PROP_BACKGROUND_IMAGE, DOMString("url('"+url+"')") );
......@@ -325,7 +325,7 @@ void HTMLFrameElementImpl::parseAttribute(AttributeImpl *attr)
switch(attr->id())
{
case ATTR_SRC:
setLocation(attr->value().string().trimmed());
setLocation(attr->value().trimSpaces().string());
break;
case ATTR_FRAMEBORDER:
{
......@@ -755,7 +755,7 @@ void HTMLIFrameElementImpl::parseAttribute(AttributeImpl *attr )
addHTMLAlignment( attr->value() );
break;
case ATTR_SRC:
url = attr->value().string().trimmed();
url = attr->value().trimSpaces().string();
setNeedComputeContent();
// ### synchronously start the process?
break;
......
......@@ -679,7 +679,7 @@ void HTMLFormElementImpl::submit( )
}
#endif // KHTML_NO_WALLET
QString url = getAttribute(ATTR_ACTION).string().trimmed();
QString url = getAttribute(ATTR_ACTION).trimSpaces().string();
// ignore base url if 'action' attribute is empty.
if (url.isEmpty())
url = formUrl.url();
......
......@@ -55,7 +55,7 @@ void HTMLBaseElementImpl::parseAttribute(AttributeImpl *attr)
switch(attr->id())
{
case ATTR_HREF:
m_href = attr->value().string().trimmed();
m_href = attr->value().trimSpaces().string();
process();
break;
case ATTR_TARGET:
......@@ -116,9 +116,9 @@ void HTMLLinkElementImpl::parseAttribute(AttributeImpl *attr)
switch (attr->id())
{
case ATTR_HREF: {
const QString hrefUrl = attr->val()->string().trimmed();
const DOMString hrefUrl = attr->value().trimSpaces();
if (!hrefUrl.isEmpty()) {
m_url = document()->completeURL(hrefUrl);
m_url = document()->completeURL(hrefUrl.string());
}
process();
break;
......@@ -385,7 +385,7 @@ void HTMLScriptElementImpl::parseAttribute(AttributeImpl *attr)
// we'll only start doing things once we get insertedIntoDocument()
if (m_evaluated || m_cachedScript || m_createdByParser || !inDocument())
return;
QString url = attr->value().string().trimmed();
const DOMString url = attr->value().trimSpaces();
if (!url.isEmpty())
loadFromUrl(url);
break;
......@@ -490,7 +490,7 @@ void HTMLScriptElementImpl::insertedIntoDocument()
if (m_createdByParser)
return;
QString url = getAttribute(ATTR_SRC).string().trimmed();
const DOMString url = getAttribute(ATTR_SRC).trimSpaces();
if (!url.isEmpty()) {
loadFromUrl(url);
return;
......@@ -622,7 +622,7 @@ void HTMLScriptElementImpl::setDefer(bool defer)
DOMString HTMLScriptElementImpl::src() const
{
return document()->completeURL(getAttribute(ATTR_SRC).string().trimmed());
return document()->completeURL(getAttribute(ATTR_SRC).trimSpaces().string());
}
void HTMLScriptElementImpl::setSrc(const DOMString &value)
......
......@@ -84,7 +84,7 @@ void HTMLImageElementImpl::parseAttribute(AttributeImpl *attr)
setChanged();
//Start loading the image already, to generate events
const QString imgSrcUrl = attr->value().string().trimmed();
const DOMString imgSrcUrl = attr->value().trimSpaces();
if (!imgSrcUrl.isEmpty()) { //### why do we not hide or something when setting this?
CachedImage* newImage = document()->docLoader()->requestImage(imgSrcUrl);
if (newImage && newImage != m_image) {
......@@ -151,7 +151,7 @@ void HTMLImageElementImpl::parseAttribute(AttributeImpl *attr)
if ( attr->value()[0] == '#' )
usemap = attr->value().lower();
else {
QString url = document()->completeURL(attr->value().string().trimmed());
QString url = document()->completeURL(attr->value().trimSpaces().string());
// ### we remove the part before the anchor and hope
// the map is on the same html page....
usemap = url;
......@@ -438,7 +438,7 @@ void HTMLMapElementImpl::parseAttribute(AttributeImpl *attr)
}
else {
// add name with full url:
const QString url = document()->completeURL(attr->value().string().trimmed());
const QString url = document()->completeURL(attr->value().trimSpaces().string());
if(document()->isHTMLDocument())
static_cast<HTMLDocumentImpl*>(document())->mapMap[url] = this;
}
......
......@@ -81,7 +81,7 @@ void HTMLAnchorElementImpl::defaultEventHandler(EventImpl *evt)
if (k->qKeyEvent()) k->qKeyEvent()->accept();
}
QString url = getAttribute(ATTR_HREF).string().trimmed();
QString url = getAttribute(ATTR_HREF).trimSpaces().string();
QString utarget = getAttribute(ATTR_TARGET).string();
if ( e && e->button() == 1 )
......
......@@ -695,7 +695,7 @@ void HTMLEmbedElementImpl::parseAttribute(AttributeImpl *attr)
{
case ATTR_CODE:
case ATTR_SRC:
url = attr->val()->string().trimmed();
url = attr->value().trimSpaces().string();
setNeedComputeContent();
break;
case ATTR_BORDER:
......@@ -775,7 +775,7 @@ void HTMLObjectElementImpl::parseAttribute(AttributeImpl *attr)
switch ( attr->id() )
{
case ATTR_DATA:
url = attr->val()->string().trimmed();
url = attr->value().trimSpaces().string();
setNeedComputeContent();
break;
case ATTR_CLASSID:
......
......@@ -495,7 +495,7 @@ void HTMLTableElementImpl::parseAttribute(AttributeImpl *attr)
break;
case ATTR_BACKGROUND:
{
QString url = attr->val()->string().trimmed();
QString url = attr->value().trimSpaces().string();
if (!url.isEmpty()) {
url = document()->completeURL( url );
addCSSProperty(CSS_PROP_BACKGROUND_IMAGE, DOMString("url('"+url+"')") );
......@@ -638,7 +638,7 @@ void HTMLTablePartElementImpl::parseAttribute(AttributeImpl *attr)
break;
case ATTR_BACKGROUND:
{
QString url = attr->val()->string().trimmed();
QString url = attr->value().trimSpaces().string();
if (!url.isEmpty()) {
url = document()->completeURL( url );
addCSSProperty(CSS_PROP_BACKGROUND_IMAGE, DOMString("url('"+url+"')") );
......
......@@ -974,7 +974,7 @@ NodeImpl *KHTMLParser::getElement(Token* t)
KHTMLGlobal::defaultHTMLSettings()->isHideAdsEnabled() &&
!strcasecmp( t->attrs->getValue( ATTR_TYPE ), "image" ) )
{
const QString url = doc()->completeURL(t->attrs->getValue(ATTR_SRC)->string().trimmed());
const QString url = doc()->completeURL(DOMString(t->attrs->getValue(ATTR_SRC)).trimSpaces().string());
if (KHTMLGlobal::defaultHTMLSettings()->isAdFiltered(url))
return 0;
}
......@@ -1104,7 +1104,7 @@ NodeImpl *KHTMLParser::getElement(Token* t)
KHTMLGlobal::defaultHTMLSettings()->isAdFilterEnabled()&&
KHTMLGlobal::defaultHTMLSettings()->isHideAdsEnabled())
{
const QString url = doc()->completeURL(t->attrs->getValue(ATTR_SRC)->string().trimmed());
const QString url = doc()->completeURL(DOMString(t->attrs->getValue(ATTR_SRC)).trimSpaces().string());
if (KHTMLGlobal::defaultHTMLSettings()->isAdFiltered(url))
return 0;
}
......
......@@ -706,7 +706,7 @@ void ProspectiveTokenizer::processAttribute()
LocalName attrLocal = LocalName::fromString(&attrDS, IDS_NormalizeLower);
uint attribute = attrLocal.id();
if (attribute == localNamePart(ATTR_SRC) && m_urlToLoad.isEmpty())
m_urlToLoad = DOMString(QString(m_attributeValue.data(), m_attributeValue.size()).trimmed());
m_urlToLoad = DOMString(m_attributeValue.data(), m_attributeValue.size()).trimSpaces();
break;
}
case ID_LINK:
......@@ -715,7 +715,7 @@ void ProspectiveTokenizer::processAttribute()
LocalName attrLocal = LocalName::fromString(&attrDS, IDS_NormalizeLower);
uint attribute = attrLocal.id();
if (attribute == localNamePart(ATTR_HREF) && m_urlToLoad.isEmpty())
m_urlToLoad = DOMString(QString(m_attributeValue.data(), m_attributeValue.size()).trimmed());
m_urlToLoad = DOMString(m_attributeValue.data(), m_attributeValue.size()).trimSpaces();
else if (attribute == localNamePart(ATTR_REL)) {
DOMStringImpl* lowerAttribute = DOMStringImpl(DOMStringImpl::ShallowCopy, m_attributeValue.data(), m_attributeValue.size()).lower();
QString val = lowerAttribute->string();
......
......@@ -6420,7 +6420,7 @@ bool KHTMLPart::handleMouseMoveEventDrag(khtml::MouseMoveEvent *event)
if (url.isEmpty() && innerNodeImpl && innerNodeImpl->id() == ID_IMG)
{
img = static_cast<HTMLImageElementImpl *>(innerNodeImpl);
u = completeURL(img->getAttribute(ATTR_SRC).string().trimmed());
u = completeURL(img->getAttribute(ATTR_SRC).trimSpaces().string());
pix = KIconLoader::global()->loadIcon("image-x-generic", KIconLoader::Desktop);
}
else
......@@ -6719,7 +6719,7 @@ void KHTMLPart::runAdFilter()
node->id() == ID_IFRAME ||
(node->id() == ID_INPUT && static_cast<HTMLInputElementImpl *>(node)->inputType() == HTMLInputElementImpl::IMAGE ))
{
if (KHTMLGlobal::defaultHTMLSettings()->isAdFiltered(d->m_doc->completeURL(static_cast<ElementImpl *>(node)->getAttribute(ATTR_SRC).string().trimmed())))
if (KHTMLGlobal::defaultHTMLSettings()->isAdFiltered(d->m_doc->completeURL(static_cast<ElementImpl *>(node)->getAttribute(ATTR_SRC).trimSpaces().string())))
{
// Since any kids of node will be deleted, too, fastforward nextNode
// until we get outside of node.
......
......@@ -2780,7 +2780,7 @@ QMap< ElementImpl*, QChar > KHTMLView::buildFallbackAccessKeys() const
bool text_before = false;
switch( element->id()) {
case ID_A:
url = element->getAttribute(ATTR_HREF).string().trimmed();
url = element->getAttribute(ATTR_HREF).trimSpaces().string();
if( url.isEmpty()) // doesn't have href, it's only an anchor
continue;
text = static_cast< HTMLElementImpl* >( element )->innerText().string().simplified();
......
......@@ -427,16 +427,16 @@ void RenderImage::updateFromElement()
else if (element()->id() == ID_IMG)
alt = static_cast<HTMLImageElementImpl*>(element())->altText();
const QString u = element()->id() == ID_OBJECT ?
element()->getAttribute(ATTR_DATA).string().trimmed() : element()->getAttribute(ATTR_SRC).string().trimmed();
const DOMString u = element()->id() == ID_OBJECT ?
element()->getAttribute(ATTR_DATA).trimSpaces() : element()->getAttribute(ATTR_SRC).trimSpaces();
if (!u.isEmpty()) {
// Need to compute completeURL, as 'u' can be relative
// while m_cachedImage->url() is always full url
DocumentImpl *docImpl = element()->document();
const QString fullUrl = docImpl->completeURL(u);
const QString fullUrl = docImpl->completeURL(u.string());
if (!m_cachedImage || m_cachedImage->url() != fullUrl) {
CachedImage *new_image = docImpl->docLoader()->requestImage(fullUrl);
CachedImage *new_image = docImpl->docLoader()->requestImage(DOMString(fullUrl));
if (new_image && new_image != m_cachedImage) {
updateImage(new_image);
}
......
......@@ -1979,7 +1979,7 @@ void DocumentImpl::processHttpEquiv(const DOMString &equiv, const DOMString &con
v->part()->scheduleRedirection(delay, v->part()->url().url() );
} else {
pos++;
while(pos < (int)str.length() && str[pos].isSpace()) pos++;
while(pos < str.length() && str[pos].isSpace()) pos++;
str = str.mid(pos);
if(str.indexOf("url", 0, Qt::CaseInsensitive ) == 0) str = str.mid(3);
str = str.trimmed();
......@@ -1987,7 +1987,8 @@ void DocumentImpl::processHttpEquiv(const DOMString &equiv, const DOMString &con
while (str.length() && (str[str.length()-1] == ';' || str[str.length()-1] == ',')) {
str.resize(str.length()-1);
}
QString newURL = document()->completeURL(str.trimmed());
str = DOMString(str).trimSpaces().string();
QString newURL = document()->completeURL(str);
if ( ok )
v->part()->scheduleRedirection(delay, newURL, delay < 2 || newURL == URL().url());
}
......@@ -2047,7 +2048,7 @@ bool DocumentImpl::prepareMouseEvent( bool readonly, int _x, int _y, MouseEvent
//qDebug("urlnode: %s (%d)", getTagName(renderInfo.URLElement()->id()).string().toLatin1().constData(), renderInfo.URLElement()->id());
ElementImpl* e = static_cast<ElementImpl*>(renderInfo.URLElement());
const DOMString href = e->getAttribute(ATTR_HREF).string().trimmed();
const DOMString href = e->getAttribute(ATTR_HREF).trimSpaces();
const DOMString target = e->getAttribute(ATTR_TARGET);
if (!target.isNull() && !href.isNull()) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment