#include "config.h"
#include "EditingStyle.h"
#include "ApplyStyleCommand.h"
#include "CSSComputedStyleDeclaration.h"
#include "CSSParser.h"
#include "CSSRuleList.h"
#include "CSSStyleRule.h"
#include "CSSValueList.h"
#include "CSSValuePool.h"
#include "Editor.h"
#include "Frame.h"
#include "HTMLFontElement.h"
#include "HTMLInterchange.h"
#include "HTMLNames.h"
#include "Node.h"
#include "NodeTraversal.h"
#include "QualifiedName.h"
#include "RenderStyle.h"
#include "StyleFontSizeFunctions.h"
#include "StyleProperties.h"
#include "StyleResolver.h"
#include "StyleRule.h"
#include "StyledElement.h"
#include "VisibleUnits.h"
#include "htmlediting.h"
namespace WebCore {
static const CSSPropertyID editingProperties[] = {
CSSPropertyColor,
CSSPropertyFontFamily,
CSSPropertyFontSize,
CSSPropertyFontStyle,
CSSPropertyFontVariant,
CSSPropertyFontWeight,
CSSPropertyLetterSpacing,
CSSPropertyLineHeight,
CSSPropertyOrphans,
CSSPropertyTextAlign,
CSSPropertyTextIndent,
CSSPropertyTextTransform,
CSSPropertyWhiteSpace,
CSSPropertyWidows,
CSSPropertyWordSpacing,
#if PLATFORM(IOS)
CSSPropertyWebkitTapHighlightColor,
CSSPropertyWebkitCompositionFillColor,
#endif
CSSPropertyWebkitTextDecorationsInEffect,
CSSPropertyWebkitTextFillColor,
#if ENABLE(IOS_TEXT_AUTOSIZING)
CSSPropertyWebkitTextSizeAdjust,
#endif
CSSPropertyWebkitTextStrokeColor,
CSSPropertyWebkitTextStrokeWidth,
CSSPropertyBackgroundColor,
CSSPropertyTextDecoration,
};
const unsigned numAllEditingProperties = WTF_ARRAY_LENGTH(editingProperties);
const unsigned numInheritableEditingProperties = numAllEditingProperties - 2;
enum EditingPropertiesToInclude { OnlyInheritableEditingProperties, AllEditingProperties };
template <class StyleDeclarationType>
static PassRefPtr<MutableStyleProperties> copyEditingProperties(StyleDeclarationType* style, EditingPropertiesToInclude type)
{
if (type == AllEditingProperties)
return style->copyPropertiesInSet(editingProperties, numAllEditingProperties);
return style->copyPropertiesInSet(editingProperties, numInheritableEditingProperties);
}
static inline bool isEditingProperty(int id)
{
for (size_t i = 0; i < WTF_ARRAY_LENGTH(editingProperties); ++i) {
if (editingProperties[i] == id)
return true;
}
return false;
}
static PassRefPtr<MutableStyleProperties> copyPropertiesFromComputedStyle(ComputedStyleExtractor& computedStyle, EditingStyle::PropertiesToInclude propertiesToInclude)
{
switch (propertiesToInclude) {
case EditingStyle::AllProperties:
return computedStyle.copyProperties();
case EditingStyle::OnlyEditingInheritableProperties:
return copyEditingProperties(&computedStyle, OnlyInheritableEditingProperties);
case EditingStyle::EditingPropertiesInEffect:
return copyEditingProperties(&computedStyle, AllEditingProperties);
}
ASSERT_NOT_REACHED();
return 0;
}
static PassRefPtr<MutableStyleProperties> copyPropertiesFromComputedStyle(Node* node, EditingStyle::PropertiesToInclude propertiesToInclude)
{
ComputedStyleExtractor computedStyle(node);
return copyPropertiesFromComputedStyle(computedStyle, propertiesToInclude);
}
static PassRefPtr<CSSValue> extractPropertyValue(const StyleProperties* style, CSSPropertyID propertyID)
{
return style ? style->getPropertyCSSValue(propertyID) : PassRefPtr<CSSValue>();
}
static PassRefPtr<CSSValue> extractPropertyValue(ComputedStyleExtractor* computedStyle, CSSPropertyID propertyID)
{
return computedStyle->propertyValue(propertyID);
}
template<typename T>
int identifierForStyleProperty(T* style, CSSPropertyID propertyID)
{
RefPtr<CSSValue> value = extractPropertyValue(style, propertyID);
if (!value || !value->isPrimitiveValue())
return 0;
return toCSSPrimitiveValue(value.get())->getValueID();
}
template<typename T> PassRefPtr<MutableStyleProperties> getPropertiesNotIn(StyleProperties* styleWithRedundantProperties, T* baseStyle);
enum LegacyFontSizeMode { AlwaysUseLegacyFontSize, UseLegacyFontSizeOnlyIfPixelValuesMatch };
static int legacyFontSizeFromCSSValue(Document*, CSSPrimitiveValue*, bool shouldUseFixedFontDefaultSize, LegacyFontSizeMode);
static bool isTransparentColorValue(CSSValue*);
static bool hasTransparentBackgroundColor(StyleProperties*);
static PassRefPtr<CSSValue> backgroundColorInEffect(Node*);
class HTMLElementEquivalent {
WTF_MAKE_FAST_ALLOCATED;
public:
HTMLElementEquivalent(CSSPropertyID, CSSValueID primitiveValue, const QualifiedName& tagName);
virtual ~HTMLElementEquivalent() { }
virtual bool matches(const Element* element) const { return !m_tagName || element->hasTagName(*m_tagName); }
virtual bool hasAttribute() const { return false; }
virtual bool propertyExistsInStyle(const StyleProperties* style) const { return style->getPropertyCSSValue(m_propertyID); }
virtual bool valueIsPresentInStyle(Element*, StyleProperties*) const;
virtual void addToStyle(Element*, EditingStyle*) const;
protected:
HTMLElementEquivalent(CSSPropertyID);
HTMLElementEquivalent(CSSPropertyID, const QualifiedName& tagName);
const CSSPropertyID m_propertyID;
const RefPtr<CSSPrimitiveValue> m_primitiveValue;
const QualifiedName* m_tagName; };
HTMLElementEquivalent::HTMLElementEquivalent(CSSPropertyID id)
: m_propertyID(id)
, m_tagName(0)
{
}
HTMLElementEquivalent::HTMLElementEquivalent(CSSPropertyID id, const QualifiedName& tagName)
: m_propertyID(id)
, m_tagName(&tagName)
{
}
HTMLElementEquivalent::HTMLElementEquivalent(CSSPropertyID id, CSSValueID primitiveValue, const QualifiedName& tagName)
: m_propertyID(id)
, m_primitiveValue(CSSPrimitiveValue::createIdentifier(primitiveValue))
, m_tagName(&tagName)
{
ASSERT(primitiveValue != CSSValueInvalid);
}
bool HTMLElementEquivalent::valueIsPresentInStyle(Element* element, StyleProperties* style) const
{
RefPtr<CSSValue> value = style->getPropertyCSSValue(m_propertyID);
return matches(element) && value && value->isPrimitiveValue() && toCSSPrimitiveValue(value.get())->getValueID() == m_primitiveValue->getValueID();
}
void HTMLElementEquivalent::addToStyle(Element*, EditingStyle* style) const
{
style->setProperty(m_propertyID, m_primitiveValue->cssText());
}
class HTMLTextDecorationEquivalent : public HTMLElementEquivalent {
public:
HTMLTextDecorationEquivalent(CSSValueID primitiveValue, const QualifiedName& tagName);
virtual bool propertyExistsInStyle(const StyleProperties*) const;
virtual bool valueIsPresentInStyle(Element*, StyleProperties*) const;
};
HTMLTextDecorationEquivalent::HTMLTextDecorationEquivalent(CSSValueID primitiveValue, const QualifiedName& tagName)
: HTMLElementEquivalent(CSSPropertyTextDecoration, primitiveValue, tagName)
{
}
bool HTMLTextDecorationEquivalent::propertyExistsInStyle(const StyleProperties* style) const
{
return style->getPropertyCSSValue(CSSPropertyWebkitTextDecorationsInEffect) || style->getPropertyCSSValue(CSSPropertyTextDecoration);
}
bool HTMLTextDecorationEquivalent::valueIsPresentInStyle(Element* element, StyleProperties* style) const
{
RefPtr<CSSValue> styleValue = style->getPropertyCSSValue(CSSPropertyWebkitTextDecorationsInEffect);
if (!styleValue)
styleValue = style->getPropertyCSSValue(CSSPropertyTextDecoration);
return matches(element) && styleValue && styleValue->isValueList() && toCSSValueList(styleValue.get())->hasValue(m_primitiveValue.get());
}
class HTMLAttributeEquivalent : public HTMLElementEquivalent {
public:
HTMLAttributeEquivalent(CSSPropertyID, const QualifiedName& tagName, const QualifiedName& attrName);
HTMLAttributeEquivalent(CSSPropertyID, const QualifiedName& attrName);
bool matches(const Element* elem) const { return HTMLElementEquivalent::matches(elem) && elem->hasAttribute(m_attrName); }
virtual bool hasAttribute() const { return true; }
virtual bool valueIsPresentInStyle(Element*, StyleProperties*) const;
virtual void addToStyle(Element*, EditingStyle*) const;
virtual PassRefPtr<CSSValue> attributeValueAsCSSValue(Element*) const;
inline const QualifiedName& attributeName() const { return m_attrName; }
protected:
const QualifiedName& m_attrName; };
HTMLAttributeEquivalent::HTMLAttributeEquivalent(CSSPropertyID id, const QualifiedName& tagName, const QualifiedName& attrName)
: HTMLElementEquivalent(id, tagName)
, m_attrName(attrName)
{
}
HTMLAttributeEquivalent::HTMLAttributeEquivalent(CSSPropertyID id, const QualifiedName& attrName)
: HTMLElementEquivalent(id)
, m_attrName(attrName)
{
}
bool HTMLAttributeEquivalent::valueIsPresentInStyle(Element* element, StyleProperties* style) const
{
RefPtr<CSSValue> value = attributeValueAsCSSValue(element);
RefPtr<CSSValue> styleValue = style->getPropertyCSSValue(m_propertyID);
return compareCSSValuePtr(value, styleValue);
}
void HTMLAttributeEquivalent::addToStyle(Element* element, EditingStyle* style) const
{
if (RefPtr<CSSValue> value = attributeValueAsCSSValue(element))
style->setProperty(m_propertyID, value->cssText());
}
PassRefPtr<CSSValue> HTMLAttributeEquivalent::attributeValueAsCSSValue(Element* element) const
{
ASSERT(element);
if (!element->hasAttribute(m_attrName))
return 0;
RefPtr<MutableStyleProperties> dummyStyle;
dummyStyle = MutableStyleProperties::create();
dummyStyle->setProperty(m_propertyID, element->getAttribute(m_attrName));
return dummyStyle->getPropertyCSSValue(m_propertyID);
}
class HTMLFontSizeEquivalent : public HTMLAttributeEquivalent {
public:
HTMLFontSizeEquivalent();
virtual PassRefPtr<CSSValue> attributeValueAsCSSValue(Element*) const;
};
HTMLFontSizeEquivalent::HTMLFontSizeEquivalent()
: HTMLAttributeEquivalent(CSSPropertyFontSize, HTMLNames::fontTag, HTMLNames::sizeAttr)
{
}
PassRefPtr<CSSValue> HTMLFontSizeEquivalent::attributeValueAsCSSValue(Element* element) const
{
ASSERT(element);
if (!element->hasAttribute(m_attrName))
return 0;
CSSValueID size;
if (!HTMLFontElement::cssValueFromFontSizeNumber(element->getAttribute(m_attrName), size))
return 0;
return CSSPrimitiveValue::createIdentifier(size);
}
float EditingStyle::NoFontDelta = 0.0f;
EditingStyle::EditingStyle()
: m_shouldUseFixedDefaultFontSize(false)
, m_fontSizeDelta(NoFontDelta)
{
}
EditingStyle::EditingStyle(Node* node, PropertiesToInclude propertiesToInclude)
: m_shouldUseFixedDefaultFontSize(false)
, m_fontSizeDelta(NoFontDelta)
{
init(node, propertiesToInclude);
}
EditingStyle::EditingStyle(const Position& position, PropertiesToInclude propertiesToInclude)
: m_shouldUseFixedDefaultFontSize(false)
, m_fontSizeDelta(NoFontDelta)
{
init(position.deprecatedNode(), propertiesToInclude);
}
EditingStyle::EditingStyle(const StyleProperties* style)
: m_shouldUseFixedDefaultFontSize(false)
, m_fontSizeDelta(NoFontDelta)
{
if (style)
m_mutableStyle = style->mutableCopy();
extractFontSizeDelta();
}
EditingStyle::EditingStyle(CSSPropertyID propertyID, const String& value)
: m_mutableStyle(0)
, m_shouldUseFixedDefaultFontSize(false)
, m_fontSizeDelta(NoFontDelta)
{
setProperty(propertyID, value);
}
EditingStyle::~EditingStyle()
{
}
static RGBA32 cssValueToRGBA(CSSValue* colorValue)
{
if (!colorValue || !colorValue->isPrimitiveValue())
return Color::transparent;
CSSPrimitiveValue* primitiveColor = toCSSPrimitiveValue(colorValue);
if (primitiveColor->isRGBColor())
return primitiveColor->getRGBA32Value();
RGBA32 rgba = 0;
CSSParser::parseColor(rgba, colorValue->cssText());
return rgba;
}
template<typename T>
static inline RGBA32 textColorFromStyle(T* style)
{
return cssValueToRGBA(extractPropertyValue(style, CSSPropertyColor).get());
}
template<typename T>
static inline RGBA32 backgroundColorFromStyle(T* style)
{
return cssValueToRGBA(extractPropertyValue(style, CSSPropertyBackgroundColor).get());
}
static inline RGBA32 rgbaBackgroundColorInEffect(Node* node)
{
return cssValueToRGBA(backgroundColorInEffect(node).get());
}
static int textAlignResolvingStartAndEnd(int textAlign, int direction)
{
switch (textAlign) {
case CSSValueCenter:
case CSSValueWebkitCenter:
return CSSValueCenter;
case CSSValueJustify:
return CSSValueJustify;
case CSSValueLeft:
case CSSValueWebkitLeft:
return CSSValueLeft;
case CSSValueRight:
case CSSValueWebkitRight:
return CSSValueRight;
case CSSValueStart:
return direction != CSSValueRtl ? CSSValueLeft : CSSValueRight;
case CSSValueEnd:
return direction == CSSValueRtl ? CSSValueRight : CSSValueLeft;
}
return CSSValueInvalid;
}
template<typename T>
static int textAlignResolvingStartAndEnd(T* style)
{
return textAlignResolvingStartAndEnd(identifierForStyleProperty(style, CSSPropertyTextAlign), identifierForStyleProperty(style, CSSPropertyDirection));
}
void EditingStyle::init(Node* node, PropertiesToInclude propertiesToInclude)
{
if (isTabSpanTextNode(node))
node = tabSpanNode(node)->parentNode();
else if (isTabSpanNode(node))
node = node->parentNode();
ComputedStyleExtractor computedStyleAtPosition(node);
m_mutableStyle = copyPropertiesFromComputedStyle(computedStyleAtPosition,
propertiesToInclude == EditingPropertiesInEffect ? OnlyEditingInheritableProperties : propertiesToInclude);
if (propertiesToInclude == EditingPropertiesInEffect) {
if (RefPtr<CSSValue> value = backgroundColorInEffect(node))
m_mutableStyle->setProperty(CSSPropertyBackgroundColor, value->cssText());
if (RefPtr<CSSValue> value = computedStyleAtPosition.propertyValue(CSSPropertyWebkitTextDecorationsInEffect))
m_mutableStyle->setProperty(CSSPropertyTextDecoration, value->cssText());
}
if (node && node->computedStyle()) {
RenderStyle* renderStyle = node->computedStyle();
removeTextFillAndStrokeColorsIfNeeded(renderStyle);
if (renderStyle->fontDescription().keywordSize())
m_mutableStyle->setProperty(CSSPropertyFontSize, computedStyleAtPosition.getFontSizeCSSValuePreferringKeyword()->cssText());
}
m_shouldUseFixedDefaultFontSize = computedStyleAtPosition.useFixedFontDefaultSize();
extractFontSizeDelta();
}
void EditingStyle::removeTextFillAndStrokeColorsIfNeeded(RenderStyle* renderStyle)
{
if (!renderStyle->textFillColor().isValid())
m_mutableStyle->removeProperty(CSSPropertyWebkitTextFillColor);
if (!renderStyle->textStrokeColor().isValid())
m_mutableStyle->removeProperty(CSSPropertyWebkitTextStrokeColor);
}
void EditingStyle::setProperty(CSSPropertyID propertyID, const String& value, bool important)
{
if (!m_mutableStyle)
m_mutableStyle = MutableStyleProperties::create();
m_mutableStyle->setProperty(propertyID, value, important);
}
void EditingStyle::extractFontSizeDelta()
{
if (!m_mutableStyle)
return;
if (m_mutableStyle->getPropertyCSSValue(CSSPropertyFontSize)) {
m_mutableStyle->removeProperty(CSSPropertyWebkitFontSizeDelta);
return;
}
RefPtr<CSSValue> value = m_mutableStyle->getPropertyCSSValue(CSSPropertyWebkitFontSizeDelta);
if (!value || !value->isPrimitiveValue())
return;
CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value.get());
if (!primitiveValue->isPx())
return;
m_fontSizeDelta = primitiveValue->getFloatValue();
m_mutableStyle->removeProperty(CSSPropertyWebkitFontSizeDelta);
}
bool EditingStyle::isEmpty() const
{
return (!m_mutableStyle || m_mutableStyle->isEmpty()) && m_fontSizeDelta == NoFontDelta;
}
bool EditingStyle::textDirection(WritingDirection& writingDirection) const
{
if (!m_mutableStyle)
return false;
RefPtr<CSSValue> unicodeBidi = m_mutableStyle->getPropertyCSSValue(CSSPropertyUnicodeBidi);
if (!unicodeBidi || !unicodeBidi->isPrimitiveValue())
return false;
CSSValueID unicodeBidiValue = toCSSPrimitiveValue(unicodeBidi.get())->getValueID();
if (unicodeBidiValue == CSSValueEmbed) {
RefPtr<CSSValue> direction = m_mutableStyle->getPropertyCSSValue(CSSPropertyDirection);
if (!direction || !direction->isPrimitiveValue())
return false;
writingDirection = toCSSPrimitiveValue(direction.get())->getValueID() == CSSValueLtr ? LeftToRightWritingDirection : RightToLeftWritingDirection;
return true;
}
if (unicodeBidiValue == CSSValueNormal) {
writingDirection = NaturalWritingDirection;
return true;
}
return false;
}
void EditingStyle::setStyle(PassRefPtr<MutableStyleProperties> style)
{
m_mutableStyle = style;
m_shouldUseFixedDefaultFontSize = false;
extractFontSizeDelta();
}
void EditingStyle::overrideWithStyle(const StyleProperties* style)
{
return mergeStyle(style, OverrideValues);
}
void EditingStyle::clear()
{
m_mutableStyle.clear();
m_shouldUseFixedDefaultFontSize = false;
m_fontSizeDelta = NoFontDelta;
}
PassRefPtr<EditingStyle> EditingStyle::copy() const
{
RefPtr<EditingStyle> copy = EditingStyle::create();
if (m_mutableStyle)
copy->m_mutableStyle = m_mutableStyle->mutableCopy();
copy->m_shouldUseFixedDefaultFontSize = m_shouldUseFixedDefaultFontSize;
copy->m_fontSizeDelta = m_fontSizeDelta;
return copy;
}
PassRefPtr<EditingStyle> EditingStyle::extractAndRemoveBlockProperties()
{
RefPtr<EditingStyle> blockProperties = EditingStyle::create();
if (!m_mutableStyle)
return blockProperties;
blockProperties->m_mutableStyle = m_mutableStyle->copyBlockProperties();
m_mutableStyle->removeBlockProperties();
return blockProperties;
}
PassRefPtr<EditingStyle> EditingStyle::extractAndRemoveTextDirection()
{
RefPtr<EditingStyle> textDirection = EditingStyle::create();
textDirection->m_mutableStyle = MutableStyleProperties::create();
textDirection->m_mutableStyle->setProperty(CSSPropertyUnicodeBidi, CSSValueEmbed, m_mutableStyle->propertyIsImportant(CSSPropertyUnicodeBidi));
textDirection->m_mutableStyle->setProperty(CSSPropertyDirection, m_mutableStyle->getPropertyValue(CSSPropertyDirection),
m_mutableStyle->propertyIsImportant(CSSPropertyDirection));
m_mutableStyle->removeProperty(CSSPropertyUnicodeBidi);
m_mutableStyle->removeProperty(CSSPropertyDirection);
return textDirection;
}
void EditingStyle::removeBlockProperties()
{
if (!m_mutableStyle)
return;
m_mutableStyle->removeBlockProperties();
}
void EditingStyle::removeStyleAddedByNode(Node* node)
{
if (!node || !node->parentNode())
return;
RefPtr<MutableStyleProperties> parentStyle = copyPropertiesFromComputedStyle(node->parentNode(), EditingPropertiesInEffect);
RefPtr<MutableStyleProperties> nodeStyle = copyPropertiesFromComputedStyle(node, EditingPropertiesInEffect);
removeEquivalentProperties(*parentStyle);
removeEquivalentProperties(*nodeStyle);
}
void EditingStyle::removeStyleConflictingWithStyleOfNode(Node* node)
{
if (!node || !node->parentNode() || !m_mutableStyle)
return;
RefPtr<MutableStyleProperties> parentStyle = copyPropertiesFromComputedStyle(node->parentNode(), EditingPropertiesInEffect);
RefPtr<EditingStyle> nodeStyle = EditingStyle::create(node, EditingPropertiesInEffect);
nodeStyle->removeEquivalentProperties(*parentStyle);
MutableStyleProperties* style = nodeStyle->style();
unsigned propertyCount = style->propertyCount();
for (unsigned i = 0; i < propertyCount; ++i)
m_mutableStyle->removeProperty(style->propertyAt(i).id());
}
void EditingStyle::collapseTextDecorationProperties()
{
if (!m_mutableStyle)
return;
RefPtr<CSSValue> textDecorationsInEffect = m_mutableStyle->getPropertyCSSValue(CSSPropertyWebkitTextDecorationsInEffect);
if (!textDecorationsInEffect)
return;
if (textDecorationsInEffect->isValueList())
m_mutableStyle->setProperty(CSSPropertyTextDecoration, textDecorationsInEffect->cssText(), m_mutableStyle->propertyIsImportant(CSSPropertyTextDecoration));
else
m_mutableStyle->removeProperty(CSSPropertyTextDecoration);
m_mutableStyle->removeProperty(CSSPropertyWebkitTextDecorationsInEffect);
}
static const CSSPropertyID textOnlyProperties[] = {
CSSPropertyTextDecoration,
CSSPropertyWebkitTextDecorationsInEffect,
CSSPropertyFontStyle,
CSSPropertyFontWeight,
CSSPropertyColor,
};
TriState EditingStyle::triStateOfStyle(EditingStyle* style) const
{
if (!style || !style->m_mutableStyle)
return FalseTriState;
return triStateOfStyle(style->m_mutableStyle.get(), DoNotIgnoreTextOnlyProperties);
}
template<typename T>
TriState EditingStyle::triStateOfStyle(T* styleToCompare, ShouldIgnoreTextOnlyProperties shouldIgnoreTextOnlyProperties) const
{
RefPtr<MutableStyleProperties> difference = getPropertiesNotIn(m_mutableStyle.get(), styleToCompare);
if (shouldIgnoreTextOnlyProperties == IgnoreTextOnlyProperties)
difference->removePropertiesInSet(textOnlyProperties, WTF_ARRAY_LENGTH(textOnlyProperties));
if (difference->isEmpty())
return TrueTriState;
if (difference->propertyCount() == m_mutableStyle->propertyCount())
return FalseTriState;
return MixedTriState;
}
TriState EditingStyle::triStateOfStyle(const VisibleSelection& selection) const
{
if (!selection.isCaretOrRange())
return FalseTriState;
if (selection.isCaret())
return triStateOfStyle(EditingStyle::styleAtSelectionStart(selection).get());
TriState state = FalseTriState;
bool nodeIsStart = true;
for (Node* node = selection.start().deprecatedNode(); node; node = NodeTraversal::next(node)) {
if (node->renderer() && node->hasEditableStyle()) {
ComputedStyleExtractor computedStyle(node);
TriState nodeState = triStateOfStyle(&computedStyle, node->isTextNode() ? EditingStyle::DoNotIgnoreTextOnlyProperties : EditingStyle::IgnoreTextOnlyProperties);
if (nodeIsStart) {
state = nodeState;
nodeIsStart = false;
} else if (state != nodeState && node->isTextNode()) {
state = MixedTriState;
break;
}
}
if (node == selection.end().deprecatedNode())
break;
}
return state;
}
bool EditingStyle::conflictsWithInlineStyleOfElement(StyledElement* element, EditingStyle* extractedStyle, Vector<CSSPropertyID>* conflictingProperties) const
{
ASSERT(element);
ASSERT(!conflictingProperties || conflictingProperties->isEmpty());
const StyleProperties* inlineStyle = element->inlineStyle();
if (!m_mutableStyle || !inlineStyle)
return false;
unsigned propertyCount = m_mutableStyle->propertyCount();
for (unsigned i = 0; i < propertyCount; ++i) {
CSSPropertyID propertyID = m_mutableStyle->propertyAt(i).id();
if (propertyID == CSSPropertyWhiteSpace && isTabSpanNode(element))
continue;
if (propertyID == CSSPropertyWebkitTextDecorationsInEffect && inlineStyle->getPropertyCSSValue(CSSPropertyTextDecoration)) {
if (!conflictingProperties)
return true;
conflictingProperties->append(CSSPropertyTextDecoration);
if (extractedStyle)
extractedStyle->setProperty(CSSPropertyTextDecoration, inlineStyle->getPropertyValue(CSSPropertyTextDecoration), inlineStyle->propertyIsImportant(CSSPropertyTextDecoration));
continue;
}
if (!inlineStyle->getPropertyCSSValue(propertyID))
continue;
if (propertyID == CSSPropertyUnicodeBidi && inlineStyle->getPropertyCSSValue(CSSPropertyDirection)) {
if (!conflictingProperties)
return true;
conflictingProperties->append(CSSPropertyDirection);
if (extractedStyle)
extractedStyle->setProperty(propertyID, inlineStyle->getPropertyValue(propertyID), inlineStyle->propertyIsImportant(propertyID));
}
if (!conflictingProperties)
return true;
conflictingProperties->append(propertyID);
if (extractedStyle)
extractedStyle->setProperty(propertyID, inlineStyle->getPropertyValue(propertyID), inlineStyle->propertyIsImportant(propertyID));
}
return conflictingProperties && !conflictingProperties->isEmpty();
}
static const Vector<std::unique_ptr<HTMLElementEquivalent>>& htmlElementEquivalents()
{
static NeverDestroyed<Vector<std::unique_ptr<HTMLElementEquivalent>>> HTMLElementEquivalents;
if (!HTMLElementEquivalents.get().size()) {
HTMLElementEquivalents.get().append(std::make_unique<HTMLElementEquivalent>(CSSPropertyFontWeight, CSSValueBold, HTMLNames::bTag));
HTMLElementEquivalents.get().append(std::make_unique<HTMLElementEquivalent>(CSSPropertyFontWeight, CSSValueBold, HTMLNames::strongTag));
HTMLElementEquivalents.get().append(std::make_unique<HTMLElementEquivalent>(CSSPropertyVerticalAlign, CSSValueSub, HTMLNames::subTag));
HTMLElementEquivalents.get().append(std::make_unique<HTMLElementEquivalent>(CSSPropertyVerticalAlign, CSSValueSuper, HTMLNames::supTag));
HTMLElementEquivalents.get().append(std::make_unique<HTMLElementEquivalent>(CSSPropertyFontStyle, CSSValueItalic, HTMLNames::iTag));
HTMLElementEquivalents.get().append(std::make_unique<HTMLElementEquivalent>(CSSPropertyFontStyle, CSSValueItalic, HTMLNames::emTag));
HTMLElementEquivalents.get().append(std::make_unique<HTMLTextDecorationEquivalent>(CSSValueUnderline, HTMLNames::uTag));
HTMLElementEquivalents.get().append(std::make_unique<HTMLTextDecorationEquivalent>(CSSValueLineThrough, HTMLNames::sTag));
HTMLElementEquivalents.get().append(std::make_unique<HTMLTextDecorationEquivalent>(CSSValueLineThrough, HTMLNames::strikeTag));
}
return HTMLElementEquivalents;
}
bool EditingStyle::conflictsWithImplicitStyleOfElement(HTMLElement* element, EditingStyle* extractedStyle, ShouldExtractMatchingStyle shouldExtractMatchingStyle) const
{
if (!m_mutableStyle)
return false;
const Vector<std::unique_ptr<HTMLElementEquivalent>>& HTMLElementEquivalents = htmlElementEquivalents();
for (size_t i = 0; i < HTMLElementEquivalents.size(); ++i) {
const HTMLElementEquivalent* equivalent = HTMLElementEquivalents[i].get();
if (equivalent->matches(element) && equivalent->propertyExistsInStyle(m_mutableStyle.get())
&& (shouldExtractMatchingStyle == ExtractMatchingStyle || !equivalent->valueIsPresentInStyle(element, m_mutableStyle.get()))) {
if (extractedStyle)
equivalent->addToStyle(element, extractedStyle);
return true;
}
}
return false;
}
static const Vector<std::unique_ptr<HTMLAttributeEquivalent>>& htmlAttributeEquivalents()
{
static NeverDestroyed<Vector<std::unique_ptr<HTMLAttributeEquivalent>>> HTMLAttributeEquivalents;
if (!HTMLAttributeEquivalents.get().size()) {
HTMLAttributeEquivalents.get().append(std::make_unique<HTMLAttributeEquivalent>(CSSPropertyColor, HTMLNames::fontTag, HTMLNames::colorAttr));
HTMLAttributeEquivalents.get().append(std::make_unique<HTMLAttributeEquivalent>(CSSPropertyFontFamily, HTMLNames::fontTag, HTMLNames::faceAttr));
HTMLAttributeEquivalents.get().append(std::make_unique<HTMLFontSizeEquivalent>());
HTMLAttributeEquivalents.get().append(std::make_unique<HTMLAttributeEquivalent>(CSSPropertyDirection, HTMLNames::dirAttr));
HTMLAttributeEquivalents.get().append(std::make_unique<HTMLAttributeEquivalent>(CSSPropertyUnicodeBidi, HTMLNames::dirAttr));
}
return HTMLAttributeEquivalents;
}
bool EditingStyle::conflictsWithImplicitStyleOfAttributes(HTMLElement* element) const
{
ASSERT(element);
if (!m_mutableStyle)
return false;
const Vector<std::unique_ptr<HTMLAttributeEquivalent>>& HTMLAttributeEquivalents = htmlAttributeEquivalents();
for (size_t i = 0; i < HTMLAttributeEquivalents.size(); ++i) {
if (HTMLAttributeEquivalents[i]->matches(element) && HTMLAttributeEquivalents[i]->propertyExistsInStyle(m_mutableStyle.get())
&& !HTMLAttributeEquivalents[i]->valueIsPresentInStyle(element, m_mutableStyle.get()))
return true;
}
return false;
}
bool EditingStyle::extractConflictingImplicitStyleOfAttributes(HTMLElement* element, ShouldPreserveWritingDirection shouldPreserveWritingDirection,
EditingStyle* extractedStyle, Vector<QualifiedName>& conflictingAttributes, ShouldExtractMatchingStyle shouldExtractMatchingStyle) const
{
ASSERT(element);
ASSERT(!extractedStyle || shouldPreserveWritingDirection == PreserveWritingDirection);
if (!m_mutableStyle)
return false;
const Vector<std::unique_ptr<HTMLAttributeEquivalent>>& HTMLAttributeEquivalents = htmlAttributeEquivalents();
bool removed = false;
for (size_t i = 0; i < HTMLAttributeEquivalents.size(); ++i) {
const HTMLAttributeEquivalent* equivalent = HTMLAttributeEquivalents[i].get();
if (shouldPreserveWritingDirection == PreserveWritingDirection && equivalent->attributeName() == HTMLNames::dirAttr)
continue;
if (!equivalent->matches(element) || !equivalent->propertyExistsInStyle(m_mutableStyle.get())
|| (shouldExtractMatchingStyle == DoNotExtractMatchingStyle && equivalent->valueIsPresentInStyle(element, m_mutableStyle.get())))
continue;
if (extractedStyle)
equivalent->addToStyle(element, extractedStyle);
conflictingAttributes.append(equivalent->attributeName());
removed = true;
}
return removed;
}
bool EditingStyle::styleIsPresentInComputedStyleOfNode(Node* node) const
{
if (!m_mutableStyle)
return true;
ComputedStyleExtractor computedStyle(node);
return getPropertiesNotIn(m_mutableStyle.get(), &computedStyle)->isEmpty();
}
bool EditingStyle::elementIsStyledSpanOrHTMLEquivalent(const HTMLElement* element)
{
bool elementIsSpanOrElementEquivalent = false;
if (element->hasTagName(HTMLNames::spanTag))
elementIsSpanOrElementEquivalent = true;
else {
const Vector<std::unique_ptr<HTMLElementEquivalent>>& HTMLElementEquivalents = htmlElementEquivalents();
size_t i;
for (i = 0; i < HTMLElementEquivalents.size(); ++i) {
if (HTMLElementEquivalents[i]->matches(element)) {
elementIsSpanOrElementEquivalent = true;
break;
}
}
}
if (!element->hasAttributes())
return elementIsSpanOrElementEquivalent;
unsigned matchedAttributes = 0;
const Vector<std::unique_ptr<HTMLAttributeEquivalent>>& HTMLAttributeEquivalents = htmlAttributeEquivalents();
for (size_t i = 0; i < HTMLAttributeEquivalents.size(); ++i) {
if (HTMLAttributeEquivalents[i]->matches(element) && HTMLAttributeEquivalents[i]->attributeName() != HTMLNames::dirAttr)
matchedAttributes++;
}
if (!elementIsSpanOrElementEquivalent && !matchedAttributes)
return false;
if (element->getAttribute(HTMLNames::classAttr) == AppleStyleSpanClass)
matchedAttributes++;
if (element->hasAttribute(HTMLNames::styleAttr)) {
if (const StyleProperties* style = element->inlineStyle()) {
unsigned propertyCount = style->propertyCount();
for (unsigned i = 0; i < propertyCount; ++i) {
if (!isEditingProperty(style->propertyAt(i).id()))
return false;
}
}
matchedAttributes++;
}
ASSERT(matchedAttributes <= element->attributeCount());
return matchedAttributes >= element->attributeCount();
}
void EditingStyle::prepareToApplyAt(const Position& position, ShouldPreserveWritingDirection shouldPreserveWritingDirection)
{
if (!m_mutableStyle)
return;
RefPtr<EditingStyle> editingStyleAtPosition = EditingStyle::create(position, EditingPropertiesInEffect);
StyleProperties* styleAtPosition = editingStyleAtPosition->m_mutableStyle.get();
RefPtr<CSSValue> unicodeBidi;
RefPtr<CSSValue> direction;
if (shouldPreserveWritingDirection == PreserveWritingDirection) {
unicodeBidi = m_mutableStyle->getPropertyCSSValue(CSSPropertyUnicodeBidi);
direction = m_mutableStyle->getPropertyCSSValue(CSSPropertyDirection);
}
removeEquivalentProperties(*styleAtPosition);
if (textAlignResolvingStartAndEnd(m_mutableStyle.get()) == textAlignResolvingStartAndEnd(styleAtPosition))
m_mutableStyle->removeProperty(CSSPropertyTextAlign);
if (textColorFromStyle(m_mutableStyle.get()) == textColorFromStyle(styleAtPosition))
m_mutableStyle->removeProperty(CSSPropertyColor);
if (hasTransparentBackgroundColor(m_mutableStyle.get())
|| cssValueToRGBA(m_mutableStyle->getPropertyCSSValue(CSSPropertyBackgroundColor).get()) == rgbaBackgroundColorInEffect(position.containerNode()))
m_mutableStyle->removeProperty(CSSPropertyBackgroundColor);
if (unicodeBidi && unicodeBidi->isPrimitiveValue()) {
m_mutableStyle->setProperty(CSSPropertyUnicodeBidi, static_cast<CSSValueID>(toCSSPrimitiveValue(unicodeBidi.get())->getValueID()));
if (direction && direction->isPrimitiveValue())
m_mutableStyle->setProperty(CSSPropertyDirection, static_cast<CSSValueID>(toCSSPrimitiveValue(direction.get())->getValueID()));
}
}
void EditingStyle::mergeTypingStyle(Document& document)
{
RefPtr<EditingStyle> typingStyle = document.frame()->selection().typingStyle();
if (!typingStyle || typingStyle == this)
return;
mergeStyle(typingStyle->style(), OverrideValues);
}
void EditingStyle::mergeInlineStyleOfElement(StyledElement* element, CSSPropertyOverrideMode mode, PropertiesToInclude propertiesToInclude)
{
ASSERT(element);
if (!element->inlineStyle())
return;
switch (propertiesToInclude) {
case AllProperties:
mergeStyle(element->inlineStyle(), mode);
return;
case OnlyEditingInheritableProperties:
mergeStyle(copyEditingProperties(element->inlineStyle(), OnlyInheritableEditingProperties).get(), mode);
return;
case EditingPropertiesInEffect:
mergeStyle(copyEditingProperties(element->inlineStyle(), AllEditingProperties).get(), mode);
return;
}
}
static inline bool elementMatchesAndPropertyIsNotInInlineStyleDecl(const HTMLElementEquivalent* equivalent, const StyledElement* element,
EditingStyle::CSSPropertyOverrideMode mode, StyleProperties* style)
{
return equivalent->matches(element) && (!element->inlineStyle() || !equivalent->propertyExistsInStyle(element->inlineStyle()))
&& (mode == EditingStyle::OverrideValues || !equivalent->propertyExistsInStyle(style));
}
static PassRefPtr<MutableStyleProperties> extractEditingProperties(const StyleProperties* style, EditingStyle::PropertiesToInclude propertiesToInclude)
{
if (!style)
return 0;
switch (propertiesToInclude) {
case EditingStyle::AllProperties:
case EditingStyle::EditingPropertiesInEffect:
return copyEditingProperties(style, AllEditingProperties);
case EditingStyle::OnlyEditingInheritableProperties:
return copyEditingProperties(style, OnlyInheritableEditingProperties);
}
ASSERT_NOT_REACHED();
return 0;
}
void EditingStyle::mergeInlineAndImplicitStyleOfElement(StyledElement* element, CSSPropertyOverrideMode mode, PropertiesToInclude propertiesToInclude)
{
RefPtr<EditingStyle> styleFromRules = EditingStyle::create();
styleFromRules->mergeStyleFromRulesForSerialization(element);
if (element->inlineStyle())
styleFromRules->m_mutableStyle->mergeAndOverrideOnConflict(*element->inlineStyle());
styleFromRules->m_mutableStyle = extractEditingProperties(styleFromRules->m_mutableStyle.get(), propertiesToInclude);
mergeStyle(styleFromRules->m_mutableStyle.get(), mode);
const Vector<std::unique_ptr<HTMLElementEquivalent>>& elementEquivalents = htmlElementEquivalents();
for (size_t i = 0; i < elementEquivalents.size(); ++i) {
if (elementMatchesAndPropertyIsNotInInlineStyleDecl(elementEquivalents[i].get(), element, mode, m_mutableStyle.get()))
elementEquivalents[i]->addToStyle(element, this);
}
const Vector<std::unique_ptr<HTMLAttributeEquivalent>>& attributeEquivalents = htmlAttributeEquivalents();
for (size_t i = 0; i < attributeEquivalents.size(); ++i) {
if (attributeEquivalents[i]->attributeName() == HTMLNames::dirAttr)
continue; if (elementMatchesAndPropertyIsNotInInlineStyleDecl(attributeEquivalents[i].get(), element, mode, m_mutableStyle.get()))
attributeEquivalents[i]->addToStyle(element, this);
}
}
PassRefPtr<EditingStyle> EditingStyle::wrappingStyleForSerialization(Node* context, bool shouldAnnotate)
{
RefPtr<EditingStyle> wrappingStyle;
if (shouldAnnotate) {
wrappingStyle = EditingStyle::create(context, EditingStyle::EditingPropertiesInEffect);
wrappingStyle->removeStyleAddedByNode(enclosingNodeOfType(firstPositionInOrBeforeNode(context), isMailBlockquote, CanCrossEditingBoundary));
wrappingStyle->collapseTextDecorationProperties();
return wrappingStyle.release();
}
wrappingStyle = EditingStyle::create();
for (Node* node = context; node && !node->isDocumentNode(); node = node->parentNode()) {
if (node->isStyledElement() && !isMailBlockquote(node)) {
wrappingStyle->mergeInlineAndImplicitStyleOfElement(toStyledElement(node), EditingStyle::DoNotOverrideValues,
EditingStyle::EditingPropertiesInEffect);
}
}
return wrappingStyle.release();
}
static void mergeTextDecorationValues(CSSValueList* mergedValue, const CSSValueList* valueToMerge)
{
RefPtr<CSSPrimitiveValue> underline = cssValuePool().createIdentifierValue(CSSValueUnderline);
RefPtr<CSSPrimitiveValue> lineThrough = cssValuePool().createIdentifierValue(CSSValueLineThrough);
if (valueToMerge->hasValue(underline.get()) && !mergedValue->hasValue(underline.get()))
mergedValue->append(underline.get());
if (valueToMerge->hasValue(lineThrough.get()) && !mergedValue->hasValue(lineThrough.get()))
mergedValue->append(lineThrough.get());
}
void EditingStyle::mergeStyle(const StyleProperties* style, CSSPropertyOverrideMode mode)
{
if (!style)
return;
if (!m_mutableStyle) {
m_mutableStyle = style->mutableCopy();
return;
}
unsigned propertyCount = style->propertyCount();
for (unsigned i = 0; i < propertyCount; ++i) {
StyleProperties::PropertyReference property = style->propertyAt(i);
RefPtr<CSSValue> value = m_mutableStyle->getPropertyCSSValue(property.id());
if ((property.id() == CSSPropertyTextDecoration || property.id() == CSSPropertyWebkitTextDecorationsInEffect) && property.value()->isValueList() && value) {
if (value->isValueList()) {
mergeTextDecorationValues(toCSSValueList(value.get()), toCSSValueList(property.value()));
continue;
}
value = 0; }
if (mode == OverrideValues || (mode == DoNotOverrideValues && !value))
m_mutableStyle->setProperty(property.id(), property.value()->cssText(), property.isImportant());
}
int oldFontSizeDelta = m_fontSizeDelta;
extractFontSizeDelta();
m_fontSizeDelta += oldFontSizeDelta;
}
static PassRefPtr<MutableStyleProperties> styleFromMatchedRulesForElement(Element* element, unsigned rulesToInclude)
{
RefPtr<MutableStyleProperties> style = MutableStyleProperties::create();
auto matchedRules = element->document().ensureStyleResolver().styleRulesForElement(element, rulesToInclude);
for (unsigned i = 0; i < matchedRules.size(); ++i) {
if (matchedRules[i]->isStyleRule())
style->mergeAndOverrideOnConflict(static_pointer_cast<StyleRule>(matchedRules[i])->properties());
}
return style.release();
}
void EditingStyle::mergeStyleFromRules(StyledElement* element)
{
RefPtr<MutableStyleProperties> styleFromMatchedRules = styleFromMatchedRulesForElement(element,
StyleResolver::AuthorCSSRules | StyleResolver::CrossOriginCSSRules);
if (m_mutableStyle)
styleFromMatchedRules->mergeAndOverrideOnConflict(*m_mutableStyle);
clear();
m_mutableStyle = styleFromMatchedRules;
}
void EditingStyle::mergeStyleFromRulesForSerialization(StyledElement* element)
{
mergeStyleFromRules(element);
RefPtr<MutableStyleProperties> fromComputedStyle = MutableStyleProperties::create();
ComputedStyleExtractor computedStyle(element);
{
unsigned propertyCount = m_mutableStyle->propertyCount();
for (unsigned i = 0; i < propertyCount; ++i) {
StyleProperties::PropertyReference property = m_mutableStyle->propertyAt(i);
CSSValue* value = property.value();
if (!value->isPrimitiveValue())
continue;
if (toCSSPrimitiveValue(value)->isPercentage()) {
if (RefPtr<CSSValue> computedPropertyValue = computedStyle.propertyValue(property.id()))
fromComputedStyle->addParsedProperty(CSSProperty(property.id(), computedPropertyValue.release()));
}
}
}
m_mutableStyle->mergeAndOverrideOnConflict(*fromComputedStyle);
}
static void removePropertiesInStyle(MutableStyleProperties* styleToRemovePropertiesFrom, StyleProperties* style)
{
unsigned propertyCount = style->propertyCount();
Vector<CSSPropertyID> propertiesToRemove(propertyCount);
for (unsigned i = 0; i < propertyCount; ++i)
propertiesToRemove[i] = style->propertyAt(i).id();
styleToRemovePropertiesFrom->removePropertiesInSet(propertiesToRemove.data(), propertiesToRemove.size());
}
void EditingStyle::removeStyleFromRulesAndContext(StyledElement* element, Node* context)
{
ASSERT(element);
if (!m_mutableStyle)
return;
RefPtr<MutableStyleProperties> styleFromMatchedRules = styleFromMatchedRulesForElement(element, StyleResolver::AllButEmptyCSSRules);
if (styleFromMatchedRules && !styleFromMatchedRules->isEmpty())
m_mutableStyle = getPropertiesNotIn(m_mutableStyle.get(), styleFromMatchedRules.get());
RefPtr<EditingStyle> computedStyle = EditingStyle::create(context, EditingPropertiesInEffect);
if (computedStyle->m_mutableStyle) {
if (!computedStyle->m_mutableStyle->getPropertyCSSValue(CSSPropertyBackgroundColor))
computedStyle->m_mutableStyle->setProperty(CSSPropertyBackgroundColor, CSSValueTransparent);
removePropertiesInStyle(computedStyle->m_mutableStyle.get(), styleFromMatchedRules.get());
m_mutableStyle = getPropertiesNotIn(m_mutableStyle.get(), computedStyle->m_mutableStyle.get());
}
if (isStyleSpanOrSpanWithOnlyStyleAttribute(element)) {
if (!styleFromMatchedRules->getPropertyCSSValue(CSSPropertyDisplay) && identifierForStyleProperty(m_mutableStyle.get(), CSSPropertyDisplay) == CSSValueInline)
m_mutableStyle->removeProperty(CSSPropertyDisplay);
if (!styleFromMatchedRules->getPropertyCSSValue(CSSPropertyFloat) && identifierForStyleProperty(m_mutableStyle.get(), CSSPropertyFloat) == CSSValueNone)
m_mutableStyle->removeProperty(CSSPropertyFloat);
}
}
void EditingStyle::removePropertiesInElementDefaultStyle(Element* element)
{
if (!m_mutableStyle || m_mutableStyle->isEmpty())
return;
RefPtr<StyleProperties> defaultStyle = styleFromMatchedRulesForElement(element, StyleResolver::UAAndUserCSSRules);
removePropertiesInStyle(m_mutableStyle.get(), defaultStyle.get());
}
template<typename T>
void EditingStyle::removeEquivalentProperties(const T& style)
{
Vector<CSSPropertyID> propertiesToRemove;
for (auto& property : m_mutableStyle->m_propertyVector) {
if (style.propertyMatches(property.id(), property.value()))
propertiesToRemove.append(property.id());
}
for (auto& property : propertiesToRemove)
m_mutableStyle->removeProperty(property);
}
void EditingStyle::forceInline()
{
if (!m_mutableStyle)
m_mutableStyle = MutableStyleProperties::create();
const bool propertyIsImportant = true;
m_mutableStyle->setProperty(CSSPropertyDisplay, CSSValueInline, propertyIsImportant);
}
bool EditingStyle::convertPositionStyle()
{
if (!m_mutableStyle)
return false;
RefPtr<CSSPrimitiveValue> sticky = cssValuePool().createIdentifierValue(CSSValueWebkitSticky);
if (m_mutableStyle->propertyMatches(CSSPropertyPosition, sticky.get())) {
m_mutableStyle->setProperty(CSSPropertyPosition, cssValuePool().createIdentifierValue(CSSValueStatic), m_mutableStyle->propertyIsImportant(CSSPropertyPosition));
return false;
}
RefPtr<CSSPrimitiveValue> fixed = cssValuePool().createIdentifierValue(CSSValueFixed);
if (m_mutableStyle->propertyMatches(CSSPropertyPosition, fixed.get())) {
m_mutableStyle->setProperty(CSSPropertyPosition, cssValuePool().createIdentifierValue(CSSValueAbsolute), m_mutableStyle->propertyIsImportant(CSSPropertyPosition));
return true;
}
RefPtr<CSSPrimitiveValue> absolute = cssValuePool().createIdentifierValue(CSSValueAbsolute);
if (m_mutableStyle->propertyMatches(CSSPropertyPosition, absolute.get()))
return true;
return false;
}
bool EditingStyle::isFloating()
{
RefPtr<CSSValue> v = m_mutableStyle->getPropertyCSSValue(CSSPropertyFloat);
RefPtr<CSSPrimitiveValue> noneValue = cssValuePool().createIdentifierValue(CSSValueNone);
return v && !v->equals(*noneValue);
}
int EditingStyle::legacyFontSize(Document* document) const
{
RefPtr<CSSValue> cssValue = m_mutableStyle->getPropertyCSSValue(CSSPropertyFontSize);
if (!cssValue || !cssValue->isPrimitiveValue())
return 0;
return legacyFontSizeFromCSSValue(document, toCSSPrimitiveValue(cssValue.get()),
m_shouldUseFixedDefaultFontSize, AlwaysUseLegacyFontSize);
}
PassRefPtr<EditingStyle> EditingStyle::styleAtSelectionStart(const VisibleSelection& selection, bool shouldUseBackgroundColorInEffect)
{
if (selection.isNone())
return 0;
Position position = adjustedSelectionStartForStyleComputation(selection);
Node* positionNode = position.containerNode();
if (selection.isRange() && positionNode && positionNode->isTextNode() && position.computeOffsetInContainerNode() == positionNode->maxCharacterOffset())
position = nextVisuallyDistinctCandidate(position);
Element* element = position.element();
if (!element)
return 0;
RefPtr<EditingStyle> style = EditingStyle::create(element, EditingStyle::AllProperties);
style->mergeTypingStyle(element->document());
if (shouldUseBackgroundColorInEffect && (selection.isRange() || hasTransparentBackgroundColor(style->m_mutableStyle.get()))) {
RefPtr<Range> range(selection.toNormalizedRange());
if (PassRefPtr<CSSValue> value = backgroundColorInEffect(range->commonAncestorContainer(IGNORE_EXCEPTION)))
style->setProperty(CSSPropertyBackgroundColor, value->cssText());
}
return style;
}
WritingDirection EditingStyle::textDirectionForSelection(const VisibleSelection& selection, EditingStyle* typingStyle, bool& hasNestedOrMultipleEmbeddings)
{
hasNestedOrMultipleEmbeddings = true;
if (selection.isNone())
return NaturalWritingDirection;
Position position = selection.start().downstream();
Node* node = position.deprecatedNode();
if (!node)
return NaturalWritingDirection;
Position end;
if (selection.isRange()) {
end = selection.end().upstream();
Node* pastLast = Range::create(*end.document(), position.parentAnchoredEquivalent(), end.parentAnchoredEquivalent())->pastLastNode();
for (Node* n = node; n && n != pastLast; n = NodeTraversal::next(n)) {
if (!n->isStyledElement())
continue;
RefPtr<CSSValue> unicodeBidi = ComputedStyleExtractor(n).propertyValue(CSSPropertyUnicodeBidi);
if (!unicodeBidi || !unicodeBidi->isPrimitiveValue())
continue;
CSSValueID unicodeBidiValue = toCSSPrimitiveValue(unicodeBidi.get())->getValueID();
if (unicodeBidiValue == CSSValueEmbed || unicodeBidiValue == CSSValueBidiOverride)
return NaturalWritingDirection;
}
}
if (selection.isCaret()) {
WritingDirection direction;
if (typingStyle && typingStyle->textDirection(direction)) {
hasNestedOrMultipleEmbeddings = false;
return direction;
}
node = selection.visibleStart().deepEquivalent().deprecatedNode();
}
Node* block = enclosingBlock(node);
WritingDirection foundDirection = NaturalWritingDirection;
for (; node != block; node = node->parentNode()) {
if (!node->isStyledElement())
continue;
ComputedStyleExtractor computedStyle(node);
RefPtr<CSSValue> unicodeBidi = computedStyle.propertyValue(CSSPropertyUnicodeBidi);
if (!unicodeBidi || !unicodeBidi->isPrimitiveValue())
continue;
CSSValueID unicodeBidiValue = toCSSPrimitiveValue(unicodeBidi.get())->getValueID();
if (unicodeBidiValue == CSSValueNormal)
continue;
if (unicodeBidiValue == CSSValueBidiOverride)
return NaturalWritingDirection;
ASSERT(unicodeBidiValue == CSSValueEmbed);
RefPtr<CSSValue> direction = computedStyle.propertyValue(CSSPropertyDirection);
if (!direction || !direction->isPrimitiveValue())
continue;
CSSValueID directionValue = toCSSPrimitiveValue(direction.get())->getValueID();
if (directionValue != CSSValueLtr && directionValue != CSSValueRtl)
continue;
if (foundDirection != NaturalWritingDirection)
return NaturalWritingDirection;
if (selection.isRange() && !end.deprecatedNode()->isDescendantOf(node))
return NaturalWritingDirection;
foundDirection = directionValue == CSSValueLtr ? LeftToRightWritingDirection : RightToLeftWritingDirection;
}
hasNestedOrMultipleEmbeddings = false;
return foundDirection;
}
static void reconcileTextDecorationProperties(MutableStyleProperties* style)
{
RefPtr<CSSValue> textDecorationsInEffect = style->getPropertyCSSValue(CSSPropertyWebkitTextDecorationsInEffect);
RefPtr<CSSValue> textDecoration = style->getPropertyCSSValue(CSSPropertyTextDecoration);
ASSERT(!textDecorationsInEffect || !textDecoration);
if (textDecorationsInEffect) {
style->setProperty(CSSPropertyTextDecoration, textDecorationsInEffect->cssText());
style->removeProperty(CSSPropertyWebkitTextDecorationsInEffect);
textDecoration = textDecorationsInEffect;
}
if (textDecoration && !textDecoration->isValueList())
style->removeProperty(CSSPropertyTextDecoration);
}
StyleChange::StyleChange(EditingStyle* style, const Position& position)
: m_applyBold(false)
, m_applyItalic(false)
, m_applyUnderline(false)
, m_applyLineThrough(false)
, m_applySubscript(false)
, m_applySuperscript(false)
{
Document* document = position.anchorNode() ? &position.anchorNode()->document() : 0;
if (!style || !style->style() || !document || !document->frame())
return;
Node* node = position.containerNode();
if (!node)
return;
ComputedStyleExtractor computedStyle(node);
RefPtr<MutableStyleProperties> mutableStyle = getPropertiesNotIn(style->style(), &computedStyle);
reconcileTextDecorationProperties(mutableStyle.get());
if (!document->frame()->editor().shouldStyleWithCSS())
extractTextStyles(document, mutableStyle.get(), computedStyle.useFixedFontDefaultSize());
if (isTabSpanTextNode(position.deprecatedNode()) || isTabSpanNode((position.deprecatedNode())))
mutableStyle->removeProperty(CSSPropertyWhiteSpace);
if (mutableStyle->getPropertyCSSValue(CSSPropertyUnicodeBidi) && !style->style()->getPropertyCSSValue(CSSPropertyDirection))
mutableStyle->setProperty(CSSPropertyDirection, style->style()->getPropertyValue(CSSPropertyDirection));
m_cssStyle = mutableStyle->asText().stripWhiteSpace();
}
static void setTextDecorationProperty(MutableStyleProperties* style, const CSSValueList* newTextDecoration, CSSPropertyID propertyID)
{
if (newTextDecoration->length())
style->setProperty(propertyID, newTextDecoration->cssText(), style->propertyIsImportant(propertyID));
else {
style->removeProperty(propertyID);
}
}
void StyleChange::extractTextStyles(Document* document, MutableStyleProperties* style, bool shouldUseFixedFontDefaultSize)
{
ASSERT(style);
if (identifierForStyleProperty(style, CSSPropertyFontWeight) == CSSValueBold) {
style->removeProperty(CSSPropertyFontWeight);
m_applyBold = true;
}
int fontStyle = identifierForStyleProperty(style, CSSPropertyFontStyle);
if (fontStyle == CSSValueItalic || fontStyle == CSSValueOblique) {
style->removeProperty(CSSPropertyFontStyle);
m_applyItalic = true;
}
RefPtr<CSSValue> textDecoration = style->getPropertyCSSValue(CSSPropertyTextDecoration);
if (textDecoration && textDecoration->isValueList()) {
RefPtr<CSSPrimitiveValue> underline = cssValuePool().createIdentifierValue(CSSValueUnderline);
RefPtr<CSSPrimitiveValue> lineThrough = cssValuePool().createIdentifierValue(CSSValueLineThrough);
RefPtr<CSSValueList> newTextDecoration = toCSSValueList(textDecoration.get())->copy();
if (newTextDecoration->removeAll(underline.get()))
m_applyUnderline = true;
if (newTextDecoration->removeAll(lineThrough.get()))
m_applyLineThrough = true;
setTextDecorationProperty(style, newTextDecoration.get(), CSSPropertyTextDecoration);
}
int verticalAlign = identifierForStyleProperty(style, CSSPropertyVerticalAlign);
switch (verticalAlign) {
case CSSValueSub:
style->removeProperty(CSSPropertyVerticalAlign);
m_applySubscript = true;
break;
case CSSValueSuper:
style->removeProperty(CSSPropertyVerticalAlign);
m_applySuperscript = true;
break;
}
if (style->getPropertyCSSValue(CSSPropertyColor)) {
m_applyFontColor = Color(textColorFromStyle(style)).serialized();
style->removeProperty(CSSPropertyColor);
}
m_applyFontFace = style->getPropertyValue(CSSPropertyFontFamily);
m_applyFontFace.replaceWithLiteral('\'', "");
style->removeProperty(CSSPropertyFontFamily);
if (RefPtr<CSSValue> fontSize = style->getPropertyCSSValue(CSSPropertyFontSize)) {
if (!fontSize->isPrimitiveValue())
style->removeProperty(CSSPropertyFontSize); else if (int legacyFontSize = legacyFontSizeFromCSSValue(document, toCSSPrimitiveValue(fontSize.get()),
shouldUseFixedFontDefaultSize, UseLegacyFontSizeOnlyIfPixelValuesMatch)) {
m_applyFontSize = String::number(legacyFontSize);
style->removeProperty(CSSPropertyFontSize);
}
}
}
static void diffTextDecorations(MutableStyleProperties* style, CSSPropertyID propertID, CSSValue* refTextDecoration)
{
RefPtr<CSSValue> textDecoration = style->getPropertyCSSValue(propertID);
if (!textDecoration || !textDecoration->isValueList() || !refTextDecoration || !refTextDecoration->isValueList())
return;
RefPtr<CSSValueList> newTextDecoration = toCSSValueList(textDecoration.get())->copy();
CSSValueList* valuesInRefTextDecoration = toCSSValueList(refTextDecoration);
for (size_t i = 0; i < valuesInRefTextDecoration->length(); i++)
newTextDecoration->removeAll(valuesInRefTextDecoration->item(i));
setTextDecorationProperty(style, newTextDecoration.get(), propertID);
}
static bool fontWeightIsBold(CSSValue* fontWeight)
{
if (!fontWeight)
return false;
if (!fontWeight->isPrimitiveValue())
return false;
switch (toCSSPrimitiveValue(fontWeight)->getValueID()) {
case CSSValue100:
case CSSValue200:
case CSSValue300:
case CSSValue400:
case CSSValue500:
case CSSValueNormal:
return false;
case CSSValueBold:
case CSSValue600:
case CSSValue700:
case CSSValue800:
case CSSValue900:
return true;
default:
break;
}
ASSERT_NOT_REACHED(); return false;
}
template<typename T>
static bool fontWeightIsBold(T* style)
{
return fontWeightIsBold(extractPropertyValue(style, CSSPropertyFontWeight).get());
}
template<typename T>
static PassRefPtr<MutableStyleProperties> extractPropertiesNotIn(StyleProperties* styleWithRedundantProperties, T* baseStyle)
{
ASSERT(styleWithRedundantProperties);
RefPtr<EditingStyle> result = EditingStyle::create(styleWithRedundantProperties);
result->removeEquivalentProperties(*baseStyle);
RefPtr<MutableStyleProperties> mutableStyle = result->style();
RefPtr<CSSValue> baseTextDecorationsInEffect = extractPropertyValue(baseStyle, CSSPropertyWebkitTextDecorationsInEffect);
diffTextDecorations(mutableStyle.get(), CSSPropertyTextDecoration, baseTextDecorationsInEffect.get());
diffTextDecorations(mutableStyle.get(), CSSPropertyWebkitTextDecorationsInEffect, baseTextDecorationsInEffect.get());
if (extractPropertyValue(baseStyle, CSSPropertyFontWeight) && fontWeightIsBold(mutableStyle.get()) == fontWeightIsBold(baseStyle))
mutableStyle->removeProperty(CSSPropertyFontWeight);
if (extractPropertyValue(baseStyle, CSSPropertyColor) && textColorFromStyle(mutableStyle.get()) == textColorFromStyle(baseStyle))
mutableStyle->removeProperty(CSSPropertyColor);
if (extractPropertyValue(baseStyle, CSSPropertyTextAlign)
&& textAlignResolvingStartAndEnd(mutableStyle.get()) == textAlignResolvingStartAndEnd(baseStyle))
mutableStyle->removeProperty(CSSPropertyTextAlign);
if (extractPropertyValue(baseStyle, CSSPropertyBackgroundColor) && backgroundColorFromStyle(mutableStyle.get()) == backgroundColorFromStyle(baseStyle))
mutableStyle->removeProperty(CSSPropertyBackgroundColor);
return mutableStyle.release();
}
template<typename T>
PassRefPtr<MutableStyleProperties> getPropertiesNotIn(StyleProperties* styleWithRedundantProperties, T* baseStyle)
{
return extractPropertiesNotIn(styleWithRedundantProperties, baseStyle);
}
static bool isCSSValueLength(CSSPrimitiveValue* value)
{
return value->isFontIndependentLength();
}
int legacyFontSizeFromCSSValue(Document* document, CSSPrimitiveValue* value, bool shouldUseFixedFontDefaultSize, LegacyFontSizeMode mode)
{
ASSERT(document);
if (isCSSValueLength(value)) {
int pixelFontSize = value->getIntValue(CSSPrimitiveValue::CSS_PX);
int legacyFontSize = Style::legacyFontSizeForPixelSize(pixelFontSize, shouldUseFixedFontDefaultSize, *document);
int cssPrimitiveEquivalent = legacyFontSize - 1 + CSSValueXSmall;
if (mode == AlwaysUseLegacyFontSize || Style::fontSizeForKeyword(cssPrimitiveEquivalent, shouldUseFixedFontDefaultSize, *document) == pixelFontSize)
return legacyFontSize;
return 0;
}
if (CSSValueXSmall <= value->getValueID() && value->getValueID() <= CSSValueWebkitXxxLarge)
return value->getValueID() - CSSValueXSmall + 1;
return 0;
}
bool isTransparentColorValue(CSSValue* cssValue)
{
if (!cssValue)
return true;
if (!cssValue->isPrimitiveValue())
return false;
CSSPrimitiveValue* value = toCSSPrimitiveValue(cssValue);
if (value->isRGBColor())
return !alphaChannel(value->getRGBA32Value());
return value->getValueID() == CSSValueTransparent;
}
bool hasTransparentBackgroundColor(StyleProperties* style)
{
RefPtr<CSSValue> cssValue = style->getPropertyCSSValue(CSSPropertyBackgroundColor);
return isTransparentColorValue(cssValue.get());
}
PassRefPtr<CSSValue> backgroundColorInEffect(Node* node)
{
for (Node* ancestor = node; ancestor; ancestor = ancestor->parentNode()) {
if (RefPtr<CSSValue> value = ComputedStyleExtractor(ancestor).propertyValue(CSSPropertyBackgroundColor)) {
if (!isTransparentColorValue(value.get()))
return value.release();
}
}
return 0;
}
}