Skip to content

Commit

Permalink
Convert AppleInterchangeNewline and AppleConvertedSpace to be constan…
Browse files Browse the repository at this point in the history
…t expressions instead of macros

https://bugs.webkit.org/show_bug.cgi?id=270710
rdar://124290908

Reviewed by Abrar Rahman Protyasha and Ryosuke Niwa.

* Source/WebCore/editing/HTMLInterchange.cpp:
(WebCore::convertHTMLTextToInterchangeFormat):
* Source/WebCore/editing/HTMLInterchange.h:
* Source/WebCore/editing/ReplaceSelectionCommand.cpp:
(WebCore::isInterchangeNewlineNode):
(WebCore::isInterchangeConvertedSpaceSpan):
(WebCore::isInlineNodeWithStyle):
* Source/WebCore/editing/cocoa/HTMLConverter.mm:
(HTMLConverter::_processElement):
* Source/WebCore/editing/markup.cpp:
(WebCore::serializePreservingVisualAppearanceInternal):
(WebCore::createFragmentFromText):

Canonical link: https://commits.webkit.org/275856@main
  • Loading branch information
rr-codes committed Mar 8, 2024
1 parent bda55ec commit f2cb2e2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 17 deletions.
13 changes: 6 additions & 7 deletions Source/WebCore/editing/HTMLInterchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ String convertHTMLTextToInterchangeFormat(const String& in, const Text* node)
if (node->renderer() && node->renderer()->style().preserveNewline())
return in;

const char convertedSpaceString[] = "<span class=\"" AppleConvertedSpace "\">\xA0</span>";
static_assert((static_cast<unsigned char>('\xA0') == noBreakSpace), "ConvertedSpaceStringSpace is NoBreakSpace");
static NeverDestroyed<const String> convertedSpaceString { makeString("<span class=\"", AppleConvertedSpace, "\">", noBreakSpace, "</span>") };

StringBuilder s;

Expand All @@ -61,24 +60,24 @@ String convertHTMLTextToInterchangeFormat(const String& in, const Text* node)
unsigned add = count % 3;
switch (add) {
case 0:
s.append(convertedSpaceString, ' ', convertedSpaceString);
s.append(convertedSpaceString.get(), ' ', convertedSpaceString.get());
add = 3;
break;
case 1:
if (i == 0 || i + 1 == in.length()) // at start or end of string
s.append(convertedSpaceString);
s.append(convertedSpaceString.get());
else
s.append(' ');
break;
case 2:
if (i == 0) {
// at start of string
s.append(convertedSpaceString, ' ');
s.append(convertedSpaceString.get(), ' ');
} else if (i + 2 == in.length()) {
// at end of string
s.append(convertedSpaceString, convertedSpaceString);
s.append(convertedSpaceString.get(), convertedSpaceString.get());
} else {
s.append(convertedSpaceString, ' ');
s.append(convertedSpaceString.get(), ' ');
}
break;
}
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/editing/HTMLInterchange.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ namespace WebCore {

class Text;

#define AppleInterchangeNewline "Apple-interchange-newline"
#define AppleConvertedSpace "Apple-converted-space"
constexpr auto AppleInterchangeNewline = "Apple-interchange-newline"_s;
constexpr auto AppleConvertedSpace = "Apple-converted-space"_s;
constexpr auto WebKitMSOListQuirksStyle = "WebKit-mso-list-quirks-style"_s;

constexpr auto ApplePasteAsQuotation = "Apple-paste-as-quotation"_s;
Expand Down
8 changes: 3 additions & 5 deletions Source/WebCore/editing/ReplaceSelectionCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,14 @@ class ReplacementFragment {

static bool isInterchangeNewlineNode(const Node& node)
{
static NeverDestroyed<String> interchangeNewlineClassString = AppleInterchangeNewline ""_s;
RefPtr br = dynamicDowncast<HTMLBRElement>(node);
return br && br->attributeWithoutSynchronization(classAttr) == interchangeNewlineClassString;
return br && br->attributeWithoutSynchronization(classAttr) == AppleInterchangeNewline;
}

static bool isInterchangeConvertedSpaceSpan(const Node& node)
{
static NeverDestroyed<String> convertedSpaceSpanClassString = AppleConvertedSpace ""_s;
RefPtr element = dynamicDowncast<HTMLElement>(node);
return element && element->attributeWithoutSynchronization(classAttr) == convertedSpaceSpanClassString;
return element && element->attributeWithoutSynchronization(classAttr) == AppleConvertedSpace;
}

static Position positionAvoidingPrecedingNodes(Position position)
Expand Down Expand Up @@ -1086,7 +1084,7 @@ static bool isInlineNodeWithStyle(const Node& node)
// one of our internal classes.
const AtomString& classAttributeValue = element->attributeWithoutSynchronization(classAttr);
if (classAttributeValue == AppleTabSpanClass
|| classAttributeValue == AppleConvertedSpace ""_s
|| classAttributeValue == AppleConvertedSpace
|| classAttributeValue == ApplePasteAsQuotation)
return true;

Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/editing/cocoa/HTMLConverter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ static NSInteger _colCompare(id block1, id block2, void *)
Element* blockElement = _blockLevelElementForNode(element.parentInComposedTree());
NSString *breakClass = element.getAttribute(classAttr);
NSString *blockTag = blockElement ? (NSString *)blockElement->tagName() : nil;
BOOL isExtraBreak = [@"Apple-interchange-newline" isEqualToString:breakClass];
BOOL isExtraBreak = [AppleInterchangeNewline.createNSString() isEqualToString:breakClass];
BOOL blockElementIsParagraph = ([@"P" isEqualToString:blockTag] || [@"LI" isEqualToString:blockTag] || ([blockTag hasPrefix:@"H"] && 2 == [blockTag length]));
if (isExtraBreak)
_flags.hasTrailingNewline = YES;
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/editing/markup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ static RefPtr<Node> highestAncestorToWrapMarkup(const Position& start, const Pos
static String serializePreservingVisualAppearanceInternal(const Position& start, const Position& end, Vector<Ref<Node>>* nodes, ResolveURLs resolveURLs, SerializeComposedTree serializeComposedTree, IgnoreUserSelectNone ignoreUserSelectNone,
AnnotateForInterchange annotate, ConvertBlocksToInlines convertBlocksToInlines, StandardFontFamilySerializationMode standardFontFamilySerializationMode, MSOListMode msoListMode, PreserveBaseElement preserveBaseElement)
{
static NeverDestroyed<const String> interchangeNewlineString(MAKE_STATIC_STRING_IMPL("<br class=\"" AppleInterchangeNewline "\">"));
static NeverDestroyed<const String> interchangeNewlineString { makeString("<br class=\"", AppleInterchangeNewline, "\">") };

if (!(start < end))
return emptyString();
Expand Down Expand Up @@ -1285,7 +1285,7 @@ Ref<DocumentFragment> createFragmentFromText(const SimpleRange& context, const S

auto createHTMLBRElement = [document]() {
auto element = HTMLBRElement::create(document);
element->setAttributeWithoutSynchronization(classAttr, AppleInterchangeNewline ""_s);
element->setAttributeWithoutSynchronization(classAttr, AppleInterchangeNewline);
return element;
};

Expand Down

0 comments on commit f2cb2e2

Please sign in to comment.