HTMLPreElement.cpp (2687B)
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 "mozilla/dom/HTMLPreElement.h" 8 9 #include "mozilla/MappedDeclarationsBuilder.h" 10 #include "mozilla/dom/HTMLPreElementBinding.h" 11 #include "nsAttrValueInlines.h" 12 #include "nsGkAtoms.h" 13 #include "nsStyleConsts.h" 14 15 NS_IMPL_NS_NEW_HTML_ELEMENT(Pre) 16 17 namespace mozilla::dom { 18 19 HTMLPreElement::~HTMLPreElement() = default; 20 21 NS_IMPL_ELEMENT_CLONE(HTMLPreElement) 22 23 bool HTMLPreElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute, 24 const nsAString& aValue, 25 nsIPrincipal* aMaybeScriptedPrincipal, 26 nsAttrValue& aResult) { 27 if (aNamespaceID == kNameSpaceID_None) { 28 if (aAttribute == nsGkAtoms::width) { 29 return aResult.ParseIntValue(aValue); 30 } 31 } 32 33 return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue, 34 aMaybeScriptedPrincipal, aResult); 35 } 36 37 void HTMLPreElement::MapAttributesIntoRule( 38 MappedDeclarationsBuilder& aBuilder) { 39 // wrap: empty 40 if (aBuilder.GetAttr(nsGkAtoms::wrap)) { 41 // Equivalent to expanding `white-space: pre-wrap` 42 aBuilder.SetKeywordValue(eCSSProperty_white_space_collapse, 43 StyleWhiteSpaceCollapse::Preserve); 44 aBuilder.SetKeywordValue(eCSSProperty_text_wrap_mode, 45 StyleTextWrapMode::Wrap); 46 } 47 48 nsGenericHTMLElement::MapCommonAttributesInto(aBuilder); 49 } 50 51 NS_IMETHODIMP_(bool) 52 HTMLPreElement::IsAttributeMapped(const nsAtom* aAttribute) const { 53 if (!mNodeInfo->Equals(nsGkAtoms::pre)) { 54 return nsGenericHTMLElement::IsAttributeMapped(aAttribute); 55 } 56 57 static const MappedAttributeEntry attributes[] = { 58 {nsGkAtoms::wrap}, 59 {nullptr}, 60 }; 61 62 static const MappedAttributeEntry* const map[] = { 63 attributes, 64 sCommonAttributeMap, 65 }; 66 67 return FindAttributeDependence(aAttribute, map); 68 } 69 70 nsMapRuleToAttributesFunc HTMLPreElement::GetAttributeMappingFunction() const { 71 if (!mNodeInfo->Equals(nsGkAtoms::pre)) { 72 return nsGenericHTMLElement::GetAttributeMappingFunction(); 73 } 74 75 return &MapAttributesIntoRule; 76 } 77 78 JSObject* HTMLPreElement::WrapNode(JSContext* aCx, 79 JS::Handle<JSObject*> aGivenProto) { 80 return HTMLPreElement_Binding::Wrap(aCx, this, aGivenProto); 81 } 82 83 } // namespace mozilla::dom