Skip to content

Commit

Permalink
attributed string from html
Browse files Browse the repository at this point in the history
  • Loading branch information
owenpshaw committed Jun 18, 2024
1 parent 525f95a commit 34a0631
Show file tree
Hide file tree
Showing 3 changed files with 839 additions and 1 deletion.
32 changes: 32 additions & 0 deletions Frameworks/Foundation/JSAttributedString.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// #import "JSTextAttachment.js"
// #import "JSFont.js"
// #import "JSColor.js"
// #import "JSHTMLTextParser.js"
'use strict';

(function(){
Expand Down Expand Up @@ -197,6 +198,37 @@ JSClass("JSAttributedString", JSObject, {
this.initWithAttachment(attachment);
},

initWithHTML: function(html, options){
if (options === undefined){
options = {};
}
var string = "";
var run = JSAttributedStringRun(JSRange.Zero, {});
var runs = [run];
if (html !== null && html !== undefined){
var parser = JSHTMLTextParser.initWithHTML(html);
parser.delegate = {
htmlTextParserDidFindAttributedText: function(parser, text, attributes){
if (run.range.length === 0){
run.attributes = JSCopy(attributes);
run.range.length += text.length;
}else if (run.onlyContainsEqualAttributes(attributes)){
run.range.length += text.length;
}else{
run = JSAttributedStringRun(JSRange(run.range.end, text.length), attributes);
runs.push(run);
}
string += text;
}
};
parser.state.preserveWhitespace = options.preserveWhitespace === true;
parser.state.baseURL = options.baseURL || null;
parser.parse();
}
this._string = string;
this._runs = runs;
},

// MARK: - Getting the unattributed string value

getString: function(){
Expand Down
Loading

0 comments on commit 34a0631

Please sign in to comment.