Scoped HTML text extraction under an explicit inclusion policy, distinct from rendered visible text or accessibility output.
Substantively revised October 21, 2026. Author interpretation, documented cases and hypothetical examples are distinguished below.
Removing angle-bracketed tags can leave a script's words in the supposed article passage. Collecting every text event can add a sidebar. Adding a space between every event can split one word in two. An output that looks like plain text can therefore encode several incompatible ideas of what the source passage contains.
Direct answer
Define the passage boundary and text-inclusion policy before extracting HTML. Distinguish source text, a parser's selected output and the text actually presented by a browser. Check entity handling, script and style exclusion, inline adjacency, explicit line boundaries and material outside the selected passage. A controlled parser experiment can verify its own stated policy; it cannot establish computed visibility, an accessibility tree or a general browser rendering result. Preserve the source and identify which representation the resulting quotation or analysis actually uses.
Key takeaways
Plain-looking output is not a uniquely defined extraction target. Source markup contains structural information, character references and content with different roles. A tool needs an explicit answer to what should be included, where the passage begins and ends, and how the selected pieces should be joined.
Inline boundaries and paragraph boundaries need different treatment. Two data events can be parts of one word, while a paragraph or deliberate break may require a separator. Joining every event with a space and joining every event with nothing can each create errors under different conditions.
A source-oriented extraction can deliberately retain hidden text while excluding scripts and styles. That is a policy choice, not proof that the retained text is visible to a user. Record the intended relationship and verify the actual consumer separately when the claim depends on rendering.
Definitions and relationships
Source HTML is the serialized markup received or retained for examination. A parser exposes a representation or events derived from that source. An extraction policy chooses a region, includes or excludes content and decides how to combine the selected text into an output stream.
A rendered presentation depends on more than source text. Document structure, styling and the actual consuming environment can affect what is displayed. A textual extractor that has not evaluated those conditions should not describe its output as the exact words a user sees.
A quotation also depends on an identifiable source and extraction basis. If whitespace or content is deliberately transformed, record that relationship. The result may be useful for search or analysis without being a byte-level transcript of source markup or a measured visible-text capture.
The problem with a passage assembled from unrelated content
Imagine a hypothetical researcher capturing the paragraph of a web article that supports a claim. A script removes tags from the entire document and returns a long string. The relevant sentence appears, so the researcher assumes the capture succeeded and stores the result as the passage.
The string may also include navigation, hidden explanatory text, a sidebar or script data. Later analysis can treat those additions as if the article author presented them together in one passage. The presence of the desired sentence concealed the absence of a clear inclusion boundary.
The practical question is what qualifies each output character for inclusion. A passage extractor should answer that question more precisely than everything left after tags disappear. Establish the selected region and text policy before using the result as evidence for a claim about the source.
Root cause: markup removal is mistaken for interpretation
Tags are visually distinctive, so removing them feels like separating content from machinery. That intuition fails because content roles are not determined simply by whether a character lies between angle brackets. Script and style data can occur between start and end tags, while character references inside prose need interpretation to produce the intended text.
The same problem appears at the joining stage. Parser callbacks divide input according to processing boundaries, not necessarily word or sentence boundaries. A bold element can wrap the second half of a word. A break element can separate lines without contributing an ordinary data string.
The repair is to treat extraction as an authored transformation with testable rules. Select the intended region, define the exclusions and joining behavior, and inspect examples that expose each decision. A recognizable output is insufficient when several different policies produce plausible but materially different passages.
Framework: the Passage Extraction Contract
The Passage Extraction Contract is the author's proposed inspection method. Its components are Region, Content Roles, Joining Rules and Claimed Representation. It is a way to make an extraction decision reviewable, not a universal HTML-to-text algorithm or a browser conformance procedure.
Region specifies where the intended passage comes from. In a controlled fixture, that can be an article element with a known identifier. In a real workflow, use a supported selector and inspect whether it identifies the desired source region. A successful match should not silently include unrelated navigation or a neighboring article.
Content Roles states which kinds of source content belong in the output. Scripts, styles, comments and visible prose have different roles. A source-analysis task may deliberately include material excluded by a visible-text task. Make the choice explicit rather than presenting one convenient collection method as the definition of content.
Joining Rules defines how selected text fragments relate. Preserve inline adjacency where it matters, and introduce separators for the block or break boundaries the policy recognizes. Do not infer a word boundary merely because a parser delivered two callbacks.
Claimed Representation describes what the result establishes. Is it a policy-based source excerpt, a DOM property value, a rendered-text observation or something else? Record the actual implementation and check. An unperformed browser test cannot be supplied by renaming a Python string as visible text.
Applying the contract to an article excerpt
Start with a preserved authorized source and identify the smallest region needed for the research question. State the output's purpose. Search indexing, exact quotation and review of the user-visible presentation can require different evidence even when they begin from the same document.
Construct a short example containing prose with an entity, inline markup within a word, a paragraph boundary, a deliberate break and content that should be excluded. Include a neighboring region so the selector's boundary is tested, rather than evaluating exclusions only within an already isolated paragraph.
Then compare the exact output with the written policy. If hidden source text is included, label the result accordingly. If the intended claim concerns what a reader sees, evaluate the actual consuming environment rather than treating the source extractor's output as evidence of styling or presentation behavior it did not inspect.
Documented example: Python's parser exposes events
Python's HTMLParser documentation describes handler callbacks for tags and data, character-reference conversion and limitations around matching or implicit end tags. Those features support an event-based controlled exercise, but they do not turn a small callback collector into a complete browser document builder. [S1]
Version detail matters here. The consulted Python 3.11 documentation includes a scripting parameter added in a later maintenance release. The actual experiment ran Python 3.11.9 and used convert_charrefs only. It does not claim to execute an option unavailable in that recorded runtime.
The useful distinction is between an event interface and the extraction policy built on it. The library supplies events; the author chooses which region and roles to include and how to join them. Those choices remain the subject of the test rather than becoming invisible assumptions inside the word parser.
Documented example: HTML parsing includes tree construction
The HTML Standard describes tokenization followed by tree construction, with a document model as the output of the parsing process. That model supplies context for why a list of source events should not automatically be treated as the document a browser constructs. [S2]
The local experiment does not implement those complete algorithms. Its region tracking is deliberately limited to a balanced invented article and listed void elements. It is a specimen for explaining one text policy, not a production DOM implementation or a promise of recovery for arbitrary malformed markup.
A separate event example records that no implicit paragraph-end callbacks are emitted for its selected incomplete source. That observation belongs to the actual Python callback interface. The article does not manufacture a browser comparison or claim that this one case characterizes every difference between processing engines.
Documented example: textContent is not rendered visibility
MDN distinguishes textContent from innerText, including the treatment of script, style and hidden content. The distinction is relevant because a phrase such as get the text can refer to materially different representations. Choosing a familiar property name does not remove the need to state what is being observed. [S3]
This reference is documentation evidence only. The research below does not execute either browser property. Its custom selected-text policy differs from an unfiltered text collector and should not be labeled as a measured textContent or innerText implementation.
The operational lesson is to match the tool to the claim. If the research depends on a rendered presentation, a source-only extractor leaves a missing observation. If the task intentionally studies source content, retain that scope and explain why material hidden in a presentation may still belong to the chosen source analysis.
Case study: one article, six policy lines
The author created balanced invented markup containing one article with a known identifier. Its heading contained Alpha & Beta. A paragraph split Start here. around an emphasis element. Another paragraph carried the hidden attribute and the invented words Hidden note. A later paragraph contained a deliberate break between Second and line.
The specimen also split tokenization across plain text and a strong element, included script and style data, added a comment and placed an Outside label in a neighboring aside. These are synthetic strings, not a captured publisher page, a real quotation or a report about an application's current rendering.
The selected policy captured only the identified article, excluded script and style content, ignored comments and inserted separators for its listed block and break boundaries. It deliberately retained hidden source text. The resulting six lines were Alpha & Beta, Start here., Hidden note, Second, line. and tokenization.
The exact result matters. The entity became the intended ampersand, the inline emphasis did not destroy the existing space, and the divided word did not acquire a new space. The policy's output is an inspectable transformation, while its retained hidden text makes clear that it is not a claim about visible presentation.
What the twenty-five assertions establish
The Python 3.11.9 report records twenty-five passing assertions. They cover the exact six-line output, completion of the controlled article tracking, entity conversion, exclusions, retained hidden text, inline adjacency, a deliberate break and a comparison with an unfiltered data collector.
The unfiltered collector includes script data, style data, hidden text and the neighboring Outside text. A simple tag-removal expression leaves both the entity spelling and script text. Those outcomes demonstrate different extraction policies on the specimen; they are not claims that every such technique produces the same result on all HTML.
The controlled extractor also returns the same fixture output when fed seven-character chunks. Empty and missing targets produce empty text. The separate incomplete paragraph example produces no end-tag callbacks. There is no actual browser, CSS evaluation, accessibility-tree inspection, general sanitizer or full HTML conformance test in this research.
Failure example: inserting spaces at every callback
An operator may try to improve a text collector by joining every received data fragment with a space. That avoids some accidental concatenation, but it can invent a word boundary where the source did not contain one. The synthetic tokenization example makes this error visible without relying on a long document.
The source places token outside a strong element and ization inside it. The intended inline sequence is tokenization. A universal callback separator can create token ization instead. The parser's division of events did not authorize the extractor to change the word.
The correction is a joining policy tied to the relevant structure and purpose. Preserve inline adjacency where appropriate, and create explicit separators for selected block or break boundaries. A small exact-output fixture is more informative than a vague check that the result contains mostly readable words.
Failure example: calling retained hidden text visible
The policy deliberately keeps Hidden note even though the source carries a hidden attribute. This is useful as a negative control on the scope of the claim. The experiment can verify that the string was included, but it has not observed a user-facing presentation that displays it.
Renaming that output visible text would therefore create an unsupported conclusion without changing any experiment result. The problem would be the label assigned to the evidence, rather than a failed assertion. Stronger claims require observations that actually cover rendering and the chosen consumer.
For a real project, decide whether the text belongs to the research purpose. If it does, record that source-analysis policy. If the task requires what a reader can perceive in an actual presentation, use an appropriate observation and keep it separate from the source collector's inclusion decisions.
Why a useful excerpt is still a transformation
The policy adds line separators and trims selected boundary whitespace. It does not preserve every source byte. That can be appropriate for a searchable excerpt, but the relationship should remain visible when the result is later quoted or compared with the source.
An editor who needs exact source fidelity should retain the original markup or another appropriate artifact alongside the derived text. A reference to the selected region helps explain where the excerpt came from, while a recorded policy explains why its characters and boundaries differ from the serialization.
The extractor also cannot establish permission to reproduce the text or the truth of the captured claim. Those questions remain separate. The experiment's useful achievement is a precise account of what its source-selection policy includes and changes, not a universal approval of the resulting passage for every use.
Common mistakes and specific corrections
The first mistake is stripping tags from the whole document before deciding what passage is relevant. Select the intended region and test a neighboring-region exclusion. Finding the desired sentence somewhere in a large output does not establish an appropriate capture boundary.
The second is treating every data callback as visible prose. Define content roles and inspect script and style handling. An event stream can contain text that is not part of the article passage the researcher meant to capture.
The third is equating a source-oriented policy with a browser property. State the actual implementation and retain unperformed presentation checks as unperformed. Documentation about an API explains a distinction; it is not evidence that the local research executed that API.
The fourth is describing a small balanced-markup tracker as a production parser. Keep its restrictions explicit. Arbitrary nesting, malformed markup, script behavior and full browser tree construction require more than the limited fixture demonstrates.
Counterargument: a browser already knows which text is visible
For a task about a real presentation, observing the actual browser can be the appropriate route. That counterargument correctly challenges attempts to infer rendered behavior from source alone. It does not make every source-oriented extraction task meaningless or imply that one property supplies every research requirement.
The first decision remains the representation the claim needs. A source-analysis task may intentionally include content not presently displayed, while a presentation review needs observations of the chosen environment. These tasks can share a source but require different inclusion policies and acceptance evidence.
There is a cost to retaining both source and derived text, along with the extraction policy. The benefit is that later reviewers can understand what changed and which assumptions produced the captured passage. Choose the level of documentation according to the use, without allowing a convenient plain string to obscure its origin.
Action plan
Identify one authorized passage-capture workflow and state its purpose: source analysis, selected prose, exact quotation or presentation review. Preserve the source needed to investigate the relationship. Decide which representation the final claim should describe.
Write the passage boundary, content-role choices and joining rules in concrete terms. Build a small specimen with an entity, inline word fragments, paragraph and break boundaries, script or style data and outside content. Compare exact output against the intended policy.
Inspect the actual parser version and avoid assuming features from later maintenance documentation are present. If the requirement concerns rendering, perform an appropriate consumer check and record it independently. Do not promote a source callback test into a presentation or accessibility result.
Retain the selected source reference, derived text and observed limitations with the accepted output. This makes the excerpt explainable when a later reader asks why it includes a phrase, omits a neighboring region or introduces a line boundary absent from the literal markup.
FAQ
Does removing HTML tags produce the visible article text?
Not necessarily. The specimen's simple tag removal retains script text and an entity spelling. Visible presentation and a source transformation are different questions.
Why select a region before collecting text?
It limits which source material can contribute to the passage. The neighboring aside control shows whether the extractor accidentally adds content outside the intended article.
Does every parser data callback contain article prose?
No. The unfiltered collector receives script and style data in this specimen. Inclusion requires an explicit content policy rather than indiscriminate collection.
Why not put spaces between all collected fragments?
Some inline fragments are parts of one word. The tokenization specimen would become token ization under a universal separator rule, changing the intended text.
Why not concatenate everything without separators?
The policy may need paragraph or explicit break boundaries. Decide how those structures should contribute rather than applying one joining operation to every event.
What happened to the ampersand reference?
The tested parser configuration converted it into an ampersand in the extracted heading. The report distinguishes that result from the raw source spelling.
Why did the policy retain Hidden note?
It deliberately follows a source-content policy without evaluating visibility. Its inclusion is a control demonstrating why the output cannot be called measured visible text.
Did the test run textContent or innerText?
No. Those APIs are discussed through documentation. The actual experiment uses Python event callbacks and a custom policy on invented markup.
Did it build the document using the complete HTML algorithm?
No. It is a limited balanced-fixture tracker, not a full browser tree builder. The HTML Standard supplies conceptual context rather than an executed conformance result.
Which Python constructor options were actually used?
The Python 3.11.9 exercise uses convert_charrefs. It deliberately does not use the scripting parameter added in a later Python 3.11 maintenance release.
Did seven-character input chunks change the output?
Not for the selected fixture. The report checks that chunked feeding reproduces the same expected text. That is a bounded streaming-input observation.
What happens when the selected article is missing?
The fixture returns empty text. A real workflow should distinguish a missing match from a meaningful empty passage rather than silently treating either as accepted evidence.
Does closing the event parser validate all markup?
No. The separate incomplete paragraph specimen emits no implicit end callbacks. The article makes no claim that the event interface validates or fully repairs arbitrary markup.
Does extraction establish reuse permission or factual accuracy?
No. Those require evidence appropriate to the source and intended use. This experiment concerns the relationship between a selected source and a derived text stream.
What should accompany the accepted excerpt?
Record its source region, extraction policy, actual tool version, expected specimen and observed result. State any transformation and keep rendering or accessibility claims separate unless tested.
Recommended books
Learning XML, second edition, by Erik T. Ray, published by O'Reilly in September 2003, supplies foundational context on structured markup, text and document models. Its relevance is conceptual; HTML's current processing rules and the actual Python interface remain the technical references for this exercise.
Designing Interfaces, third edition, by Jenifer Tidwell, Charles Brewer and Aynne Valencia, provides context for distinguishing implementation data from what a user experiences. That distinction helps frame why a source excerpt cannot stand in for a presentation observation. The book is not evidence that any browser or assistive technology was tested here.
The Design of Everyday Things, revised and expanded edition, by Don Norman, published in 2013, offers context on interpreting feedback and observable outcomes. A readable extraction can create confidence beyond what its evidence supports. The Passage Extraction Contract remains the author's proposed method rather than a framework attributed to this book.
Further exploration
Take one saved passage and ask exactly how each character reached the output. Identify its source region, exclusions, entity handling and joining decisions. A useful excerpt should have an explainable relationship to the material it is used to represent.
Then test the smallest source that separates a correct capture from a plausible wrong one. An inline word fragment, hidden sentence or neighboring sidebar can expose the assumption more clearly than another large page. Record the representation actually checked before deciding what the captured passage proves.
Primary evidence sources
- [S1] convert_charrefs behavior, script/style data examples and unmatched-end-tag limitation actually read
- [S2] Tokenizer followed by tree-construction document model actually read
- [S3] textContent includes script/style and hidden text unlike innerText actually read
Related concepts: systems thinking, wealth structure, and the survival loop. Read the editorial policy and citation guide.