<commit_before>'use strict';

import RequestScreen from './RequestScreen';
import Surface from '../surface/Surface';

class HtmlScreen extends RequestScreen {

	/**
	 * Screen class that perform a request and extracts surface contents from
	 * the response content.
	 * @constructor
	 * @extends {RequestScreen}
	 */
	constructor() {
		super();

		/**
		 * Holds the title selector. Relevant to extract the <code><title></code>
		 * element from request fragments to use as the screen title.
		 * @type {!string}
		 * @default title
		 * @protected
		 */
		this.titleSelector = 'title';
	}

	/**
	 * @inheritDoc
	 */
	disposeInternal() {
		this.virtualDocumentElement = null;
		super.disposeInternal();
	}

	/**
	 * @inheritDoc
	 */
	getSurfaceContent(surfaceId) {
		var surface = this.virtualDocumentElement.querySelector('#' + surfaceId);
		if (surface) {
			var defaultChild = surface.querySelector('#' + surfaceId + '-' + Surface.DEFAULT);
			if (defaultChild) {
				return defaultChild.innerHTML;
			}
			return surface.innerHTML; // If default content not found, use surface content
		}
	}

	/**
	 * Gets the title selector.
	 * @return {!string}
	 */
	getTitleSelector() {
		return this.titleSelector;
	}

	/**
	 * @inheritDoc
	 */
	load(path) {
		return super.load(path)
			.then(content => this.resolveContentFromHtmlString(content))
			.thenCatch(err => {
				throw err;
			});
	}

	/**
	 * Resolves the screen content from the response string.
	 * @param {XMLHttpRequest} xhr
	 */
	resolveContentFromHtmlString(htmlString) {
		if (!this.virtualDocumentElement) {
			this.virtualDocumentElement = document.documentElement.cloneNode();
		}

		this.virtualDocumentElement.innerHTML = htmlString;

		var title = this.virtualDocumentElement.querySelector(this.titleSelector);
		if (title) {
			this.setTitle(title.innerHTML.trim());<commit_msg>Fixes HtmlScreen to share same virtualDocumentElement to optimize memory usage<commit_after>'use strict';

import RequestScreen from './RequestScreen';
import Surface from '../surface/Surface';

class HtmlScreen extends RequestScreen {

	/**
	 * Screen class that perform a request and extracts surface contents from
	 * the response content.
	 * @constructor
	 * @extends {RequestScreen}
	 */
	constructor() {
		super();

		/**
		 * Holds the title selector. Relevant to extract the <code><title></code>
		 * element from request fragments to use as the screen title.
		 * @type {!string}
		 * @default title
		 * @protected
		 */
		this.titleSelector = 'title';
	}

	/**
	 * @inheritDoc
	 */
	getSurfaceContent(surfaceId) {
		var surface = HtmlScreen.virtualDocumentElement.querySelector('#' + surfaceId);
		if (surface) {
			var defaultChild = surface.querySelector('#' + surfaceId + '-' + Surface.DEFAULT);
			if (defaultChild) {
				return defaultChild.innerHTML;
			}
			return surface.innerHTML; // If default content not found, use surface content
		}
	}

	/**
	 * Gets the title selector.
	 * @return {!string}
	 */
	getTitleSelector() {
		return this.titleSelector;
	}

	/**
	 * @inheritDoc
	 */
	load(path) {
		return super.load(path)
			.then(content => this.resolveContentFromHtmlString(content))
			.thenCatch(err => {
				throw err;
			});
	}

	/**
	 * Resolves the screen content from the response string.
	 * @param {XMLHttpRequest} xhr
	 */
	resolveContentFromHtmlString(htmlString) {
		if (!HtmlScreen.virtualDocumentElement) {
			HtmlScreen.virtualDocumentElement = document.documentElement.cloneNode();
		}

		HtmlScreen.virtualDocumentElement.innerHTML = htmlString;

		var title = HtmlScreen.virtualDocumentElement.querySelector(this.titleSelector);
		if (title) {
			this.setTitle(title.innerHTML.trim());
{'fraction_non_alphanumeric': 0.12394672465343844, 'fraction_numerical': 0.0, 'mean_word_length': 3.72400513478819, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6781527', 'n_tokens_mistral': 1188, 'n_tokens_neox': 1037, 'n_words': 269}