Skip to content

Commit

Permalink
Ignore phone numbers in code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharykeeping committed Oct 31, 2023
1 parent 6aba695 commit f49fd93
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
18 changes: 17 additions & 1 deletion docker/customHtmlRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,14 +484,30 @@ 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;
if (pageContent && event.lastEvent.tagName) {
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,
Expand Down
6 changes: 6 additions & 0 deletions docker/test/phone-numbers-without-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<code><span><span>+61 2 9953 3000</span></span></code>";
const messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});
});

0 comments on commit f49fd93

Please sign in to comment.