if (!aData->mStyleContext) return;
MapAttributesIntoRule(const nsMappedAttributes* aAttributes,                      nsRuleData* aData){  // XXX Bug 211636:  This function is used by a single style rule  // that's used to match two different type of elements -- tables, and  // table cells.  (nsHTMLTableCellElement overrides  // WalkContentStyleRules so that this happens.)  This violates the  // nsIStyleRule contract, since it's the same style rule object doing  // the mapping in two different ways.  It's also incorrect since it's  // testing the display type of the style context rather than checking  // which *element* it's matching (style rules should not stop matching  // when the display type is changed).  nsCompatibility mode = aData->mPresContext->CompatibilityMode();  if (aData->mSID == eStyleStruct_TableBorder) {    const nsStyleDisplay* readDisplay = aData->mStyleContext->GetStyleDisplay();    if (readDisplay->mDisplay != NS_STYLE_DISPLAY_TABLE_CELL) {      // cellspacing       const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::cellspacing);      if (value && value->Type() == nsAttrValue::eInteger) {        if (aData->mTableData->mBorderSpacing.mXValue.GetUnit() == eCSSUnit_Null)          aData->mTableData->mBorderSpacing.mXValue.SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Pixel);        if (aData->mTableData->mBorderSpacing.mYValue.GetUnit() == eCSSUnit_Null)          aData->mTableData->mBorderSpacing.mYValue.SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Pixel);      }      else if (value && value->Type() == nsAttrValue::ePercent && eCompatibility_NavQuirks == mode) {        // in quirks mode, treat a % cellspacing value a pixel value.        if (aData->mTableData->mBorderSpacing.mXValue.GetUnit() == eCSSUnit_Null)          aData->mTableData->mBorderSpacing.mXValue.SetFloatValue(100.0f * value->GetPercentValue(), eCSSUnit_Pixel);        if (aData->mTableData->mBorderSpacing.mYValue.GetUnit() == eCSSUnit_Null)          aData->mTableData->mBorderSpacing.mYValue.SetFloatValue(100.0f * value->GetPercentValue(), eCSSUnit_Pixel);      }    }  }   else if (aData->mSID == eStyleStruct_Table) {    const nsStyleDisplay* readDisplay = aData->mStyleContext->GetStyleDisplay();    if (readDisplay->mDisplay != NS_STYLE_DISPLAY_TABLE_CELL) {      MapTableBorderInto(aAttributes, aData, 0);      const nsAttrValue* value;      // layout      if (aData->mTableData->mLayout.GetUnit() == eCSSUnit_Null) {        value = aAttributes->GetAttr(nsHTMLAtoms::layout);        if (value && value->Type() == nsAttrValue::eEnum)          aData->mTableData->mLayout.SetIntValue(value->GetEnumValue(), eCSSUnit_Enumerated);      }            // cols      value = aAttributes->GetAttr(nsHTMLAtoms::cols);      if (value) {        if (value->Type() == nsAttrValue::eInteger)           aData->mTableData->mCols.SetIntValue(value->GetIntegerValue(), eCSSUnit_Integer);        else // COLS had no value, so it refers to all columns          aData->mTableData->mCols.SetIntValue(NS_STYLE_TABLE_COLS_ALL, eCSSUnit_Enumerated);      }      // rules      value = aAttributes->GetAttr(nsHTMLAtoms::rules);      if (value && value->Type() == nsAttrValue::eEnum)        aData->mTableData->mRules.SetIntValue(value->GetEnumValue(), eCSSUnit_Enumerated);    }  }  else if (aData->mSID == eStyleStruct_Margin) {    const nsStyleDisplay* readDisplay = aData->mStyleContext->GetStyleDisplay();      if (readDisplay->mDisplay != NS_STYLE_DISPLAY_TABLE_CELL) {      // align; Check for enumerated type (it may be another type if      // illegal)      const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::align);      if (value && value->Type() == nsAttrValue::eEnum) {        if (value->GetEnumValue() == NS_STYLE_TEXT_ALIGN_CENTER ||            value->GetEnumValue() == NS_STYLE_TEXT_ALIGN_MOZ_CENTER) {          nsCSSRect& margin = aData->mMarginData->mMargin;          if (margin.mLeft.GetUnit() == eCSSUnit_Null)            margin.mLeft.SetAutoValue();          if (margin.mRight.GetUnit() == eCSSUnit_Null)            margin.mRight.SetAutoValue();        }      }      // hspace is mapped into left and right margin,       // vspace is mapped into top and bottom margins      // - *** Quirks Mode only ***      if (eCompatibility_NavQuirks == mode) {        value = aAttributes->GetAttr(nsHTMLAtoms::hspace);        if (value && value->Type() == nsAttrValue::eInteger) {          nsCSSRect& margin = aData->mMarginData->mMargin;          if (margin.mLeft.GetUnit() == eCSSUnit_Null)            margin.mLeft.SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Pixel);           if (margin.mRight.GetUnit() == eCSSUnit_Null)            margin.mRight.SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Pixel);        }        value = aAttributes->GetAttr(nsHTMLAtoms::vspace);        if (value && value->Type() == nsAttrValue::eInteger) {          nsCSSRect& margin = aData->mMarginData->mMargin;          if (margin.mTop.GetUnit() == eCSSUnit_Null)            margin.mTop.SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Pixel);           if (margin.mBottom.GetUnit() == eCSSUnit_Null)            margin.mBottom.SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Pixel);         }      }    }  }  else if (aData->mSID == eStyleStruct_Padding) {    const nsStyleDisplay* readDisplay = aData->mStyleContext->GetStyleDisplay();    if (readDisplay->mDisplay == NS_STYLE_DISPLAY_TABLE_CELL) {      const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::cellpadding);      if (value) {        nsAttrValue::ValueType valueType = value->Type();        if (valueType == nsAttrValue::eInteger || valueType == nsAttrValue::ePercent) {          // We have cellpadding.  This will override our padding values if we don't          // have any set.          nsCSSValue padVal;          if (valueType == nsAttrValue::eInteger)            padVal.SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Pixel);          else {            // when we support % cellpadding in standard mode, uncomment the following            float pctVal = value->GetPercentValue();            //if (eCompatibility_NavQuirks == mode) {              // in quirks mode treat a pct cellpadding value as a pixel value              padVal.SetFloatValue(100.0f * pctVal, eCSSUnit_Pixel);            //}            //else {            //  padVal.SetPercentValue(pctVal);            //}          }          if (aData->mMarginData->mPadding.mLeft.GetUnit() == eCSSUnit_Null)            aData->mMarginData->mPadding.mLeft = padVal;          if (aData->mMarginData->mPadding.mRight.GetUnit() == eCSSUnit_Null)            aData->mMarginData->mPadding.mRight = padVal;          if (aData->mMarginData->mPadding.mTop.GetUnit() == eCSSUnit_Null)            aData->mMarginData->mPadding.mTop = padVal;          if (aData->mMarginData->mPadding.mBottom.GetUnit() == eCSSUnit_Null)            aData->mMarginData->mPadding.mBottom = padVal;        }      }    }  }  else if (aData->mSID == eStyleStruct_Position) {    const nsStyleDisplay* readDisplay = aData->mStyleContext->GetStyleDisplay();      if (readDisplay->mDisplay != NS_STYLE_DISPLAY_TABLE_CELL) {      // width: value      if (aData->mPositionData->mWidth.GetUnit() == eCSSUnit_Null) {        const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::width);        if (value && value->Type() == nsAttrValue::eInteger)           aData->mPositionData->mWidth.SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Pixel);        else if (value && value->Type() == nsAttrValue::ePercent)          aData->mPositionData->mWidth.SetPercentValue(value->GetPercentValue());      }      // height: value      if (aData->mPositionData->mHeight.GetUnit() == eCSSUnit_Null) {        const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::height);        if (value && value->Type() == nsAttrValue::eInteger)           aData->mPositionData->mHeight.SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Pixel);        else if (value && value->Type() == nsAttrValue::ePercent)          aData->mPositionData->mHeight.SetPercentValue(value->GetPercentValue());       }    }  }  else if (aData->mSID == eStyleStruct_Visibility) {    const nsStyleDisplay* readDisplay = aData->mStyleContext->GetStyleDisplay();      if (readDisplay->mDisplay != NS_STYLE_DISPLAY_TABLE_CELL)      nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);  }  else if (aData->mSID == eStyleStruct_Border) {    if (!aData->mStyleContext) return;    const nsStyleTableBorder* tableStyle = aData->mStyleContext->GetStyleTableBorder();    const nsStyleDisplay* readDisplay = aData->mStyleContext->GetStyleDisplay();    if (readDisplay->mDisplay == NS_STYLE_DISPLAY_TABLE_CELL) {      if (NS_STYLE_BORDER_SEPARATE == tableStyle->mBorderCollapse) {        // Set the cell's border from the table in the separate border        // model. If there is a border on the table, then the mapping to        // rules=all will take care of borders in the collapsing model.        // But if rules="none", we don't want to do this.        const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::border);        const nsAttrValue* rulesValue = aAttributes->GetAttr(nsHTMLAtoms::rules);        if ((!rulesValue || rulesValue->Type() != nsAttrValue::eEnum ||             rulesValue->GetEnumValue() != NS_STYLE_TABLE_RULES_NONE) &&            value &&            ((value->Type() == nsAttrValue::eInteger &&              value->GetIntegerValue() > 0) ||             value->IsEmptyString())) {          if (aData->mMarginData->mBorderWidth.mLeft.GetUnit() == eCSSUnit_Null)            aData->mMarginData->mBorderWidth.mLeft.SetFloatValue(1.0f, eCSSUnit_Pixel);          if (aData->mMarginData->mBorderWidth.mRight.GetUnit() == eCSSUnit_Null)            aData->mMarginData->mBorderWidth.mRight.SetFloatValue(1.0f, eCSSUnit_Pixel);          if (aData->mMarginData->mBorderWidth.mTop.GetUnit() == eCSSUnit_Null)            aData->mMarginData->mBorderWidth.mTop.SetFloatValue(1.0f, eCSSUnit_Pixel);          if (aData->mMarginData->mBorderWidth.mBottom.GetUnit() == eCSSUnit_Null)            aData->mMarginData->mBorderWidth.mBottom.SetFloatValue(1.0f, eCSSUnit_Pixel);          PRUint8 borderStyle = (eCompatibility_NavQuirks == mode)                                 ? NS_STYLE_BORDER_STYLE_BG_INSET : NS_STYLE_BORDER_STYLE_INSET;          // BG_INSET results in a border color based on background colors          // used for NavQuirks only...          if (aData->mMarginData->mBorderStyle.mLeft.GetUnit() == eCSSUnit_Null)            aData->mMarginData->mBorderStyle.mLeft.SetIntValue(borderStyle, eCSSUnit_Enumerated);          if (aData->mMarginData->mBorderStyle.mRight.GetUnit() == eCSSUnit_Null)            aData->mMarginData->mBorderStyle.mRight.SetIntValue(borderStyle, eCSSUnit_Enumerated);          if (aData->mMarginData->mBorderStyle.mTop.GetUnit() == eCSSUnit_Null)            aData->mMarginData->mBorderStyle.mTop.SetIntValue(borderStyle, eCSSUnit_Enumerated);          if (aData->mMarginData->mBorderStyle.mBottom.GetUnit() == eCSSUnit_Null)            aData->mMarginData->mBorderStyle.mBottom.SetIntValue(borderStyle, eCSSUnit_Enumerated);        }      }    }    else {      // default border style is the Nav4.6 extension which uses the      // background color as the basis of the outset border. If the      // table has a transparent background then it finds the closest      // ancestor that has a non-transparent      // background. NS_STYLE_BORDER_OUTSET uses the border color of      // the table and if that is not set, then it uses the color.      PRUint8 borderStyle = (eCompatibility_NavQuirks == mode)                             ? NS_STYLE_BORDER_STYLE_BG_OUTSET :                              NS_STYLE_BORDER_STYLE_OUTSET;      // bordercolor      const nsAttrValue* value = aAttributes->GetAttr(nsHTMLAtoms::bordercolor);      nscolor color;      if (value && value->GetColorValue(color)) {        if (aData->mMarginData->mBorderColor.mLeft.GetUnit() == eCSSUnit_Null)          aData->mMarginData->mBorderColor.mLeft.SetColorValue(color);        if (aData->mMarginData->mBorderColor.mRight.GetUnit() == eCSSUnit_Null)          aData->mMarginData->mBorderColor.mRight.SetColorValue(color);        if (aData->mMarginData->mBorderColor.mTop.GetUnit() == eCSSUnit_Null)          aData->mMarginData->mBorderColor.mTop.SetColorValue(color);        if (aData->mMarginData->mBorderColor.mBottom.GetUnit() == eCSSUnit_Null)          aData->mMarginData->mBorderColor.mBottom.SetColorValue(color);        borderStyle = NS_STYLE_BORDER_STYLE_OUTSET; // use css outset      }      else if (NS_STYLE_BORDER_COLLAPSE == tableStyle->mBorderCollapse) {        // make the color grey        nscolor color = NS_RGB(80, 80, 80);        if (aData->mMarginData->mBorderColor.mLeft.GetUnit() == eCSSUnit_Null)          aData->mMarginData->mBorderColor.mLeft.SetColorValue(color);        if (aData->mMarginData->mBorderColor.mRight.GetUnit() == eCSSUnit_Null)          aData->mMarginData->mBorderColor.mRight.SetColorValue(color);        if (aData->mMarginData->mBorderColor.mTop.GetUnit() == eCSSUnit_Null)          aData->mMarginData->mBorderColor.mTop.SetColorValue(color);        if (aData->mMarginData->mBorderColor.mBottom.GetUnit() == eCSSUnit_Null)          aData->mMarginData->mBorderColor.mBottom.SetColorValue(color);      }      // border and frame      MapTableBorderInto(aAttributes, aData, borderStyle);    }  }  else if (aData->mSID == eStyleStruct_Background) {    const nsStyleDisplay* readDisplay = aData->mStyleContext->GetStyleDisplay();      if (readDisplay->mDisplay != NS_STYLE_DISPLAY_TABLE_CELL)      nsGenericHTMLElement::MapBackgroundAttributesInto(aAttributes, aData);  }}