145145#include " StyleTreeResolver.h"
146146#include " TextIterator.h"
147147#include " TouchAction.h"
148+ #include " TrustedType.h"
148149#include " TypedElementDescendantIteratorInlines.h"
149150#include " VisibilityAdjustment.h"
150151#include " VoidCallback.h"
@@ -2000,7 +2001,31 @@ ExceptionOr<bool> Element::toggleAttribute(const AtomString& qualifiedName, std:
20002001 return true ;
20012002}
20022003
2003- ExceptionOr<void > Element::setAttribute (const AtomString& qualifiedName, const AtomString& value)
2004+ static ExceptionOr<String> getTrustedTypesCompliantAttributeValue (const String& attributeType, const TrustedTypeOrString& value, Element* element, String sink)
2005+ {
2006+ auto stringValueHolder = WTF::switchOn (value,
2007+ [&](const String& str) -> ExceptionOr<String> {
2008+ if (attributeType.isNull ())
2009+ return String (str);
2010+ return trustedTypeCompliantString (stringToTrustedType (attributeType), *(element->document ().scriptExecutionContext ()), str, sink);
2011+ },
2012+ [](const RefPtr<TrustedHTML>& trustedHTML) -> ExceptionOr<String> {
2013+ return trustedHTML->toString ();
2014+ },
2015+ [](const RefPtr<TrustedScript>& trustedScript) -> ExceptionOr<String> {
2016+ return trustedScript->toString ();
2017+ },
2018+ [](const RefPtr<TrustedScriptURL>& trustedScriptURL) -> ExceptionOr<String> {
2019+ return trustedScriptURL->toString ();
2020+ }
2021+ );
2022+ if (stringValueHolder.hasException ())
2023+ return stringValueHolder.releaseException ();
2024+
2025+ return stringValueHolder.releaseReturnValue ();
2026+ }
2027+
2028+ ExceptionOr<void > Element::setAttribute (const AtomString& qualifiedName, const TrustedTypeOrString& value)
20042029{
20052030 if (!Document::isValidName (qualifiedName))
20062031 return Exception { ExceptionCode::InvalidCharacterError, makeString (" Invalid qualified name: '" , qualifiedName, " '" ) };
@@ -2009,8 +2034,18 @@ ExceptionOr<void> Element::setAttribute(const AtomString& qualifiedName, const A
20092034 auto caseAdjustedQualifiedName = shouldIgnoreAttributeCase (*this ) ? qualifiedName.convertToASCIILowercase () : qualifiedName;
20102035 unsigned index = elementData () ? elementData ()->findAttributeIndexByName (caseAdjustedQualifiedName, false ) : ElementData::attributeNotFound;
20112036 auto name = index != ElementData::attributeNotFound ? attributeAt (index).name () : QualifiedName { nullAtom (), caseAdjustedQualifiedName, nullAtom () };
2012- setAttributeInternal (index, name, value, InSynchronizationOfLazyAttribute::No);
2037+ if (!document ().scriptExecutionContext ()->settingsValues ().trustedTypesEnabled )
2038+ setAttributeInternal (index, name, std::get<AtomString>(value), InSynchronizationOfLazyAttribute::No);
2039+ else {
2040+ auto sink = nullString ();
2041+ String attributeType = getTrustedTypeForAttribute (name.localName (), getAttribute (name), " " _s, " " _s);
2042+ auto attributeValue = getTrustedTypesCompliantAttributeValue (attributeType, value, this , " Element setAttribute" _s);
20132043
2044+ if (attributeValue.hasException ())
2045+ return attributeValue.releaseException ();
2046+
2047+ setAttributeInternal (index, name, AtomString (attributeValue.releaseReturnValue ()), InSynchronizationOfLazyAttribute::No);
2048+ }
20142049 return { };
20152050}
20162051
@@ -2314,7 +2349,7 @@ void Element::setElementsArrayAttribute(const QualifiedName& attributeName, std:
23142349
23152350 auto newElements = copyToVectorOf<WeakPtr<Element, WeakPtrImplWithEventTargetData>>(*elements);
23162351 explicitlySetAttrElementsMap ().set (attributeName, WTFMove (newElements));
2317-
2352+
23182353 if (CheckedPtr cache = document ().existingAXObjectCache ()) {
23192354 for (auto element : elements.value ()) {
23202355 // FIXME: Should this pass `element` instead of `*this`?
@@ -3227,7 +3262,7 @@ static void appendAttributes(StringBuilder& builder, const Element& element)
32273262 classNamesToDump = maxNumClassNames;
32283263 addEllipsis = true ;
32293264 }
3230-
3265+
32313266 for (size_t i = 0 ; i < classNamesToDump; ++i) {
32323267 if (i > 0 )
32333268 builder.append (' ' );
@@ -3395,12 +3430,23 @@ ExceptionOr<QualifiedName> Element::parseAttributeName(const AtomString& namespa
33953430 return parsedAttributeName;
33963431}
33973432
3398- ExceptionOr<void > Element::setAttributeNS (const AtomString& namespaceURI, const AtomString& qualifiedName, const AtomString & value)
3433+ ExceptionOr<void > Element::setAttributeNS (const AtomString& namespaceURI, const AtomString& qualifiedName, const TrustedTypeOrString & value)
33993434{
34003435 auto result = parseAttributeName (namespaceURI, qualifiedName);
34013436 if (result.hasException ())
34023437 return result.releaseException ();
3403- setAttribute (result.releaseReturnValue (), value);
3438+ if (!document ().scriptExecutionContext ()->settingsValues ().trustedTypesEnabled )
3439+ setAttribute (result.releaseReturnValue (), std::get<AtomString>(value));
3440+ else {
3441+ String attributeType = getTrustedTypeForAttribute (qualifiedName, getAttribute (qualifiedName), " " _s, namespaceURI);
3442+ auto attributeValue = getTrustedTypesCompliantAttributeValue (attributeType, value, this , " Element setAttributeNS" _s);
3443+
3444+ if (attributeValue.hasException ())
3445+ return attributeValue.releaseException ();
3446+
3447+ setAttribute (result.releaseReturnValue (), AtomString (attributeValue.releaseReturnValue ()));
3448+ }
3449+
34043450 return { };
34053451}
34063452
0 commit comments