27 Commits
Author SHA1 Message Date
Chris (ChrisJr404) 91ed06d7c5 Add top-level Text function for BeautifulSoup-style text extraction
Adds goquery.Text(s, *TextOptions), a package-level counterpart to the
Selection.Text method that gives control over how the text of distinct
text nodes is joined (Separator), whether each node is trimmed of
surrounding whitespace (Trim), and which text nodes are included (Keep).
This covers the common need to extract clean, readable text from a
document - for example joining fragments with a space and dropping the
text of script/style elements - without hand-rolling a node walk.

Passing a nil TextOptions keeps the behaviour identical to Selection.Text.
Includes tests and a runnable example.
2026-08-18 08:54:59 -04:00
jvoisin 9285d9bf28 perf: presize set in appendWithoutDuplicates to final expected size
BenchmarkAddNodesBig (DocW().Find("li") duplicated to ~1500 nodes,
then AddNodes onto an empty selection), -count=20:

                 sec/op             B/op           allocs/op
    before    214.3µs ±12%     27.58Ki ±0%       24
    after     104.8µs ±24%     45.24Ki ±0%       16
                  -51%             +64%          -33%

The B/op increase is the cost of presizing the map to its final
capacity in one shot rather than letting the runtime grow it
through smaller bucket arrays (which are freed but counted in
total bytes allocated). Peak resident memory is comparable.
2026-05-29 22:02:39 +02:00
Saddam Azy 3951e60495 increase performance by migrating from bytes.Buffer to strings.Builder 2025-02-27 08:48:50 +07:00
Mitar 93ca487049 There is no InnerHtml. 2022-04-23 14:34:58 -07:00
Martin Angers 7a43c97117 Make documentation more consistent 2021-10-25 20:31:05 -04:00
Martin Angers b6dc0bd0ea Fix some small linting issues 2021-10-25 20:16:05 -04:00
Anthony Gedeon d35d4cec3e fix(doc): update docs for accurate representation of function 2021-10-25 18:16:30 -05:00
Anthony Gedeon 98eb88c22b Fixes PuerkitoBio#377, renders the html that can be manipulated with an writer as input 2021-10-24 21:08:06 -05:00
thiemok d89086ead1 fix(manipulation): address issues from code review of #235
* SetHml remains unchanged, see comment on the isParent param of
eachNodeHtml
* Removed unused functions
* Added documentation on eachNodeHtml
* Fixed node parsing cache keys and added test
* Refactored some tests for clarity
2020-09-28 15:37:51 +02:00
Martin Angers 5dccaa115b optimize winnowNodes and ClosestNodes in the same way 2016-08-28 11:55:36 -04:00
Martin Angers 26f4f54c91 simplify string search expressions, remove unused code 2016-08-28 11:09:29 -04:00
Martin Angers 6841d28c6f comment and explain the appendWithoutDuplicates logic 2016-08-27 22:29:53 -04:00
Martin Angers 7bd630023a reuse the deduplication set map in mapNodes 2016-08-27 22:25:02 -04:00
Martin Angers c7533ba696 maintain a set of existing target nodes to deduplicate
The old implementation used nested loops to check if one of the
new nodes was already in the existing nodes. This commit
maintains a map[*html.Node]bool so that a single loop over
the new elements is required. This is slower for small
slices though, and allocates more. The next step is for
appendWithoutDuplicates to accept a map as argument so
that the set is not recreated on each call from mapNodes.
2016-08-27 18:55:21 -04:00
Martin Angers 81e6517758 doc: fix list for godoc 2016-02-02 12:40:27 -05:00
Martin Angers b0437209ef add NodeName and OuterHtml utility functions 2016-02-02 12:28:50 -05:00
Martin Angers 040cf98281 change paths to golang.org/x/, fails to build because cascadia has to switch too 2014-11-05 20:30:33 -05:00
Martin Angers 2c6687f6d3 lint 2014-04-11 12:00:17 -04:00
Dobrosław Żybort 474b169363 Update exp/html import paths 2013-02-12 20:32:16 +01:00
Martin Angers ab7dea3da3 update to use new exp/html package 2012-09-09 18:44:47 -04:00
Martin Angers 6e1e76af6b refactor Document to make Root a Selection, with all API available 2012-09-02 14:45:50 -04:00
Martin Angers f1e54e5311 add Add() and AndSelf(), with tests 2012-08-31 10:27:45 -04:00
Martin Angers 437a1dc2a1 work on filter.go, needs tests now 2012-08-30 22:47:12 -04:00
Martin Angers de74d0bf97 reorganize code in what seems like a sane structure 2012-08-30 17:35:41 -04:00
Martin Angers 85ad689f6a add Index() methods 2012-08-30 17:24:18 -04:00
Martin Angers 2db07845a9 rework some internals, similar to jQuery 2012-08-30 14:36:36 -04:00
Martin Angers 07f9164340 start documenting, add Contains and Has 2012-08-30 12:16:33 -04:00