forked from ashi009/node-fast-html-parser
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
99 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,109 @@ | ||
import { decode } from 'he'; | ||
import NodeType from './type'; | ||
import Node from './node'; | ||
import HTMLElement from './html'; | ||
import { trimText } from '../utils'; | ||
import Node from './node'; | ||
import NodeType from './type'; | ||
|
||
|
||
/** | ||
* TextNode to contain a text element in DOM tree. | ||
* @param {string} value [description] | ||
*/ | ||
export default class TextNode extends Node { | ||
public constructor(rawText: string, parentNode: HTMLElement) { | ||
super(parentNode); | ||
this._rawText = rawText; | ||
} | ||
|
||
/** | ||
* Node Type declaration. | ||
* @type {Number} | ||
*/ | ||
public nodeType = NodeType.TEXT_NODE; | ||
|
||
private _rawText: string; | ||
private _trimmedRawText?: string; | ||
private _trimmedText?: string; | ||
|
||
public get rawText() { | ||
return this._rawText; | ||
} | ||
|
||
/** | ||
* Set rawText and invalidate trimmed caches | ||
*/ | ||
public set rawText(text: string) { | ||
this._rawText = text; | ||
this._trimmedRawText = void 0; | ||
this._trimmedText = void 0; | ||
} | ||
|
||
/** | ||
* Returns raw text with all whitespace trimmed except single leading/trailing non-breaking space | ||
*/ | ||
public get trimmedRawText() { | ||
if (this._trimmedRawText !== undefined) return this._trimmedRawText; | ||
this._trimmedRawText = trimText(this.rawText); | ||
return this._trimmedRawText; | ||
} | ||
|
||
/** | ||
* Returns text with all whitespace trimmed except single leading/trailing non-breaking space | ||
*/ | ||
public get trimmedText() { | ||
if (this._trimmedText !== undefined) return this._trimmedText; | ||
this._trimmedText = trimText(this.text); | ||
return this._trimmedText; | ||
} | ||
|
||
/** | ||
* Get unescaped text value of current node and its children. | ||
* @return {string} text content | ||
*/ | ||
public get text() { | ||
return decode(this.rawText); | ||
} | ||
|
||
/** | ||
* Detect if the node contains only white space. | ||
* @return {boolean} | ||
*/ | ||
public get isWhitespace() { | ||
return /^(\s| )*$/.test(this.rawText); | ||
} | ||
|
||
public toString() { | ||
return this.rawText; | ||
} | ||
public constructor(rawText: string, parentNode: HTMLElement) { | ||
super(parentNode); | ||
this._rawText = rawText; | ||
} | ||
|
||
/** | ||
* Node Type declaration. | ||
* @type {Number} | ||
*/ | ||
public nodeType = NodeType.TEXT_NODE; | ||
|
||
private _rawText: string; | ||
private _trimmedRawText?: string; | ||
private _trimmedText?: string; | ||
|
||
public get rawText() { | ||
return this._rawText; | ||
} | ||
|
||
/** | ||
* Set rawText and invalidate trimmed caches | ||
*/ | ||
public set rawText(text: string) { | ||
this._rawText = text; | ||
this._trimmedRawText = void 0; | ||
this._trimmedText = void 0; | ||
} | ||
|
||
/** | ||
* Returns raw text with all whitespace trimmed except single leading/trailing non-breaking space | ||
*/ | ||
public get trimmedRawText() { | ||
if (this._trimmedRawText !== undefined) return this._trimmedRawText; | ||
this._trimmedRawText = trimText(this.rawText); | ||
return this._trimmedRawText; | ||
} | ||
|
||
/** | ||
* Returns text with all whitespace trimmed except single leading/trailing non-breaking space | ||
*/ | ||
public get trimmedText() { | ||
if (this._trimmedText !== undefined) return this._trimmedText; | ||
this._trimmedText = trimText(this.text); | ||
return this._trimmedText; | ||
} | ||
|
||
/** | ||
* Get unescaped text value of current node and its children. | ||
* @return {string} text content | ||
*/ | ||
public get text() { | ||
return decode(this.rawText); | ||
} | ||
|
||
/** | ||
* Detect if the node contains only white space. | ||
* @return {boolean} | ||
*/ | ||
public get isWhitespace() { | ||
return /^(\s| )*$/.test(this.rawText); | ||
} | ||
|
||
public toString() { | ||
return this.rawText; | ||
} | ||
} | ||
|
||
/** | ||
* Trim whitespace except single leading/trailing non-breaking space | ||
*/ | ||
function trimText(text: string): string { | ||
let i = 0; | ||
let startPos; | ||
let endPos; | ||
|
||
while (i >= 0 && i < text.length) { | ||
if (/\S/.test(text[i])) { | ||
if (startPos === undefined) { | ||
startPos = i; | ||
i = text.length; | ||
} else { | ||
endPos = i; | ||
i = void 0; | ||
} | ||
} | ||
|
||
if (startPos === undefined) i++; | ||
else i--; | ||
} | ||
|
||
if (startPos === undefined) startPos = 0; | ||
if (endPos === undefined) endPos = text.length - 1; | ||
|
||
const hasLeadingSpace = startPos > 0 && /[^\S\r\n]/.test(text[startPos - 1]); | ||
const hasTrailingSpace = endPos < (text.length - 1) && /[^\S\r\n]/.test(text[endPos + 1]); | ||
|
||
return (hasLeadingSpace ? ' ' : '') + text.slice(startPos, endPos + 1) + (hasTrailingSpace ? ' ' : ''); | ||
} |
This file was deleted.
Oops, something went wrong.