@@ -839,10 +839,10 @@ const inject = {
839
839
_rebaseAttrs : {
840
840
a : "href" ,
841
841
form : "action" ,
842
- img : "data-pat-inject-rebase- src" ,
842
+ img : "src" ,
843
843
object : "data" ,
844
- source : "data-pat-inject-rebase- src" ,
845
- video : "data-pat-inject-rebase- src" ,
844
+ source : "src" ,
845
+ video : "src" ,
846
846
} ,
847
847
848
848
_rebaseOptions : {
@@ -867,13 +867,17 @@ const inject = {
867
867
return "" ;
868
868
}
869
869
870
- // Create temporary DOM node and store the html contents on it where
871
- // the original src attribute is kept.
872
- const node = document . createElement ( "div" ) ;
873
- node . innerHTML = html . replace ( / ( \s ) ( s r c \s * ) = / gi, '$1src="" data-pat-inject-rebase-$2=' ) . trim ( ) ;
874
-
875
- const selector = Object . keys ( this . _rebaseAttrs ) . join ( "," ) ;
876
- for ( const el_ of node . querySelectorAll ( selector ) ) {
870
+ // Parse the html string into a DOM tree.
871
+ const page = document . createElement ( "div" ) ;
872
+ page . innerHTML = html ;
873
+
874
+ // Create efficient selector for any tag with it's corresponding
875
+ // attribute from _rebaseAttrs. Note: the current data structure allows
876
+ // only for one attribute to be rebased per tag.
877
+ const rebase_selector = Object . entries ( this . _rebaseAttrs )
878
+ . map ( ( [ tag , attr ] ) => `${ tag } [${ attr } ]` )
879
+ . join ( ", " ) ;
880
+ for ( const el_ of page . querySelectorAll ( rebase_selector ) ) {
877
881
const attr = this . _rebaseAttrs [ el_ . tagName . toLowerCase ( ) ] ;
878
882
let value = el_ . getAttribute ( attr ) ;
879
883
@@ -895,7 +899,10 @@ const inject = {
895
899
}
896
900
897
901
for ( const [ pattern_name , opts ] of Object . entries ( this . _rebaseOptions ) ) {
898
- for ( const el_ of node . querySelectorAll ( `[data-pat-${ pattern_name } ]` ) ) {
902
+ for ( const el_ of dom . querySelectorAllAndMe (
903
+ page ,
904
+ `[data-pat-${ pattern_name } ]`
905
+ ) ) {
899
906
const pattern = registry . patterns ?. [ pattern_name ] ;
900
907
const pattern_parser = pattern ?. parser ;
901
908
if ( ! pattern_parser ) {
@@ -927,10 +934,7 @@ const inject = {
927
934
}
928
935
}
929
936
930
- return node
931
- . innerHTML
932
- . replace ( / s r c = " " d a t a - p a t - i n j e c t - r e b a s e - / g, "" )
933
- . trim ( ) ;
937
+ return page . innerHTML . trim ( ) ;
934
938
} ,
935
939
936
940
_parseRawHtml ( html , url = "" ) {
0 commit comments