Skip to content

Commit

Permalink
Refactor HTMLConverter::_processText() to take a Text node
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=268446

Reviewed by Chris Dumez.

As it is only ever invoked with a Text node, might as well enshrine
that and benefit from being able to use dynamicDowncast<>.

* Source/WebCore/editing/cocoa/HTMLConverter.mm:
(HTMLConverter::_processText):
(HTMLConverter::_traverseNode):

Canonical link: https://commits.webkit.org/273887@main
  • Loading branch information
annevk committed Feb 1, 2024
1 parent d8f4d32 commit 9bc6dae
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions Source/WebCore/editing/cocoa/HTMLConverter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ + (void)document:(NSObject **)outDocument attachment:(NSTextAttachment **)outAtt
void _addTableForElement(Element* tableElement);
void _addTableCellForElement(Element* tableCellElement);
void _addMarkersToList(NSTextList *list, NSRange range);
void _processText(CharacterData&);
void _processText(Text&);
void _adjustTrailingNewline();
};

Expand Down Expand Up @@ -2033,24 +2033,24 @@ static NSInteger _colCompare(id block1, id block2, void *)
_exitBlockquote();
}

void HTMLConverter::_processText(CharacterData& characterData)
void HTMLConverter::_processText(Text& text)
{
if (m_ignoreUserSelectNoneContent && m_userSelectNoneStateCache.nodeOnlyContainsUserSelectNone(characterData))
if (m_ignoreUserSelectNoneContent && m_userSelectNoneStateCache.nodeOnlyContainsUserSelectNone(text))
return;
NSUInteger textLength = [_attrStr length];
unichar lastChar = (textLength > 0) ? [[_attrStr string] characterAtIndex:textLength - 1] : '\n';
BOOL suppressLeadingSpace = ((_flags.isSoft && lastChar == ' ') || lastChar == '\n' || lastChar == '\r' || lastChar == '\t' || lastChar == NSParagraphSeparatorCharacter || lastChar == NSLineSeparatorCharacter || lastChar == NSFormFeedCharacter || lastChar == WebNextLineCharacter);
NSRange rangeToReplace = NSMakeRange(textLength, 0);

String originalString = characterData.data();
String originalString = text.data();
unsigned startOffset = 0;
unsigned endOffset = originalString.length();
if (&characterData == m_start.containerNode()) {
if (&text == m_start.containerNode()) {
startOffset = m_start.offsetInContainerNode();
_domRangeStartIndex = [_attrStr length];
_flags.reachedStart = YES;
}
if (&characterData == m_end.containerNode()) {
if (&text == m_end.containerNode()) {
endOffset = m_end.offsetInContainerNode();
_flags.reachedEnd = YES;
}
Expand All @@ -2060,7 +2060,7 @@ static NSInteger _colCompare(id block1, id block2, void *)

// FIXME: Use RenderText's content instead.
bool wasSpace = false;
if (_caches->propertyValueForNode(characterData, CSSPropertyWhiteSpace).startsWith("pre"_s)) {
if (_caches->propertyValueForNode(text, CSSPropertyWhiteSpace).startsWith("pre"_s)) {
if (textLength && originalString.length() && _flags.isSoft) {
unichar c = originalString.characterAt(0);
if (c == '\n' || c == '\r' || c == NSParagraphSeparatorCharacter || c == NSLineSeparatorCharacter || c == NSFormFeedCharacter || c == WebNextLineCharacter)
Expand All @@ -2083,7 +2083,7 @@ static NSInteger _colCompare(id block1, id block2, void *)
builder.append(c);
else {
if (!noBreakSpaceRepresentation)
noBreakSpaceRepresentation = _caches->propertyValueForNode(characterData, CSSPropertyWebkitNbspMode) == "space"_s ? ' ' : noBreakSpace;
noBreakSpaceRepresentation = _caches->propertyValueForNode(text, CSSPropertyWebkitNbspMode) == "space"_s ? ' ' : noBreakSpace;
builder.append(noBreakSpaceRepresentation);
}
wasSpace = false;
Expand All @@ -2096,7 +2096,7 @@ static NSInteger _colCompare(id block1, id block2, void *)
}

if (outputString.length()) {
String textTransform = _caches->propertyValueForNode(characterData, CSSPropertyTextTransform);
String textTransform = _caches->propertyValueForNode(text, CSSPropertyTextTransform);
if (textTransform == "capitalize"_s)
outputString = capitalize(outputString, ' '); // FIXME: Needs to take locale into account to work correctly.
else if (textTransform == "uppercase"_s)
Expand All @@ -2107,7 +2107,7 @@ static NSInteger _colCompare(id block1, id block2, void *)
[_attrStr replaceCharactersInRange:rangeToReplace withString:outputString];
rangeToReplace.length = outputString.length();
if (rangeToReplace.length)
[_attrStr setAttributes:aggregatedAttributesForAncestors(characterData) range:rangeToReplace];
[_attrStr setAttributes:aggregatedAttributesForAncestors(text) range:rangeToReplace];
_flags.isSoft = wasSpace;
}
}
Expand Down Expand Up @@ -2171,8 +2171,8 @@ static NSInteger _colCompare(id block1, id block2, void *)
_exitElement(*element, depth, std::min(startIndex, [_attrStr length]));
}
}
} else if (node.nodeType() == Node::TEXT_NODE)
_processText(downcast<CharacterData>(node));
} else if (RefPtr text = dynamicDowncast<Text>(node))
_processText(*text);

if (isEnd)
_flags.reachedEnd = YES;
Expand Down

0 comments on commit 9bc6dae

Please sign in to comment.