Removal before counting
The HTML is parsed into a document, then everything a reader never sees is dropped: scripts, styles, comments, and elements the markup itself marks as hidden (the hidden attribute or an inline display:none). Content inside <nav>, <header>, and <footer> regions is excluded next. Only the text of what remains is kept. Because the input is parsed rather than scanned as a string, tags and attributes can never leak into the total.