From 435c9d6f38cab6d38b1c83bb611ef8fcf4db6d93 Mon Sep 17 00:00:00 2001 From: Salis Braimah Date: Thu, 7 Sep 2017 06:33:11 +0000 Subject: [PATCH 1/2] collapse spaces and line breaks Added a collapseSpaces method in util that removes double spacing and line endings. This helps eliminate unecessary text nodes being generated in elm --- src/parser.ts | 3 ++- src/utils.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/parser.ts b/src/parser.ts index 59bb086..7a9b947 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -1,6 +1,6 @@ 'use strict'; -import {Parser} from 'htmlparser2'; +import { Parser } from 'htmlparser2'; import * as utils from './utils'; @@ -20,6 +20,7 @@ export function convert(html: string, options: { indent: { with: string, size: n let indentWith = options.indent.with; let depth = -1; let context = []; + html = utils.sanitizeHtml(html); let onOpenTag = (name: string, attributes: any): void => { let pre = ''; let tag = []; diff --git a/src/utils.ts b/src/utils.ts index a6f8b45..3045a34 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -10,4 +10,16 @@ export function styleToElm(value: string): string[] { let arr = s.split(/\s*:\s*(.+)/).filter((s) => !!s.length); return '( "' + arr.join('", "') + '" )'; }); +} + +export function sanitizeHtml(value: string): string { + return collapseSpaces(value); +} + +function collapseSpaces(value: string): string { + let + regex = /(\s{2,}|\n)/gm, + collapsedValue = value.replace(regex, ' '); + + return collapsedValue; } \ No newline at end of file From 100a979864134ead1aaa8f8806f9be68c2c880c1 Mon Sep 17 00:00:00 2001 From: Salis Braimah Date: Sat, 7 Oct 2017 12:22:34 +0000 Subject: [PATCH 2/2] transform data and aria attributes into elm attribute declarations --- src/parser.ts | 9 ++++++++- src/utils.ts | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/parser.ts b/src/parser.ts index 7a9b947..6c5cf37 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -64,6 +64,13 @@ export function convert(html: string, options: { indent: { with: string, size: n depth--; elm += ']'; }; + let postParseOps = (elmCode): string => { + return utils.ariaToAttributes( + utils.dataToAttributes( + utils.fixTypeDeclaration(elmCode) + ) + ); + }; let parser = new Parser({ onopentag: onOpenTag, ontext: onText, @@ -72,5 +79,5 @@ export function convert(html: string, options: { indent: { with: string, size: n parser.write(html); parser.end(''); - return elm; + return postParseOps(elm); } diff --git a/src/utils.ts b/src/utils.ts index 3045a34..7ecf68e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -22,4 +22,25 @@ function collapseSpaces(value: string): string { collapsedValue = value.replace(regex, ' '); return collapsedValue; +} + + +export function ariaToAttributes(value: string): string { + let + regex = /(aria-.+?)\s/gm, + ariaAsAttributes = value.replace(regex, 'attribute "$1" '); + + return ariaAsAttributes; +} + +export function dataToAttributes(value: string): string { + let + regex = /(data-.+?)\s/gm, + dataAsAttributes = value.replace(regex, 'attribute "$1" '); + + return dataAsAttributes; +} + +export function fixTypeDeclaration(value: string): string { + return value.replace(/type'/, "type_"); } \ No newline at end of file