DEV: Remove HTML parser from Tautologistics. (#7344)

This commit is contained in:
Bianca Nenciu
2019-04-10 12:21:22 +03:00
committed by Régis Hanol
parent d0fe42e2ef
commit b5008586c5
9 changed files with 49 additions and 1037 deletions

View File

@@ -10,7 +10,6 @@
//= require ./deprecated
// Stuff we need to load first
//= require ./discourse/helpers/parse-html
//= require ./discourse/lib/to-markdown
//= require ./discourse/lib/utilities
//= require ./discourse/lib/page-visible

View File

@@ -1,8 +0,0 @@
/* global Tautologistics */
export default function parseHTML(rawHtml) {
const builder = new Tautologistics.NodeHtmlParser.HtmlBuilder();
const parser = new Tautologistics.NodeHtmlParser.Parser(builder);
parser.parseComplete(rawHtml);
return builder.dom;
}

View File

@@ -1,5 +1,3 @@
import parseHTML from "discourse/helpers/parse-html";
const trimLeft = text => text.replace(/^\s+/, "");
const trimRight = text => text.replace(/\s+$/, "");
const countPipes = text => (text.replace(/\\\|/, "").match(/\|/g) || []).length;
@@ -495,10 +493,9 @@ function tags() {
class Element {
constructor(element, parent, previous, next) {
this.name = element.name;
this.type = element.type;
this.data = element.data;
this.children = element.children;
this.attributes = element.attributes || {};
this.attributes = element.attributes;
if (parent) {
this.parent = parent;
@@ -554,14 +551,7 @@ class Element {
}
toMarkdown() {
switch (this.type) {
case "text":
return this.text();
break;
case "tag":
return this.tag().toMarkdown();
break;
}
return this.name === "#text" ? this.text() : this.tag().toMarkdown();
}
filterParentNames(names) {
@@ -628,7 +618,42 @@ function putPlaceholders(html) {
match = codeRegEx.exec(origHtml);
}
const elements = parseHTML(trimUnwanted(html));
const transformNode = node => {
if (node.nodeName !== "#text" && node.length !== undefined) {
const ret = [];
for (let i = 0; i < node.length; ++i) {
if (node[i].nodeName !== "#comment") {
ret.push(transformNode(node[i]));
}
}
return ret;
}
const ret = {
name: node.nodeName.toLowerCase(),
data: node.data,
children: [],
attributes: {}
};
if (node.nodeName === "#text") {
return ret;
}
for (let i = 0; i < node.childNodes.length; ++i) {
if (node.childNodes[i].nodeName !== "#comment") {
ret.children.push(transformNode(node.childNodes[i]));
}
}
for (let i = 0; i < node.attributes.length; ++i) {
ret.attributes[node.attributes[i].name] = node.attributes[i].value;
}
return ret;
};
const elements = transformNode($.parseHTML(trimUnwanted(html)));
return { elements, placeholders };
}

View File

@@ -30,5 +30,4 @@
//= require virtual-dom
//= require virtual-dom-amd
//= require highlight.js
//= require htmlparser.js
//= require intersection-observer