145
145
#include " StyleTreeResolver.h"
146
146
#include " TextIterator.h"
147
147
#include " TouchAction.h"
148
+ #include " TrustedType.h"
148
149
#include " TypedElementDescendantIteratorInlines.h"
149
150
#include " VisibilityAdjustment.h"
150
151
#include " VoidCallback.h"
@@ -2000,7 +2001,31 @@ ExceptionOr<bool> Element::toggleAttribute(const AtomString& qualifiedName, std:
2000
2001
return true ;
2001
2002
}
2002
2003
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)
2004
2029
{
2005
2030
if (!Document::isValidName (qualifiedName))
2006
2031
return Exception { ExceptionCode::InvalidCharacterError, makeString (" Invalid qualified name: '" , qualifiedName, " '" ) };
@@ -2009,8 +2034,18 @@ ExceptionOr<void> Element::setAttribute(const AtomString& qualifiedName, const A
2009
2034
auto caseAdjustedQualifiedName = shouldIgnoreAttributeCase (*this ) ? qualifiedName.convertToASCIILowercase () : qualifiedName;
2010
2035
unsigned index = elementData () ? elementData ()->findAttributeIndexByName (caseAdjustedQualifiedName, false ) : ElementData::attributeNotFound;
2011
2036
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);
2013
2043
2044
+ if (attributeValue.hasException ())
2045
+ return attributeValue.releaseException ();
2046
+
2047
+ setAttributeInternal (index, name, AtomString (attributeValue.releaseReturnValue ()), InSynchronizationOfLazyAttribute::No);
2048
+ }
2014
2049
return { };
2015
2050
}
2016
2051
@@ -2314,7 +2349,7 @@ void Element::setElementsArrayAttribute(const QualifiedName& attributeName, std:
2314
2349
2315
2350
auto newElements = copyToVectorOf<WeakPtr<Element, WeakPtrImplWithEventTargetData>>(*elements);
2316
2351
explicitlySetAttrElementsMap ().set (attributeName, WTFMove (newElements));
2317
-
2352
+
2318
2353
if (CheckedPtr cache = document ().existingAXObjectCache ()) {
2319
2354
for (auto element : elements.value ()) {
2320
2355
// FIXME: Should this pass `element` instead of `*this`?
@@ -3227,7 +3262,7 @@ static void appendAttributes(StringBuilder& builder, const Element& element)
3227
3262
classNamesToDump = maxNumClassNames;
3228
3263
addEllipsis = true ;
3229
3264
}
3230
-
3265
+
3231
3266
for (size_t i = 0 ; i < classNamesToDump; ++i) {
3232
3267
if (i > 0 )
3233
3268
builder.append (' ' );
@@ -3395,12 +3430,23 @@ ExceptionOr<QualifiedName> Element::parseAttributeName(const AtomString& namespa
3395
3430
return parsedAttributeName;
3396
3431
}
3397
3432
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)
3399
3434
{
3400
3435
auto result = parseAttributeName (namespaceURI, qualifiedName);
3401
3436
if (result.hasException ())
3402
3437
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
+
3404
3450
return { };
3405
3451
}
3406
3452
0 commit comments