Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert_only_text makes mistakes when i copy a text html that includes a style tag #934

Closed
helianthuswhite opened this issue Jan 16, 2023 · 0 comments

Comments

@helianthuswhite
Copy link

Jodit Version: 3.24.2

Browser: Chrome
OS: Mac
Is React App: True

Code
I copied a text from the mac memo and chose "insert_only_text" option. However, i got a result which is not my expect.

After read the source code, i found it not handle the text which includes a <style> tag.

So i change it like this, that works.

// A *self-contained* demonstration of the problem follows...
/**
 * Extract plain text from HTML text
 */
export function stripTags(
	html: string | Node,
	doc: Document = document
): string {
	const tmp = doc.createElement('div');

	if (isString(html)) {
		tmp.innerHTML = html;
	} else {
		tmp.appendChild(html);
	}

	$$('DIV, P, BR, H1, H2, H3, H4, H5, H6, HR, STYLE', tmp).forEach(p => {
		const pr = p.parentNode;
		if (!pr) {
			return;
		}

		if (p.nodeName === 'STYLE') {
			tmp.removeChild(p);
		}

		const nx = p.nextSibling;

		if (Dom.isText(nx) && /^\s/.test(nx.nodeValue || '')) {
			return;
		}

		if (nx) {
			pr.insertBefore(doc.createTextNode(' '), nx);
		}
	});

	return trim(tmp.innerText) || '';
}

Expected behavior:
image

Actual behavior:
image

xdan added a commit that referenced this issue Feb 13, 2023
@xdan xdan closed this as completed Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants