Bug 1881906 - Make `HTMLEditUtils` treat `<br>` elements always inline r=m_kato

As far as I've tested, `<br>` element is always treated as usual even if its
`display` is set to anything except `none`.  Therefore, preceding collapsible
white-spaces and `<br>` element should be treated as visible even if the
following `<br>` element is `display:block` or something.

Note that if `display:none` is specified, we should not treat it as a line
break, but our editor cannot work properly in the case and `HTMLEditUtils`
currently uses the default style of HTML elements if `display:none` is
specified.  Therefore, this patch makes `HTMLEditUtils` always treat `<br>`
as inline.

Differential Revision: https://phabricator.services.mozilla.com/D203403
This commit is contained in:
Masayuki Nakano 2024-03-05 11:47:04 +00:00
Родитель 2808903c63
Коммит e970224f24
2 изменённых файлов: 48 добавлений и 0 удалений

Просмотреть файл

@ -224,6 +224,12 @@ bool HTMLEditUtils::IsBlockElement(const nsIContent& aContent,
if (MOZ_UNLIKELY(!aContent.IsElement())) {
return false;
}
// If it's a <br>, we should always treat it as an inline element because
// its preceding collapse white-spaces and another <br> works same as usual
// even if you set its style to `display:block`.
if (aContent.IsHTMLElement(nsGkAtoms::br)) {
return false;
}
if (!StaticPrefs::editor_block_inline_check_use_computed_style() ||
aBlockInlineCheck == BlockInlineCheck::UseHTMLDefaultStyle) {
return IsHTMLBlockElementByDefault(aContent);
@ -271,6 +277,12 @@ bool HTMLEditUtils::IsInlineContent(const nsIContent& aContent,
if (!aContent.IsElement()) {
return true;
}
// If it's a <br>, we should always treat it as an inline element because
// its preceding collapse white-spaces and another <br> works same as usual
// even if you set its style to `display:block`.
if (aContent.IsHTMLElement(nsGkAtoms::br)) {
return true;
}
if (!StaticPrefs::editor_block_inline_check_use_computed_style() ||
aBlockInlineCheck == BlockInlineCheck::UseHTMLDefaultStyle) {
return !IsHTMLBlockElementByDefault(aContent);

Просмотреть файл

@ -0,0 +1,36 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Typing white-space and another character at end of a block should preserve the white-space when br element is always block</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
br { display: block; }
</style>
<script>
"use strict";
addEventListener("load", () => {
test(
() => {
const editingHost = document.querySelector("div[contenteditable]");
editingHost.focus();
getSelection().collapse(editingHost.firstChild, "abc".length);
document.execCommand("insertText", false, " ");
document.execCommand("insertText", false, "d");
assert_in_array(
editingHost.innerHTML,
[
"abc d",
"abc d<br>",
]
);
},
"Inserting white-space and a character at block end should keep the white-space even before block <br> element"
);
}, {once: true});
</script>
</head>
<body><div contenteditable>abc</div></body>
</html>