htmlContent.ts 3.3 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

8 9 10 11 12 13 14
/**
 * MarkedString can be used to render human readable text. It is either a markdown string
 * or a code-block that provides a language and a code snippet. Note that
 * markdown strings will be sanitized - that means html will be escaped.
 */
export type MarkedString = string | { language: string; value: string };

15 16 17 18 19
export interface IHTMLContentElementCode {
	language: string;
	value: string;
}

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
export function markedStringsEquals(a:MarkedString | MarkedString[], b:MarkedString |MarkedString[]): boolean {
	if (!a && !b) {
		return true;
	}
	if (!a || !b) {
		return false;
	}

	if (Array.isArray(a)) {
		if (!Array.isArray(b)) {
			return false;
		}
		return markedStringArrEquals(<MarkedString[]> a, <MarkedString[]> b);
	}
	return markedStringEqual(<MarkedString> a, <MarkedString> b);
}


function markedStringArrEquals(a:MarkedString[], b:MarkedString[]): boolean {
	let aLen = a.length,
		bLen = b.length;

	if (aLen !== bLen) {
		return false;
	}

	for (let i = 0; i < aLen; i++) {
		if (!markedStringEqual(a[i], b[i])) {
			return false;
		}
	}

	return true;
}
function markedStringEqual(a:MarkedString, b:MarkedString): boolean {
	if (!a && !b) {
		return true;
	}
	if (!a || !b) {
		return false;
	}
	if (typeof a === 'string') {
		return typeof b === 'string' && a === b;
	}
	return (
		a['language'] === b['language']
		&& a['value'] === b['value']
	);
}

70 71
export function textToMarkedString(text: string) : MarkedString {
	return text.replace(/[\\`*_{}[\]()#+\-.!]/g, '\\$&'); // escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
72 73 74
}


E
Erich Gamma 已提交
75
export interface IHTMLContentElement {
76 77 78
	/**
	 * supports **bold**, __italics__, and [[actions]]
	 */
E
Erich Gamma 已提交
79 80 81 82 83 84 85 86 87
	formattedText?:string;
	text?: string;
	className?: string;
	style?: string;
	customStyle?: any;
	tagName?: string;
	children?: IHTMLContentElement[];
	isText?: boolean;
	role?: string;
88
	markdown?: string;
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	code?: IHTMLContentElementCode;
}

function htmlContentElementCodeEqual(a:IHTMLContentElementCode, b:IHTMLContentElementCode): boolean {
	if (!a && !b) {
		return true;
	}
	if (!a || !b) {
		return false;
	}
	return (
		a.language === b.language
		&& a.value === b.value
	);
}

function htmlContentElementEqual(a:IHTMLContentElement, b:IHTMLContentElement): boolean {
	return (
		a.formattedText === b.formattedText
		&& a.text === b.text
		&& a.className === b.className
		&& a.style === b.style
		&& a.customStyle === b.customStyle
		&& a.tagName === b.tagName
		&& a.isText === b.isText
		&& a.role === b.role
		&& a.markdown === b.markdown
		&& htmlContentElementCodeEqual(a.code, b.code)
		&& htmlContentElementArrEquals(a.children, b.children)
	);
}

export function htmlContentElementArrEquals(a:IHTMLContentElement[], b:IHTMLContentElement[]): boolean {
	if (!a && !b) {
		return true;
	}
	if (!a || !b) {
		return false;
	}

	let aLen = a.length,
		bLen = b.length;

	if (aLen !== bLen) {
		return false;
	}

	for (let i = 0; i < aLen; i++) {
		if (!htmlContentElementEqual(a[i], b[i])) {
			return false;
		}
	}

	return true;
}