From f49fd9371f0145524ff92606b97c66cc94c95dea Mon Sep 17 00:00:00 2001 From: Zachary Keeping Date: Wed, 1 Nov 2023 10:07:41 +1100 Subject: [PATCH] Ignore phone numbers in code blocks --- docker/customHtmlRules.js | 18 +++++++++++++++++- docker/test/phone-numbers-without-links.js | 6 ++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/docker/customHtmlRules.js b/docker/customHtmlRules.js index 9d57aa88..43eedb7e 100644 --- a/docker/customHtmlRules.js +++ b/docker/customHtmlRules.js @@ -484,6 +484,19 @@ exports.addCustomHtmlRule = () => { "Checks for phone numbers that aren't in hyperlinks with a \"tel:\" prefix.", init: function (parser, reporter) { const self = this; + let isInCodeBlock = false; + parser.addListener("tagstart", (event) => { + const tagName = event.tagName.toLowerCase(); + if (tagName === "code") { + isInCodeBlock = true; + } + }); + parser.addListener("tagend", (event) => { + const tagName = event.tagName.toLowerCase(); + if (tagName === "code") { + isInCodeBlock = false; + } + }); parser.addListener("text", (event) => { if (event.raw && event.lastEvent && findPhoneNumbersInText(event.raw, "AU").length) { const pageContent = event.lastEvent.raw; @@ -491,7 +504,10 @@ exports.addCustomHtmlRule = () => { const tagName = event.lastEvent.tagName.toLowerCase(); const mapAttrs = parser.getMapAttrs(event.lastEvent.attrs); const href = mapAttrs["href"]; - if (!(tagName === "a" && href && href.startsWith("tel:"))) { + const isLink = tagName === "a"; + const isTelLink = isLink && href && href.startsWith("tel:"); + + if (!(isTelLink || isInCodeBlock)) { reporter.warn( "Phone number must be in a hyperlink.", event.line, diff --git a/docker/test/phone-numbers-without-links.js b/docker/test/phone-numbers-without-links.js index e4503959..d497254a 100644 --- a/docker/test/phone-numbers-without-links.js +++ b/docker/test/phone-numbers-without-links.js @@ -84,4 +84,10 @@ describe(`Rules: ${ruleId}`, () => { expect(messages.length).to.be(0); }); }); + + it("a phone number in a code block should not error", () => { + const code = "+61 2 9953 3000"; + const messages = HTMLHint.verify(code, ruleOptions); + expect(messages.length).to.be(0); + }); }); \ No newline at end of file