blob: cec8ce8e9989061a85f609dd019f0208875d1f44 [file] [log] [blame]
/*
* This file is part of the DOM implementation for KDE.
*
* Copyright (C) 2003 Lars Knoll (knoll@kde.org)
* Copyright (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
* Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "cssparser.h"
#include "Document.h"
#include "css_ruleimpl.h"
#include "css_stylesheetimpl.h"
#include "css_valueimpl.h"
#include "csshelper.h"
#include "CSSPropertyNames.h"
#include "CSSValueKeywords.h"
#include <KURL.h>
#include <kxmlcore/Assertions.h>
#include <stdlib.h>
#define YYDEBUG 0
#if YYDEBUG > 0
extern int cssyydebug;
#endif
extern int cssyyparse(void* parser);
namespace WebCore {
ValueList::~ValueList()
{
size_t numValues = m_values.size();
for (size_t i = 0; i < numValues; i++)
if (m_values[i].unit == Value::Function)
delete m_values[i].function;
}
CSSParser* CSSParser::currentParser = 0;
CSSParser::CSSParser(bool strictParsing)
{
#ifdef CSS_DEBUG
kdDebug( 6080 ) << "CSSParser::CSSParser this=" << this << endl;
#endif
strict = strictParsing;
parsedProperties = (CSSProperty **)fastMalloc(32 * sizeof(CSSProperty *));
numParsedProperties = 0;
maxParsedProperties = 32;
data = 0;
valueList = 0;
id = 0;
important = false;
m_inParseShorthand = 0;
m_currentShorthand = 0;
m_implicitShorthand = false;
defaultNamespace = starAtom;
yy_start = 1;
#if YYDEBUG > 0
cssyydebug = 1;
#endif
}
CSSParser::~CSSParser()
{
clearProperties();
fastFree(parsedProperties);
delete valueList;
fastFree(data);
deleteAllValues(m_floatingSelectors);
deleteAllValues(m_floatingValueLists);
deleteAllValues(m_floatingFunctions);
}
void ParseString::lower()
{
for (int i = 0; i < length; i++)
string[i] = QChar(string[i]).lower().unicode();
}
void CSSParser::setupParser(const char* prefix, const String& string, const char* suffix)
{
int length = string.length() + strlen(prefix) + strlen(suffix) + 2;
data = (unsigned short*)fastMalloc(length * sizeof(unsigned short));
for (unsigned i = 0; i < strlen(prefix); i++)
data[i] = prefix[i];
memcpy(data + strlen(prefix), string.unicode(), string.length() * sizeof(unsigned short));
unsigned start = strlen(prefix) + string.length();
unsigned end = start + strlen(suffix);
for (unsigned i = start; i < end; i++)
data[i] = suffix[i - start];
data[length - 1] = 0;
data[length - 2] = 0;
yy_hold_char = 0;
yyleng = 0;
yytext = yy_c_buf_p = data;
yy_hold_char = *yy_c_buf_p;
}
void CSSParser::parseSheet(CSSStyleSheet* sheet, const String& string)
{
styleElement = sheet;
defaultNamespace = starAtom; // Reset the default namespace.
setupParser("", string, "");
CSSParser* old = currentParser;
currentParser = this;
cssyyparse(this);
currentParser = old;
rule = 0;
}
PassRefPtr<CSSRule> CSSParser::parseRule(CSSStyleSheet *sheet, const String &string)
{
styleElement = sheet;
setupParser("@-khtml-rule{", string, "} ");
CSSParser* old = currentParser;
currentParser = this;
cssyyparse(this);
currentParser = old;
return rule.release();
}
bool CSSParser::parseValue( CSSMutableStyleDeclaration *declaration, int _id, const String &string,
bool _important)
{
styleElement = declaration->stylesheet();
setupParser("@-khtml-value{", string, "} ");
id = _id;
important = _important;
CSSParser* old = currentParser;
currentParser = this;
cssyyparse(this);
currentParser = old;
rule = 0;
bool ok = false;
if (numParsedProperties) {
ok = true;
declaration->addParsedProperties(parsedProperties, numParsedProperties);
clearProperties();
}
return ok;
}
RGBA32 CSSParser::parseColor(const String &string)
{
RGBA32 color = 0;
RefPtr<CSSMutableStyleDeclaration>dummyStyleDeclaration = new CSSMutableStyleDeclaration;
CSSParser parser(true);
// First try creating a color specified by name or the "#" syntax.
if (!parser.parseColor(string.deprecatedString(), color)) {
// Now try to create a color from the rgb() or rgba() syntax.
if (parser.parseColor(dummyStyleDeclaration.get(), string)) {
CSSValue *value = parser.parsedProperties[0]->value();
if (value->cssValueType() == CSSValue::CSS_PRIMITIVE_VALUE) {
CSSPrimitiveValue *primitiveValue = static_cast<CSSPrimitiveValue *>(value);
color = primitiveValue->getRGBColorValue();
}
}
}
return color;
}
bool CSSParser::parseColor( CSSMutableStyleDeclaration *declaration, const String &string )
{
styleElement = declaration->stylesheet();
setupParser("@-khtml-decls{color:", string, "} ");
CSSParser* old = currentParser;
currentParser = this;
cssyyparse(this);
currentParser = old;
rule = 0;
bool ok = false;
if (numParsedProperties && parsedProperties[0]->m_id == CSS_PROP_COLOR)
ok = true;
return ok;
}
bool CSSParser::parseDeclaration( CSSMutableStyleDeclaration *declaration, const String &string )
{
styleElement = declaration->stylesheet();
setupParser("@-khtml-decls{", string, "} ");
CSSParser* old = currentParser;
currentParser = this;
cssyyparse(this);
currentParser = old;
rule = 0;
bool ok = false;
if (numParsedProperties) {
ok = true;
declaration->addParsedProperties(parsedProperties, numParsedProperties);
clearProperties();
}
return ok;
}
void CSSParser::addProperty(int propId, CSSValue *value, bool important)
{
CSSProperty *prop = new CSSProperty(propId, value, important, m_currentShorthand, m_implicitShorthand);
if (numParsedProperties >= maxParsedProperties) {
maxParsedProperties += 32;
parsedProperties = (CSSProperty **)fastRealloc(parsedProperties,
maxParsedProperties*sizeof(CSSProperty *));
}
parsedProperties[numParsedProperties++] = prop;
}
void CSSParser::clearProperties()
{
for (int i = 0; i < numParsedProperties; i++)
delete parsedProperties[i];
numParsedProperties = 0;
}
Document *CSSParser::document() const
{
StyleBase *root = styleElement;
Document *doc = 0;
while (root->parent())
root = root->parent();
if (root->isCSSStyleSheet())
doc = static_cast<CSSStyleSheet*>(root)->doc();
return doc;
}
bool CSSParser::validUnit(Value* value, Units unitflags, bool strict)
{
if ( unitflags & FNonNeg && value->fValue < 0 )
return false;
bool b = false;
switch( value->unit ) {
case CSSPrimitiveValue::CSS_NUMBER:
b = (unitflags & FNumber);
if ( !b && ( (unitflags & FLength) && (value->fValue == 0 || !strict ) ) ) {
value->unit = CSSPrimitiveValue::CSS_PX;
b = true;
}
if ( !b && ( unitflags & FInteger ) &&
(value->fValue - (int)value->fValue) < 0.001 )
b = true;
break;
case CSSPrimitiveValue::CSS_PERCENTAGE:
b = (unitflags & FPercent);
break;
case Value::Q_EMS:
case CSSPrimitiveValue::CSS_EMS:
case CSSPrimitiveValue::CSS_EXS:
case CSSPrimitiveValue::CSS_PX:
case CSSPrimitiveValue::CSS_CM:
case CSSPrimitiveValue::CSS_MM:
case CSSPrimitiveValue::CSS_IN:
case CSSPrimitiveValue::CSS_PT:
case CSSPrimitiveValue::CSS_PC:
b = (unitflags & FLength);
break;
case CSSPrimitiveValue::CSS_MS:
case CSSPrimitiveValue::CSS_S:
b = (unitflags & FTime);
break;
case CSSPrimitiveValue::CSS_DEG:
case CSSPrimitiveValue::CSS_RAD:
case CSSPrimitiveValue::CSS_GRAD:
case CSSPrimitiveValue::CSS_HZ:
case CSSPrimitiveValue::CSS_KHZ:
case CSSPrimitiveValue::CSS_DIMENSION:
default:
break;
}
return b;
}
bool CSSParser::parseValue( int propId, bool important )
{
if ( !valueList ) return false;
Value *value = valueList->current();
if ( !value )
return false;
int id = value->id;
if (id == CSS_VAL_INHERIT) {
addProperty(propId, new CSSInheritedValue(), important);
return true;
}
else if (id == CSS_VAL_INITIAL) {
addProperty(propId, new CSSInitialValue(), important);
return true;
}
bool valid_primitive = false;
CSSValue *parsedValue = 0;
switch(propId) {
/* The comment to the left defines all valid value of this properties as defined
* in CSS 2, Appendix F. Property index
*/
/* All the CSS properties are not supported by the renderer at the moment.
* Note that all the CSS2 Aural properties are only checked, if CSS_AURAL is defined
* (see parseAuralValues). As we don't support them at all this seems reasonable.
*/
case CSS_PROP_SIZE: // <length>{1,2} | auto | portrait | landscape | inherit
case CSS_PROP_QUOTES: // [<string> <string>]+ | none | inherit
// case CSS_PROP_PAGE: // <identifier> | auto // ### CHECK
// ### To be done
if (id)
valid_primitive = true;
break;
case CSS_PROP_UNICODE_BIDI: // normal | embed | bidi-override | inherit
if ( id == CSS_VAL_NORMAL ||
id == CSS_VAL_EMBED ||
id == CSS_VAL_BIDI_OVERRIDE )
valid_primitive = true;
break;
case CSS_PROP_POSITION: // static | relative | absolute | fixed | inherit
if ( id == CSS_VAL_STATIC ||
id == CSS_VAL_RELATIVE ||
id == CSS_VAL_ABSOLUTE ||
id == CSS_VAL_FIXED )
valid_primitive = true;
break;
case CSS_PROP_PAGE_BREAK_AFTER: // auto | always | avoid | left | right | inherit
case CSS_PROP_PAGE_BREAK_BEFORE: // auto | always | avoid | left | right | inherit
if ( id == CSS_VAL_AUTO ||
id == CSS_VAL_ALWAYS ||
id == CSS_VAL_AVOID ||
id == CSS_VAL_LEFT ||
id == CSS_VAL_RIGHT )
valid_primitive = true;
break;
case CSS_PROP_PAGE_BREAK_INSIDE: // avoid | auto | inherit
if ( id == CSS_VAL_AUTO ||
id == CSS_VAL_AVOID )
valid_primitive = true;
break;
case CSS_PROP_EMPTY_CELLS: // show | hide | inherit
if ( id == CSS_VAL_SHOW ||
id == CSS_VAL_HIDE )
valid_primitive = true;
break;
case CSS_PROP_CONTENT: // [ <string> | <uri> | <counter> | attr(X) | open-quote |
// close-quote | no-open-quote | no-close-quote ]+ | inherit
return parseContent( propId, important );
break;
case CSS_PROP_WHITE_SPACE: // normal | pre | nowrap | inherit
if (id == CSS_VAL_NORMAL ||
id == CSS_VAL_PRE ||
id == CSS_VAL_PRE_WRAP ||
id == CSS_VAL_PRE_LINE ||
id == CSS_VAL_NOWRAP)
valid_primitive = true;
break;
case CSS_PROP_CLIP: // <shape> | auto | inherit
if ( id == CSS_VAL_AUTO )
valid_primitive = true;
else if ( value->unit == Value::Function )
return parseShape( propId, important );
break;
#if __APPLE__
case CSS_PROP__KHTML_DASHBOARD_REGION: // <dashboard-region> | <dashboard-region>
if ( value->unit == Value::Function || id == CSS_VAL_NONE)
return parseDashboardRegions( propId, important );
break;
#endif
/* Start of supported CSS properties with validation. This is needed for parseShorthand to work
* correctly and allows optimization in WebCore::applyRule(..)
*/
case CSS_PROP_CAPTION_SIDE: // top | bottom | left | right | inherit
if (id == CSS_VAL_LEFT || id == CSS_VAL_RIGHT ||
id == CSS_VAL_TOP || id == CSS_VAL_BOTTOM)
valid_primitive = true;
break;
case CSS_PROP_BORDER_COLLAPSE: // collapse | separate | inherit
if ( id == CSS_VAL_COLLAPSE || id == CSS_VAL_SEPARATE )
valid_primitive = true;
break;
case CSS_PROP_VISIBILITY: // visible | hidden | collapse | inherit
if (id == CSS_VAL_VISIBLE || id == CSS_VAL_HIDDEN || id == CSS_VAL_COLLAPSE)
valid_primitive = true;
break;
case CSS_PROP_OVERFLOW: // visible | hidden | scroll | auto | marquee | overlay | inherit
if (id == CSS_VAL_VISIBLE || id == CSS_VAL_HIDDEN || id == CSS_VAL_SCROLL || id == CSS_VAL_AUTO ||
id == CSS_VAL_MARQUEE || id == CSS_VAL_OVERLAY)
valid_primitive = true;
break;
case CSS_PROP_LIST_STYLE_POSITION: // inside | outside | inherit
if ( id == CSS_VAL_INSIDE || id == CSS_VAL_OUTSIDE )
valid_primitive = true;
break;
case CSS_PROP_LIST_STYLE_TYPE:
// disc | circle | square | decimal | decimal-leading-zero | lower-roman |
// upper-roman | lower-greek | lower-alpha | lower-latin | upper-alpha |
// upper-latin | hebrew | armenian | georgian | cjk-ideographic | hiragana |
// katakana | hiragana-iroha | katakana-iroha | none | inherit
if ((id >= CSS_VAL_DISC && id <= CSS_VAL_KATAKANA_IROHA) || id == CSS_VAL_NONE)
valid_primitive = true;
break;
case CSS_PROP_DISPLAY:
// inline | block | list-item | run-in | inline-block | table |
// inline-table | table-row-group | table-header-group | table-footer-group | table-row |
// table-column-group | table-column | table-cell | table-caption | box | inline-box | none | inherit
if ((id >= CSS_VAL_INLINE && id <= CSS_VAL__KHTML_INLINE_BOX) || id == CSS_VAL_NONE)
valid_primitive = true;
break;
case CSS_PROP_DIRECTION: // ltr | rtl | inherit
if ( id == CSS_VAL_LTR || id == CSS_VAL_RTL )
valid_primitive = true;
break;
case CSS_PROP_TEXT_TRANSFORM: // capitalize | uppercase | lowercase | none | inherit
if ((id >= CSS_VAL_CAPITALIZE && id <= CSS_VAL_LOWERCASE) || id == CSS_VAL_NONE)
valid_primitive = true;
break;
case CSS_PROP_FLOAT: // left | right | none | inherit + center for buggy CSS
if ( id == CSS_VAL_LEFT || id == CSS_VAL_RIGHT ||
id == CSS_VAL_NONE || id == CSS_VAL_CENTER)
valid_primitive = true;
break;
case CSS_PROP_CLEAR: // none | left | right | both | inherit
if ( id == CSS_VAL_NONE || id == CSS_VAL_LEFT ||
id == CSS_VAL_RIGHT|| id == CSS_VAL_BOTH)
valid_primitive = true;
break;
case CSS_PROP_TEXT_ALIGN:
// left | right | center | justify | khtml_left | khtml_right | khtml_center | <string> | inherit
if ( ( id >= CSS_VAL__KHTML_AUTO && id <= CSS_VAL__KHTML_CENTER ) ||
value->unit == CSSPrimitiveValue::CSS_STRING )
valid_primitive = true;
break;
case CSS_PROP_OUTLINE_STYLE: // <border-style> | auto | inherit
if (id == CSS_VAL_AUTO) {
valid_primitive = true;
break;
} // Fall through!
case CSS_PROP_BORDER_TOP_STYLE: //// <border-style> | inherit
case CSS_PROP_BORDER_RIGHT_STYLE: // Defined as: none | hidden | dotted | dashed |
case CSS_PROP_BORDER_BOTTOM_STYLE: // solid | double | groove | ridge | inset | outset
case CSS_PROP_BORDER_LEFT_STYLE: ////
if (id >= CSS_VAL_NONE && id <= CSS_VAL_DOUBLE)
valid_primitive = true;
break;
case CSS_PROP_FONT_WEIGHT: // normal | bold | bolder | lighter | 100 | 200 | 300 | 400 |
// 500 | 600 | 700 | 800 | 900 | inherit
if (id >= CSS_VAL_NORMAL && id <= CSS_VAL_900) {
// Allready correct id
valid_primitive = true;
} else if ( validUnit( value, FInteger|FNonNeg, false ) ) {
int weight = (int)value->fValue;
if ( (weight % 100) )
break;
weight /= 100;
if ( weight >= 1 && weight <= 9 ) {
id = CSS_VAL_100 + weight - 1;
valid_primitive = true;
}
}
break;
case CSS_PROP_BORDER_SPACING: {
const int properties[2] = { CSS_PROP__KHTML_BORDER_HORIZONTAL_SPACING,
CSS_PROP__KHTML_BORDER_VERTICAL_SPACING };
int num = valueList->size();
if (num == 1) {
if (!parseValue(properties[0], important)) return false;
CSSValue* value = parsedProperties[numParsedProperties-1]->value();
addProperty(properties[1], value, important);
return true;
}
else if (num == 2) {
if (!parseValue(properties[0], important)) return false;
if (!parseValue(properties[1], important)) return false;
return true;
}
return false;
}
case CSS_PROP__KHTML_BORDER_HORIZONTAL_SPACING:
case CSS_PROP__KHTML_BORDER_VERTICAL_SPACING:
valid_primitive = validUnit(value, FLength|FNonNeg, strict);
break;
case CSS_PROP_SCROLLBAR_FACE_COLOR: // IE5.5
case CSS_PROP_SCROLLBAR_SHADOW_COLOR: // IE5.5
case CSS_PROP_SCROLLBAR_HIGHLIGHT_COLOR: // IE5.5
case CSS_PROP_SCROLLBAR_3DLIGHT_COLOR: // IE5.5
case CSS_PROP_SCROLLBAR_DARKSHADOW_COLOR: // IE5.5
case CSS_PROP_SCROLLBAR_TRACK_COLOR: // IE5.5
case CSS_PROP_SCROLLBAR_ARROW_COLOR: // IE5.5
if ( strict )
break;
/* nobreak */
case CSS_PROP_OUTLINE_COLOR: // <color> | invert | inherit
// outline has "invert" as additional keyword.
if ( propId == CSS_PROP_OUTLINE_COLOR && id == CSS_VAL_INVERT ) {
valid_primitive = true;
break;
}
/* nobreak */
case CSS_PROP_BACKGROUND_COLOR: // <color> | inherit
case CSS_PROP_BORDER_TOP_COLOR: // <color> | inherit
case CSS_PROP_BORDER_RIGHT_COLOR: // <color> | inherit
case CSS_PROP_BORDER_BOTTOM_COLOR: // <color> | inherit
case CSS_PROP_BORDER_LEFT_COLOR: // <color> | inherit
case CSS_PROP_COLOR: // <color> | inherit
case CSS_PROP_TEXT_LINE_THROUGH_COLOR: // CSS3 text decoration colors
case CSS_PROP_TEXT_UNDERLINE_COLOR:
case CSS_PROP_TEXT_OVERLINE_COLOR:
if (id == CSS_VAL__KHTML_TEXT)
valid_primitive = true; // Always allow this, even when strict parsing is on,
// since we use this in our UA sheets.
else if ( id >= CSS_VAL_AQUA && id <= CSS_VAL_WINDOWTEXT || id == CSS_VAL_MENU ||
(id >= CSS_VAL_GREY && id < CSS_VAL__KHTML_TEXT && !strict) ) {
valid_primitive = true;
} else {
parsedValue = parseColor();
if ( parsedValue )
valueList->next();
}
break;
case CSS_PROP_CURSOR:
// [ auto | crosshair | default | pointer | progress | move | e-resize | ne-resize |
// nw-resize | n-resize | se-resize | sw-resize | s-resize | w-resize | text |
// wait | help ] ] | inherit
if (!strict && id == CSS_VAL_HAND) { // MSIE 5 compatibility :/
id = CSS_VAL_POINTER;
valid_primitive = true;
} else if (id >= CSS_VAL_AUTO && id <= CSS_VAL_HELP)
valid_primitive = true;
else if (value->unit == CSSPrimitiveValue::CSS_URI) {
String uri = parseURL(domString(value->string));
if (!uri.isEmpty()) {
parsedValue = new CSSImageValue(String(KURL(styleElement->baseURL().deprecatedString(), uri.deprecatedString()).url()), styleElement);
valueList->next();
}
}
break;
case CSS_PROP_BACKGROUND_ATTACHMENT:
case CSS_PROP_BACKGROUND_CLIP:
case CSS_PROP_BACKGROUND_IMAGE:
case CSS_PROP_BACKGROUND_ORIGIN:
case CSS_PROP_BACKGROUND_POSITION:
case CSS_PROP_BACKGROUND_POSITION_X:
case CSS_PROP_BACKGROUND_POSITION_Y:
case CSS_PROP_BACKGROUND_REPEAT: {
CSSValue *val1 = 0, *val2 = 0;
int propId1, propId2;
if (parseBackgroundProperty(propId, propId1, propId2, val1, val2)) {
addProperty(propId1, val1, important);
if (val2)
addProperty(propId2, val2, important);
return true;
}
return false;
}
case CSS_PROP_LIST_STYLE_IMAGE: // <uri> | none | inherit
if (id == CSS_VAL_NONE) {
parsedValue = new CSSImageValue();
valueList->next();
}
else if (value->unit == CSSPrimitiveValue::CSS_URI) {
// ### allow string in non strict mode?
String uri = parseURL( domString( value->string ) );
if (!uri.isEmpty()) {
parsedValue = new CSSImageValue(
String(KURL(styleElement->baseURL().deprecatedString(), uri.deprecatedString()).url()),
styleElement);
valueList->next();
}
}
break;
case CSS_PROP_OUTLINE_WIDTH: // <border-width> | inherit
case CSS_PROP_BORDER_TOP_WIDTH: //// <border-width> | inherit
case CSS_PROP_BORDER_RIGHT_WIDTH: // Which is defined as
case CSS_PROP_BORDER_BOTTOM_WIDTH: // thin | medium | thick | <length>
case CSS_PROP_BORDER_LEFT_WIDTH: ////
if (id == CSS_VAL_THIN || id == CSS_VAL_MEDIUM || id == CSS_VAL_THICK)
valid_primitive = true;
else
valid_primitive = ( validUnit( value, FLength, strict ) );
break;
case CSS_PROP_LETTER_SPACING: // normal | <length> | inherit
case CSS_PROP_WORD_SPACING: // normal | <length> | inherit
if ( id == CSS_VAL_NORMAL )
valid_primitive = true;
else
valid_primitive = validUnit( value, FLength, strict );
break;
case CSS_PROP_WORD_WRAP: // normal | break-word
if (id == CSS_VAL_NORMAL || id == CSS_VAL_BREAK_WORD)
valid_primitive = true;
break;
case CSS_PROP__KHTML_FONT_SIZE_DELTA: // <length>
valid_primitive = validUnit( value, FLength, strict );
break;
case CSS_PROP__KHTML_NBSP_MODE: // normal | space
if (id == CSS_VAL_NORMAL || id == CSS_VAL_SPACE)
valid_primitive = true;
break;
case CSS_PROP__KHTML_LINE_BREAK: // normal | after-white-space
if (id == CSS_VAL_NORMAL || id == CSS_VAL_AFTER_WHITE_SPACE)
valid_primitive = true;
break;
case CSS_PROP__KHTML_MATCH_NEAREST_MAIL_BLOCKQUOTE_COLOR: // normal | match
if (id == CSS_VAL_NORMAL || id == CSS_VAL_MATCH)
valid_primitive = true;
break;
case CSS_PROP_TEXT_INDENT: // <length> | <percentage> | inherit
case CSS_PROP_PADDING_TOP: //// <padding-width> | inherit
case CSS_PROP_PADDING_RIGHT: // Which is defined as
case CSS_PROP_PADDING_BOTTOM: // <length> | <percentage>
case CSS_PROP_PADDING_LEFT: ////
case CSS_PROP__KHTML_PADDING_START:
valid_primitive = ( !id && validUnit( value, FLength|FPercent, strict ) );
break;
case CSS_PROP_MAX_HEIGHT: // <length> | <percentage> | none | inherit
case CSS_PROP_MAX_WIDTH: // <length> | <percentage> | none | inherit
if (id == CSS_VAL_NONE || id == CSS_VAL_INTRINSIC || id == CSS_VAL_MIN_INTRINSIC) {
valid_primitive = true;
break;
}
/* nobreak */
case CSS_PROP_MIN_HEIGHT: // <length> | <percentage> | inherit
case CSS_PROP_MIN_WIDTH: // <length> | <percentage> | inherit
if (id == CSS_VAL_INTRINSIC || id == CSS_VAL_MIN_INTRINSIC)
valid_primitive = true;
else
valid_primitive = ( !id && validUnit( value, FLength|FPercent|FNonNeg, strict ) );
break;
case CSS_PROP_FONT_SIZE:
// <absolute-size> | <relative-size> | <length> | <percentage> | inherit
if (id >= CSS_VAL_XX_SMALL && id <= CSS_VAL_LARGER)
valid_primitive = true;
else
valid_primitive = ( validUnit( value, FLength|FPercent, strict ) );
break;
case CSS_PROP_FONT_STYLE: // normal | italic | oblique | inherit
if ( id == CSS_VAL_NORMAL || id == CSS_VAL_ITALIC || id == CSS_VAL_OBLIQUE)
valid_primitive = true;
break;
case CSS_PROP_FONT_VARIANT: // normal | small-caps | inherit
if ( id == CSS_VAL_NORMAL || id == CSS_VAL_SMALL_CAPS)
valid_primitive = true;
break;
case CSS_PROP_VERTICAL_ALIGN:
// baseline | sub | super | top | text-top | middle | bottom | text-bottom |
// <percentage> | <length> | inherit
if ( id >= CSS_VAL_BASELINE && id <= CSS_VAL__KHTML_BASELINE_MIDDLE )
valid_primitive = true;
else
valid_primitive = ( !id && validUnit( value, FLength|FPercent, strict ) );
break;
case CSS_PROP_HEIGHT: // <length> | <percentage> | auto | inherit
case CSS_PROP_WIDTH: // <length> | <percentage> | auto | inherit
if (id == CSS_VAL_AUTO || id == CSS_VAL_INTRINSIC || id == CSS_VAL_MIN_INTRINSIC)
valid_primitive = true;
else
// ### handle multilength case where we allow relative units
valid_primitive = ( !id && validUnit( value, FLength|FPercent|FNonNeg, strict ) );
break;
case CSS_PROP_BOTTOM: // <length> | <percentage> | auto | inherit
case CSS_PROP_LEFT: // <length> | <percentage> | auto | inherit
case CSS_PROP_RIGHT: // <length> | <percentage> | auto | inherit
case CSS_PROP_TOP: // <length> | <percentage> | auto | inherit
case CSS_PROP_MARGIN_TOP: //// <margin-width> | inherit
case CSS_PROP_MARGIN_RIGHT: // Which is defined as
case CSS_PROP_MARGIN_BOTTOM: // <length> | <percentage> | auto | inherit
case CSS_PROP_MARGIN_LEFT: ////
case CSS_PROP__KHTML_MARGIN_START:
if ( id == CSS_VAL_AUTO )
valid_primitive = true;
else
valid_primitive = ( !id && validUnit( value, FLength|FPercent, strict ) );
break;
case CSS_PROP_Z_INDEX: // auto | <integer> | inherit
// qDebug("parsing z-index: id=%d, fValue=%f", id, value->fValue );
if ( id == CSS_VAL_AUTO ) {
valid_primitive = true;
break;
}
/* nobreak */
case CSS_PROP_ORPHANS: // <integer> | inherit
case CSS_PROP_WIDOWS: // <integer> | inherit
// ### not supported later on
valid_primitive = ( !id && validUnit( value, FInteger, false ) );
break;
case CSS_PROP_LINE_HEIGHT: // normal | <number> | <length> | <percentage> | inherit
if ( id == CSS_VAL_NORMAL )
valid_primitive = true;
else
valid_primitive = ( !id && validUnit( value, FNumber|FLength|FPercent, strict ) );
break;
case CSS_PROP_FONT_FAMILY:
// [[ <family-name> | <generic-family> ],]* [<family-name> | <generic-family>] | inherit
{
parsedValue = parseFontFamily();
break;
}
case CSS_PROP_TEXT_DECORATION:
case CSS_PROP__KHTML_TEXT_DECORATIONS_IN_EFFECT:
// none | [ underline || overline || line-through || blink ] | inherit
if (id == CSS_VAL_NONE) {
valid_primitive = true;
} else {
CSSValueList *list = new CSSValueList;
bool is_valid = true;
while( is_valid && value ) {
switch ( value->id ) {
case CSS_VAL_BLINK:
break;
case CSS_VAL_UNDERLINE:
case CSS_VAL_OVERLINE:
case CSS_VAL_LINE_THROUGH:
list->append( new CSSPrimitiveValue( value->id ) );
break;
default:
is_valid = false;
}
value = valueList->next();
}
//kdDebug( 6080 ) << "got " << list->length() << "d decorations" << endl;
if(list->length() && is_valid) {
parsedValue = list;
valueList->next();
} else {
delete list;
}
}
break;
case CSS_PROP_TABLE_LAYOUT: // auto | fixed | inherit
if ( id == CSS_VAL_AUTO || id == CSS_VAL_FIXED )
valid_primitive = true;
break;
/* CSS3 properties */
case CSS_PROP__KHTML_APPEARANCE:
if ((id >= CSS_VAL_CHECKBOX && id <= CSS_VAL_TEXTFIELD) || id == CSS_VAL_NONE)
valid_primitive = true;
break;
case CSS_PROP__KHTML_BINDING:
#ifndef KHTML_NO_XBL
if (id == CSS_VAL_NONE)
valid_primitive = true;
else {
CSSValueList* values = new CSSValueList();
Value* val;
CSSValue* parsedValue = 0;
while ((val = valueList->current())) {
if (val->unit == CSSPrimitiveValue::CSS_URI) {
String value = parseURL(domString(val->string));
parsedValue = new CSSPrimitiveValue(
String(KURL(styleElement->baseURL().deprecatedString(), value.deprecatedString()).url()),
CSSPrimitiveValue::CSS_URI);
}
if (parsedValue)
values->append(parsedValue);
else
break;
valueList->next();
}
if ( values->length() ) {
addProperty( propId, values, important );
valueList->next();
return true;
}
delete values;
return false;
}
#endif
break;
case CSS_PROP_BORDER_IMAGE:
if (id == CSS_VAL_NONE)
valid_primitive = true;
else
return parseBorderImage(propId, important);
break;
case CSS_PROP_BORDER_TOP_RIGHT_RADIUS:
case CSS_PROP_BORDER_TOP_LEFT_RADIUS:
case CSS_PROP_BORDER_BOTTOM_LEFT_RADIUS:
case CSS_PROP_BORDER_BOTTOM_RIGHT_RADIUS:
case CSS_PROP_BORDER_RADIUS: {
int num = valueList->size();
if (num != 1 && num != 2)
return false;
valid_primitive = validUnit(value, FLength, strict);
if (!valid_primitive)
return false;
CSSPrimitiveValue* parsedValue1 = new CSSPrimitiveValue(value->fValue,
(CSSPrimitiveValue::UnitTypes)value->unit);
CSSPrimitiveValue* parsedValue2 = parsedValue1;
if (num == 2) {
value = valueList->next();
valid_primitive = validUnit(value, FLength, strict);
if (!valid_primitive) {
delete parsedValue1;
return false;
}
parsedValue2 = new CSSPrimitiveValue(value->fValue, (CSSPrimitiveValue::UnitTypes)value->unit);
}
Pair* pair = new Pair;
pair->setFirst(parsedValue1);
pair->setSecond(parsedValue2);
CSSPrimitiveValue* val = new CSSPrimitiveValue(pair);
addProperty(propId, val, important);
return true;
}
case CSS_PROP_OUTLINE_OFFSET:
valid_primitive = validUnit(value, FLength, strict);
break;
case CSS_PROP_TEXT_SHADOW: // CSS2 property, dropped in CSS2.1, back in CSS3, so treat as CSS3
if (id == CSS_VAL_NONE)
valid_primitive = true;
else
return parseShadow(propId, important);
break;
case CSS_PROP_OPACITY:
valid_primitive = validUnit(value, FNumber, strict);
break;
case CSS_PROP__KHTML_BOX_ALIGN:
if (id == CSS_VAL_STRETCH || id == CSS_VAL_START || id == CSS_VAL_END ||
id == CSS_VAL_CENTER || id == CSS_VAL_BASELINE)
valid_primitive = true;
break;
case CSS_PROP__KHTML_BOX_DIRECTION:
if (id == CSS_VAL_NORMAL || id == CSS_VAL_REVERSE)
valid_primitive = true;
break;
case CSS_PROP__KHTML_BOX_LINES:
if (id == CSS_VAL_SINGLE || id == CSS_VAL_MULTIPLE)
valid_primitive = true;
break;
case CSS_PROP__KHTML_BOX_ORIENT:
if (id == CSS_VAL_HORIZONTAL || id == CSS_VAL_VERTICAL ||
id == CSS_VAL_INLINE_AXIS || id == CSS_VAL_BLOCK_AXIS)
valid_primitive = true;
break;
case CSS_PROP__KHTML_BOX_PACK:
if (id == CSS_VAL_START || id == CSS_VAL_END ||
id == CSS_VAL_CENTER || id == CSS_VAL_JUSTIFY)
valid_primitive = true;
break;
case CSS_PROP__KHTML_BOX_FLEX:
valid_primitive = validUnit(value, FNumber, strict);
break;
case CSS_PROP__KHTML_BOX_FLEX_GROUP:
case CSS_PROP__KHTML_BOX_ORDINAL_GROUP:
valid_primitive = validUnit(value, FInteger|FNonNeg, true);
break;
case CSS_PROP_BOX_SIZING: {
// We don't preface this with -khtml, since MacIE defined this property without the prefix.
// Thus the damage has been done, and it's known that this property's definition isn't going
// to fluctuate.
if (id == CSS_VAL_BORDER_BOX || id == CSS_VAL_CONTENT_BOX)
valid_primitive = true;
break;
}
case CSS_PROP__KHTML_MARQUEE: {
const int properties[5] = { CSS_PROP__KHTML_MARQUEE_DIRECTION, CSS_PROP__KHTML_MARQUEE_INCREMENT,
CSS_PROP__KHTML_MARQUEE_REPETITION,
CSS_PROP__KHTML_MARQUEE_STYLE, CSS_PROP__KHTML_MARQUEE_SPEED };
return parseShorthand(propId, properties, 5, important);
}
case CSS_PROP__KHTML_MARQUEE_DIRECTION:
if (id == CSS_VAL_FORWARDS || id == CSS_VAL_BACKWARDS || id == CSS_VAL_AHEAD ||
id == CSS_VAL_REVERSE || id == CSS_VAL_LEFT || id == CSS_VAL_RIGHT || id == CSS_VAL_DOWN ||
id == CSS_VAL_UP || id == CSS_VAL_AUTO)
valid_primitive = true;
break;
case CSS_PROP__KHTML_MARQUEE_INCREMENT:
if (id == CSS_VAL_SMALL || id == CSS_VAL_LARGE || id == CSS_VAL_MEDIUM)
valid_primitive = true;
else
valid_primitive = validUnit(value, FLength|FPercent, strict);
break;
case CSS_PROP__KHTML_MARQUEE_STYLE:
if (id == CSS_VAL_NONE || id == CSS_VAL_SLIDE || id == CSS_VAL_SCROLL || id == CSS_VAL_ALTERNATE ||
id == CSS_VAL_UNFURL)
valid_primitive = true;
break;
case CSS_PROP__KHTML_MARQUEE_REPETITION:
if (id == CSS_VAL_INFINITE)
valid_primitive = true;
else
valid_primitive = validUnit(value, FInteger|FNonNeg, strict);
break;
case CSS_PROP__KHTML_MARQUEE_SPEED:
if (id == CSS_VAL_NORMAL || id == CSS_VAL_SLOW || id == CSS_VAL_FAST)
valid_primitive = true;
else
valid_primitive = validUnit(value, FTime|FInteger|FNonNeg, strict);
break;
case CSS_PROP__KHTML_USER_DRAG: // auto | none | element
if (id == CSS_VAL_AUTO || id == CSS_VAL_NONE || id == CSS_VAL_ELEMENT)
valid_primitive = true;
break;
case CSS_PROP__KHTML_USER_MODIFY: // read-only | read-write
if (id == CSS_VAL_READ_ONLY || id == CSS_VAL_READ_WRITE)
valid_primitive = true;
break;
case CSS_PROP__KHTML_USER_SELECT: // auto | none | text
if (id == CSS_VAL_AUTO || id == CSS_VAL_NONE || id == CSS_VAL_TEXT || id == CSS_VAL_IGNORE)
valid_primitive = true;
break;
case CSS_PROP_TEXT_OVERFLOW: // clip | ellipsis
if (id == CSS_VAL_CLIP || id == CSS_VAL_ELLIPSIS)
valid_primitive = true;
break;
case CSS_PROP__KHTML_MARGIN_COLLAPSE: {
const int properties[2] = { CSS_PROP__KHTML_MARGIN_TOP_COLLAPSE,
CSS_PROP__KHTML_MARGIN_BOTTOM_COLLAPSE };
int num = valueList->size();
if (num == 1) {
if (!parseValue(properties[0], important)) return false;
CSSValue* value = parsedProperties[numParsedProperties-1]->value();
addProperty(properties[1], value, important);
return true;
}
else if (num == 2) {
if (!parseValue(properties[0], important)) return false;
if (!parseValue(properties[1], important)) return false;
return true;
}
return false;
}
case CSS_PROP__KHTML_MARGIN_TOP_COLLAPSE:
case CSS_PROP__KHTML_MARGIN_BOTTOM_COLLAPSE:
if (id == CSS_VAL_COLLAPSE || id == CSS_VAL_SEPARATE || id == CSS_VAL_DISCARD)
valid_primitive = true;
break;
case CSS_PROP_TEXT_LINE_THROUGH_MODE:
case CSS_PROP_TEXT_OVERLINE_MODE:
case CSS_PROP_TEXT_UNDERLINE_MODE:
if (id == CSS_VAL_CONTINUOUS || id == CSS_VAL_SKIP_WHITE_SPACE)
valid_primitive = true;
break;
case CSS_PROP_TEXT_LINE_THROUGH_STYLE:
case CSS_PROP_TEXT_OVERLINE_STYLE:
case CSS_PROP_TEXT_UNDERLINE_STYLE:
if (id == CSS_VAL_NONE || id == CSS_VAL_SOLID || id == CSS_VAL_DOUBLE ||
id == CSS_VAL_DASHED || id == CSS_VAL_DOT_DASH || id == CSS_VAL_DOT_DOT_DASH ||
id == CSS_VAL_WAVE)
valid_primitive = true;
break;
case CSS_PROP_TEXT_LINE_THROUGH_WIDTH:
case CSS_PROP_TEXT_OVERLINE_WIDTH:
case CSS_PROP_TEXT_UNDERLINE_WIDTH:
if (id == CSS_VAL_AUTO || id == CSS_VAL_NORMAL || id == CSS_VAL_THIN ||
id == CSS_VAL_MEDIUM || id == CSS_VAL_THICK)
valid_primitive = true;
else
valid_primitive = !id && validUnit(value, FNumber|FLength|FPercent, strict);
break;
// End of CSS3 properties
// Apple specific properties. These will never be standardized and are purely to
// support custom WebKit-based Apple applications.
case CSS_PROP__KHTML_LINE_CLAMP:
valid_primitive = (!id && validUnit(value, FPercent, false));
break;
case CSS_PROP__KHTML_TEXT_SIZE_ADJUST:
if (id == CSS_VAL_AUTO || id == CSS_VAL_NONE)
valid_primitive = true;
break;
case CSS_PROP__KHTML_RTL_ORDERING:
if (id == CSS_VAL_LOGICAL || id == CSS_VAL_VISUAL)
valid_primitive = true;
break;
/* shorthand properties */
case CSS_PROP_BACKGROUND:
// ['background-color' || 'background-image' ||'background-repeat' ||
// 'background-attachment' || 'background-position'] | inherit
return parseBackgroundShorthand(important);
case CSS_PROP_BORDER:
// [ 'border-width' || 'border-style' || <color> ] | inherit
{
const int properties[3] = { CSS_PROP_BORDER_WIDTH, CSS_PROP_BORDER_STYLE,
CSS_PROP_BORDER_COLOR };
return parseShorthand(propId, properties, 3, important);
}
case CSS_PROP_BORDER_TOP:
// [ 'border-top-width' || 'border-style' || <color> ] | inherit
{
const int properties[3] = { CSS_PROP_BORDER_TOP_WIDTH, CSS_PROP_BORDER_TOP_STYLE,
CSS_PROP_BORDER_TOP_COLOR};
return parseShorthand(propId, properties, 3, important);
}
case CSS_PROP_BORDER_RIGHT:
// [ 'border-right-width' || 'border-style' || <color> ] | inherit
{
const int properties[3] = { CSS_PROP_BORDER_RIGHT_WIDTH, CSS_PROP_BORDER_RIGHT_STYLE,
CSS_PROP_BORDER_RIGHT_COLOR };
return parseShorthand(propId, properties, 3, important);
}
case CSS_PROP_BORDER_BOTTOM:
// [ 'border-bottom-width' || 'border-style' || <color> ] | inherit
{
const int properties[3] = { CSS_PROP_BORDER_BOTTOM_WIDTH, CSS_PROP_BORDER_BOTTOM_STYLE,
CSS_PROP_BORDER_BOTTOM_COLOR };
return parseShorthand(propId, properties, 3, important);
}
case CSS_PROP_BORDER_LEFT:
// [ 'border-left-width' || 'border-style' || <color> ] | inherit
{
const int properties[3] = { CSS_PROP_BORDER_LEFT_WIDTH, CSS_PROP_BORDER_LEFT_STYLE,
CSS_PROP_BORDER_LEFT_COLOR };
return parseShorthand(propId, properties, 3, important);
}
case CSS_PROP_OUTLINE:
// [ 'outline-color' || 'outline-style' || 'outline-width' ] | inherit
{
const int properties[3] = { CSS_PROP_OUTLINE_WIDTH, CSS_PROP_OUTLINE_STYLE,
CSS_PROP_OUTLINE_COLOR };
return parseShorthand(propId, properties, 3, important);
}
case CSS_PROP_BORDER_COLOR:
// <color>{1,4} | inherit
{
const int properties[4] = { CSS_PROP_BORDER_TOP_COLOR, CSS_PROP_BORDER_RIGHT_COLOR,
CSS_PROP_BORDER_BOTTOM_COLOR, CSS_PROP_BORDER_LEFT_COLOR };
return parse4Values(propId, properties, important);
}
case CSS_PROP_BORDER_WIDTH:
// <border-width>{1,4} | inherit
{
const int properties[4] = { CSS_PROP_BORDER_TOP_WIDTH, CSS_PROP_BORDER_RIGHT_WIDTH,
CSS_PROP_BORDER_BOTTOM_WIDTH, CSS_PROP_BORDER_LEFT_WIDTH };
return parse4Values(propId, properties, important);
}
case CSS_PROP_BORDER_STYLE:
// <border-style>{1,4} | inherit
{
const int properties[4] = { CSS_PROP_BORDER_TOP_STYLE, CSS_PROP_BORDER_RIGHT_STYLE,
CSS_PROP_BORDER_BOTTOM_STYLE, CSS_PROP_BORDER_LEFT_STYLE };
return parse4Values(propId, properties, important);
}
case CSS_PROP_MARGIN:
// <margin-width>{1,4} | inherit
{
const int properties[4] = { CSS_PROP_MARGIN_TOP, CSS_PROP_MARGIN_RIGHT,
CSS_PROP_MARGIN_BOTTOM, CSS_PROP_MARGIN_LEFT };
return parse4Values(propId, properties, important);
}
case CSS_PROP_PADDING:
// <padding-width>{1,4} | inherit
{
const int properties[4] = { CSS_PROP_PADDING_TOP, CSS_PROP_PADDING_RIGHT,
CSS_PROP_PADDING_BOTTOM, CSS_PROP_PADDING_LEFT };
return parse4Values(propId, properties, important);
}
case CSS_PROP_FONT:
// [ [ 'font-style' || 'font-variant' || 'font-weight' ]? 'font-size' [ / 'line-height' ]?
// 'font-family' ] | caption | icon | menu | message-box | small-caption | status-bar | inherit
if (id >= CSS_VAL_CAPTION && id <= CSS_VAL_STATUS_BAR)
valid_primitive = true;
else
return parseFont(important);
case CSS_PROP_LIST_STYLE:
{
const int properties[3] = { CSS_PROP_LIST_STYLE_TYPE, CSS_PROP_LIST_STYLE_POSITION,
CSS_PROP_LIST_STYLE_IMAGE };
return parseShorthand(propId, properties, 3, important);
}
default:
#if SVG_SUPPORT
if (parseSVGValue(propId, important))
return true;
#endif
break;
}
if ( valid_primitive ) {
if ( id != 0 ) {
// qDebug(" new value: id=%d", id );
parsedValue = new CSSPrimitiveValue( id );
} else if ( value->unit == CSSPrimitiveValue::CSS_STRING )
parsedValue = new CSSPrimitiveValue( domString( value->string ),
(CSSPrimitiveValue::UnitTypes) value->unit );
else if ( value->unit >= CSSPrimitiveValue::CSS_NUMBER &&
value->unit <= CSSPrimitiveValue::CSS_KHZ ) {
// qDebug(" new value: value=%.2f, unit=%d", value->fValue, value->unit );
parsedValue = new CSSPrimitiveValue( value->fValue,
(CSSPrimitiveValue::UnitTypes) value->unit );
} else if ( value->unit >= Value::Q_EMS ) {
// qDebug(" new quirks value: value=%.2f, unit=%d", value->fValue, value->unit );
parsedValue = new CSSQuirkPrimitiveValue( value->fValue, CSSPrimitiveValue::CSS_EMS );
}
valueList->next();
}
if ( parsedValue ) {
addProperty( propId, parsedValue, important );
return true;
}
return false;
}
void CSSParser::addBackgroundValue(CSSValue*& lval, CSSValue* rval)
{
if (lval) {
if (lval->isValueList())
static_cast<CSSValueList*>(lval)->append(rval);
else {
CSSValue* oldVal = lval;
CSSValueList* list = new CSSValueList();
lval = list;
list->append(oldVal);
list->append(rval);
}
}
else
lval = rval;
}
bool CSSParser::parseBackgroundShorthand(bool important)
{
// Position must come before color in this array because a plain old "0" is a legal color
// in quirks mode but it's usually the X coordinate of a position.
const int numProperties = 7;
const int properties[numProperties] = { CSS_PROP_BACKGROUND_IMAGE, CSS_PROP_BACKGROUND_REPEAT,
CSS_PROP_BACKGROUND_ATTACHMENT, CSS_PROP_BACKGROUND_POSITION, CSS_PROP_BACKGROUND_CLIP,
CSS_PROP_BACKGROUND_ORIGIN, CSS_PROP_BACKGROUND_COLOR };
enterShorthand(CSS_PROP_BACKGROUND);
bool parsedProperty[numProperties] = { false }; // compiler will repeat false as necessary
CSSValue* values[numProperties] = { 0 }; // compiler will repeat 0 as necessary
CSSValue* positionYValue = 0;
int i;
while (valueList->current()) {
Value* val = valueList->current();
if (val->unit == Value::Operator && val->iValue == ',') {
// We hit the end. Fill in all remaining values with the initial value.
valueList->next();
for (i = 0; i < numProperties; ++i) {
if (properties[i] == CSS_PROP_BACKGROUND_COLOR && parsedProperty[i])
// Color is not allowed except as the last item in a list. Reject the entire
// property.
goto fail;
if (!parsedProperty[i] && properties[i] != CSS_PROP_BACKGROUND_COLOR) {
addBackgroundValue(values[i], new CSSInitialValue());
if (properties[i] == CSS_PROP_BACKGROUND_POSITION)
addBackgroundValue(positionYValue, new CSSInitialValue());
}
parsedProperty[i] = false;
}
if (!valueList->current())
break;
}
bool found = false;
for (i = 0; !found && i < numProperties; ++i) {
if (!parsedProperty[i]) {
CSSValue *val1 = 0, *val2 = 0;
int propId1, propId2;
if (parseBackgroundProperty(properties[i], propId1, propId2, val1, val2)) {
parsedProperty[i] = found = true;
addBackgroundValue(values[i], val1);
if (properties[i] == CSS_PROP_BACKGROUND_POSITION)
addBackgroundValue(positionYValue, val2);
}
}
}
// if we didn't find at least one match, this is an
// invalid shorthand and we have to ignore it
if (!found)
goto fail;
}
// Fill in any remaining properties with the initial value.
for (i = 0; i < numProperties; ++i) {
if (!parsedProperty[i]) {
addBackgroundValue(values[i], new CSSInitialValue());
if (properties[i] == CSS_PROP_BACKGROUND_POSITION)
addBackgroundValue(positionYValue, new CSSInitialValue());
}
}
// Now add all of the properties we found.
for (i = 0; i < numProperties; i++) {
if (properties[i] == CSS_PROP_BACKGROUND_POSITION) {
addProperty(CSS_PROP_BACKGROUND_POSITION_X, values[i], important);
addProperty(CSS_PROP_BACKGROUND_POSITION_Y, positionYValue, important);
}
else
addProperty(properties[i], values[i], important);
}
exitShorthand();
return true;
fail:
exitShorthand();
for (int k = 0; k < numProperties; k++)
delete values[k];
delete positionYValue;
return false;
}
bool CSSParser::parseShorthand(int propId, const int *properties, int numProperties, bool important)
{
// We try to match as many properties as possible
// We set up an array of booleans to mark which property has been found,
// and we try to search for properties until it makes no longer any sense.
enterShorthand(propId);
bool found = false;
bool fnd[6]; // Trust me ;)
for (int i = 0; i < numProperties; i++)
fnd[i] = false;
while (valueList->current()) {
found = false;
for (int propIndex = 0; !found && propIndex < numProperties; ++propIndex) {
if (!fnd[propIndex]) {
if (parseValue( properties[propIndex], important))
fnd[propIndex] = found = true;
}
}
// if we didn't find at least one match, this is an
// invalid shorthand and we have to ignore it
if (!found) {
exitShorthand();
return false;
}
}
// Fill in any remaining properties with the initial value.
m_implicitShorthand = true;
for (int i = 0; i < numProperties; ++i) {
if (!fnd[i])
addProperty(properties[i], new CSSInitialValue(), important);
}
m_implicitShorthand = false;
exitShorthand();
return true;
}
bool CSSParser::parse4Values(int propId, const int *properties, bool important)
{
/* From the CSS 2 specs, 8.3
* If there is only one value, it applies to all sides. If there are two values, the top and
* bottom margins are set to the first value and the right and left margins are set to the second.
* If there are three values, the top is set to the first value, the left and right are set to the
* second, and the bottom is set to the third. If there are four values, they apply to the top,
* right, bottom, and left, respectively.
*/
int num = inShorthand() ? 1 : valueList->size();
enterShorthand(propId);
// the order is top, right, bottom, left
switch (num) {
case 1: {
if (!parseValue(properties[0], important)) {
exitShorthand();
return false;
}
CSSValue *value = parsedProperties[numParsedProperties-1]->value();
m_implicitShorthand = true;
addProperty(properties[1], value, important);
addProperty(properties[2], value, important);
addProperty(properties[3], value, important);
m_implicitShorthand = false;
break;
}
case 2: {
if (!parseValue(properties[0], important) || !parseValue(properties[1], important)) {
exitShorthand();
return false;
}
CSSValue *value = parsedProperties[numParsedProperties-2]->value();
m_implicitShorthand = true;
addProperty(properties[2], value, important);
value = parsedProperties[numParsedProperties-2]->value();
addProperty(properties[3], value, important);
m_implicitShorthand = false;
break;
}
case 3: {
if (!parseValue(properties[0], important) || !parseValue(properties[1], important) ||
!parseValue(properties[2], important)) {
exitShorthand();
return false;
}
CSSValue *value = parsedProperties[numParsedProperties-2]->value();
m_implicitShorthand = true;
addProperty(properties[3], value, important);
m_implicitShorthand = false;
break;
}
case 4: {
if (!parseValue(properties[0], important) || !parseValue(properties[1], important) ||
!parseValue(properties[2], important) || !parseValue(properties[3], important)) {
exitShorthand();
return false;
}
break;
}
default: {
exitShorthand();
return false;
}
}
exitShorthand();
return true;
}
// [ <string> | <uri> | <counter> | attr(X) | open-quote | close-quote | no-open-quote | no-close-quote ]+ | inherit
// in CSS 2.1 this got somewhat reduced:
// [ <string> | attr(X) | open-quote | close-quote | no-open-quote | no-close-quote ]+ | inherit
bool CSSParser::parseContent(int propId, bool important)
{
CSSValueList* values = new CSSValueList;
while (Value* val = valueList->current()) {
CSSValue* parsedValue = 0;
if (val->unit == CSSPrimitiveValue::CSS_URI) {
// url
String value = parseURL(domString(val->string));
parsedValue = new CSSImageValue(
String(KURL(styleElement->baseURL().deprecatedString(), value.deprecatedString()).url()), styleElement);
} else if (val->unit == Value::Function) {
// attr(X)
ValueList *args = val->function->args;
DeprecatedString fname = qString(val->function->name).lower();
if (fname != "attr(" || !args)
return false;
if (args->size() != 1)
return false;
Value *a = args->current();
String attrName = domString(a->string);
if (document()->isHTMLDocument())
attrName = attrName.lower();
parsedValue = new CSSPrimitiveValue(attrName, CSSPrimitiveValue::CSS_ATTR);
} else if (val->unit == CSSPrimitiveValue::CSS_IDENT) {
// open-quote
// close-quote
// no-open-quote
// no-close-quote
// FIXME: These are not yet implemented (http://bugzilla.opendarwin.org/show_bug.cgi?id=6503).
} else if (val->unit == CSSPrimitiveValue::CSS_STRING) {
parsedValue = new CSSPrimitiveValue(domString(val->string), CSSPrimitiveValue::CSS_STRING);
}
if (!parsedValue)
break;
values->append(parsedValue);
valueList->next();
}
if (values->length()) {
addProperty(propId, values, important);
valueList->next();
return true;
}
delete values;
return false;
}
CSSValue* CSSParser::parseBackgroundColor()
{
int id = valueList->current()->id;
if (id == CSS_VAL__KHTML_TEXT || (id >= CSS_VAL_AQUA && id <= CSS_VAL_WINDOWTEXT) || id == CSS_VAL_MENU ||
(id >= CSS_VAL_GREY && id < CSS_VAL__KHTML_TEXT && !strict))
return new CSSPrimitiveValue(id);
return parseColor();
}
CSSValue* CSSParser::parseBackgroundImage()
{
if (valueList->current()->id == CSS_VAL_NONE)
return new CSSImageValue();
if (valueList->current()->unit == CSSPrimitiveValue::CSS_URI) {
String uri = parseURL(domString(valueList->current()->string));
if (!uri.isEmpty())
return new CSSImageValue(String(KURL(styleElement->baseURL().deprecatedString(), uri.deprecatedString()).url()),
styleElement);
}
return 0;
}
CSSValue* CSSParser::parseBackgroundPositionXY(bool& xFound, bool& yFound)
{
int id = valueList->current()->id;
if (id == CSS_VAL_LEFT || id == CSS_VAL_TOP || id == CSS_VAL_RIGHT || id == CSS_VAL_BOTTOM || id == CSS_VAL_CENTER) {
int percent = 0;
if (id == CSS_VAL_LEFT || id == CSS_VAL_RIGHT) {
if (xFound)
return 0;
xFound = true;
if (id == CSS_VAL_RIGHT)
percent = 100;
}
else if (id == CSS_VAL_TOP || id == CSS_VAL_BOTTOM) {
if (yFound)
return 0;
yFound = true;
if (id == CSS_VAL_BOTTOM)
percent = 100;
}
else if (id == CSS_VAL_CENTER)
// Center is ambiguous, so we're not sure which position we've found yet, an x or a y.
percent = 50;
return new CSSPrimitiveValue(percent, CSSPrimitiveValue::CSS_PERCENTAGE);
}
if (validUnit(valueList->current(), FPercent|FLength, strict))
return new CSSPrimitiveValue(valueList->current()->fValue,
(CSSPrimitiveValue::UnitTypes)valueList->current()->unit);
return 0;
}
void CSSParser::parseBackgroundPosition(CSSValue*& value1, CSSValue*& value2)
{
value1 = value2 = 0;
Value* value = valueList->current();
// Parse the first value. We're just making sure that it is one of the valid keywords or a percentage/length.
bool value1IsX = false, value1IsY = false;
value1 = parseBackgroundPositionXY(value1IsX, value1IsY);
if (!value1)
return;
// It only takes one value for background-position to be correctly parsed if it was specified in a shorthand (since we
// can assume that any other values belong to the rest of the shorthand). If we're not parsing a shorthand, though, the
// value was explicitly specified for our property.
value = valueList->next();
// First check for the comma. If so, we are finished parsing this value or value pair.
if (value && value->unit == Value::Operator && value->iValue == ',')
value = 0;
bool value2IsX = false, value2IsY = false;
if (value) {
value2 = parseBackgroundPositionXY(value2IsX, value2IsY);
if (value2)
valueList->next();
else {
if (!inShorthand()) {
delete value1;
value1 = 0;
return;
}
}
}
if (!value2)
// Only one value was specified. If that value was not a keyword, then it sets the x position, and the y position
// is simply 50%. This is our default.
// For keywords, the keyword was either an x-keyword (left/right), a y-keyword (top/bottom), or an ambiguous keyword (center).
// For left/right/center, the default of 50% in the y is still correct.
value2 = new CSSPrimitiveValue(50, CSSPrimitiveValue::CSS_PERCENTAGE);
if (value1IsY || value2IsX) {
// Swap our two values.
CSSValue* val = value2;
value2 = value1;
value1 = val;
}
}
bool CSSParser::parseBackgroundProperty(int propId, int& propId1, int& propId2,
CSSValue*& retValue1, CSSValue*& retValue2)
{
CSSValueList *values = 0, *values2 = 0;
Value* val;
CSSValue *value = 0, *value2 = 0;
bool allowComma = false;
retValue1 = retValue2 = 0;
propId1 = propId;
propId2 = propId;
if (propId == CSS_PROP_BACKGROUND_POSITION) {
propId1 = CSS_PROP_BACKGROUND_POSITION_X;
propId2 = CSS_PROP_BACKGROUND_POSITION_Y;
}
while ((val = valueList->current())) {
CSSValue *currValue = 0, *currValue2 = 0;
if (allowComma) {
if (val->unit != Value::Operator || val->iValue != ',')
goto failed;
valueList->next();
allowComma = false;
}
else {
switch (propId) {
case CSS_PROP_BACKGROUND_ATTACHMENT:
if (val->id == CSS_VAL_SCROLL || val->id == CSS_VAL_FIXED) {
currValue = new CSSPrimitiveValue(val->id);
valueList->next();
}
break;
case CSS_PROP_BACKGROUND_COLOR:
currValue = parseBackgroundColor();
if (currValue)
valueList->next();
break;
case CSS_PROP_BACKGROUND_IMAGE:
currValue = parseBackgroundImage();
if (currValue)
valueList->next();
break;
case CSS_PROP_BACKGROUND_CLIP:
case CSS_PROP_BACKGROUND_ORIGIN:
if (val->id == CSS_VAL_BORDER || val->id == CSS_VAL_PADDING || val->id == CSS_VAL_CONTENT) {
currValue = new CSSPrimitiveValue(val->id);
valueList->next();
}
break;
case CSS_PROP_BACKGROUND_POSITION:
parseBackgroundPosition(currValue, currValue2);
// unlike the other functions, parseBackgroundPosition advances the valueList pointer
break;
case CSS_PROP_BACKGROUND_POSITION_X: {
bool xFound = false, yFound = true;
currValue = parseBackgroundPositionXY(xFound, yFound);
if (currValue)
valueList->next();
break;
}
case CSS_PROP_BACKGROUND_POSITION_Y: {
bool xFound = true, yFound = false;
currValue = parseBackgroundPositionXY(xFound, yFound);
if (currValue)
valueList->next();
break;
}
case CSS_PROP_BACKGROUND_REPEAT:
if (val->id >= CSS_VAL_REPEAT && val->id <= CSS_VAL_NO_REPEAT) {
currValue = new CSSPrimitiveValue(val->id);
valueList->next();
}
break;
}
if (!currValue)
goto failed;
if (value && !values) {
values = new CSSValueList();
values->append(value);
value = 0;
}
if (value2 && !values2) {
values2 = new CSSValueList();
values2->append(value2);
value2 = 0;
}
if (values)
values->append(currValue);
else
value = currValue;
if (currValue2) {
if (values2)
values2->append(currValue2);
else
value2 = currValue2;
}
allowComma = true;
}
// When parsing the 'background' shorthand property, we let it handle building up the lists for all
// properties.
if (inShorthand())
break;
}
if (values && values->length()) {
retValue1 = values;
if (values2 && values2->length())
retValue2 = values2;
return true;
}
if (value) {
retValue1 = value;
retValue2 = value2;
return true;
}
failed:
delete values; delete values2;
delete value; delete value2;
return false;
}
#if __APPLE__
#define DASHBOARD_REGION_NUM_PARAMETERS 6
#define DASHBOARD_REGION_SHORT_NUM_PARAMETERS 2
static Value *skipCommaInDashboardRegion (ValueList *args)
{
if ( args->size() == (DASHBOARD_REGION_NUM_PARAMETERS*2-1) ||
args->size() == (DASHBOARD_REGION_SHORT_NUM_PARAMETERS*2-1)) {
Value *current = args->current();
if (current->unit == Value::Operator && current->iValue == ',' )
return args->next();
}
return args->current();
}
bool CSSParser::parseDashboardRegions( int propId, bool important )
{
bool valid = true;
Value *value = valueList->current();
if (value->id == CSS_VAL_NONE) {
addProperty( propId, new CSSPrimitiveValue( value->id ), important );
return valid;
}
RefPtr<DashboardRegion> firstRegion = new DashboardRegion;
DashboardRegion* region = 0;
while (value) {
if (region == 0) {
region = firstRegion.get();
} else {
RefPtr<DashboardRegion> nextRegion = new DashboardRegion();
region->m_next = nextRegion;
region = nextRegion.get();
}
if ( value->unit != Value::Function) {
valid = false;
break;
}
// Commas count as values, so allow:
// dashboard-region(label, type, t, r, b, l) or dashboard-region(label type t r b l)
// dashboard-region(label, type, t, r, b, l) or dashboard-region(label type t r b l)
// also allow
// dashboard-region(label, type) or dashboard-region(label type)
// dashboard-region(label, type) or dashboard-region(label type)
ValueList* args = value->function->args;
int numArgs = value->function->args->size();
if ((numArgs != DASHBOARD_REGION_NUM_PARAMETERS && numArgs != (DASHBOARD_REGION_NUM_PARAMETERS*2-1)) &&
(numArgs != DASHBOARD_REGION_SHORT_NUM_PARAMETERS && numArgs != (DASHBOARD_REGION_SHORT_NUM_PARAMETERS*2-1))){
valid = false;
break;
}
DeprecatedString fname = qString( value->function->name ).lower();
if (fname != "dashboard-region(" ) {
valid = false;
break;
}
// First arg is a label.
Value* arg = args->current();
if (arg->unit != CSSPrimitiveValue::CSS_IDENT) {
valid = false;
break;
}
region->m_label = qString(arg->string);
// Second arg is a type.
arg = args->next();
arg = skipCommaInDashboardRegion (args);
if (arg->unit != CSSPrimitiveValue::CSS_IDENT) {
valid = false;
break;
}
DeprecatedString geometryType = qString(arg->string).lower();
if (geometryType == "circle")
region->m_isCircle = true;
else if (geometryType == "rectangle")
region->m_isRectangle = true;
else {
valid = false;
break;
}
region->m_geometryType = qString(arg->string);
if (numArgs == DASHBOARD_REGION_SHORT_NUM_PARAMETERS || numArgs == (DASHBOARD_REGION_SHORT_NUM_PARAMETERS*2-1)) {
CSSPrimitiveValue *amount = arg->id == CSS_VAL_AUTO ?
new CSSPrimitiveValue(CSS_VAL_AUTO) :
new CSSPrimitiveValue((double)0, (CSSPrimitiveValue::UnitTypes) arg->unit );
region->setTop( amount );
region->setRight( amount );
region->setBottom( amount );
region->setLeft( amount );
}
else {
// Next four arguments must be offset numbers
int i;
for (i = 0; i < 4; i++) {
arg = args->next();
arg = skipCommaInDashboardRegion (args);
valid = arg->id == CSS_VAL_AUTO || validUnit( arg, FLength, strict );
if ( !valid )
break;
CSSPrimitiveValue *amount = arg->id == CSS_VAL_AUTO ?
new CSSPrimitiveValue(CSS_VAL_AUTO) :
new CSSPrimitiveValue(arg->fValue, (CSSPrimitiveValue::UnitTypes) arg->unit );
if ( i == 0 )
region->setTop( amount );
else if ( i == 1 )
region->setRight( amount );
else if ( i == 2 )
region->setBottom( amount );
else
region->setLeft( amount );
}
}
value = valueList->next();
}
if (valid)
addProperty(propId, new CSSPrimitiveValue(firstRegion.release()), important);
return valid;
}
#endif
bool CSSParser::parseShape( int propId, bool important )
{
Value *value = valueList->current();
ValueList *args = value->function->args;
DeprecatedString fname = qString( value->function->name ).lower();
if ( fname != "rect(" || !args )
return false;
// rect( t, r, b, l ) || rect( t r b l )
if ( args->size() != 4 && args->size() != 7 )
return false;
RectImpl *rect = new RectImpl();
bool valid = true;
int i = 0;
Value *a = args->current();
while ( a ) {
valid = a->id == CSS_VAL_AUTO || validUnit( a, FLength, strict );
if ( !valid )
break;
CSSPrimitiveValue *length = a->id == CSS_VAL_AUTO ?
new CSSPrimitiveValue(CSS_VAL_AUTO) :
new CSSPrimitiveValue( a->fValue, (CSSPrimitiveValue::UnitTypes) a->unit );
if ( i == 0 )
rect->setTop( length );
else if ( i == 1 )
rect->setRight( length );
else if ( i == 2 )
rect->setBottom( length );
else
rect->setLeft( length );
a = args->next();
if ( a && args->size() == 7 ) {
if ( a->unit == Value::Operator && a->iValue == ',' ) {
a = args->next();
} else {
valid = false;
break;
}
}
i++;
}
if ( valid ) {
addProperty( propId, new CSSPrimitiveValue( rect ), important );
valueList->next();
return true;
}
delete rect;
return false;
}
// [ 'font-style' || 'font-variant' || 'font-weight' ]? 'font-size' [ / 'line-height' ]? 'font-family'
bool CSSParser::parseFont( bool important )
{
bool valid = true;
Value *value = valueList->current();
FontValue *font = new FontValue;
// optional font-style, font-variant and font-weight
while ( value ) {
int id = value->id;
if ( id ) {
if ( id == CSS_VAL_NORMAL ) {
// do nothing, it's the inital value for all three
}
/*
else if ( id == CSS_VAL_INHERIT ) {
// set all non set ones to inherit
// This is not that simple as the inherit could also apply to the following font-size.
// very ahrd to tell without looking ahead.
inherit = true;
} */
else if ( id == CSS_VAL_ITALIC || id == CSS_VAL_OBLIQUE ) {
if ( font->style )
goto invalid;
font->style = new CSSPrimitiveValue( id );
} else if ( id == CSS_VAL_SMALL_CAPS ) {
if ( font->variant )
goto invalid;
font->variant = new CSSPrimitiveValue( id );
} else if ( id >= CSS_VAL_BOLD && id <= CSS_VAL_LIGHTER ) {
if ( font->weight )
goto invalid;
font->weight = new CSSPrimitiveValue( id );
} else {
valid = false;
}
} else if ( !font->weight && validUnit( value, FInteger|FNonNeg, true ) ) {
int weight = (int)value->fValue;
int val = 0;
if ( weight == 100 )
val = CSS_VAL_100;
else if ( weight == 200 )
val = CSS_VAL_200;
else if ( weight == 300 )
val = CSS_VAL_300;
else if ( weight == 400 )
val = CSS_VAL_400;
else if ( weight == 500 )
val = CSS_VAL_500;
else if ( weight == 600 )
val = CSS_VAL_600;
else if ( weight == 700 )
val = CSS_VAL_700;
else if ( weight == 800 )
val = CSS_VAL_800;
else if ( weight == 900 )
val = CSS_VAL_900;
if ( val )
font->weight = new CSSPrimitiveValue( val );
else
valid = false;
} else {
valid = false;
}
if ( !valid )
break;
value = valueList->next();
}
if ( !value )
goto invalid;
// set undefined values to default
if ( !font->style )
font->style = new CSSPrimitiveValue( CSS_VAL_NORMAL );
if ( !font->variant )
font->variant = new CSSPrimitiveValue( CSS_VAL_NORMAL );
if ( !font->weight )
font->weight = new CSSPrimitiveValue( CSS_VAL_NORMAL );
// kdDebug( 6080 ) << " got style, variant and weight current=" << valueList->currentValue << endl;
// now a font size _must_ come
// <absolute-size> | <relative-size> | <length> | <percentage> | inherit
if ( value->id >= CSS_VAL_XX_SMALL && value->id <= CSS_VAL_LARGER )
font->size = new CSSPrimitiveValue( value->id );
else if ( validUnit( value, FLength|FPercent, strict ) ) {
font->size = new CSSPrimitiveValue( value->fValue, (CSSPrimitiveValue::UnitTypes) value->unit );
}
value = valueList->next();
if ( !font->size || !value )
goto invalid;
// kdDebug( 6080 ) << " got size" << endl;
if ( value->unit == Value::Operator && value->iValue == '/' ) {
// line-height
value = valueList->next();
if ( !value )
goto invalid;
if ( value->id == CSS_VAL_NORMAL ) {
// default value, nothing to do
} else if ( validUnit( value, FNumber|FLength|FPercent, strict ) ) {
font->lineHeight = new CSSPrimitiveValue( value->fValue, (CSSPrimitiveValue::UnitTypes) value->unit );
} else {
goto invalid;
}
value = valueList->next();
if ( !value )
goto invalid;
}
if (!font->lineHeight)
font->lineHeight = new CSSPrimitiveValue( CSS_VAL_NORMAL );
// kdDebug( 6080 ) << " got line height current=" << valueList->currentValue << endl;
// font family must come now
font->family = parseFontFamily();
if ( valueList->current() || !font->family )
goto invalid;
// kdDebug( 6080 ) << " got family, parsing ok!" << endl;
addProperty( CSS_PROP_FONT, font, important );
return true;
invalid:
// kdDebug(6080) << " -> invalid" << endl;
delete font;
return false;
}
CSSValueList *CSSParser::parseFontFamily()
{
// kdDebug( 6080 ) << "CSSParser::parseFontFamily current=" << valueList->currentValue << endl;
CSSValueList *list = new CSSValueList;
Value *value = valueList->current();
FontFamilyValue* currFamily = 0;
while ( value ) {
Value* nextValue = valueList->next();
bool nextValBreaksFont = !nextValue ||
(nextValue->unit == Value::Operator && nextValue->iValue == ',');
bool nextValIsFontName = nextValue &&
((nextValue->id >= CSS_VAL_SERIF && nextValue->id <= CSS_VAL__KHTML_BODY) ||
(nextValue->unit == CSSPrimitiveValue::CSS_STRING || nextValue->unit == CSSPrimitiveValue::CSS_IDENT));
if (value->id >= CSS_VAL_SERIF && value->id <= CSS_VAL__KHTML_BODY) {
if (currFamily) {
currFamily->parsedFontName += ' ';
currFamily->parsedFontName += qString(value->string);
}
else if (nextValBreaksFont || !nextValIsFontName)
list->append(new CSSPrimitiveValue(value->id));
else
list->append(currFamily = new FontFamilyValue(qString(value->string)));
}
else if (value->unit == CSSPrimitiveValue::CSS_STRING) {
// Strings never share in a family name.
currFamily = 0;
list->append(new FontFamilyValue(qString( value->string)));
}
else if (value->unit == CSSPrimitiveValue::CSS_IDENT) {
if (currFamily) {
currFamily->parsedFontName += ' ';
currFamily->parsedFontName += qString(value->string);
}
else if (nextValBreaksFont || !nextValIsFontName)
list->append(new FontFamilyValue(qString( value->string)));
else
list->append(currFamily = new FontFamilyValue(qString(value->string)));
}
else {
break;
}
if (!nextValue)
break;
if (nextValBreaksFont) {
value = valueList->next();
currFamily = 0;
}
else if (nextValIsFontName)
value = nextValue;
else
break;
}
if ( !list->length() ) {
delete list;
list = 0;
}
return list;
}
bool CSSParser::parseColor(const DeprecatedString &name, RGBA32& rgb)
{
int len = name.length();
if ( !len )
return false;
bool ok;
if ( len == 3 || len == 6 ) {
int val = name.toInt(&ok, 16);
if ( ok ) {
if (len == 6) {
rgb = (0xff << 24) | val;
return true;
}
else if ( len == 3 ) {
// #abc converts to #aabbcc according to the specs
rgb = (0xff << 24) |
(val&0xf00)<<12 | (val&0xf00)<<8 |
(val&0xf0)<<8 | (val&0xf0)<<4 |
(val&0xf)<<4 | (val&0xf);
return true;
}
}
}
// try a little harder
Color tc;
tc.setNamedColor(name.lower());
if (tc.isValid()) {
rgb = tc.rgb();
return true;
}
return false;
}
CSSPrimitiveValue *CSSParser::parseColor()
{
return parseColorFromValue(valueList->current());
}
CSSPrimitiveValue *CSSParser::parseColorFromValue(Value* value)
{
RGBA32 c = Color::transparent;
if ( !strict && value->unit == CSSPrimitiveValue::CSS_NUMBER &&
value->fValue >= 0. && value->fValue < 1000000. ) {
DeprecatedString str;
str.sprintf( "%06d", (int)(value->fValue+.5) );
if (!CSSParser::parseColor( str, c ))
return 0;
} else if (value->unit == CSSPrimitiveValue::CSS_RGBCOLOR ||
value->unit == CSSPrimitiveValue::CSS_IDENT ||
(!strict && value->unit == CSSPrimitiveValue::CSS_DIMENSION)) {
if (!CSSParser::parseColor( qString( value->string ), c))
return 0;
}
else if ( value->unit == Value::Function &&
value->function->args != 0 &&
value->function->args->size() == 5 /* rgb + two commas */ &&
qString( value->function->name ).lower() == "rgb(" ) {
ValueList *args = value->function->args;
Value *v = args->current();
if ( !validUnit( v, FInteger|FPercent, true ) )
return 0;
int r = (int) ( v->fValue * (v->unit == CSSPrimitiveValue::CSS_PERCENTAGE ? 256./100. : 1.) );
v = args->next();
if ( v->unit != Value::Operator && v->iValue != ',' )
return 0;
v = args->next();
if ( !validUnit( v, FInteger|FPercent, true ) )
return 0;
int g = (int) ( v->fValue * (v->unit == CSSPrimitiveValue::CSS_PERCENTAGE ? 256./100. : 1.) );
v = args->next();
if ( v->unit != Value::Operator && v->iValue != ',' )
return 0;
v = args->next();
if ( !validUnit( v, FInteger|FPercent, true ) )
return 0;
int b = (int) ( v->fValue * (v->unit == CSSPrimitiveValue::CSS_PERCENTAGE ? 256./100. : 1.) );
r = kMax( 0, kMin( 255, r ) );
g = kMax( 0, kMin( 255, g ) );
b = kMax( 0, kMin( 255, b ) );
c = makeRGB( r, g, b );
}
else if ( value->unit == Value::Function &&
value->function->args != 0 &&
value->function->args->size() == 7 /* rgba + three commas */ &&
qString( value->function->name ).lower() == "rgba(" ) {
ValueList *args = value->function->args;
Value *v = args->current();
if ( !validUnit( v, FInteger|FPercent, true ) )
return 0;
int r = (int) ( v->fValue * (v->unit == CSSPrimitiveValue::CSS_PERCENTAGE ? 256./100. : 1.) );
v = args->next();
if ( v->unit != Value::Operator && v->iValue != ',' )
return 0;
v = args->next();
if ( !validUnit( v, FInteger|FPercent, true ) )
return 0;
int g = (int) ( v->fValue * (v->unit == CSSPrimitiveValue::CSS_PERCENTAGE ? 256./100. : 1.) );
v = args->next();
if ( v->unit != Value::Operator && v->iValue != ',' )
return 0;
v = args->next();
if ( !validUnit( v, FInteger|FPercent, true ) )
return 0;
int b = (int) ( v->fValue * (v->unit == CSSPrimitiveValue::CSS_PERCENTAGE ? 256./100. : 1.) );
v = args->next();
if ( v->unit != Value::Operator && v->iValue != ',' )
return 0;
v = args->next();
if ( !validUnit( v, FNumber, true ) )
return 0;
r = kMax( 0, kMin( 255, r ) );
g = kMax( 0, kMin( 255, g ) );
b = kMax( 0, kMin( 255, b ) );
int a = (int)(kMax( 0.0, kMin( 1.0, v->fValue ) ) * 255);
c = makeRGBA( r, g, b, a );
}
else
return 0;
return new CSSPrimitiveValue(c);
}
// This class tracks parsing state for shadow values. If it goes out of scope (e.g., due to an early return)
// without the allowBreak bit being set, then it will clean up all of the objects and destroy them.
struct ShadowParseContext {
ShadowParseContext()
:values(0), x(0), y(0), blur(0), color(0),
allowX(true), allowY(false), allowBlur(false), allowColor(true),
allowBreak(true)
{}
~ShadowParseContext() {
if (!allowBreak) {
delete values;
delete x;
delete y;
delete blur;
delete color;
}
}
bool allowLength() { return allowX || allowY || allowBlur; }
bool failed() { return allowBreak = false; }
void commitValue() {
// Handle the ,, case gracefully by doing nothing.
if (x || y || blur || color) {
if (!values)
values = new CSSValueList();
// Construct the current shadow value and add it to the list.
values->append(new ShadowValue(x, y, blur, color));
}
// Now reset for the next shadow value.
x = y = blur = color = 0;
allowX = allowColor = allowBreak = true;
allowY = allowBlur = false;
}
void commitLength(Value* v) {
CSSPrimitiveValue* val = new CSSPrimitiveValue(v->fValue,
(CSSPrimitiveValue::UnitTypes)v->unit);
if (allowX) {
x = val;
allowX = false; allowY = true; allowColor = false; allowBreak = false;
}
else if (allowY) {
y = val;
allowY = false; allowBlur = true; allowColor = true; allowBreak = true;
}
else if (allowBlur) {
blur = val;
allowBlur = false;
}
}
void commitColor(CSSPrimitiveValue* val) {
color = val;
allowColor = false;
if (allowX)
allowBreak = false;
else
allowBlur = false;
}
CSSValueList* values;
CSSPrimitiveValue* x;
CSSPrimitiveValue* y;
CSSPrimitiveValue* blur;
CSSPrimitiveValue* color;
bool allowX;
bool allowY;
bool allowBlur;
bool allowColor;
bool allowBreak;
};
bool CSSParser::parseShadow(int propId, bool important)
{
ShadowParseContext context;
Value* val;
while ((val = valueList->current())) {
// Check for a comma break first.
if (val->unit == Value::Operator) {
if (val->iValue != ',' || !context.allowBreak)
// Other operators aren't legal or we aren't done with the current shadow
// value. Treat as invalid.
return context.failed();
// The value is good. Commit it.
context.commitValue();
}
// Check to see if we're a length.
else if (validUnit(val, FLength, true)) {
// We required a length and didn't get one. Invalid.
if (!context.allowLength())
return context.failed();
// A length is allowed here. Construct the value and add it.
context.commitLength(val);
}
else {
// The only other type of value that's ok is a color value.
CSSPrimitiveValue* parsedColor = 0;
bool isColor = (val->id >= CSS_VAL_AQUA && val->id <= CSS_VAL_WINDOWTEXT || val->id == CSS_VAL_MENU ||
(val->id >= CSS_VAL_GREY && val->id <= CSS_VAL__KHTML_TEXT && !strict));
if (isColor) {
if (!context.allowColor)
return context.failed();
parsedColor = new CSSPrimitiveValue(val->id);
}
if (!parsedColor)
// It's not built-in. Try to parse it as a color.
parsedColor = parseColorFromValue(val);
if (!parsedColor || !context.allowColor)
return context.failed(); // This value is not a color or length and is invalid or
// it is a color, but a color isn't allowed at this point.
context.commitColor(parsedColor);
}
valueList->next();
}
if (context.allowBreak) {
context.commitValue();
if (context.values->length()) {
addProperty(propId, context.values, important);
valueList->next();
return true;
}
}
return context.failed();
}
struct BorderImageParseContext
{
BorderImageParseContext()
:m_allowBreak(false), m_allowNumber(false), m_allowSlash(false), m_allowWidth(false),
m_allowRule(false), m_image(0), m_top(0), m_right(0), m_bottom(0), m_left(0), m_borderTop(0), m_borderRight(0), m_borderBottom(0),
m_borderLeft(0), m_horizontalRule(0), m_verticalRule(0)
{}
~BorderImageParseContext() {
if (!m_allowBreak) {
delete m_image;
delete m_top; delete m_right; delete m_bottom; delete m_left;
delete m_borderTop; delete m_borderRight; delete m_borderBottom; delete m_borderLeft;
}
}
bool failed() { return m_allowBreak = false; }
bool allowBreak() const { return m_allowBreak; }
bool allowNumber() const { return m_allowNumber; }
bool allowSlash() const { return m_allowSlash; }
bool allowWidth() const { return m_allowWidth; }
bool allowRule() const { return m_allowRule; }
void commitImage(CSSImageValue* image) { m_image = image; m_allowNumber = true; }
void commitNumber(Value* v) {
CSSPrimitiveValue* val = new CSSPrimitiveValue(v->fValue,
(CSSPrimitiveValue::UnitTypes)v->unit);
if (!m_top)
m_top = val;
else if (!m_right)
m_right = val;
else if (!m_bottom)
m_bottom = val;
else {
assert(!m_left);
m_left = val;
}
m_allowBreak = m_allowSlash = m_allowRule = true;
m_allowNumber = !m_left;
}
void commitSlash() { m_allowBreak = m_allowSlash = m_allowNumber = false; m_allowWidth = true; }
void commitWidth(Value* val) {
if (!m_borderTop)
m_borderTop = val;
else if (!m_borderRight)
m_borderRight = val;
else if (!m_borderBottom)
m_borderBottom = val;
else {
assert(!m_borderLeft);
m_borderLeft = val;
}
m_allowBreak = m_allowRule = true;
m_allowWidth = !m_borderLeft;
}
void commitRule(int keyword) {
if (!m_horizontalRule)
m_horizontalRule = keyword;
else if (!m_verticalRule)
m_verticalRule = keyword;
m_allowRule = !m_verticalRule;
}
void commitBorderImage(CSSParser* p, int propId, bool important) {
// We need to clone and repeat values for any omissions.
if (!m_right) {
m_right = new CSSPrimitiveValue(m_top->getFloatValue(m_top->primitiveType()), (CSSPrimitiveValue::UnitTypes)m_top->primitiveType());
m_bottom = new CSSPrimitiveValue(m_top->getFloatValue(m_top->primitiveType()), (CSSPrimitiveValue::UnitTypes)m_top->primitiveType());
m_left = new CSSPrimitiveValue(m_top->getFloatValue(m_top->primitiveType()), (CSSPrimitiveValue::UnitTypes)m_top->primitiveType());
}
if (!m_bottom) {
m_bottom = new CSSPrimitiveValue(m_top->getFloatValue(m_top->primitiveType()), (CSSPrimitiveValue::UnitTypes)m_top->primitiveType());
m_left = new CSSPrimitiveValue(m_right->getFloatValue(m_right->primitiveType()), (CSSPrimitiveValue::UnitTypes)m_right->primitiveType());
}
if (!m_left)
m_left = new CSSPrimitiveValue(m_top->getFloatValue(m_top->primitiveType()), (CSSPrimitiveValue::UnitTypes)m_top->primitiveType());
// Now build a rect value to hold all four of our primitive values.
RectImpl* rect = new RectImpl;
rect->setTop(m_top); rect->setRight(m_right); rect->setBottom(m_bottom); rect->setLeft(m_left);
// Fill in STRETCH as the default if it wasn't specified.
if (!m_horizontalRule)
m_horizontalRule = CSS_VAL_STRETCH;
if (!m_verticalRule)
m_verticalRule = CSS_VAL_STRETCH;
// Make our new border image value now and add it as the result.
CSSBorderImageValue* borderImage = new CSSBorderImageValue(m_image, rect, m_horizontalRule, m_verticalRule);
p->addProperty(propId, borderImage, important);
// Now we have to deal with the border widths. The best way to deal with these is to actually put these values into a value
// list and then make our parsing machinery do the parsing.
if (m_borderTop) {
ValueList newList;
newList.addValue(*m_borderTop);
if (m_borderRight)
newList.addValue(*m_borderRight);
if (m_borderBottom)
newList.addValue(*m_borderBottom);
if (m_borderLeft)
newList.addValue(*m_borderLeft);
p->valueList = &newList;
p->parseValue(CSS_PROP_BORDER_WIDTH, important);
p->valueList = 0;
}
}
bool m_allowBreak;
bool m_allowNumber;
bool m_allowSlash;
bool m_allowWidth;
bool m_allowRule;
CSSImageValue* m_image;
CSSPrimitiveValue* m_top;
CSSPrimitiveValue* m_right;
CSSPrimitiveValue* m_bottom;
CSSPrimitiveValue* m_left;
Value* m_borderTop;
Value* m_borderRight;
Value* m_borderBottom;
Value* m_borderLeft;
int m_horizontalRule;
int m_verticalRule;
};
bool CSSParser::parseBorderImage(int propId, bool important)
{
// Look for an image initially. If the first value is not a URI, then we're done.
BorderImageParseContext context;
Value* val = valueList->current();
if (val->unit != CSSPrimitiveValue::CSS_URI)
return context.failed();
String uri = parseURL(domString(val->string));
if (uri.isEmpty())
return context.failed();
context.commitImage(new CSSImageValue(String(KURL(styleElement->baseURL().deprecatedString(), uri.deprecatedString()).url()),
styleElement));
while ((val = valueList->next())) {
if (context.allowNumber() && validUnit(val, FInteger|FNonNeg|FPercent, true)) {
context.commitNumber(val);
} else if (context.allowSlash() && val->unit == Value::Operator && val->iValue == '/') {
context.commitSlash();
} else if (context.allowWidth() &&
(val->id == CSS_VAL_THIN || val->id == CSS_VAL_MEDIUM || val->id == CSS_VAL_THICK || validUnit(val, FLength, strict))) {
context.commitWidth(val);
} else if (context.allowRule() &&
(val->id == CSS_VAL_STRETCH || val->id == CSS_VAL_ROUND || val->id == CSS_VAL_REPEAT)) {
context.commitRule(val->id);
} else {
// Something invalid was encountered.
return context.failed();
}
}
if (context.allowBreak()) {
// Need to fully commit as a single value.
context.commitBorderImage(this, propId, important);
return true;
}
return context.failed();
}
#if CSS_DEBUG
static inline int yyerror(const char *str)
{
kdDebug( 6080 ) << "CSS parse error " << str << endl;
return 1;
}
#else
static inline int yyerror(const char*) { return 1; }
#endif
#define END_TOKEN 0
#include "CSSGrammar.h"
int CSSParser::lex( void *_yylval ) {
YYSTYPE *yylval = (YYSTYPE *)_yylval;
int token = lex();
int length;
unsigned short *t = text( &length );
#ifdef TOKEN_DEBUG
qDebug("CSSTokenizer: got token %d: '%s'", token, token == END_TOKEN ? "" : DeprecatedString( (QChar *)t, length ).latin1() );
#endif
switch( token ) {
case WHITESPACE:
case SGML_CD:
case INCLUDES:
case DASHMATCH:
break;
case URI:
case STRING:
case IDENT:
case HASH:
case DIMEN:
case UNICODERANGE:
case FUNCTION:
yylval->string.string = t;
yylval->string.length = length;
break;
case IMPORT_SYM:
case PAGE_SYM:
case MEDIA_SYM:
case FONT_FACE_SYM:
case CHARSET_SYM:
case NAMESPACE_SYM:
case IMPORTANT_SYM:
break;
case QEMS:
length--;
case GRADS:
length--;
case DEGS:
case RADS:
case KHERZ:
length--;
case MSECS:
case HERZ:
case EMS:
case EXS:
case PXS:
case CMS:
case MMS:
case INS:
case PTS:
case PCS:
length--;
case SECS:
case PERCENTAGE:
length--;
case NUMBER:
yylval->val = DeprecatedString( (QChar *)t, length ).toDouble();
//qDebug("value = %s, converted=%.2f", DeprecatedString( (QChar *)t, length ).latin1(), yylval->val );
break;
default:
break;
}
return token;
}
static inline int toHex(char c)
{
if ('0' <= c && c <= '9')
return c - '0';
if ('a' <= c && c <= 'f')
return c - 'a' + 10;
if ('A' <= c && c<= 'F')
return c - 'A' + 10;
return 0;
}
unsigned short *CSSParser::text(int *length)
{
unsigned short *start = yytext;
int l = yyleng;
switch( yyTok ) {
case STRING:
l--;
/* nobreak */
case HASH:
start++;
l--;
break;
case URI:
// "url("{w}{string}{w}")"
// "url("{w}{url}{w}")"
// strip "url(" and ")"
start += 4;
l -= 5;
// strip {w}
while ( l &&
(*start == ' ' || *start == '\t' || *start == '\r' ||
*start == '\n' || *start == '\f' ) ) {
start++; l--;
}
if ( *start == '"' || *start == '\'' ) {
start++; l--;
}
while ( l &&
(start[l-1] == ' ' || start[l-1] == '\t' || start[l-1] == '\r' ||
start[l-1] == '\n' || start[l-1] == '\f' ) ) {
l--;
}
if ( l && (start[l-1] == '\"' || start[l-1] == '\'' ) )
l--;
default:
break;
}
// process escapes
unsigned short *out = start;
unsigned short *escape = 0;
for ( int i = 0; i < l; i++ ) {
unsigned short *current = start+i;
if ( escape == current - 1 ) {
if ( ( *current >= '0' && *current <= '9' ) ||
( *current >= 'a' && *current <= 'f' ) ||
( *current >= 'A' && *current <= 'F' ) )
continue;
if ( yyTok == STRING &&
( *current == '\n' || *current == '\r' || *current == '\f' ) ) {
// ### handle \r\n case
if ( *current != '\r' )
escape = 0;
continue;
}
// in all other cases copy the char to output
// ###
*out++ = *current;
escape = 0;
continue;
}
if ( escape == current - 2 && yyTok == STRING &&
*(current-1) == '\r' && *current == '\n' ) {
escape = 0;
continue;
}
if ( escape > current - 7 &&
( ( *current >= '0' && *current <= '9' ) ||
( *current >= 'a' && *current <= 'f' ) ||
( *current >= 'A' && *current <= 'F' ) ) )
continue;
if ( escape ) {
// add escaped char
int uc = 0;
escape++;
while ( escape < current ) {
uc *= 16;
uc += toHex( *escape );
escape++;
}
// can't handle chars outside ucs2
if ( uc > 0xffff )
uc = 0xfffd;
*(out++) = (unsigned short)uc;
escape = 0;
if ( *current == ' ' ||
*current == '\t' ||
*current == '\r' ||
*current == '\n' ||
*current == '\f' )
continue;
}
if ( !escape && *current == '\\' ) {
escape = current;
continue;
}
*(out++) = *current;
}
if ( escape ) {
// add escaped char
int uc = 0;
escape++;
while ( escape < start+l ) {
uc *= 16;
uc += toHex( *escape );
escape++;
}
// can't handle chars outside ucs2
if ( uc > 0xffff )
uc = 0xfffd;
*(out++) = (unsigned short)uc;
}
*length = out - start;
return start;
}
CSSSelector* CSSParser::createFloatingSelector()
{
CSSSelector* selector = new CSSSelector;
m_floatingSelectors.add(selector);
return selector;
}
CSSSelector* CSSParser::sinkFloatingSelector(CSSSelector* selector)
{
if (selector) {
ASSERT(m_floatingSelectors.contains(selector));
m_floatingSelectors.remove(selector);
}
return selector;
}
ValueList* CSSParser::createFloatingValueList()
{
ValueList* list = new ValueList;
m_floatingValueLists.add(list);
return list;
}
ValueList* CSSParser::sinkFloatingValueList(ValueList* list)
{
if (list) {
ASSERT(m_floatingValueLists.contains(list));
m_floatingValueLists.remove(list);
}
return list;
}
Function* CSSParser::createFloatingFunction()
{
Function* function = new Function;
m_floatingFunctions.add(function);
return function;
}
Function* CSSParser::sinkFloatingFunction(Function* function)
{
if (function) {
ASSERT(m_floatingFunctions.contains(function));
m_floatingFunctions.remove(function);
}
return function;
}
Value& CSSParser::sinkFloatingValue(Value& value)
{
if (value.unit == Value::Function) {
ASSERT(m_floatingFunctions.contains(value.function));
m_floatingFunctions.remove(value.function);
}
return value;
}
MediaList* CSSParser::createMediaList()
{
MediaList* list = new MediaList;
m_parsedStyleObjects.append(list);
return list;
}
CSSRule* CSSParser::createImportRule(const ParseString& URL, MediaList* media)
{
if (!media)
return 0;
if (!styleElement)
return 0;
if (!styleElement->isCSSStyleSheet())
return 0;
CSSImportRule* rule = new CSSImportRule(styleElement, domString(URL), media);
m_parsedStyleObjects.append(rule);
return rule;
}
CSSRule* CSSParser::createMediaRule(MediaList* media, CSSRuleList* rules)
{
if (!media)
return 0;
if (!rules)
return 0;
if (!styleElement)
return 0;
if (!styleElement->isCSSStyleSheet())
return 0;
CSSMediaRule* rule = new CSSMediaRule(styleElement, media, rules);
m_parsedStyleObjects.append(rule);
return rule;
}
CSSRuleList* CSSParser::createRuleList()
{
CSSRuleList* list = new CSSRuleList;
m_parsedRuleLists.append(list);
return list;
}
CSSRule* CSSParser::createStyleRule(CSSSelector* selector)
{
CSSStyleRule* rule = 0;
if (selector) {
rule = new CSSStyleRule(styleElement);
m_parsedStyleObjects.append(rule);
rule->setSelector(sinkFloatingSelector(selector));
rule->setDeclaration(new CSSMutableStyleDeclaration(rule, parsedProperties, numParsedProperties));
}
clearProperties();
return rule;
}
#define YY_DECL int CSSParser::lex()
#define yyconst const
typedef int yy_state_type;
typedef unsigned YY_CHAR;
// this line makes sure we treat all Unicode chars correctly.
#define YY_SC_TO_UI(c) (c > 0xff ? 0xff : c)
#define YY_DO_BEFORE_ACTION \
yytext = yy_bp; \
yyleng = (int) (yy_cp - yy_bp); \
yy_hold_char = *yy_cp; \
*yy_cp = 0; \
yy_c_buf_p = yy_cp;
#define YY_BREAK break;
#define ECHO
#define YY_RULE_SETUP
#define INITIAL 0
#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1)
#define yyterminate() yyTok = END_TOKEN; return yyTok
#define YY_FATAL_ERROR(a)
#include "tokenizer.cpp"
}