HTMLFontElement.cpp (3864B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "HTMLFontElement.h" 8 9 #include "mozilla/MappedDeclarationsBuilder.h" 10 #include "mozilla/dom/Document.h" 11 #include "mozilla/dom/HTMLFontElementBinding.h" 12 #include "nsAttrValueInlines.h" 13 #include "nsAttrValueOrString.h" 14 #include "nsContentUtils.h" 15 16 NS_IMPL_NS_NEW_HTML_ELEMENT(Font) 17 18 namespace mozilla::dom { 19 20 HTMLFontElement::~HTMLFontElement() = default; 21 22 JSObject* HTMLFontElement::WrapNode(JSContext* aCx, 23 JS::Handle<JSObject*> aGivenProto) { 24 return HTMLFontElement_Binding::Wrap(aCx, this, aGivenProto); 25 } 26 27 NS_IMPL_ELEMENT_CLONE(HTMLFontElement) 28 29 bool HTMLFontElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute, 30 const nsAString& aValue, 31 nsIPrincipal* aMaybeScriptedPrincipal, 32 nsAttrValue& aResult) { 33 if (aNamespaceID == kNameSpaceID_None) { 34 if (aAttribute == nsGkAtoms::size) { 35 int32_t size = nsContentUtils::ParseLegacyFontSize(aValue); 36 if (size) { 37 aResult.SetTo(size, &aValue); 38 return true; 39 } 40 return false; 41 } 42 if (aAttribute == nsGkAtoms::color) { 43 return aResult.ParseColor(aValue); 44 } 45 } 46 47 return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue, 48 aMaybeScriptedPrincipal, aResult); 49 } 50 51 void HTMLFontElement::MapAttributesIntoRule( 52 MappedDeclarationsBuilder& aBuilder) { 53 // face: string list 54 if (!aBuilder.PropertyIsSet(eCSSProperty_font_family)) { 55 const nsAttrValue* value = aBuilder.GetAttr(nsGkAtoms::face); 56 if (value && 57 (value->Type() == nsAttrValue::eString || 58 value->Type() == nsAttrValue::eAtom) && 59 !value->IsEmptyString()) { 60 aBuilder.SetFontFamily( 61 NS_ConvertUTF16toUTF8(nsAttrValueOrString(value).String())); 62 } 63 } 64 // size: int 65 if (!aBuilder.PropertyIsSet(eCSSProperty_font_size)) { 66 const nsAttrValue* value = aBuilder.GetAttr(nsGkAtoms::size); 67 if (value && value->Type() == nsAttrValue::eInteger) { 68 aBuilder.SetKeywordValue(eCSSProperty_font_size, 69 value->GetIntegerValue()); 70 } 71 } 72 if (!aBuilder.PropertyIsSet(eCSSProperty_color)) { 73 // color: color 74 const nsAttrValue* value = aBuilder.GetAttr(nsGkAtoms::color); 75 nscolor color; 76 if (value && value->GetColorValue(color)) { 77 aBuilder.SetColorValue(eCSSProperty_color, color); 78 } 79 } 80 if (aBuilder.Document().GetCompatibilityMode() == eCompatibility_NavQuirks) { 81 // Make <a><font color="red">text</font></a> give the text a red underline 82 // in quirks mode. The StyleTextDecorationLine_COLOR_OVERRIDE flag only 83 // affects quirks mode rendering. 84 const nsAttrValue* value = aBuilder.GetAttr(nsGkAtoms::color); 85 nscolor color; 86 if (value && value->GetColorValue(color)) { 87 aBuilder.SetTextDecorationColorOverride(); 88 } 89 } 90 91 nsGenericHTMLElement::MapCommonAttributesInto(aBuilder); 92 } 93 94 NS_IMETHODIMP_(bool) 95 HTMLFontElement::IsAttributeMapped(const nsAtom* aAttribute) const { 96 static const MappedAttributeEntry attributes[] = { 97 {nsGkAtoms::face}, {nsGkAtoms::size}, {nsGkAtoms::color}, {nullptr}}; 98 99 static const MappedAttributeEntry* const map[] = { 100 attributes, 101 sCommonAttributeMap, 102 }; 103 104 return FindAttributeDependence(aAttribute, map); 105 } 106 107 nsMapRuleToAttributesFunc HTMLFontElement::GetAttributeMappingFunction() const { 108 return &MapAttributesIntoRule; 109 } 110 111 } // namespace mozilla::dom