From abe7b0bbbbd843b8854faf7e057e2bae0d6dc481 Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Thu, 9 Feb 2023 18:46:54 +0100 Subject: [PATCH] fix: text in achors --- application/source/template/prepare.go | 82 ++++++++++--------- .../examples/example1/original/test4.html | 12 +++ .../examples/example1/template/data.yaml | 35 +++++++- .../examples/example1/template/test1.html | 4 +- .../examples/example1/template/test2.html | 2 +- .../examples/example1/template/test3.html | 4 +- .../examples/example1/template/test4.html | 12 +++ 7 files changed, 105 insertions(+), 46 deletions(-) create mode 100644 development/examples/example1/original/test4.html create mode 100644 development/examples/example1/template/test4.html diff --git a/application/source/template/prepare.go b/application/source/template/prepare.go index 6da81c0..038bf35 100644 --- a/application/source/template/prepare.go +++ b/application/source/template/prepare.go @@ -278,6 +278,19 @@ func prepareTextNodes(node *html.Node, storage *types.PageData) { func runNodes(n *html.Node, storage *types.PageData) { + nodeList := []*html.Node{} + for c := n.FirstChild; c != nil; c = c.NextSibling { + nodeList = append(nodeList, c) + } + + for _, n := range nodeList { + checkNodes(n, storage) + runNodes(n, storage) + } + +} + +func checkNodes(n *html.Node, storage *types.PageData) { if n.Parent != nil { if n.Parent.Type == html.ElementNode { if n.Parent.Data == "script" || n.Parent.Data == "style" { @@ -286,49 +299,44 @@ func runNodes(n *html.Node, storage *types.PageData) { } } - if n.Type == html.TextNode { + if n.Type != html.TextNode { + return + } - content := strings.TrimSpace(n.Data) - if content == "" { - return - } + content := strings.TrimSpace(n.Data) + if content == "" { + return + } - id, err := util.BuildTextKey(content) - if err != nil { - id = util.GetNextId() - } + id, err := util.BuildTextKey(content) + if err != nil || id == "" { + id = util.GetNextId() + } - parent := n.Parent - span := &html.Node{ - Type: html.ElementNode, - Data: atom.Span.String(), - Attr: []html.Attribute{ - { - Key: "id", - Val: id, - }, - { - Key: "data-replace-self", - Val: "path:text." + id + ".text", - }, + parent := n.Parent + span := &html.Node{ + Type: html.ElementNode, + Data: atom.Span.String(), + Attr: []html.Attribute{ + { + Key: "id", + Val: id, }, - } - parent.InsertBefore(span, n) - parent.RemoveChild(n) - - span.AppendChild(n) - - storage.Text = append(storage.Text, types.Text{ - Id: id, - Text: content, - }) - - return + { + Key: "data-replace-self", + Val: "path:text." + id + ".text", + }, + }, } + parent.InsertBefore(span, n) + parent.RemoveChild(n) - for c := n.FirstChild; c != nil; c = c.NextSibling { - runNodes(c, storage) - } + span.AppendChild(n) + + storage.Text = append(storage.Text, types.Text{ + Id: id, + Text: content, + }) } diff --git a/development/examples/example1/original/test4.html b/development/examples/example1/original/test4.html new file mode 100644 index 0000000..a29b556 --- /dev/null +++ b/development/examples/example1/original/test4.html @@ -0,0 +1,12 @@ +<!DOCTYPE html><html lang="en"><head> + +</head> + +<body> + +<main> + +<p>Das ist ein Text <a href="a.html">one-time password</a>.</p> +</main> + +</body></html> \ No newline at end of file diff --git a/development/examples/example1/template/data.yaml b/development/examples/example1/template/data.yaml index 4eaf644..2f5a7f0 100755 --- a/development/examples/example1/template/data.yaml +++ b/development/examples/example1/template/data.yaml @@ -18,12 +18,10 @@ test1.html: href: /test.html hreflang: "" title: test-link - content: "" - id: yes-a-html href: /a.html hreflang: "" title: "Yes" - content: "" text: - text: test-link id: test-link @@ -33,6 +31,10 @@ test1.html: id: the-request-was-inco-2640993422 - text: Try submitting your id: try-submitting-your + - text: request + id: request + - text: again (sometimes this helps). + id: again-sometimes-this-2086042570 - text: Contact support if you continue to have problems. id: contact-support-if-y-3404332025 - text: |- @@ -70,7 +72,6 @@ test2.html: href: /a.html hreflang: "" title: "Yes" - content: "" text: - text: Bad xxxx id: bad-xxxx @@ -78,6 +79,10 @@ test2.html: id: the-request-was-inco-2640993422 - text: Try submitting your id: try-submitting-your + - text: request + id: request + - text: again (sometimes this helps). + id: again-sometimes-this-2086042570 - text: Contact support if you continue to have problems. id: contact-support-if-y-3404332025 - text: |- @@ -107,7 +112,6 @@ test3.html: href: /a.html hreflang: "" title: "Yes" - content: "" text: - text: Bad xxxx id: bad-xxxx @@ -115,6 +119,10 @@ test3.html: id: the-request-was-inco-2640993422 - text: Try submitting your id: try-submitting-your + - text: request + id: request + - text: again (sometimes this helps). + id: again-sometimes-this-2086042570 - text: Contact support if you continue to have problems. id: contact-support-if-y-3404332025 - text: |- @@ -124,3 +132,22 @@ test3.html: - text: 'You can try the following steps:' id: you-can-try-the-foll-3363859033 translations: [] +test4.html: + export: en/test4.html + lang: en + title: "" + meta: {} + images: [] + anchors: + - id: a-html + href: a.html + hreflang: "" + title: "" + text: + - text: Das ist ein Text + id: das-ist-ein-text + - text: one-time password + id: one-time-password + - text: . + id: id1000 + translations: [] diff --git a/development/examples/example1/template/test1.html b/development/examples/example1/template/test1.html index a978fb5..5ce870e 100644 --- a/development/examples/example1/template/test1.html +++ b/development/examples/example1/template/test1.html @@ -55,7 +55,7 @@ <monster-state> <img width="16" height="16" alt="tick" title="yes" src="data:image/gif;base64,R0lGODdhEAAQAMwAAPj7+FmhUYjNfGuxYYDJdYTIeanOpT+DOTuANXi/bGOrWj6CONzv2sPjv2Cm V1unU4zPgI/Sg6DJnJ3ImTh8Mtbs00aNP1CZSGy0YqLEn47RgXW8amasW7XWsmmvX2iuXiwAAAAAEAAQAAAFVyAgjmRpnihqGCkpDQ - PbGkNUOFk6DZqgHCNGg2T4QAQBoIiRSAwBE4VA4FACKgkB5NGReASFZEmxsQ0whPDi9BiACYQAInXhwOUtgCUQoORFCGt/g4QAIQA7" id="tickyesdata-image-gi-4013311193" data-attributes="alt path:content.tickyesdata-image-gi-4013311193.alt,src path:content.tickyesdata-image-gi-4013311193.src,title path:content.tickyesdata-image-gi-4013311193.title"/> + PbGkNUOFk6DZqgHCNGg2T4QAQBoIiRSAwBE4VA4FACKgkB5NGReASFZEmxsQ0whPDi9BiACYQAInXhwOUtgCUQoORFCGt/g4QAIQA7" id="tickyesdata-image-gi-4013311193" data-attributes="src path:content.tickyesdata-image-gi-4013311193.src,alt path:content.tickyesdata-image-gi-4013311193.alt,title path:content.tickyesdata-image-gi-4013311193.title"/> <h1 class="deco"><span id="bad-xxxx" data-replace-self="path:text.bad-xxxx.text">Bad xxxx</span></h1> <p><span id="the-request-was-inco-2640993422" data-replace-self="path:text.the-request-was-inco-2640993422.text">The request was incorrect, the server could not process it. </span></p><p> @@ -64,7 +64,7 @@ <ul> - <li><span id="try-submitting-your" data-replace-self="path:text.try-submitting-your.text">Try submitting your </span><a href="/a.html" title="Yes" id="yes-a-html" data-attributes="title path:anchors.yes-a-html.title,href path:anchors.yes-a-html.href,hreflang path:anchors.yes-a-html.hreflang">request</a> again (sometimes this helps).</li> + <li><span id="try-submitting-your" data-replace-self="path:text.try-submitting-your.text">Try submitting your </span><a href="/a.html" title="Yes" id="yes-a-html" data-attributes="href path:anchors.yes-a-html.href,title path:anchors.yes-a-html.title,hreflang path:anchors.yes-a-html.hreflang"><span id="request" data-replace-self="path:text.request.text">request</span></a><span id="again-sometimes-this-2086042570" data-replace-self="path:text.again-sometimes-this-2086042570.text"> again (sometimes this helps).</span></li> <li><span id="contact-support-if-y-3404332025" data-replace-self="path:text.contact-support-if-y-3404332025.text">Contact support if you continue to have problems.</span></li> <li><span id="if-you-received-this-423958995" data-replace-self="path:text.if-you-received-this-423958995.text">If you received this message as a result of a request to the server API, then check the structure diff --git a/development/examples/example1/template/test2.html b/development/examples/example1/template/test2.html index 223887f..bbde8eb 100644 --- a/development/examples/example1/template/test2.html +++ b/development/examples/example1/template/test2.html @@ -48,7 +48,7 @@ <ul> - <li><span id="try-submitting-your" data-replace-self="path:text.try-submitting-your.text">Try submitting your </span><a href="/a.html" title="Yes" id="yes-a-html" data-attributes="href path:anchors.yes-a-html.href,title path:anchors.yes-a-html.title,hreflang path:anchors.yes-a-html.hreflang">request</a> again (sometimes this helps).</li> + <li><span id="try-submitting-your" data-replace-self="path:text.try-submitting-your.text">Try submitting your </span><a href="/a.html" title="Yes" id="yes-a-html" data-attributes="href path:anchors.yes-a-html.href,title path:anchors.yes-a-html.title,hreflang path:anchors.yes-a-html.hreflang"><span id="request" data-replace-self="path:text.request.text">request</span></a><span id="again-sometimes-this-2086042570" data-replace-self="path:text.again-sometimes-this-2086042570.text"> again (sometimes this helps).</span></li> <li><span id="contact-support-if-y-3404332025" data-replace-self="path:text.contact-support-if-y-3404332025.text">Contact support if you continue to have problems.</span></li> <li><span id="if-you-received-this-423958995" data-replace-self="path:text.if-you-received-this-423958995.text">If you received this message as a result of a request to the server API, then check the structure diff --git a/development/examples/example1/template/test3.html b/development/examples/example1/template/test3.html index b374b70..c7514f3 100644 --- a/development/examples/example1/template/test3.html +++ b/development/examples/example1/template/test3.html @@ -39,7 +39,7 @@ <monster-state> <img width="16" height="16" alt="tick" title="yes" src="data:image/gif;base64,R0lGODdhEAAQAMwAAPj7+FmhUYjNfGuxYYDJdYTIeanOpT+DOTuANXi/bGOrWj6CONzv2sPjv2Cm V1unU4zPgI/Sg6DJnJ3ImTh8Mtbs00aNP1CZSGy0YqLEn47RgXW8amasW7XWsmmvX2iuXiwAAAAAEAAQAAAFVyAgjmRpnihqGCkpDQ - PbGkNUOFk6DZqgHCNGg2T4QAQBoIiRSAwBE4VA4FACKgkB5NGReASFZEmxsQ0whPDi9BiACYQAInXhwOUtgCUQoORFCGt/g4QAIQA7" id="tickyesdata-image-gi-4013311193" data-attributes="alt path:content.tickyesdata-image-gi-4013311193.alt,src path:content.tickyesdata-image-gi-4013311193.src,title path:content.tickyesdata-image-gi-4013311193.title"/> + PbGkNUOFk6DZqgHCNGg2T4QAQBoIiRSAwBE4VA4FACKgkB5NGReASFZEmxsQ0whPDi9BiACYQAInXhwOUtgCUQoORFCGt/g4QAIQA7" id="tickyesdata-image-gi-4013311193" data-attributes="src path:content.tickyesdata-image-gi-4013311193.src,alt path:content.tickyesdata-image-gi-4013311193.alt,title path:content.tickyesdata-image-gi-4013311193.title"/> <h1 class="deco"><span id="bad-xxxx" data-replace-self="path:text.bad-xxxx.text">Bad xxxx</span></h1> <p><span id="the-request-was-inco-2640993422" data-replace-self="path:text.the-request-was-inco-2640993422.text">The request was incorrect, the server could not process it. </span></p><p> @@ -48,7 +48,7 @@ <ul> - <li><span id="try-submitting-your" data-replace-self="path:text.try-submitting-your.text">Try submitting your </span><a href="/a.html" title="Yes" id="yes-a-html" data-attributes="href path:anchors.yes-a-html.href,title path:anchors.yes-a-html.title,hreflang path:anchors.yes-a-html.hreflang">request</a> again (sometimes this helps).</li> + <li><span id="try-submitting-your" data-replace-self="path:text.try-submitting-your.text">Try submitting your </span><a href="/a.html" title="Yes" id="yes-a-html" data-attributes="href path:anchors.yes-a-html.href,title path:anchors.yes-a-html.title,hreflang path:anchors.yes-a-html.hreflang"><span id="request" data-replace-self="path:text.request.text">request</span></a><span id="again-sometimes-this-2086042570" data-replace-self="path:text.again-sometimes-this-2086042570.text"> again (sometimes this helps).</span></li> <li><span id="contact-support-if-y-3404332025" data-replace-self="path:text.contact-support-if-y-3404332025.text">Contact support if you continue to have problems.</span></li> <li><span id="if-you-received-this-423958995" data-replace-self="path:text.if-you-received-this-423958995.text">If you received this message as a result of a request to the server API, then check the structure diff --git a/development/examples/example1/template/test4.html b/development/examples/example1/template/test4.html new file mode 100644 index 0000000..e0ad769 --- /dev/null +++ b/development/examples/example1/template/test4.html @@ -0,0 +1,12 @@ +<!DOCTYPE html><html lang="en" data-attributes="lang path:lang"><head> + +</head> + +<body> + +<main> + +<p><span id="das-ist-ein-text" data-replace-self="path:text.das-ist-ein-text.text">Das ist ein Text </span><a href="a.html" id="a-html" data-attributes="href path:anchors.a-html.href,title path:anchors.a-html.title,hreflang path:anchors.a-html.hreflang"><span id="one-time-password" data-replace-self="path:text.one-time-password.text">one-time password</span></a><span id="id1000" data-replace-self="path:text.id1000.text">.</span></p> +</main> + +</body></html> \ No newline at end of file -- GitLab