Deduplication of optional CSS rules - Previously, as rules were checked for each element returned by the query in a loop, some rules got added to the final document multiple times. This commit fixes this.
Zoom zoomrmc+git@gmail.com
Wed, 20 Sep 2023 04:41:19 +0400
1 files changed,
18 insertions(+),
15 deletions(-)
jump to
M
src/hastyscribe.nim
→
src/hastyscribe.nim
@@ -10,6 +10,7 @@ tables,
httpclient, logging, critbits, + sets, ] from nimquery import querySelectorAll@@ -248,6 +249,7 @@ else:
warn "Field '" & id & "' not defined." result = result.replace(field, "") + proc load_styles(hs: var HastyScribe) = type StyleRuleMatches = array[0..1, string]@@ -278,7 +280,7 @@ for def in stylesheet_notes.findAll(peg_notestyle_def):
var matches: StyleRuleMatches discard def.match(peg_notestyle_def, matches) hs.noteStyles[matches[1].strip] = matches[0].strip - # Links -> already in `const.css_rules_links` + # Links -> already in `consts.css_rules_links` # Snippet Definition: # {{test -> My test snippet}}@@ -349,20 +351,21 @@ ## the set of "hastystyles" CSS rules and prepares a custom CSS with the
## used resources. let html = document.parseHtml() var rules: seq[string] - # Check icons - for icon in html.querySelectorAll("span[class^=fa-]"): - rules.add getTableValue(hs.iconStyles, icon.attr("class"), "Icon") - # Check badges - for badge in html.querySelectorAll("span[class^=badge-]"): - rules.add getTableValue(hs.badgeStyles, badge.attr("class"), "Badge") - # Check notes - for note in html.querySelectorAll("div.tip, div.warning, div.note, div.sidebar"): - rules.add getTableValue(hs.noteStyles, note.attr("class"), "Note") - # Check links - # Init with `document-top`: it's added to the document later with `utils.add_jump_to_top_links` - var linkHrefs: CritBitTree[void] = ["#document-top"].toCritBitTree() - for link in html.querySelectorAll("a[href]"): linkHrefs.incl(link.attr("href")) - block linkStyles: + + block icons_badges_notes: + var selSet: HashSet[string] + template fillFrom(rules: var seq[string]; t: Table[string, string]; selector, obj: string) = + for el in html.querySelectorAll(selector): selSet.incl(el.attr("class")) + for selV in selSet.items: rules.add(getTableValue(t, selV, obj)) + selSet.init() + rules.fillFrom(hs.iconStyles, "span[class^=fa-]", "Icon") # Check icons + rules.fillFrom(hs.badgeStyles, "span[class^=badge-]", "Badge") # Check badges + rules.fillFrom(hs.noteStyles, "div.tip, div.warning, div.note, div.sidebar", "Note") # Check notes + + block linkStyles: # Check links + # Init with `document-top`: it's added to the document later with `utils.add_jump_to_top_links` + var linkHrefs: CritBitTree[void] = ["#document-top"].toCritBitTree() + for link in html.querySelectorAll("a[href]"): linkHrefs.incl(link.attr("href")) var linkRulesSet: tuple[exts, doms, protos: CritBitTree[void]] for href in linkHrefs.keys: block search: