Skip to content

Commit

Permalink
refactor: adjusted server parse function
Browse files Browse the repository at this point in the history
  • Loading branch information
jahilldev committed May 25, 2023
1 parent adb6b98 commit 2f230e6
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import {
*
* -------------------------------- */

function parseHtml(html: string) {
const markup = sanitiseHtml(html);
let currentParent = createElement({ tagName: 'body', tagRange: [0, markup.length] });
function parseHtml(inputHtml: string) {
const html = sanitiseHtml(inputHtml);
let currentParent = createElement({ tagName: 'body', tagRange: [0, html.length] });
const nodeStack = [currentParent];
let lastText = -1;
let match: RegExpExecArray;
let noNestedTagIndex: undefined | number = undefined;
const dataEnd = markup.length;
const dataEnd = html.length;

while ((match = htmlRegex.exec(markup))) {
while ((match = htmlRegex.exec(html))) {
const { 0: matchText, 3: attributes, 4: closingSlash } = match;
let { 1: leadingSlash, 2: tagName } = match;

Expand All @@ -38,7 +38,7 @@ function parseHtml(html: string) {
const isSelfClosing = selfClosingTags.includes(tagName);

if (lastText > -1 && lastText + matchLength < tagEnd) {
const textValue = parseString(markup.substring(lastText, tagStart));
const textValue = parseString(html.substring(lastText, tagStart));

if (textValue) {
currentParent.childNodes.push(createText(textValue, createRange(tagStart, tagEnd)));
Expand Down Expand Up @@ -85,20 +85,20 @@ function parseHtml(html: string) {
if (isBlockText(tagName)) {
const closeMarkup = `</${tagName}>`;
const closeIndex = tagName
? markup.toLocaleLowerCase().indexOf(closeMarkup, htmlRegex.lastIndex)
: markup.indexOf(closeMarkup, htmlRegex.lastIndex);
? html.toLocaleLowerCase().indexOf(closeMarkup, htmlRegex.lastIndex)
: html.indexOf(closeMarkup, htmlRegex.lastIndex);
const textEnd = closeIndex === -1 ? dataEnd : closeIndex;

if (isIgnored(tagName)) {
const text = markup.substring(tagEnd, textEnd).replace(/^\s+|\s+$/g, '');
const text = html.substring(tagEnd, textEnd).replace(/^\s+|\s+$/g, '');

if (text.length > 0 && /\S/.test(text)) {
currentParent.childNodes.push(createText(text, createRange(tagStart, tagEnd)));
}
}

if (closeIndex === -1) {
lastText = htmlRegex.lastIndex = markup.length + 1;
lastText = htmlRegex.lastIndex = html.length + 1;
} else {
lastText = htmlRegex.lastIndex = closeIndex + closeMarkup.length;
leadingSlash = '/';
Expand Down

0 comments on commit 2f230e6

Please sign in to comment.