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

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

diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js
index 6cf6807..81e09b2 100644
--- a/app/assets/javascripts/application.js
+++ b/app/assets/javascripts/application.js
@@ -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
diff --git a/app/assets/javascripts/discourse/helpers/parse-html.js.es6 b/app/assets/javascripts/discourse/helpers/parse-html.js.es6
deleted file mode 100644
index c9469fa..0000000
--- a/app/assets/javascripts/discourse/helpers/parse-html.js.es6
+++ /dev/null
@@ -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;
-}
diff --git a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 b/app/assets/javascripts/discourse/lib/to-markdown.js.es6
index ee3bd1e..8e4eaa4 100644
--- a/app/assets/javascripts/discourse/lib/to-markdown.js.es6
+++ b/app/assets/javascripts/discourse/lib/to-markdown.js.es6
@@ -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 };
 }
 
diff --git a/app/assets/javascripts/vendor.js b/app/assets/javascripts/vendor.js
index 0503964..6304b98 100644
--- a/app/assets/javascripts/vendor.js
+++ b/app/assets/javascripts/vendor.js
@@ -30,5 +30,4 @@
 //= require virtual-dom
 //= require virtual-dom-amd
 //= require highlight.js
-//= require htmlparser.js
 //= require intersection-observer
diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb
index 1943417..c32f603 100644
--- a/lib/pretty_text.rb
+++ b/lib/pretty_text.rb
@@ -82,7 +82,6 @@ module PrettyText
     ctx_load_manifest(ctx, "markdown-it-bundle.js")
     root_path = "#{Rails.root}/app/assets/javascripts/"
 
-    apply_es6_file(ctx, root_path, "discourse/helpers/parse-html")
     apply_es6_file(ctx, root_path, "discourse/lib/to-markdown")
     apply_es6_file(ctx, root_path, "discourse/lib/utilities")
 
diff --git a/lib/tasks/javascript.rake b/lib/tasks/javascript.rake
index cf5f2e1..004fad2 100644
--- a/lib/tasks/javascript.rake
+++ b/lib/tasks/javascript.rake
@@ -51,8 +51,6 @@ task 'javascript:update' do
       source: 'highlight.js/build/.',
       destination: 'highlightjs'
     }, {
-      source: 'htmlparser/lib/htmlparser.js'
-    }, {
       source: 'jquery-resize/jquery.ba-resize.js'
     }, {
       source: 'jquery.autoellipsis/src/jquery.autoellipsis.js',
diff --git a/test/javascripts/lib/category-badge-test.js.es6 b/test/javascripts/lib/category-badge-test.js.es6
index 0a9f343..9d0c02e 100644
--- a/test/javascripts/lib/category-badge-test.js.es6
+++ b/test/javascripts/lib/category-badge-test.js.es6
@@ -2,7 +2,6 @@ import createStore from "helpers/create-store";
 
 QUnit.module("lib:category-link");
 
-import parseHTML from "discourse/helpers/parse-html";
 import { categoryBadgeHTML } from "discourse/helpers/category-link";
 
 QUnit.test("categoryBadge without a category", assert => {
@@ -18,24 +17,19 @@ QUnit.test("Regular categoryBadge", assert => {
     color: "ff0",
     text_color: "f00"
   });
-  const tag = parseHTML(categoryBadgeHTML(category))[0];
+  const tag = $.parseHTML(categoryBadgeHTML(category))[0];
 
-  assert.equal(tag.name, "a", "it creates a `a` wrapper tag");
+  assert.equal(tag.tagName, "A", "it creates a `a` wrapper tag");
   assert.equal(
-    tag.attributes["class"].trim(),
+    tag.className.trim(),
     "badge-wrapper",
     "it has the correct class"
   );
 
   const label = tag.children[1];
+  assert.equal(label.title, "cool description", "it has the correct title");
   assert.equal(
-    label.attributes.title,
-    "cool description",
-    "it has the correct title"
-  );
-
-  assert.equal(
-    label.children[0].children[0].data,
+    label.children[0].innerText,
     "hello",
     "it has the category name"
   );
@@ -44,10 +38,10 @@ QUnit.test("Regular categoryBadge", assert => {
 QUnit.test("undefined color", assert => {
   const store = createStore();
   const noColor = store.createRecord("category", { name: "hello", id: 123 });
-  const tag = parseHTML(categoryBadgeHTML(noColor))[0];
+  const tag = $.parseHTML(categoryBadgeHTML(noColor))[0];
 
   assert.blank(
-    tag.attributes.style,
+    tag.attributes["style"],
     "it has no color style because there are no colors"
   );
 });
@@ -89,11 +83,11 @@ QUnit.test("category names are wrapped in dir-spans", assert => {
     id: 234
   });
 
-  let tag = parseHTML(categoryBadgeHTML(rtlCategory))[0];
+  let tag = $.parseHTML(categoryBadgeHTML(rtlCategory))[0];
   let dirSpan = tag.children[1].children[0];
-  assert.equal(dirSpan.attributes.dir, "rtl");
+  assert.equal(dirSpan.dir, "rtl");
 
-  tag = parseHTML(categoryBadgeHTML(ltrCategory))[0];
+  tag = $.parseHTML(categoryBadgeHTML(ltrCategory))[0];
   dirSpan = tag.children[1].children[0];
-  assert.equal(dirSpan.attributes.dir, "ltr");
+  assert.equal(dirSpan.dir, "ltr");
 });
diff --git a/test/javascripts/test_helper.js b/test/javascripts/test_helper.js
index 4559fb9..de9f4a2 100644
--- a/test/javascripts/test_helper.js
+++ b/test/javascripts/test_helper.js
@@ -25,7 +25,6 @@
 //= require markdown-it-bundle
 //= require application
 //= require plugin
-//= require htmlparser.js
 //= require admin
 
 //= require sinon/pkg/sinon
diff --git a/vendor/assets/javascripts/htmlparser.js b/vendor/assets/javascripts/htmlparser.js
deleted file mode 100644
index 04668e0..0000000
--- a/vendor/assets/javascripts/htmlparser.js
+++ /dev/null
@@ -1,993 +0,0 @@
-/***********************************************
-Copyright 2010 - 2012 Chris Winberry <chris@winberry.net>. All rights reserved.
-Permission is hereby granted, free of charge, to any person obtaining a copy

[... diff too long, it was truncated ...]

GitHub sha: b5008586

2 Likes

This commit has been mentioned on Discourse Meta. There might be relevant details there: