Luke Briggs

Added ability to search for all matches of a given term through the text within an element

1 +using System;
2 +using System.Collections;
3 +using System.Collections.Generic;
4 +using Dom;
5 +
6 +
7 +namespace PowerUI{
8 +
9 + public partial class HtmlElement{
10 +
11 + /// <summary>
12 + /// Searches for the given term within this element.
13 + /// </summary>
14 + public List<SearchResult> searchFor(string term){
15 + List<SearchResult> results = new List<SearchResult>();
16 +
17 + // For each text node.. (using anElement.allText, the convenience iterator)
18 + foreach(TextNode text in allText){
19 +
20 + // Any of the standard TextNode attribs are available, like TextNode.data:
21 + var textContent = text.data;
22 +
23 + // Following loop is from https://stackoverflow.com/questions/2641326/finding-all-positions-of-substring-in-a-larger-string-in-c-sharp
24 + for (int index = 0;; index += term.Length) {
25 + index = textContent.IndexOf(term, index);
26 + if (index == -1) {
27 + // Aren't any more
28 + break;
29 + }
30 +
31 + // Got a match - add it as a search result:
32 + results.Add(new SearchResult(text, index));
33 + }
34 + }
35 +
36 + return results;
37 + }
38 +
39 + }
40 +
41 + /// <summary>
42 + /// Represents a search result found by HtmlElement.searchFor.
43 + /// </summary>
44 + public class SearchResult{
45 + public TextNode Node;
46 + public int Index;
47 +
48 + public SearchResult(TextNode node, int index){
49 + Node = node;
50 + Index = index;
51 + }
52 + }
53 +
54 +}
...\ No newline at end of file ...\ No newline at end of file
1 +fileFormatVersion: 2
2 +guid: 6dd1095a6f8ce524fbdb5fab8177a78a
3 +timeCreated: 1521854532
4 +licenseType: Free
5 +MonoImporter:
6 + serializedVersion: 2
7 + defaultReferences: []
8 + executionOrder: 0
9 + icon: {instanceID: 0}
10 + userData:
11 + assetBundleName:
12 + assetBundleVariant: