1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:27:35 +00:00

LibWeb: Move innerText from DOM::Element to HTML::HTMLElement

This commit is contained in:
Luke 2020-11-11 09:46:53 +00:00 committed by Andreas Kling
parent bb22b04d44
commit 397049aae8
6 changed files with 40 additions and 38 deletions

View file

@ -24,7 +24,10 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/StringBuilder.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/HTMLElement.h>
#include <LibWeb/Layout/LayoutText.h>
namespace Web::HTML {
@ -95,4 +98,37 @@ void HTMLElement::set_content_editable(const String& content_editable)
// FIXME: otherwise the attribute setter must throw a "SyntaxError" DOMException.
}
void HTMLElement::set_inner_text(StringView text)
{
remove_all_children();
append_child(document().create_text_node(text));
set_needs_style_update(true);
document().schedule_style_update();
document().invalidate_layout();
}
String HTMLElement::inner_text()
{
StringBuilder builder;
// innerText for element being rendered takes visibility into account, so force a layout and then walk the layout tree.
document().layout();
if (!layout_node())
return text_content();
Function<void(const LayoutNode&)> recurse = [&](auto& node) {
for (auto* child = node.first_child(); child; child = child->next_sibling()) {
if (child->is_text())
builder.append(downcast<LayoutText>(*child).text_for_rendering());
if (child->is_break())
builder.append('\n');
recurse(*child);
}
};
recurse(*layout_node());
return builder.to_string();
}
}

View file

@ -43,6 +43,9 @@ public:
String content_editable() const;
void set_content_editable(const String&);
String inner_text();
void set_inner_text(StringView);
private:
virtual bool is_html_element() const final { return true; }

View file

@ -7,4 +7,5 @@ interface HTMLElement : Element {
attribute DOMString contentEditable;
[LegacyNullToEmptyString] attribute DOMString innerText;
}