瀏覽代碼

Make HTML markup converter more robust

Danilo Bargen 8 年之前
父節點
當前提交
0a7c4f5fe6
共有 2 個文件被更改,包括 25 次插入23 次删除
  1. 14 23
      src/filters.ts
  2. 11 0
      tests/filters.js

+ 14 - 23
src/filters.ts

@@ -50,29 +50,20 @@ angular.module('3ema.filters', [])
  */
 .filter('htmlToAsciiMarkup', function() {
     return (text) => {
-        let out = text
-
-        // Bold
-        .replace(/<b>/g, '*')
-        .replace(/<\/b>/g, '*')
-        .replace(/<strong>/g, '*')
-        .replace(/<\/strong>/g, '*')
-
-        // Italic
-        .replace(/<i>/g, '_')
-        .replace(/<\/i>/g, '_')
-        .replace(/<em>/g, '_')
-        .replace(/<\/em>/g, '_')
-
-        // Strikethrough
-        .replace(/<strike>/g, '~')
-        .replace(/<\/strike>/g, '~')
-        .replace(/<del>/g, '~')
-        .replace(/<\/del>/g, '~')
-        .replace(/<s>/g, '~')
-        .replace(/<\/s>/g, '~')
-
-        ;
+        let tags = [
+            ['b', '*'],
+            ['strong', '*'],
+            ['i', '_'],
+            ['em', '_'],
+            ['strike', '~'],
+            ['del', '~'],
+            ['s', '~'],
+        ];
+        let out = text;
+        for (let tag of tags) {
+            out = out.replace(new RegExp('<\\s*' + tag[0] + '(\\s[^>]*|\\s*)>', 'gi'), tag[1]);
+            out = out.replace(new RegExp('<\\s*\/\\s*' + tag[0] + '\\s*>', 'gi'), tag[1]);
+        }
         return out;
     };
 })

+ 11 - 0
tests/filters.js

@@ -108,6 +108,9 @@ describe('Filters', function() {
         it('converts bold text', () => {
             this.testPatterns([
                 ['<b>bold</b>', '*bold*'],
+                ['< 	b >bold</b>', '*bold*'],
+                ['<B>bold</b>', '*bold*'],
+                ['<b class="asdf">bold</b>', '*bold*'],
                 ['<strong>bold</strong>', '*bold*'],
                 ['<b><b>bold</b></b>', '**bold**'],
                 ['<b><strong>bold</strong></b>', '**bold**'],
@@ -117,6 +120,7 @@ describe('Filters', function() {
         it('converts italic text', () => {
             this.testPatterns([
                 ['<i>italic</i>', '_italic_'],
+                ['<i onclick="alert(1)">italic</i>', '_italic_'],
                 ['<em>italic</em>', '_italic_'],
                 ['<i><em>italic</em></i>', '__italic__'],
             ]);
@@ -126,11 +130,18 @@ describe('Filters', function() {
             this.testPatterns([
                 ['<strike>strikethrough</strike>', '~strikethrough~'],
                 ['<del>strikethrough</del>', '~strikethrough~'],
+                ['<del href="/">strikethrough</del>', '~strikethrough~'],
                 ['<s>strikethrough</s>', '~strikethrough~'],
                 ['<strike><del><s>strikethrough</s></del></strike>', '~~~strikethrough~~~'],
             ]);
         });
 
+        it('does not affect other tags', () => {
+            this.testPatterns([
+                ['<script>alert("pho soup time")</script>', '<script>alert("pho soup time")</script>'],
+            ]);
+        });
+
         it('combination of all', () => {
             this.testPatterns([
                 ['<b><em><del>foo</del></em></b>', '*_~foo~_*'],