@@ -3,7 +3,8 @@ use super::node::{Node, write_html_list};
33use super :: span:: SourceSpan ;
44use crate :: dom:: vecmap:: VecMap ;
55use crate :: dom:: walk:: Children ;
6- use crate :: { Attribute , VecSet } ;
6+ use crate :: { Attribute , Text , VecSet } ;
7+ use html_escape:: decode_html_entities;
78use ownable:: { IntoOwned , ToBorrowed , ToOwned } ;
89use serde:: Serialize ;
910use std:: borrow:: Cow ;
@@ -141,4 +142,37 @@ impl Element<'_> {
141142 writer. push ( '>' ) ;
142143 }
143144 }
145+
146+ /// Strip all tags.
147+ ///
148+ /// Note that html entities are not removed, only tags.
149+ /// Use [`Self::to_text`], or [`Text::decode`] on the result.
150+ pub fn strip_tags ( & self ) -> Text < ' _ > {
151+ if let & [ Node :: Text ( t) ] = & self . children . as_slice ( ) {
152+ t. to_borrowed ( )
153+ } else {
154+ let mut result = String :: new ( ) ;
155+ self . write_strip_tags ( & mut result) ;
156+ Text ( result. into ( ) )
157+ }
158+ }
159+
160+ fn write_strip_tags ( & self , writer : & mut String ) {
161+ for c in & self . children {
162+ match c {
163+ Node :: Text ( t) => writer. push_str ( t) ,
164+ Node :: Element ( e) => e. write_strip_tags ( writer) ,
165+ Node :: Comment ( _) => ( ) ,
166+ }
167+ }
168+ }
169+
170+ /// Strip tags and decode html-entities.
171+ pub fn to_text ( & self ) -> Cow < ' _ , str > {
172+ let t = self . strip_tags ( ) . 0 ;
173+ match decode_html_entities ( & t) {
174+ Cow :: Borrowed ( _) => t, // it's only returned as borrowed if the whole input is passed though
175+ Cow :: Owned ( o) => Cow :: Owned ( o) ,
176+ }
177+ }
144178}
0 commit comments