diff options
author | Luke <luke.wilde@live.co.uk> | 2020-11-11 09:46:53 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-11-11 12:15:05 +0100 |
commit | 397049aae8a0c6f849e6f03a4033e1a7990e80ce (patch) | |
tree | 3531d69a569435e8216719d92fe1bc5882dd5fd1 | |
parent | bb22b04d442b67652b8fe12619074926846f1bb4 (diff) | |
download | serenity-397049aae8a0c6f849e6f03a4033e1a7990e80ce.zip |
LibWeb: Move innerText from DOM::Element to HTML::HTMLElement
-rw-r--r-- | Libraries/LibWeb/DOM/Element.cpp | 34 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/Element.h | 3 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/Element.idl | 1 | ||||
-rw-r--r-- | Libraries/LibWeb/HTML/HTMLElement.cpp | 36 | ||||
-rw-r--r-- | Libraries/LibWeb/HTML/HTMLElement.h | 3 | ||||
-rw-r--r-- | Libraries/LibWeb/HTML/HTMLElement.idl | 1 |
6 files changed, 40 insertions, 38 deletions
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp index 11c6baa912..51b77ea9f7 100644 --- a/Libraries/LibWeb/DOM/Element.cpp +++ b/Libraries/LibWeb/DOM/Element.cpp @@ -41,7 +41,6 @@ #include <LibWeb/Layout/LayoutTableCell.h> #include <LibWeb/Layout/LayoutTableRow.h> #include <LibWeb/Layout/LayoutTableRowGroup.h> -#include <LibWeb/Layout/LayoutText.h> #include <LibWeb/Layout/LayoutTreeBuilder.h> namespace Web::DOM { @@ -296,39 +295,6 @@ String Element::inner_html() const return builder.to_string(); } -void Element::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 Element::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(); -} - bool Element::is_focused() const { return document().focused_element() == this; diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h index 19dba993e5..467ca8263e 100644 --- a/Libraries/LibWeb/DOM/Element.h +++ b/Libraries/LibWeb/DOM/Element.h @@ -93,9 +93,6 @@ public: String inner_html() const; void set_inner_html(StringView); - String inner_text(); - void set_inner_text(StringView); - bool is_focused() const; virtual bool is_focusable() const { return false; } diff --git a/Libraries/LibWeb/DOM/Element.idl b/Libraries/LibWeb/DOM/Element.idl index 009972252c..d5c4f03c9c 100644 --- a/Libraries/LibWeb/DOM/Element.idl +++ b/Libraries/LibWeb/DOM/Element.idl @@ -9,7 +9,6 @@ interface Element : Node { ArrayFromVector querySelectorAll(DOMString selectors); attribute DOMString innerHTML; - attribute DOMString innerText; [Reflect] attribute DOMString id; [Reflect=class] attribute DOMString className; diff --git a/Libraries/LibWeb/HTML/HTMLElement.cpp b/Libraries/LibWeb/HTML/HTMLElement.cpp index ae3c6c8d13..edf35366df 100644 --- a/Libraries/LibWeb/HTML/HTMLElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLElement.cpp @@ -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(); +} + } diff --git a/Libraries/LibWeb/HTML/HTMLElement.h b/Libraries/LibWeb/HTML/HTMLElement.h index 1009c0d39e..9e0e38a467 100644 --- a/Libraries/LibWeb/HTML/HTMLElement.h +++ b/Libraries/LibWeb/HTML/HTMLElement.h @@ -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; } diff --git a/Libraries/LibWeb/HTML/HTMLElement.idl b/Libraries/LibWeb/HTML/HTMLElement.idl index 4966f0af18..5df049324b 100644 --- a/Libraries/LibWeb/HTML/HTMLElement.idl +++ b/Libraries/LibWeb/HTML/HTMLElement.idl @@ -7,4 +7,5 @@ interface HTMLElement : Element { attribute DOMString contentEditable; + [LegacyNullToEmptyString] attribute DOMString innerText; } |